10 years ago.

Converting a float variable into Highbyte and Lowbyte (char-array) for sending via CAN

Hello,

I read from an acceleration sensor register the high and low byte (2x 8Bit) of e.g. the x-acceleration and convert it to a float variable for further complexer calculations. Afterwards I want to send the calculation result via CAN.

Now, when I have a float variable xy, what is the fastest way to convert this float variable back into a Highbyte and Lowbyte (char array) to send it via CAN on the mbed?

float xy = -1.33919; char buffer[2];

....here the needed conversion to the char array

can1.write(CANMessage(CANID, buffer, 2);

Greetings Klaus

A float is 4 bytes, so won't fit in 2 bytes.

Does your receiver expect it in a certain format, or is that your own so you can choose yourself which format it is going to be? Does it need to be two bytes? If yes, then we for sure need to know more about the range of values that you need to be able to send. If it is 4 bytes in any format you can send it exactly.

posted by Erik - 02 May 2014

Hello,

I read the register of the MPU6500 acceleration sensor e.g. the x-axis acceleration. This x-axis acceleration is presented as two bytes (high byte, low byte, with 2x8 = 16 bit resolution).The acceleration goes e.g. from -2g to +2g. So I would know the exact limits. Then I transform it to a float for further calculations. Afterwards I just need the result in the format as in the sensor registers for sending via can. There, on the CAN-matrix, it is also usual to represent a floating decimal with two bytes - what would match the original representation in the sensor register.

Greetings Klaus

posted by Klaus Weber 02 May 2014

It for sure needs to be fixed point as two bytes, simply not enough to make a floating point implementation. But the issue is mainly the further calculations, what do they do to the max/min values that should be supported?

But if you got an implementation in two-byte format in the target, send it in the same format.

posted by Erik - 02 May 2014
Be the first to answer this question.