Optimization complementary filter

Hello everyone,

I'm trying to calculate the roll and the pitch of my system, a basic inertial unit for monitoring RC plane. I'm using the build-in IMU of an Arduino Nano 33 BLE Sense.

I can compute with the lines below :

roll = 0.25 * (roll + float(xgyr) * 0.1) + 0.75 * atan2((double)yacc, (double)sqrt(xacc * xacc + zacc * zacc)) * 180 / PI; //X
pitch = 0.25 * (pitch + float(ygyr) * 0.1) + 0.75 * atan2((double)xacc, (double)sqrt(yacc * yacc + zacc * zacc)) * 180 / PI; //Y

However, the calculation is quite strange. For example, if I apply a rotation continuoulsy around the roll axis, I have this sequence of angle : 0 / 45 / 90 / 45 / 0 / -45 / -90 / -45 / 0.

But, I would like something like that : 0 / 45 / 90 / 135 / 180 / 225 / 270 / 315 / 360(or zero).

The phenomena is the same around the pitch axis.
Have you some ideas for that ?

Thank you in advance.

Guillaume

atan2()'s returned value is in the range [-pi, +pi] radians. as you convert using * 180 / PI your result is in [-180°, +180°] interval

you might want to try to shift this by PI (or 180°) to get a [0°, 360°] interval

These are not the correct equations to get Euler angles, and don't correspond to any conventional angular system.

Thank you for your response. I have tried to switch 180/Pi by just Pi but it not seems to work.

Really ? Could you indicate me a website to find the good equations ?
Thank you in advance.

https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing

It is very important to understand that with Euler angles, the order of rotations matters. There are at least a dozen different angular system definitions.

I have tried these lines :

roll = 0.25 * (roll + float(xgyr) * 0.1) + 0.75 * atan2(yacc, zacc) * 180/PI; //X
pitch = 0.25 * (pitch + float(ygyr) * 0.1) + 0.75 * atan2(-xacc, sqrt(yacc * yacc + zacc * zacc)) * 180/PI; //Y

It is working well for compute the roll and pitch angles (using the xyz sequence) but there is something strange about the range of value :
[-90;+90] for pitch ;
[-180;+180] for roll.

If that possible, I would like [-180;+180] also for pitch, did I misunderstand something about Euler angles ?

Yes. There are always at least two rotations that give you the exact same orientation, so there is no reason to allow 0-360 range for three angles.

Oh I see, so there is no solution for compute a [-180;+180] or [0;360] range in roll and pitch axis in the same time ?

There are always TWO solutions.

You don't gain anything by having the additional range.

My purpose is to use these datas for emulate an artificial horizon, so it will be useful to have the full range. So, that means Euler's angles are not the good way for doing that ?

No, it won't. You clearly don't yet understand the math.

No one uses Euler angles for absolute orientation these days, because of the singularity problems this thread has touched on. Use a quaternion to represent absolute orientation instead.

Euler angles are fine to represent nearly level flight.

I understand the problematic now. I am sorry, I am beginner about navigation topics.
So, I need to a quaternion-based algorithm.

According to my internet searches, I have seen the existence of the Madwick's algorithm (seems quaternion-based). Do you think that a good way or there is something more simple ?

The Madgwick and Mahony AHRS filters are popular and libraries for either one are available for the Nano BLE Sense. The Mahony algorithm is shorter and faster.

For either of these filters to work, you must calibrate the IMU, most importantly, the magnetometer. The approach I use is described in Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers

Thank you. I will try to implement the Mahony filter. I will come back to you with my new results.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.