Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Loading...
Searching...
No Matches
can Namespace Reference

Utilities for interacting with CAN devices. More...

Namespaces

namespace  motor
 Utilities for interacting with motor boards over a CAN interface.
 

Classes

struct  packettype_t
 The types of CAN packets that we recognize. More...
 

Typedefs

using callbackid_t = std::tuple<deviceid_t, telemtype_t, uint32_t>
 An ID for a telemetry callback.
 
using deviceserial_t = uint8_t
 The type of the device serial number.
 
using telemetry_t = int32_t
 The type of telemetry data.
 
using deviceid_t = std::pair<devicegroup_t, deviceserial_t>
 A unique identifier for a CAN device.
 

Enumerations

enum class  devicegroup_t {
  broadcast = DEVICE_GROUP_BROADCAST , reserved = DEVICE_GROUP_RESERVED , master = DEVICE_GROUP_MASTER , power = DEVICE_GROUP_POWER ,
  motor = DEVICE_GROUP_MOTOR_CONTROL , telemetry = DEVICE_GROUP_TELEMETRY , gpio = DEVICE_GROUP_GPIO_BOARDS , science = DEVICE_GROUP_SCIENCE
}
 The possible device group codes. More...
 
enum class  telemtype_t {
  voltage = PACKET_TELEMETRY_VOLTAGE , current = PACKET_TELEMETRY_CURRENT , pwr_rail = PACKET_TELEMETRY_PWR_RAIL_STATE , temp = PACKET_TELEMETRY_TEMPERATURE ,
  angle = PACKET_TELEMETRY_ANG_POSITION , gps_lat = PACKET_TELEMETRY_GPS_LAT , gps_lon = PACKET_TELEMETRY_GPS_LON , mag_dir = PACKET_TELEMETRY_MAG_DIR ,
  accel_x = PACKET_TELEMETRY_ACCEL_X , accel_y = PACKET_TELEMETRY_ACCEL_Y , accel_z = PACKET_TELEMETRY_ACCEL_Z , gyro_x = PACKET_TELEMETRY_GYRO_X ,
  gyro_y = PACKET_TELEMETRY_GYRO_Y , gyro_z = PACKET_TELEMETRY_GYRO_Z , limit_switch = PACKET_TELEMETRY_LIM_SW_STATE , adc_raw = PACKET_TELEMETRY_ADC_RAW
}
 The type of telemetries that devices can report. More...
 

Functions

void initCAN ()
 Initialize the CAN interface.
 
void sendCANPacket (const CANPacket &packet)
 Send a CAN packet.
 
robot::types::DataPoint< telemetry_tgetDeviceTelemetry (deviceid_t id, telemtype_t telemType)
 Get the latest telemetry from a CAN device.
 
void pullDeviceTelemetry (deviceid_t id, telemtype_t telemType)
 Ping the given CAN device to send the given telemetry data.
 
void scheduleTelemetryPull (deviceid_t id, telemtype_t telemType, std::chrono::milliseconds period)
 Periodically pull the latest telemetry data from the specified CAN device asychronously.
 
void unscheduleTelemetryPull (deviceid_t id, telemtype_t telemType)
 Stop pulling the latest telemetry data from the given device.
 
void unscheduleAllTelemetryPulls ()
 Stop pulling the latest telemetry data from all currently scheduled devices.
 
callbackid_t addDeviceTelemetryCallback (deviceid_t id, telemtype_t telemType, const std::function< void(deviceid_t, telemtype_t, robot::types::DataPoint< telemetry_t >)> &callback)
 
void removeDeviceTelemetryCallback (callbackid_t id)
 Remove a previously registered telemetry callback.
 
callbackid_t addDeviceTelemetryCallback (deviceid_t id, telemtype_t telemType, const std::function< void(deviceid_t, telemtype_t, robot::types::DataPoint< telemetry_t >)> &callback)
 Add a callback which is invoked when data is recieved.
 
devicegroup_t getDeviceGroup (const CANPacket &packet)
 Get the device group from the id of the given packet.
 
deviceserial_t getDeviceSerial (const CANPacket &packet)
 Get the serial number from the id of the given packet.
 
deviceid_t getDeviceGroupAndSerial (const CANPacket &packet)
 Get the unique identifier from the id of the given packet.
 
deviceserial_t getSenderDeviceSerial (const CANPacket &packet)
 Get the device serial code of the sender of the given packet.
 
devicegroup_t getSenderDeviceGroup (const CANPacket &packet)
 Get the device group of the sender of the given packet.
 
deviceid_t getSenderDeviceGroupAndSerial (const CANPacket &packet)
 Get the device group and serial code of the sender of the given packet.
 
std::string packetToString (const CANPacket &packet)
 Get a string representation of the given CAN packet.
 

Detailed Description

Utilities for interacting with CAN devices.

Typedef Documentation

◆ callbackid_t

An ID for a telemetry callback.

Users should not construct these themselves.

◆ deviceid_t

A unique identifier for a CAN device.

A CAN device is uniquely identified by its group code and its serial number.

Enumeration Type Documentation

◆ devicegroup_t

enum class can::devicegroup_t
strong

The possible device group codes.

Every can device is in a group. These are the possible groups.

◆ telemtype_t

enum class can::telemtype_t
strong

The type of telemetries that devices can report.

