jAER project on SourceForge

ch.unizh.ini.jaer.projects.rccar
Class SiLabsC8051F320_USBIO_CarServoController

java.lang.Object
  extended by ch.unizh.ini.jaer.projects.rccar.SiLabsC8051F320_USBIO_CarServoController
All Implemented Interfaces:
de.thesycon.usbio.PnPNotifyInterface, de.thesycon.usbio.UsbIoErrorCodes, HardwareInterface, ServoInterface

public class SiLabsC8051F320_USBIO_CarServoController
extends java.lang.Object
implements de.thesycon.usbio.UsbIoErrorCodes, de.thesycon.usbio.PnPNotifyInterface, ServoInterface

Servo motor controller using USBIO driver access to SiLabsC8051F320 device for controlling car servos. It can control two servos (e.g. steering and speed) while the device monitors two other servos (e.g. steering and speed outputs from a radio receiver). When the device detects non-zero radio outputs, it blocks out the computer values and sends a status message to the host.

Author:
tobi

Field Summary
protected  de.thesycon.usbio.structs.USB_DEVICE_DESCRIPTOR deviceDescriptor
          the USBIO device descriptor
protected  int gDevList
          the devlist handle for USBIO
static java.lang.String GUID
          driver guid (Globally unique ID, for this USB driver instance
protected  de.thesycon.usbio.UsbIo gUsbIo
          the UsbIo interface to the device.
protected  int interfaceNumber
          the device number, out of all potential compatible devices that could be opened
protected  int numberOfStringDescriptors
           
static short PID
           
static int RADIO_SPEED
           
static int RADIO_STEER
           
static int SPEED_SERVO
           
static int STEERING_SERVO
           
protected  de.thesycon.usbio.structs.USB_STRING_DESCRIPTOR stringDescriptor1
          the first USB string descriptor (Vendor name) (if available)
protected  de.thesycon.usbio.structs.USB_STRING_DESCRIPTOR stringDescriptor2
          the second USB string descriptor (Product name) (if available)
protected  de.thesycon.usbio.structs.USB_STRING_DESCRIPTOR stringDescriptor3
          the third USB string descriptor (Serial number) (if available)
static short VID
           
 
Fields inherited from interface de.thesycon.usbio.UsbIoErrorCodes
USBIO_ERR_ADDITIONAL_EVENT_SIGNALLED, USBIO_ERR_ALREADY_BOUND, USBIO_ERR_ALREADY_CONFIGURED, USBIO_ERR_BABBLE_DETECTED, USBIO_ERR_BAD_START_FRAME, USBIO_ERR_BTSTUFF, USBIO_ERR_BUFFER_OVERRUN, USBIO_ERR_BUFFER_UNDERRUN, USBIO_ERR_BULK_RESTRICTION, USBIO_ERR_CANCELED, USBIO_ERR_CONTROL_NOT_SUPPORTED, USBIO_ERR_CONTROL_RESTRICTION, USBIO_ERR_CRC, USBIO_ERR_DATA_BUFFER_ERROR, USBIO_ERR_DATA_OVERRUN, USBIO_ERR_DATA_TOGGLE_MISMATCH, USBIO_ERR_DATA_UNDERRUN, USBIO_ERR_DEMO_EXPIRED, USBIO_ERR_DEV_NOT_RESPONDING, USBIO_ERR_DEVICE_GONE, USBIO_ERR_DEVICE_NOT_FOUND, USBIO_ERR_DEVICE_NOT_OPEN, USBIO_ERR_DEVICE_NOT_PRESENT, USBIO_ERR_ENDPOINT_HALTED, USBIO_ERR_EP0_RESTRICTION, USBIO_ERR_ERROR_BUSY, USBIO_ERR_ERROR_SHORT_TRANSFER, USBIO_ERR_FAILED, USBIO_ERR_FIFO, USBIO_ERR_FRAME_CONTROL_NOT_OWNED, USBIO_ERR_FRAME_CONTROL_OWNED, USBIO_ERR_INSUFFICIENT_RESOURCES, USBIO_ERR_INTERFACE_NOT_FOUND, USBIO_ERR_INTERNAL_HC_ERROR, USBIO_ERR_INTERRUPT_RESTRICTION, USBIO_ERR_INVALID_CONFIGURATION_DESCRIPTOR, USBIO_ERR_INVALID_DEVICE_STATE, USBIO_ERR_INVALID_DIRECTION, USBIO_ERR_INVALID_FUNCTION_PARAM, USBIO_ERR_INVALID_INBUFFER, USBIO_ERR_INVALID_IOCTL, USBIO_ERR_INVALID_ISO_PACKET, USBIO_ERR_INVALID_OUTBUFFER, USBIO_ERR_INVALID_PARAM, USBIO_ERR_INVALID_PARAMETER, USBIO_ERR_INVALID_PIPE_FLAGS, USBIO_ERR_INVALID_PIPE_HANDLE, USBIO_ERR_INVALID_POWER_STATE, USBIO_ERR_INVALID_RECIPIENT, USBIO_ERR_INVALID_TYPE, USBIO_ERR_INVALID_URB_FUNCTION, USBIO_ERR_ISO_NA_LATE_USBPORT, USBIO_ERR_ISO_NOT_ACCESSED_BY_HW, USBIO_ERR_ISO_NOT_ACCESSED_LATE, USBIO_ERR_ISO_RESTRICTION, USBIO_ERR_ISO_TD_ERROR, USBIO_ERR_ISOCH_REQUEST_FAILED, USBIO_ERR_LOAD_SETUP_API_FAILED, USBIO_ERR_NO_BANDWIDTH, USBIO_ERR_NO_MEMORY, USBIO_ERR_NO_SUCH_DEVICE_INSTANCE, USBIO_ERR_NOT_ACCESSED, USBIO_ERR_NOT_BOUND, USBIO_ERR_NOT_CONFIGURED, USBIO_ERR_NOT_SUPPORTED, USBIO_ERR_OPEN_PIPES, USBIO_ERR_OUT_OF_ADDRESS_SPACE, USBIO_ERR_OUT_OF_MEMORY, USBIO_ERR_PENDING_REQUESTS, USBIO_ERR_PID_CHECK_FAILURE, USBIO_ERR_PIPE_NOT_FOUND, USBIO_ERR_PIPE_RESTRICTION, USBIO_ERR_PIPE_SIZE_RESTRICTION, USBIO_ERR_POOL_EMPTY, USBIO_ERR_POWER_DOWN, USBIO_ERR_REQUEST_FAILED, USBIO_ERR_RESERVED1, USBIO_ERR_RESERVED2, USBIO_ERR_SET_CONFIG_FAILED, USBIO_ERR_SET_CONFIGURATION_FAILED, USBIO_ERR_STALL_PID, USBIO_ERR_STATUS_NOT_MAPPED, USBIO_ERR_SUCCESS, USBIO_ERR_TIMEOUT, USBIO_ERR_TOO_MUCH_ISO_PACKETS, USBIO_ERR_UNEXPECTED_PID, USBIO_ERR_USBD_BUFFER_TOO_SMALL, USBIO_ERR_USBD_INTERFACE_NOT_FOUND, USBIO_ERR_USBD_TIMEOUT, USBIO_ERR_VERSION_MISMATCH, USBIO_ERR_VID_RESTRICTION, USBIO_ERR_XACT_ERROR
 
Constructor Summary
SiLabsC8051F320_USBIO_CarServoController()
          Creates a new instance of SiLabsC8051F320_USBIO_ServoController for interface number 0
SiLabsC8051F320_USBIO_CarServoController(int n)
          Creates a new instance of SiLabsC8051F320_USBIO_ServoController
 
Method Summary
 void close()
          Closes the device.
protected  byte[] deadzoneValue(float value)
          Computes the 2-byte deadzone value to be sent to the ucontroller for a particular dead zone.
 void disableAllServos()
          disables all servos
 void disableServo(int servo)
          sends a servo value to disable the servo
 float getDeadzoneForSpeed()
           
 float getDeadzoneForSteering()
           
 short getDID()
           
 float getLastServoValue(int servo)
          Returns last servo value sent (0 before sending a value)
 float[] getLastServoValues()
          Returns last servo values sent.
 int getNumberOfStringDescriptors()
          returns number of string descriptors
 int getNumServos()
           
 short getPID()
           
 float getRadioSpeed()
          Returns the last steering value received from radio receiver
 float getRadioSteer()
          Returns the last steering value received from radio receiver
 int getRadioTimeoutMs()
           
 java.lang.String[] getStringDescriptors()
          return the string USB descriptors for the device
 java.lang.String getTypeName()
          get text name of interface, e.g.
 short getVID()
           
 int[] getVIDPID()
          return the USB VID/PID of the interface
protected  boolean hasStringIdentifier()
          checks if device has a string identifier that is a non-empty string
 boolean isOpen()
          reports if interface is open().
static void main(java.lang.String[] args)
           
 void onAdd()
           
 void onRemove()
           
 void open()
          constrcuts a new USB connection, opens it.
protected  void openUsbIo()
          This method does the hard work of opening the device, downloading the firmware, making sure everything is OK.
 void setAllServoValues(float[] values)
          sets all servos to values in one transfer
 void setDeadzoneForSpeed(float f)
          Sets the radio "dead zone".
 void setDeadzoneForSteering(float f)
          Sets the radio "dead zone".
 void setPort2(int portValue)
          sends a command to set the port 2 output (on the side of the original board) to portValue.
 void setRadioTimeoutMs(int radioTimeoutMs)
          Sets the timeout for radio control override of computer control.
 void setServoValue(int servo, float value)
          sets servo position.
 void setServoValuePWM(int servo, int pwmValue)
          directly sends a particular short value to the servo, bypassing conversion from float.
 void setSpeed(float f)
          Sets the speed controller value.
 void setSteering(float f)
          Sets the steering angle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GUID

public static final java.lang.String GUID
driver guid (Globally unique ID, for this USB driver instance

See Also:
Constant Field Values

VID

public static final short VID
See Also:
Constant Field Values

PID

public static final short PID
See Also:
Constant Field Values

STEERING_SERVO

public static final int STEERING_SERVO
See Also:
Constant Field Values

SPEED_SERVO

public static final int SPEED_SERVO
See Also:
Constant Field Values

RADIO_STEER

public static final int RADIO_STEER
See Also:
Constant Field Values

RADIO_SPEED

public static final int RADIO_SPEED
See Also:
Constant Field Values

interfaceNumber

protected int interfaceNumber
the device number, out of all potential compatible devices that could be opened


stringDescriptor1

protected de.thesycon.usbio.structs.USB_STRING_DESCRIPTOR stringDescriptor1
the first USB string descriptor (Vendor name) (if available)


stringDescriptor2

protected de.thesycon.usbio.structs.USB_STRING_DESCRIPTOR stringDescriptor2
the second USB string descriptor (Product name) (if available)


stringDescriptor3

protected de.thesycon.usbio.structs.USB_STRING_DESCRIPTOR stringDescriptor3
the third USB string descriptor (Serial number) (if available)


numberOfStringDescriptors

protected int numberOfStringDescriptors

deviceDescriptor

protected de.thesycon.usbio.structs.USB_DEVICE_DESCRIPTOR deviceDescriptor
the USBIO device descriptor


gUsbIo

protected de.thesycon.usbio.UsbIo gUsbIo
the UsbIo interface to the device. This is assigned on construction by the factory which uses it to open the device. here is used for all USBIO access to the device


gDevList

protected int gDevList
the devlist handle for USBIO

Constructor Detail

SiLabsC8051F320_USBIO_CarServoController

public SiLabsC8051F320_USBIO_CarServoController(int n)
Creates a new instance of SiLabsC8051F320_USBIO_ServoController

Parameters:
n - the interface number

SiLabsC8051F320_USBIO_CarServoController

public SiLabsC8051F320_USBIO_CarServoController()
Creates a new instance of SiLabsC8051F320_USBIO_ServoController for interface number 0

Method Detail

onAdd

public void onAdd()
Specified by:
onAdd in interface de.thesycon.usbio.PnPNotifyInterface

onRemove

public void onRemove()
Specified by:
onRemove in interface de.thesycon.usbio.PnPNotifyInterface

close

public void close()
Closes the device. Never throws an exception.

Specified by:
close in interface HardwareInterface

getNumberOfStringDescriptors

public int getNumberOfStringDescriptors()
returns number of string descriptors

Returns:
number of string descriptors: 2 for TmpDiff128, 3 for MonitorSequencer

hasStringIdentifier

protected boolean hasStringIdentifier()
checks if device has a string identifier that is a non-empty string

Returns:
false if not, true if there is one

open

public void open()
          throws HardwareInterfaceException
constrcuts a new USB connection, opens it.

Specified by:
open in interface HardwareInterface
Throws:
HardwareInterfaceException - if there is a problem. Diagnostics are printeds.

openUsbIo

protected void openUsbIo()
                  throws HardwareInterfaceException
This method does the hard work of opening the device, downloading the firmware, making sure everything is OK. This method is synchronized to prevent multiple threads from trying to open at the same time, e.g. a GUI thread and the main thread. Opening the device after it has already been opened has no effect.

Throws:
HardwareInterfaceException - if there is a problem. Diagnostics are printed to stderr.
See Also:
close()

getStringDescriptors

public java.lang.String[] getStringDescriptors()
return the string USB descriptors for the device

Returns:
String[] of length 2 of USB descriptor strings.

getVIDPID

public int[] getVIDPID()
return the USB VID/PID of the interface

Returns:
int[] of length 2 containing the Vendor ID (VID) and Product ID (PID) of the device. First element is VID, second element is PID.

getVID

public short getVID()

getPID

public short getPID()

getDID

public short getDID()
Returns:
bcdDevice (the binary coded decimel device version

isOpen

public boolean isOpen()
reports if interface is open().

Specified by:
isOpen in interface HardwareInterface
Returns:
true if already open

getNumServos

public int getNumServos()
Specified by:
getNumServos in interface ServoInterface
Returns:
2: this controller can conrol two servos

getTypeName

public java.lang.String getTypeName()
Description copied from interface: HardwareInterface
get text name of interface, e.g. "CypressFX2" or "SiLabsC8051F320"

Specified by:
getTypeName in interface HardwareInterface

deadzoneValue

protected byte[] deadzoneValue(float value)
Computes the 2-byte deadzone value to be sent to the ucontroller for a particular dead zone. The deadzone is the range of measured pwm pulse width coming from the radio receiver which is considered to be "zero" (not actuated by pressing throttle trigger or turning steering wheel). The larger the deadzone, the less control the human driver has over small steering or throttle. The smaller the deadzone, the more control the human driver has, but the more the car will react to noisy radio input and ignore what the computer control is trying to do.

Parameters:
value - the deadzone as a float value 0-1. 0 means no deadzone, the computer almost never controls the car, 1 means complete deadzone, meaning the computer always controls the car and the remote control is disabled
Returns:
byte[] with 2 bytes in big endian format, MSB is first followed by LSB. The controller PCA counter counts at 4 MHz = period of 250 ns. On the controller this value is added and subtracted from the zero count value of 6000 (1.5ms) to see if the received pulses are outside the deadzone. A deadzone of 1 means that the value sent to the controller should be 0.5ms =2000 counts.

setServoValuePWM

public void setServoValuePWM(int servo,
                             int pwmValue)
                      throws HardwareInterfaceException
directly sends a particular short value to the servo, bypassing conversion from float. The value is subtracted from 65536 and written so that the value you write encodes the HIGH time of the PWM pulse.

Parameters:
servo - the servo number
pwmValue - the value written to servo controller is 64k minus this value
Throws:
HardwareInterfaceException

setServoValue

public void setServoValue(int servo,
                          float value)
sets servo position. The float value is translated to a value that is written to the device thar results in s pulse width that varies from 0.9 ms to 2.1 ms.

Specified by:
setServoValue in interface ServoInterface
Parameters:
servo - the servo motor, 0 based
value - the value from 0 to 1. Values out of these bounds are clipped. Special value -1f turns off the servos.
See Also:
ServoInterface.setAllServoValues(float[])

disableAllServos

public void disableAllServos()
                      throws HardwareInterfaceException
Description copied from interface: ServoInterface
disables all servos

Specified by:
disableAllServos in interface ServoInterface
Throws:
HardwareInterfaceException

disableServo

public void disableServo(int servo)
                  throws HardwareInterfaceException
sends a servo value to disable the servo

Specified by:
disableServo in interface ServoInterface
Parameters:
servo - the servo number, 0 based
Throws:
HardwareInterfaceException

setAllServoValues

public void setAllServoValues(float[] values)
                       throws HardwareInterfaceException
sets all servos to values in one transfer

Specified by:
setAllServoValues in interface ServoInterface
Parameters:
values - array of value, must have length of number of servos
Throws:
HardwareInterfaceException
See Also:
ServoInterface.getNumServos()

main

public static final void main(java.lang.String[] args)

setSteering

public void setSteering(float f)
Sets the steering angle

Parameters:
f - 0-1 value, 0.5f sets straight ahead, 0 is full left, 1 is full right

setSpeed

public void setSpeed(float f)
Sets the speed controller value.

Parameters:
f - 0-1 value, 0.5 is stopped, -.5f is full reverse, 1 is full ahead

setDeadzoneForSpeed

public void setDeadzoneForSpeed(float f)
Sets the radio "dead zone". In this range the car ignores radio input and controls speed based on the computer input. Outside this range, the radio outputs on the car (which are generated by signals recieved from the car's remote control) control speed. This capability allows for user control of the car even while under computer control.

Parameters:
f - range a value such that (0.5-range) to (0.5+range) radio inputs are ignored by the car. The larger this value, the more the user must press throttle or turn steering wheel to control car. The smaller this value, the finer the control is possible, but the more likely that noisy radio signals will result in spurious control.

setDeadzoneForSteering

public void setDeadzoneForSteering(float f)
Sets the radio "dead zone". In this range the car ignores radio input and controls speed based on the computer input. Outside this range, the radio outputs on the car (which are generated by signals recieved from the car's remote control) control speed. This capability allows for user control of the car even while under computer control.

Parameters:
f - range a value such that (0.5-range) to (0.5+range) radio inputs are ignored by the car. The larger this value, the more the user must press throttle or turn steering wheel to control car. The smaller this value, the finer the control is possible, but the more likely that noisy radio signals will result in spurious control.

getDeadzoneForSpeed

public float getDeadzoneForSpeed()

getDeadzoneForSteering

public float getDeadzoneForSteering()

getRadioSteer

public float getRadioSteer()
Returns the last steering value received from radio receiver

Returns:
0-1 value, 0.5f is straight ahead, 0 is full left, 1 is full right

getRadioSpeed

public float getRadioSpeed()
Returns the last steering value received from radio receiver

Returns:
0-1 value, 0.5 is stopped, -.5f is full reverse, 1 is full ahead

getRadioTimeoutMs

public int getRadioTimeoutMs()

setRadioTimeoutMs

public void setRadioTimeoutMs(int radioTimeoutMs)
Sets the timeout for radio control override of computer control. The computer control of the car will be locked out for this many ms after the car receives a non-zero steering or speed value.

Parameters:
radioTimeoutMs - the lockout time in ms

getLastServoValues

public float[] getLastServoValues()
Description copied from interface: ServoInterface
Returns last servo values sent.

Specified by:
getLastServoValues in interface ServoInterface

getLastServoValue

public float getLastServoValue(int servo)
Description copied from interface: ServoInterface
Returns last servo value sent (0 before sending a value)

Specified by:
getLastServoValue in interface ServoInterface

setPort2

public void setPort2(int portValue)
Description copied from interface: ServoInterface
sends a command to set the port 2 output (on the side of the original board) to portValue. This port is presently set to open-drain mode on all bits.

Specified by:
setPort2 in interface ServoInterface
Parameters:
portValue - the bits to set

jAER project on SourceForge