Hi all, i'm working on a canbus connected GPS + IMU.
i'm tring to send the data over the canbus but i'm having some issues with the data formats.
I'm using 3 kind of unions, one with 32 bit for latitude and longitude
And two16 bit one for signed quantities and another for unsigned variables
With the GPS all works fine but i'm strugggling with the IMU values, I have the Roll Pitch and Yaw valuee stored in 3 floats variables I multiply the floats by the scaling factor (let's say is 100) and then I store them in the unint16_t and int16_t respectively, then i fill the bites of the canbus frames with the .b values and i send the frames.
When it comes to read the canbus (I've tryed with my Marelli Motorsport logger and with Busmaster and a PCAN Usb getting the same values) comes the troubles:
My float value is let's say is -1.23123 i multiply it by 100 and i get -123.123 that sent to the int16_t variable will become -123, the 2 bytes then are:
FF and 85
So roll.f = -123
roll.b[1] = FF
roll.b[0] = 85
Since arduino and teensy are small endian and the system receiving them is Big Endian and this message is using byte 0 and byte 1 of my can frame i store the values as
can_dof1.buf[0]=roll.b[1];
can_dof1.buf[1]=roll.b[0];
If a read the frame i correctly see FF as byte 0 and 85 as byte 1 but wen i revert it to an int number i get 654.130 instead of -123 !
If i set the signal as a little endian and i swap the bytes is still have 654.130 !!!
I guess the can systems are representing negative values in a different way ? Any help apreciated !!!
What i'm doing wrong ??
i'm tring to send the data over the canbus but i'm having some issues with the data formats.
I'm using 3 kind of unions, one with 32 bit for latitude and longitude
Code:
union conv
{
int32_t f;
uint8_t b[4];
};
Code:
union conv_short
{
int16_t f;
uint8_t b[2];
};
union conv_short_u
{
uint16_t f;
uint8_t b[2];
};
With the GPS all works fine but i'm strugggling with the IMU values, I have the Roll Pitch and Yaw valuee stored in 3 floats variables I multiply the floats by the scaling factor (let's say is 100) and then I store them in the unint16_t and int16_t respectively, then i fill the bites of the canbus frames with the .b values and i send the frames.
When it comes to read the canbus (I've tryed with my Marelli Motorsport logger and with Busmaster and a PCAN Usb getting the same values) comes the troubles:
My float value is let's say is -1.23123 i multiply it by 100 and i get -123.123 that sent to the int16_t variable will become -123, the 2 bytes then are:
FF and 85
So roll.f = -123
roll.b[1] = FF
roll.b[0] = 85
Since arduino and teensy are small endian and the system receiving them is Big Endian and this message is using byte 0 and byte 1 of my can frame i store the values as
can_dof1.buf[0]=roll.b[1];
can_dof1.buf[1]=roll.b[0];
If a read the frame i correctly see FF as byte 0 and 85 as byte 1 but wen i revert it to an int number i get 654.130 instead of -123 !
If i set the signal as a little endian and i swap the bytes is still have 654.130 !!!
I guess the can systems are representing negative values in a different way ? Any help apreciated !!!
What i'm doing wrong ??