Class CanandDevice
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
Canandgyro
,Canandmag
-
Implement
getAddress()
, usually by instantiating aCanandAddress
in the constructor and returning it - Implement
handleMessage(com.reduxrobotics.canand.CanandMessage)
which will be called asynchronously whenever new CAN messages matching the object'sCanandAddress
get received by the robot -
Run super() in the constructor so
handleMessage(com.reduxrobotics.canand.CanandMessage)
actually gets called at runtime
-
Field Summary
Modifier and TypeFieldDescriptionprotected double
The last received message timestamp. -
Constructor Summary
ConstructorDescriptionDefault constructor that just adds the device to the incoming CAN message listener. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Checks the received firmware version.void
close()
abstract CanandAddress
Returns theCanandAddress
representing the combination of CAN bus and CAN device ID that this CanandDevice refers to.Returns the firmware version received by the device.Returns the minimum firmware version this vendordep requires for this device.abstract void
A callback called when a Redux CAN message is received and should be parsed.boolean
Checks whether or not the device has sent a message within the last 2000 milliseconds.boolean
isConnected
(double timeout) Checks whether or not the device has sent a message within the last timeout seconds.void
Called beforehandleMessage(com.reduxrobotics.canand.CanandMessage)
gets called to run some common logic.void
sendCANMessage
(int apiIndex, byte[] data) Directly sends a CAN message to the device.void
sendCANMessage
(int apiIndex, long data, int length) Directly sends a CAN message to the device.toString()
-
Field Details
-
lastMessageTs
protected double lastMessageTsThe last received message timestamp. This is timed with respect to the FPGA timer, and is updated beforehandleMessage(com.reduxrobotics.canand.CanandMessage)
gets called inpreHandleMessage(com.reduxrobotics.canand.CanandMessage)
.
-
-
Constructor Details
-
CanandDevice
public CanandDevice()Default constructor that just adds the device to the incoming CAN message listener.
-
-
Method Details
-
handleMessage
A callback called when a Redux CAN message is received and should be parsed. Subclasses ofCanandDevice
should override this to update their internal state accordingly.handleMessage will be called on all Redux CAN packets received by the vendordep that match the
CanandAddress
returned bygetAddress()
.- Parameters:
msg
- aCanandMessage
representing the received message.
-
getAddress
Returns theCanandAddress
representing the combination of CAN bus and CAN device ID that this CanandDevice refers to.Implementing device subclasses should likely construct a new
CanandAddress
in their constructor and return it here.- Returns:
- the
CanandAddress
for the device.
-
preHandleMessage
Called beforehandleMessage(com.reduxrobotics.canand.CanandMessage)
gets called to run some common logic. (namely, handling setting receives and last message times) This function can be overridden to change or disable its logic.- Parameters:
msg
- aCanandMessage
representing the received message.
-
isConnected
public boolean isConnected(double timeout) Checks whether or not the device has sent a message within the last timeout seconds.- Parameters:
timeout
- window to check for message updates in seconds- Returns:
- true if there has been a message within the last timeout seconds, false if not
-
isConnected
public boolean isConnected()Checks whether or not the device has sent a message within the last 2000 milliseconds.- Returns:
- true if there has been a message within the last 2000 milliseconds, false if not
-
checkReceivedFirmwareVersion
protected void checkReceivedFirmwareVersion()Checks the received firmware version.If no firmware version has been received, complain to the driver station about potentially missing devices from the bus.
If the reported firmware version is too old, also complain to the driver station.
-
getMinimumFirmwareVersion
Returns the minimum firmware version this vendordep requires for this device. User-implmenting classes can return null to disable firmware checks.- Returns:
- minimum firmware version
-
getFirmwareVersion
Returns the firmware version received by the device. May return null if a firmware version has not been received from the device yet.- Returns:
- the received firmware version or null
-
sendCANMessage
public void sendCANMessage(int apiIndex, byte[] data) Directly sends a CAN message to the device.Implementing device classes should call this function to send messages to the device, or use
getAddress().sendCANMessage(int, byte[])
, which this aliases.- Parameters:
apiIndex
- the individual API index to value to senddata
- 0-8 byte bytes payload
-
sendCANMessage
public void sendCANMessage(int apiIndex, long data, int length) Directly sends a CAN message to the device.Implementing device classes should call this function to send messages to the device, or use
getAddress().sendCANMessage(int, long, int)
, which this aliases.- Parameters:
apiIndex
- the individual API index to value to senddata
- 0-8 byte bytes payload (little endian long)length
- length of data [0..8] inclusive
-
toString
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-