APIs description

Doxygen complete API documentation

Doxygen documentation Link to doxygen API documentation

Available classes

CAN_DRIVER

class CAN_DRIVER

This class provides an interface for communicating with a CAN bus, simplifying both emission/reception.

Public Functions

inline CAN_DRIVER(CAN_ENUM::BAUDRATE baudrate, uint8_t pinRX, uint8_t pinTX, streamLogger *streamObj, timerTool *timerToolObj, bool loopbackMode = true)

Constructs a new CAN_DRIVER object.

Complete constructor for the CAN_DRIVER class. Begin method can be called immediately after

Parameters:
  • baudrate – The baudrate of the CAN communication.

  • pinRX – The RX pin of the CAN transceiver.

  • pinTX – The TX pin of the CAN transceiver.

  • streamObj – A pointer to the streamLogger object used for logging.

  • timerToolObj – A pointer to the timerTool object used for timing.

  • loopbackMode – Whether to enable or not loopback mode.

inline CAN_DRIVER(streamLogger *streamObj, timerTool *timerToolObj, bool loopbackMode = true)

Constructs a new CAN_DRIVER object.

Partial constructor for the CAN_DRIVER class. setPins and setBaudrate methods have to be called before calling begin method.

Parameters:
  • streamObj – Pointer to a streamLogger object for logging messages.

  • timerToolObj – Pointer to a timerTool object for timing operations.

  • loopbackMode – Whether to enable or not loopback mode.

bool begin()

Initializes the CAN bus communication.

Install and initialize CAN driver

Returns:

true if the initialization was successful, false otherwise.

inline bool getIsReady() const

Returns the current readiness status of the CAN driver.

Returns:

true if the object is ready, false otherwise.

void setPins(gpio_num_t pinRX, gpio_num_t pinTX)

Sets the RX and TX pins for the CAN bus communication.

Parameters:
  • pinRX – The GPIO pin number for the CAN bus RX pin.

  • pinTX – The GPIO pin number for the CAN bus TX pin.

void setBaudrate(CAN_ENUM::BAUDRATE baudrate)

Sets the baudrate of the CAN bus.

Parameters:

baudrate – The desired baudrate to set.

bool sendAllFrames()

Sends all registered and enabled CAN frames.

This function sends all registered and enabled CAN frames that have been added to the transmit buffer using the addFrame function.

Note

After the use to addVariable function, the signal and eventually a frame are automatically added to the transmit buffer. The user is supposed tu update the variable and call this function to send the frame with the signal updated

Note

This function implies synchronous sending. All enabled signals/frames will be sent at the same time. If asynchronous sending is needed, the user can also keep a reference of each frame, use serializeFrame then sendFrame manually. Another solution is to use these references to enable/disable frames or signals and call this function.

Returns:

true if frames were sent successfully, false otherwise.

bool serializeAndSendFrame(uint32_t frameId)

Serializes and sends a CAN frame with the given ID.

This function serializes the data in the CAN frame and sends it over the CAN bus. The frame ID is used to retrieve the frame from the frame vector. The user is supposed to update the frames messages before calling this function.

Parameters:

frameId – The ID of the CAN frame to send.

Returns:

true if the frame was successfully sent, false otherwise.

bool serializeAndSendFrame(const CAN_DATATYPES::frameStruct *frame)

Serializes a CAN frame and sends it over the bus.

Overload version of serializeAndSendFrame(uint32_t frameId). This function takes a CAN frame as input, serializes it, and sends it over the CAN bus.

Parameters:

frame – The CAN frame to be serialized and sent.

Returns:

True if the frame was successfully sent, false otherwise.

bool receiveFrames()

Receives CAN frames from the bus.

This function receives CAN frames from the bus and stores them in mRawMessageRxBuffer buffer.

Note

also update all dysfunctional indicators

Returns:

true if frames were successfully received, false otherwise.

float decodeFrame(twai_message_t message, uint8_t startBit, uint8_t signalSize, CAN_ENUM::CAN_CODING_TYPE coding, CAN_ENUM::DATATYPE datatype, float factor, float offset) const

Decodes a signal from a CAN message frame.

Attention

This function is not implemented yet.

Parameters:
  • message – The CAN message frame to decode the signal from.

  • startBit – The bit position of the signal within the message frame.

  • signalSize – The size of the signal in bits.

  • coding – The coding type used for the signal.

  • datatype – The data type of the signal.

  • factor – The scaling factor to apply to the decoded value.

  • offset – The offset to apply to the decoded value.

Returns:

The decoded value of the signal.

bool decodeRawRxBuffer(uint32_t frameId, CAN_DATATYPES::frameStruct &frame)

