# Algorithm problem

**URL:** <https://forum.arduino.cc/t/algorithm-problem/1189504>\
**Category:** Sensors\
**Created:** [November 15, 2023, 4:58pm UTC](https://forum.arduino.cc/t/algorithm-problem/1189504 "2023-11-15T16:58:11Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![masteriphone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/masteriphone/32/1003454_2.png) [@masteriphone](https://forum.arduino.cc/u/masteriphone)\
**Post date:** [November 15, 2023, 5:45pm UTC](https://forum.arduino.cc/t/algorithm-problem/1189504/5 "2023-11-15T17:45:17Z")

</div>

I don’t know why it wasn’t possible to redefine the direction of the accelerometer axis before, but now everything works.  
As usual, if the acceleration axis is facing UP, then the force of gravity is directed DOWN and you just need to put a “-” in front of the acceleration value in the code:

> <https://github.com/rbv188/IMU-algorithm/blob/bd77afdb7018a46998ae74e12bcd763c5b6711b2/IMU-algorithm-master.ino#L41>

I also found two options for calculating the roll angle from quaternion, in the source code there was the first option

```arduino
result.roll = atan2(2*(q0*q1 + q2*q3),1 - 2*(q1*q1 + q2*q2))*180/3.14;
// result.roll = atan2f(q0*q1 + q2*q3, 0.5f - q1*q1 - q2*q2)*180/3.14;

```

and in the code you specified, there was a slightly different one.

> [@Controlling Servo with MPU6050](https://forum.arduino.cc/t/controlling-servo-with-mpu6050/685306/34):
>
> For full 3D orientation, most people use a 9DOF sensor, because the MPU-6050 can't define yaw (angle from North). However, for pitch and roll stabilization with your MPU-6050. you can start with this code, which uses the Mahony filter to determine the 3D orientation. Yaw will drift. Be sure to calibrate the rate gyro as every one is different. It is not so important to calibrate the accelerometer, but it helps. This code just prints out the orientation angles. // // MPU-6050 Mahony IMU&nbsp; (yaw…

But the result is not much different.  
I'm looking for algorithms that will deal with changes in roll or pitch angles during sudden movements of the device.  
[Video1](https://disk.yandex.com/i/sg-nLmTnDR3kbQ) and [video2](https://disk.yandex.com/i/WU6OBnVsJ4jPeg)  
And this [video3](https://disk.yandex.com/i/94_koT7Prvw9jA) shows the operation of DCM from open source

> <https://github.com/pololu/minimu-9-ahrs-arduino/blob/master/MinIMU9AHRS/MinIMU9AHRS.ino>

The video shows that the algorithm successfully compensates for sideways jerks of the sensor, while the orientation angles remain stable.

---

_[View the full topic](https://forum.arduino.cc/t/algorithm-problem/1189504)._
