Converting Hex data to Angle

I am using a CAN Shield + Mega 2560 to read the angle from a CANopen encoder. Its a 12 Bit encoder and so for 360 Deg the max value is 0xFFF. But my interest is only +/- 50 Deg angle from the centre point 0x000. From this spot turning clockwise should read as 0 to 500 ; and turning counter clockwise should read as 0 to -500

The sample data is given below and we are interested only in the leftmost byte ( lsb ) and the next to it ( msb) . So the first thing to do is to swap the two bytes and combine them. Then for clockwise just multiply the resulting twobyte word by 10 and display.

But I think for the CCW side I need to do a Two s complement arithmetic to get the 0 to -500 display. How to go about that ? Thanks


float raw_to_500_scale(uint16_t raw)
{
  float degrees = (raw * 360.0) / 0x0FFF;   //  0..360
  degrees = degrees * 10;  //  0..3600
  return degrees - 500;  //  use an offset of -500
}

or shorter

float raw_to_500_scale(uint16_t raw)
{
  return -500 + raw * (3600.0 / 0x0FFF);
}

I have used the idea to convert the encoder readings span from 180 to 0 to -180 Degrees by changing the offset value to 1800. All good.

But have not been able to clearly understand how this logic works .... ?

The fist step is to understand how to convert the raw value 0..8095 (0xFFF) to degrees.

Then you can scale the degrees with a factor 10 to get 0..3600

By adding / subtracting an offset you can move the range to -1800.. +1800

Thanks for the clarification ! The final code that does what I wanted.

float angleVal = ( rxBuf[1] << 8) | rxBuf[0] ;
Serial.print(angleVal * 0.08789062 *10 - 1800 ); // 360 / 4096 is the resolution per bit.