### Problem with tilt compensation

Hey all,

I'm very happy to join DiyDrones community
(Sorry for my bad english)

I'm working on my new project with a drone 3DR frame and 10DOF IMU (MPU6050, HMC5883, MS5611 http://www.drotek.fr/shop/en/home/62-imu-10dof-mpu6050-hmc5883-ms5611.html), 1 or 2 days per month for 3 years so moving very slowly between the IHM and the embedded code!

I using a MPU6050 and HMC5883 Magnetomter and chipkit pic32 Board !

If using a heading with only magnetometer with this code my yaw axis it's good (if don't move other axis):

My code:

float heading = atan2(mag.y, mag.x); // mag.x and mag.y it's raw value scaled ( raw * 0.92 )  1.3Ga

if (heading < 0)

if (heading > 2*M_PI)

return heading * toDeg;

Video :

If add a tilt compensation with accelerometer my yaw axis, move with roll and pitch inclination

My code:

float rollRads = acc.x * toRad; // acc.x = acc angle calculate with acos( accX(G) ) converted to deg

float pitchRads = acc.y * toRad; // acc.y =  acc angle calculate with acos( accY(G) ) converted to deg
float cosRoll = cos(rollRads);
float sinRoll = sin(rollRads);
float cosPitch = cos(pitchRads);
float sinPitch = sin(pitchRads);

float Xh = mag.x * cosPitch + mag.y * sinRoll * sinPitch + mag.z * cosRoll * sinPitch;
float Yh = mag.y * cosRoll - mag.z * sinRoll;

float heading = atan2(Yh, Xh);

if (heading < 0) {
}

if (heading > M_PI) {
}

return heading * toDeg;

Video:

My code is available on github (Check IMU.cpp):
https://github.com/marc-j/NeuroDrone-pic32

Thanks for your help !!

#### Replies

• I found the problem after simulation, resolved by this calcul :

float Xh = (mag.x * cosRoll) + (mag.y * sinPitch * sinRoll) - (mag.z * cosPitch * sinRoll);
float Yh = (mag.y * cosPitch) - (mag.z * sinPitch);

This reply was deleted.

### Activity

gotham liked gotham's profile
11 hours ago
Hiroki Tanaka liked Hiroki Tanaka's profile
Oct 13
Derrick Davies liked lisa TDrones's profile
Sep 23