# simple IMU - tilt magnetometer

I am in the process of building a simple IMU for an autopilot. I am using Sparkfun, 9 Degrees of Freedom - Razor IMU. It is surprisingly user friendly.

I was able to get accelerometer, gyro and magnetometer data right out of the box. After calibration of the sensors and a simple complementary filter i have reliable roll and pitch values.

Now I have a problem with the yaw...

I found here http://www.pololu.com/file/download/...?file_id=0J434 the equation to get the yaw out of the magnetometer value:

$$X_h = X_m\cdot cos(Pitch) + Z_m \cdot sin(Pitch)$$ $$Y_h= X_m\cdot sin(Roll)\cdot sin(Pitch) + Y_m\cdot cos(Roll) - Z_m\cdot sin(Roll)\cdot cos(Pitch)$$ with Xm, Ym, and Zm are magnetic sensor measurements

$$Yaw = Heading = arctan( \frac{Y_h}{X_h} )$$

It sound be simple, but I can't do it. I hope that someone who already worked on IMUs can help me solve this problem.

The graph (in Excel file) shows results (first i rolled 90 degrees, then I pitched 90 degrees then I yawed 90 degrees).

Roll and pitch are calculated by the IMU (programmed with Arduino IDE), they are good.

Yaw is calculated in Excel using gyro (good but drift over time) and I also calculated Yaw using the equation above (green line) and it is totally wrong (it should follow the gyro line / purple).

Any help will be appreciated.

• Without any electronic components wired/soldered/touched, this is simply a math and/or programming question. Off topic? FYI I just showed my kid a similar 9DOF yesterday, we used the ArduIMU firmware but didn't look behind the math (Google for more info). – Ron J. Jul 13 '13 at 12:36

=DEGREES(ATAN2(U3,T3))+Dcal