Decodes the raw receive buffer data.

This function decodes the raw receive buffer data and populates mDecodedFramesRxBuffer with frameStruct

Note

This function has to be called for any Id that needs to be decoded. Do not forget to reset decodedBuffer at the beginning of the loop!

Parameters:
  • frameId – The ID of the frame to decode.

  • frame – The CAN frame structure to fill

Returns:

true if the decoding was successful, false otherwise.

void printMappingInfo(bool logSerialMonitor, bool logStreamLogger)

Prints information about the CAN mapping (frames and signals)

This function prints information about the mapping to the serial monitor and/or on a storage with streamLogger.

Parameters:
  • logSerialMonitor – Whether to log the information to the serial monitor.

  • logStreamLogger – Whether to log the information to a storage with streamLogger.

void printSignalInfo(CAN_DATATYPES::signalStruct signal, bool logSerialMonitor, bool logStreamLogger)

Prints information about a CAN signal.

Parameters:
  • signal – The CAN signal to print information about.

  • logSerialMonitor – Whether to log the information to the serial monitor.

  • logStreamLogger – Whether to log the information to the stream logger.

template<typename T>
inline bool addVariable(T *variable, String name, uint32_t frameId, String frameName, bool forceAdd = false)

Adds a variable to the CAN signal list.

The function will append signal to existing frame or create a new one if necessary.

Template Parameters:

T – The type of the variable to add.

Parameters:
  • variable – A pointer to the variable to add.

  • name – The name of the signal.

  • frame – The frame ID of the signal.

  • frameName – The name of the frame.

  • forceAdd – Whether to force the addition of the variable to the signal list.

Returns:

true if the variable was added successfully, false otherwise.

inline CAN_DATATYPES::frameStruct *getFrameFromID(uint32_t frameID)

Retrieves a CAN frame from the internal buffer based on its ID.

Parameters:

frameID – The ID of the frame to retrieve.

Returns:

A pointer to the retrieved frame, or nullptr if the frame was not found.

inline uint32_t getFrameNumber() const

Returns the frame number in the library frame vector.

Returns:

The frame number of the frame vector

inline const std::array<twai_message_t, CAN_BUFFER_MAX_FRAME_NUMBER> *getRawRxBuffer() const

Returns a pointer to the buffer containing received CAN messages.

Returns:

A pointer to the reception buffer.

inline const uint32_t getRawBufferSize() const

Returns the size of the raw buffer used by this object.

Returns:

The size of the raw buffer in bytes.

inline CAN_DATATYPES::signalStruct *getSignalFromName(String signalName)

Retrieves a signal structure from the given signal name.

Parameters:

signalName – The name of the signal to retrieve.

Returns:

A pointer to the signal structure, or nullptr if the signal was not found.

bool recoverBus()

Attempts to recover bus until the maximum number of attempts is reached.

Returns:

true if the recovery was successful, false otherwise.

inline void monitorBus()

Monitor the CAN bus and take actions accordingly.

This function checks the health of the CAN bus with checking errors and statuses. It can disable the bus if too much errors are detected, restart it, and allow recover.

Note

The user of the can driver is highly encouraged to call this function regularly to avoid blockings and allow bus recovery

inline CAN_ENUM::HEALTH getHealth() const

Returns the current health status of the CAN driver.

Returns:

The current health status of the CAN driver.

CAN_DATATYPES

struct CAN_DATATYPES

This structs contains the definition of structs used to represent frames and signals in a CAN bus.

The frameStruct struct represents a frame in a CAN bus, containing information such as the signals it contains, its name, ID, size, and whether it is enabled or not.

The signalStruct struct represents a signal in a CAN bus, containing information such as its name, data type, and whether it is valid or not.

Additionally, this struct defines several unions used to convert between different data types and their binary representation.

struct CONVERTERS
struct frameStruct

A struct representing a CAN frame.

Public Functions

inline void setEnabledState(bool state)

Set the enabled state of the frame.

Parameters:

state – The state to set the enabled flag to.

inline void reset()

Resets the state of the object to its default values.

struct signalStruct

A struct representing a CAN signal.

CAN_ENUM

struct CAN_ENUM

This struct contains enums for CAN coding type, data type, and baud rate.

CAN_CONSTANTS

struct CAN_CONSTANTS

Defines constants for the size of data in bytes.

Public Static Attributes

static const uint8_t SIZE_1_BYTE = 1

Size of data in bytes is 1.

static const uint8_t SIZE_2_BYTES = 2

Size of data in bytes is 2.

static const uint8_t SIZE_4_BYTES = 4

Size of data in bytes is 4.

static const uint8_t SIZE_8_BYTES = 8

Size of data in bytes is 8.