Hi,
I have a project where I am reading the values from an accelerometer. I am reading this sensor every second. I am finding that even tiny variations in the readings cause a massive fluctuation in the final pitch output.
I am using the formula pitch = atan2(acc_y, acc_z) * 57.2958 to get my pitch value.
Now consider that I have a sensor orientation where I have near zero acc_y and acc_z values.
Here is a possible list of values that my sensor gives me, and as you can see, there is a tiny variation on the acc_y and acc_z values. But when I calculate the pitch values, the resulting pitch values vary by a huge margin.
acc_y = 0.0, acc_z=0.1, pitch=0.00
acc_y = 0.0, acc_z=-0.1, pitch=180
acc_y = 0.1, acc_z=0.1, pitch=45
acc_y = -0.1, acc_z=-0.1, pitch=-135
As you can see, there is much variation in the pitch values from slight variation in the y and z components of the acceleration.
I understand that I need to use some kind of sensor smoothing algorithm? Can anyone point me to the right direction?
Thanks!
How to smooth sensor values?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- bidrohini
- Posts: 202
- Joined: Thu Oct 20, 2022 10:03 am
Re: How to smooth sensor values?
Apply kalman filtering. Without that, it is not possible to get stable output from accelerometers and gyros.
- adafruit_support_bill
- Posts: 88097
- Joined: Sat Feb 07, 2009 10:11 am
Re: How to smooth sensor values?
How are you calculating those acceleration values? And what units are they supposed to be?
With the sensor level, your Z axis should be seeing about 1G (9.8 m/S^2.
With the sensor level, your Z axis should be seeing about 1G (9.8 m/S^2.
- sj_remington
- Posts: 999
- Joined: Mon Jul 27, 2020 4:51 pm
Re: How to smooth sensor values?
Those are the correct angular results for the formula you are using. Changing sign from 0.1 to -0.1, or value from 0.0 to 0.1, are not "slight variations" in value.
If you want to make a tilt sensor using that formula, the accelerometer Z axis should be approximately vertical, and the Z axis acceleration should be approximately 1 g, for small tilt angles.
For an explanation, see this tutorial: https://wiki.dfrobot.com/How_to_Use_a_T ... lt_Sensing
If you want to make a tilt sensor using that formula, the accelerometer Z axis should be approximately vertical, and the Z axis acceleration should be approximately 1 g, for small tilt angles.
For an explanation, see this tutorial: https://wiki.dfrobot.com/How_to_Use_a_T ... lt_Sensing
- seemantadutta
- Posts: 3
- Joined: Thu Jan 23, 2014 4:47 pm
Re: How to smooth sensor values?
I understand that these are mathematically correct values. But the fact still remains that on a practical level, if the output is varying too much, then it is not very useful. Thanks for the link, I will check it out.sj_remington wrote: ↑Sun Jan 22, 2023 12:33 pm Those are the correct angular results for the formula you are using. Changing sign from 0.1 to -0.1, or value from 0.0 to 0.1, are not "slight variations" in value.
The sensor gives the acceleration values directly. In this case I get 9.81 when z-axis is directly pointing down.adafruit_support_bill wrote: ↑Sun Jan 22, 2023 9:24 am How are you calculating those acceleration values? And what units are they supposed to be?
With the sensor level, your Z axis should be seeing about 1G (9.8 m/S^2.
Thank you, I will check this out.
- adafruit_support_bill
- Posts: 88097
- Joined: Sat Feb 07, 2009 10:11 am
Re: How to smooth sensor values?
With both X and Z near zero, the sensor must be rolled about 90% to one side. This puts the pitch axis parallel to the yaw axis. In that case. the result of the pitch calculation may be mathematically correct but becomes pretty meaningless.The sensor gives the acceleration values directly. In this case I get 9.81 when z-axis is directly pointing down.
This is not a problem with the sensor. It is a limitation of the calculation you are using, and no amount of filtering will fix it. This condition is known as "gimbal lock": https://en.wikipedia.org/wiki/Gimbal_lock
- sj_remington
- Posts: 999
- Joined: Mon Jul 27, 2020 4:51 pm
Re: How to smooth sensor values?
If you want to measure tilt angles with the X axis approximately vertical, rather than Z, cyclically permute the sensor axes. That avoids the gimbal lock problem.
The formula becomes pitch = atan2(acc_z, acc_x) * 57.2958
The formula becomes pitch = atan2(acc_z, acc_x) * 57.2958
Please be positive and constructive with your questions and comments.