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.

asked Nov 29, 2016 at 11:07
6
  • 1
    Are all your variables declared as float ('axa, axo, aFactor, g, ...`) ? Commented Nov 29, 2016 at 11:20
  • The variables are all declared as double except for the ax, ay, az values which are int16_t by default Commented Nov 29, 2016 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) ? Commented Nov 29, 2016 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. Commented Nov 29, 2016 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. Commented Nov 29, 2016 at 18:13

1 Answer 1

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).

answered Nov 29, 2016 at 12:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.