Class CAN

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class CAN
    extends Object
    implements Closeable
    High level class for interfacing with CAN devices conforming to the standard CAN spec.

    No packets that can be sent gets blocked by the RoboRIO, so all methods work identically in all robot modes.

    All methods are thread safe, however the CANData object passed into the read methods and the byte[] passed into the write methods need to not be modified for the duration of their respective calls.

    • Constructor Detail

      • CAN

        public CAN​(int deviceId)
        Create a new CAN communication interface with the specific device ID. This uses the team manufacturer and device types. The device ID is 6 bits (0-63).
        Parameters:
        deviceId - The device id
      • CAN

        public CAN​(int deviceId,
                   int deviceManufacturer,
                   int deviceType)
        Create a new CAN communication interface with a specific device ID, manufacturer and device type. The device ID is 6 bits, the manufacturer is 8 bits, and the device type is 5 bits.
        Parameters:
        deviceId - The device ID
        deviceManufacturer - The device manufacturer
        deviceType - The device type
    • Method Detail

      • writePacket

        public void writePacket​(byte[] data,
                                int apiId)
        Write a packet to the CAN device with a specific ID. This ID is 10 bits.
        Parameters:
        data - The data to write (8 bytes max)
        apiId - The API ID to write.
      • writePacketRepeating

        public void writePacketRepeating​(byte[] data,
                                         int apiId,
                                         int repeatMs)
        Write a repeating packet to the CAN device with a specific ID. This ID is 10 bits. The RoboRIO will automatically repeat the packet at the specified interval
        Parameters:
        data - The data to write (8 bytes max)
        apiId - The API ID to write.
        repeatMs - The period to repeat the packet at.
      • stopPacketRepeating

        public void stopPacketRepeating​(int apiId)
        Stop a repeating packet with a specific ID. This ID is 10 bits.
        Parameters:
        apiId - The API ID to stop repeating
      • readPacketNew

        public boolean readPacketNew​(int apiId,
                                     CANData data)
        Read a new CAN packet. This will only return properly once per packet received. Multiple calls without receiving another packet will return false.
        Parameters:
        apiId - The API ID to read.
        data - Storage for the received data.
        Returns:
        True if the data is valid, otherwise false.
      • readPacketLatest

        public boolean readPacketLatest​(int apiId,
                                        CANData data)
        Read a CAN packet. The will continuously return the last packet received, without accounting for packet age.
        Parameters:
        apiId - The API ID to read.
        data - Storage for the received data.
        Returns:
        True if the data is valid, otherwise false.
      • readPacketTimeout

        public boolean readPacketTimeout​(int apiId,
                                         int timeoutMs,
                                         CANData data)
        Read a CAN packet. The will return the last packet received until the packet is older then the requested timeout. Then it will return false.
        Parameters:
        apiId - The API ID to read.
        timeoutMs - The timeout time for the packet
        data - Storage for the received data.
        Returns:
        True if the data is valid, otherwise false.
      • readPeriodicPacket

        public boolean readPeriodicPacket​(int apiId,
                                          int timeoutMs,
                                          int periodMs,
                                          CANData data)
        Read a CAN packet. The will return the last packet received until the packet is older then the requested timeout. Then it will return false. The period parameter is used when you know the packet is sent at specific intervals, so calls will not attempt to read a new packet from the network until that period has passed. We do not recommend users use this API unless they know the implications.
        Parameters:
        apiId - The API ID to read.
        timeoutMs - The timeout time for the packet
        periodMs - The usual period for the packet
        data - Storage for the received data.
        Returns:
        True if the data is valid, otherwise false.