A device may offer some or none of these telemetry types.

Function Documentation

◆ addDeviceTelemetryCallback()

callbackid_t can::addDeviceTelemetryCallback ( deviceid_t id,
telemtype_t telemType,
const std::function< void(deviceid_t, telemtype_t, robot::types::DataPoint< telemetry_t >)> & callback )

Add a callback which is invoked when data is recieved.

The callback is invoked when telemetry data of the given type is received from the given device.

Parameters
idThe ID of the device the callback is listening for.
telemTypeThe type of telemetry the callback is listening for.
callbackThe callback that will be invoked with the device ID, telemetry type, and the telemetry data.
Returns
callbackid_t A callback ID, which can be used with removeDeviceTelemetryCallback() to remove a callback.

◆ getDeviceGroup()

devicegroup_t can::getDeviceGroup ( const CANPacket & packet)

Get the device group from the id of the given packet.

Parameters
packetThe packet to extract the group from.
Returns
devicegroup_t The extracted device group.

◆ getDeviceGroupAndSerial()

deviceid_t can::getDeviceGroupAndSerial ( const CANPacket & packet)

Get the unique identifier from the id of the given packet.

Parameters
packetThe packet ot extract the identifier from.
Returns
deviceid_t The extracted device identifier.

◆ getDeviceSerial()

deviceserial_t can::getDeviceSerial ( const CANPacket & packet)

Get the serial number from the id of the given packet.

Parameters
packetThe packet to extract the serial number from.
Returns
deviceserial_t The extracted serial number.

◆ getDeviceTelemetry()

robot::types::DataPoint< telemetry_t > can::getDeviceTelemetry ( deviceid_t id,
telemtype_t telemType )

Get the latest telemetry from a CAN device.

This method does NOT query for new data, it just returns the last reported value.

Parameters
idThe device group and serial number of the device.
telemTypeThe type of telemetry to get, as dictated by the specific device specs.
Returns
robot::types::DataPoint<telemetry_t> The telemetry value, with the timestamp of when it was received. If no data is available for the given telemetry type, an empty data point is returned.

◆ getSenderDeviceGroup()

devicegroup_t can::getSenderDeviceGroup ( const CANPacket & packet)

Get the device group of the sender of the given packet.

Parameters
packetThe packet to extract the device group from.
Returns
devicegroup_t The device group of the sender.

◆ getSenderDeviceGroupAndSerial()

deviceid_t can::getSenderDeviceGroupAndSerial ( const CANPacket & packet)

Get the device group and serial code of the sender of the given packet.

Parameters
packetThe packet to extract the information from.
Returns
deviceid_t The device group and serial code of the sender.

◆ getSenderDeviceSerial()

deviceserial_t can::getSenderDeviceSerial ( const CANPacket & packet)

Get the device serial code of the sender of the given packet.

Parameters
packetThe packet to extract the sender code from.
Returns
deviceserial_t The serial code of the sender.

◆ initCAN()

void can::initCAN ( )

Initialize the CAN interface.

This should only be called once.

Note
If CAN initialization fails, the program will exit.

◆ packetToString()

std::string can::packetToString ( const CANPacket & packet)

Get a string representation of the given CAN packet.

The returned string representation is in the format specified by cansend, which allows easy copy-pasting.

See also
http://manpages.ubuntu.com/manpages/focal/man1/cansend.1.html
Parameters
packetThe packet to convert to a string.
Returns
std::string The string representation of the given packet.

◆ pullDeviceTelemetry()

void can::pullDeviceTelemetry ( deviceid_t id,
telemtype_t telemType )

Ping the given CAN device to send the given telemetry data.

The CAN device will asynchronously send the new data in an unspecified amount of time. Not all CAN devices may support pulling telemetry.

Parameters
idThe device group and serial number of the device.
telemTypeThe type of telemetry to get, as dictated by the specific device specs.

◆ removeDeviceTelemetryCallback()

void can::removeDeviceTelemetryCallback ( callbackid_t id)

Remove a previously registered telemetry callback.

The callback associated with the given callback ID is removed.

Parameters
idA callback ID which was previously returned by addDeviceTelemetryCallback().

◆ scheduleTelemetryPull()

void can::scheduleTelemetryPull ( deviceid_t id,
telemtype_t telemType,
std::chrono::milliseconds period )

Periodically pull the latest telemetry data from the specified CAN device asychronously.

This method is NOT thread safe.

Parameters
idThe device group and serial number of the device.
telemTypeThe type of telemetry to get, as dictated by the specific device specs.
periodThe period to wait in between sending pull requests.

◆ sendCANPacket()

void can::sendCANPacket ( const CANPacket & packet)

Send a CAN packet.

No further formatting is done on the packet. This method is thread-safe.

Parameters
packetThe CAN packet to send.

◆ unscheduleAllTelemetryPulls()

void can::unscheduleAllTelemetryPulls ( )

Stop pulling the latest telemetry data from all currently scheduled devices.

This method is NOT thread safe.

◆ unscheduleTelemetryPull()

void can::unscheduleTelemetryPull ( deviceid_t id,
telemtype_t telemType )

Stop pulling the latest telemetry data from the given device.

This method is NOT thread safe.

Parameters
idThe device group and serial number of the device.
telemTypeThe type of telemetry to get, as dictated by the specific device specs.