ReduxLib C++ 2023.1.0
Loading...
Searching...
No Matches
redux::canand::CANandDevice Class Referenceabstract

#include <CANandDevice.h>

Inheritance diagram for redux::canand::CANandDevice:
redux::sensors::canandcoder::CANandcoder

Public Member Functions

virtual void HandleMessage (CANandMessage &msg)=0
 
virtual CANandAddressGetAddress ()=0
 

Protected Member Functions

void SetSettingById (uint8_t settingId, uint8_t *value, uint8_t length)
 
void HandleSettingRecv (uint8_t settingId, uint64_t settingValue)
 
std::optional< uint64_t > ConfirmSetSetting (uint8_t settingIdx, uint8_t *payload, uint8_t length, units::second_t timeout)
 
bool SendCANMessage (uint8_t apiIndex, uint8_t *data, uint8_t length)
 
template<std::size_t len>
requires (len < 8U)
void SendCANMessage (uint8_t msgId, std::span< std::byte, len > data)
 

Detailed Description

The base class for all CAN-communicating Redux Robotics device classes. The ReduxLib vendordep does all CAN message parsing in the Java/C++ classes themselves. 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:

  • Implement CANandDevice::getAddress (usually by instantiating a CANandAddress in the constructor and returning it)
  • Implement CANandDevice::HandleMessage which will be called asynchronously whenever new CAN messages matching the object's CANandAddress get received by the robot
  • Run redux::canand::AddCANListener(this) in the constructor so HandleMessage actually gets called at runtime
  • Run redux::canand::RemoveCANListener(this) in the destructor.

Member Function Documentation

◆ ConfirmSetSetting()

std::optional< uint64_t > redux::canand::CANandDevice::ConfirmSetSetting ( uint8_t  settingIdx,
uint8_t *  payload,
uint8_t  length,
units::second_t  timeout 
)
protected

Potentially blocking operation to send a setting and wait for a report setting message to be received to confirm the operation.

In order for this method to work on subclasses, CANandDevice::HandleSettingsRecv must be called in its CANandDevice::HandleMessage implementation when REPORT_SETTING is received.

Parameters
settingIdxSetting index to set and listen for
payloadpointer to the setting payload to send. Only first 6 bytes are used.
lengthlength of input buffer (1-6)
timeoutthe timeout to wait before giving up in seconds. Passing in 0 will return instantly (not block)
Returns
the value received by the report setting packet if existent or std::nullopt otherwise. If timeout = 0, return -1.

◆ GetAddress()

virtual CANandAddress & redux::canand::CANandDevice::GetAddress ( )
pure virtual

Returns the reference to 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
a reference to the CANandAddress for the device.

Implemented in redux::sensors::canandcoder::CANandcoder.

◆ HandleMessage()

virtual void redux::canand::CANandDevice::HandleMessage ( CANandMessage msg)
pure virtual

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 CANandDevice::GetAddress().

Parameters
msga reference to a CANandMessage representing the received message. The message may not have lifetime outside the function call.

Implemented in redux::sensors::canandcoder::CANandcoder.

◆ HandleSettingRecv()

void redux::canand::CANandDevice::HandleSettingRecv ( uint8_t  settingId,
uint64_t  settingValue 
)
protected

Method to be called in a CANandDevice::HandleMessage implementation to notify CANandDevice::ConfirmSetSetting calls of a report setting message.

Parameters
settingIdSetting index received as a byte
settingValueSetting value as an unsigned long

◆ SendCANMessage() [1/2]

bool redux::canand::CANandDevice::SendCANMessage ( uint8_t  apiIndex,
uint8_t *  data,
uint8_t  length 
)
inlineprotected

Sends a CAN message to the CANandAddress.

Parameters
apiIndexthe API index the message should have (between 0-31 inclusive)
data1-8 bytes of payload, as an array or pointer
lengththe length of the the payload buffer
Returns
if the operation was successful

◆ SendCANMessage() [2/2]

template<std::size_t len>
requires (len < 8U)
void redux::canand::CANandDevice::SendCANMessage ( uint8_t  msgId,
std::span< std::byte, len >  data 
)
inlineprotected

Send a CAN message directly to the device, but properly length checked.

Parameters
msgIdthe individual API index to value to send
data1-8 byte payload std:span of std:byte

◆ SetSettingById()

void redux::canand::CANandDevice::SetSettingById ( uint8_t  settingId,
uint8_t *  value,
uint8_t  length 
)
protected

Directly sends a CAN message to a CANandDevice to set a setting by index.

Device subclasses will usually have a more user-friendly settings interface, eliminating the need to call this function directly in the vast majority of cases.

Parameters
settingIdthe setting id value to transmit
valuea pointer to a 1-6 byte buffer to transmit as payload
lengththe length of the input buffer

The documentation for this class was generated from the following file: