### 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

DIY Robocars via Twitter
RT @TinkerGen_: "The Tinkergen MARK (\$199) is my new favorite starter robocar. It’s got everything — computer vision, deep learning, sensor…
Monday
DIY Robocars via Twitter
RT @roboton_io: Join our FREE Sumo Competition 🤖🏆 👉 https://roboton.io/ranking/vsc2020 #sumo #robot #edtech #competition #games4ed https://t.co/WOx…
Nov 16
DIY Drones via Twitter
First impressions of Tinkergen MARK robocar https://ift.tt/36IeZHc
Nov 16
DIY Robocars via Twitter
Our review of the @TinkerGen_ MARK robocar, which is the best on the market right now https://diyrobocars.com/2020/11/15/first-impressions-of-tinkergen-mark-robocar/ https://t.co/ENIlU5SfZ2
Nov 15
DIY Robocars via Twitter
RT @Ingmar_Stapel: I have now explained the OpenBot project in great detail on my blog with 12 articles step by step. I hope you enjoy read…
Nov 15
DIY Robocars via Twitter
RT @DAVGtech: This is a must attend. Click the link, follow link to read the story, sign up. #chaos2020 #digitalconnection #digitalworld ht…
Nov 15
DIY Robocars via Twitter
RT @a1k0n: Got a new chassis for outdoor races (hobbyking Quantum Vandal) but I totally didn't expect that it might cause problems for my g…
Nov 11
DIY Drones via Twitter
First impressions of the Intel OpenBot https://ift.tt/36qkVV4
Nov 10
DIY Robocars via Twitter
Nov 9
DIY Robocars via Twitter
Excellent use of cardboard instead of 3D printing! https://twitter.com/Ingmar_Stapel/status/1324960595318333441
Nov 7
DIY Robocars via Twitter
RT @chr1sa: We've got a record 50 teams competing in this month's @DIYRobocars @donkey_car virtual AI car race. Starting today at 10:00am…
Nov 7
DIY Robocars via Twitter
RT @a1k0n: Car's view, using a fisheye camera. The ceiling light tracking algorithm gave me some ideas to improve ConeSLAM, and having grou…
Nov 5
DIY Robocars via Twitter
RT @a1k0n: To get ground truth I measured the rug, found the pixel coordinates of its corners, calibrated my phone camera with my standard…
Nov 5
DIY Robocars via Twitter
RT @a1k0n: @DIYRobocars is back in December, but outside. Time to reinvestigate ConeSLAM! I rigged up a quick and dirty ground-truth captur…
Nov 5