How a pointer is created from CAN msg and a structure?

asked 2022-06-28 12:47:13 -0500

updated 2022-07-02 07:45:50 -0500

lucasw gravatar image

In a ROS node, I receive a "CAN" msg and the callback is like this.

const MsgReportWheelSpeed *ptr = (const MsgReportWheelSpeed*)msg->data.elems; 
WheelSpeedReport out;
out.header.stamp = msg->header.stamp;
out.front_left = (float)ptr->front_left * 0.01f;
out.front_right = (float)ptr->front_right * 0.01f;

The received CAN msg is like below. I am interested in the last line of the msg which is an array. Although I understand how pointer and structure work, I am confused how exactly pointer "ptr" is created here in the first line of the callback code and how data is being assigned in the 4th and 5th line of the code.

Header header
uint32 id
bool is_rtr
bool is_exrended
bool is_error
uint8 dlc
unit8[8] data

Here "WheelSpeedReport" msg is like this:

Header header
float32 front_left
float32 front_right

And "MsgReportWheelSpeed" is a structure like this:

tyedef struct{
int16_t front_left;
int16_t front_right;
} MsgReportWheelSpeed ;
edit retag flag offensive close merge delete

Comments

It's a general C/C++ data conversion approach (not ROS related) especially when involving messages that came over a network (or CAN in your case) take a look at https://stackoverflow.com/questions/1... and probably better stuff elsewhere. The 8 uint8 bytes correspond to the two 4-byte floats in the speed struct, the pointer allows reinterpreting them as floats.

lucasw gravatar image lucasw  ( 2022-07-02 08:13:38 -0500 )edit