How to smooth sensor values?

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
seemantadutta
 
Posts: 3
Joined: Thu Jan 23, 2014 4:47 pm

How to smooth sensor values?

Post by seemantadutta »

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!

User avatar
bidrohini
 
Posts: 202
Joined: Thu Oct 20, 2022 10:03 am

Re: How to smooth sensor values?

Post by bidrohini »

Apply kalman filtering. Without that, it is not possible to get stable output from accelerometers and gyros.

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: How to smooth sensor values?

Post by adafruit_support_bill »

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.

User avatar
sj_remington
 
Posts: 994
Joined: Mon Jul 27, 2020 4:51 pm

Re: How to smooth sensor values?

Post by sj_remington »

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

User avatar
seemantadutta
 
Posts: 3
Joined: Thu Jan 23, 2014 4:47 pm

Re: How to smooth sensor values?

Post by seemantadutta »

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.
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.
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.
The sensor gives the acceleration values directly. In this case I get 9.81 when z-axis is directly pointing down.
bidrohini wrote: Sun Jan 22, 2023 9:13 am Apply kalman filtering. Without that, it is not possible to get stable output from accelerometers and gyros.
Thank you, I will check this out.

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: How to smooth sensor values?

Post by adafruit_support_bill »

The sensor gives the acceleration values directly. In this case I get 9.81 when z-axis is directly pointing down.
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.

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

User avatar
sj_remington
 
Posts: 994
Joined: Mon Jul 27, 2020 4:51 pm

Re: How to smooth sensor values?

Post by sj_remington »

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

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”