0

I want to calibrate an IMU Sensor to a decimal precision. I'm programming in C. I've defined a structure called accelerations with their variables x, y and z in int16_t format.

The raw values that I get (for the x, y and z Axis respectively) from the register reading(i.e. in accelerations) are for example: 218 -924 16312

The problem that I'm facing, is that by doing the calibration's multiplication

accelerations.x = (1*0.0006*(accelerations.x)-0.1681);
accelerations.y = (1*0.0006*(accelerations.y)+0.8076);
accelerations.z = (1*0.0006*(accelerations.z)-0.1698);

the values are rounded to integers, resulting in an output as follow: 0 0 9

What am I missing here?

Desperate Morty
  • 66
  • 2
  • 11
  • Integer rounding is always floored, i.e. even 0,9999 becomes 0. Add 0,5 to all values before converting to int, and you shall have correct rounding. Consider scaling the values, to obtain better resolution at the numbers you care about. – Morten Jensen Mar 10 '21 at 10:44
  • Hi Morten. Thanks for the comment. What I want is to obtain decimal values, therefore I want to avoid the rounding. – Desperate Morty Mar 10 '21 at 10:56
  • 1
    If you store a value in a `int16_t` variable, you get rounding/chopping for free. No matter if you want it or not. – Gerhardh Mar 10 '21 at 11:01
  • 1
    Thanks @Gerhardh, this was the error. I just made a new structure with float variables, instead of int16_t. What a Blunder did I made there! – Desperate Morty Mar 10 '21 at 11:27

0 Answers0