[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 Answer 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).
Explore related questions
See similar questions with these tags.
float
('axa, axo, aFactor, g, ...`) ?double
except for the ax, ay, az values which areint16_t
by defaultint16_t
rather than floats. That way when you setax = -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.axo
is not an integer, then the statementax = -axo;
will perform rounding, and the expressionax+axo
will be non-zero.