Class CanandDevice

java.lang.Object
com.reduxrobotics.canand.CanandDevice
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
Canandgyro, Canandmag

public abstract class CanandDevice extends Object implements AutoCloseable
Base class for Redux CAN devices. The ReduxLib vendordep does all CAN message parsing in the Java/C++ classes themselves, rather than the driver. CanandDevice provides general facilities for sending and receiving CAN messages (abstracted from the actual underlying buses) as well as helper functions and constants common for all Redux CAN products. Classes implementing CanandDevice need to do the following:
  • Field Details

  • Constructor Details

    • CanandDevice

      public CanandDevice()
      Default constructor that just adds the device to the incoming CAN message listener.
  • Method Details

    • handleMessage

      public abstract void handleMessage(CanandMessage msg)
      A callback called when a Redux CAN message is received and should be parsed. Subclasses of CanandDevice 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 by getAddress().

      Parameters:
      msg - a CanandMessage representing the received message.
    • getAddress

      public abstract CanandAddress getAddress()
      Returns the CanandAddress 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

      public void preHandleMessage(CanandMessage msg)
      Called before handleMessage(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 - a CanandMessage 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

      public CanandFirmwareVersion 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

      public CanandFirmwareVersion 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 send
      data - 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 send
      data - 0-8 byte bytes payload (little endian long)
      length - length of data [0..8] inclusive
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable