1

[SOLVED] By changing the offset for the incoming data into int16_t type there won't be any rounding errors and the values I'm getting are all back to zero in the sensor its base position

This is my first project where I use Arduino.

Until today everything went good with the Arduino Pro Mini (3.3v). But as of today I got a bit of a weird problem. I am using a MPU gy-521 (doesn't really matter) and I'm working on calculating the actual movement.

The problem which occured is that I'm using a deadzone to negate the smallest numbers (I know, this isn't the right way to use data). The way I negate the numbers is with the following code

// Check deadzone values
  if (ax+axo < adz && ax+axo > -adz) ax = -axo;
  if (ay+ayo < adz && ay+ayo > -adz) ay = -ayo;
  if (az+azo < adz && az+azo > -adz) az = -azo;

  if (gx+gxo < gdz && gx+gxo > -gdz) gx = -gxo;
  if (gy+gyo < gdz && gy+gyo > -gdz) gy = -gyo;
  if (gz+gzo < gdz && gz+gzo > -gdz) gz = -gzo;

ax is accelerometer data over the x-axis, axo is the offset, adz is the accelerometer deadzone.

gx is gyroscope data over the x-axis, gxo is the offset, gdz is the gyroscope deadzone.

All of the other letters are the same, for different axis. After getting the values within the offset basically set to 0 with the offset I'm doing the following.

// Calculate acceleration
  axa = (ax+axo) / aFactor * g;
  aya = (ay+ayo) / aFactor * g;
  aza = (az+azo) / aFactor * g;

  gxa = (gx+gxo) / gFactor;
  gya = (gy+gyo) / gFactor;
  gza = (gz+gzo) / gFactor;

The a- and gFactor are mentioned in the datasheet, but these values should all be 0 if the value of axis plus the offset is within the deadzone. The problem is that I'm not just getting 0.00 values. I'm also getting -0.00 and 0.01.

0.00    0.00    -0.00   0.00    0.01    0.01 

Could someone enlighten me about what's wrong in my situation or if this is just a Arduino issue.

[EDIT]

The data originally from the sensor is from type int16_t (ax, ay, az, gx, gy, gz) and the other variables are doubles.

The values which are off are always the same ones: Accelerometer Z, Gyroscope Y and Z.

  • 1
    Are all your variables declared as float ('axa, axo, aFactor, g, ...`) ? – J. Piquard Nov 29 '16 at 11:20
  • The variables are all declared as double except for the ax, ay, az values which are int16_t by default – Mathieu Brouwers Nov 29 '16 at 11:46
  • What are changes between "Until today everything went" and "today I got a bit of a weird problem" (source code, sensors, wiring, other) ? – J. Piquard Nov 29 '16 at 14:30
  • 1
    Change axo etc... to also be int16_t rather than floats. That way when you set ax = -axo; and then do (ax+axo) there is no risk of rounding errors converting from double to int and back, you should always get exactly 0. – Andrew Nov 29 '16 at 17:01
  • If axo is not an integer, then the statement ax = -axo; will perform rounding, and the expression ax+axo will be non-zero. – Edgar Bonet Nov 29 '16 at 18:13
1

If your program is basically working but gives you rounding errors, it can be that you are facing some problems related to conversion from floating-point to binary or reverse. It is a very common problem in computer science. For example, the decimal number 0.1 has no exact binary representation, it is infinite. For more information, you can read these pages:

http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/ https://www3.ntu.edu.sg/home/ehchua/programming/java/datarepresentation.html

In order to solve your problem, you can try to apply appropriate rounding to your results and if possible avoid mixing different data types and use double only if absolutely needed (which I guess is the case in your application).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.