Hello,

I am trying to understand how to obtain a heading angle from 3D magnetometer like HMC5843. When I look at compass.cpp in AP_Compass library, I can't get how roll and pitch angles are used to compensate compass measurements.

Is there anyone who can explain the following code in Compass.cpp? (Especially the lines where headX and headY are first computed.)

void
Compass::calculate(float roll, float pitch)
{
float cos_roll;
float sin_roll;
float cos_pitch;
float sin_pitch;

cos_roll = cos(roll);  // Optimizacion, se puede sacar esto de la matriz DCM?
sin_roll = 1  - (cos_roll * cos_roll);
cos_pitch = cos(pitch);
sin_pitch = 1  - (cos_pitch * cos_pitch);

// Tilt compensated magnetic field X component:
// Tilt compensated magnetic field Y component:

// Declination correction (if supplied)
if( fabs(_declination) > 0.0 )
{
if (heading > M_PI)    // Angle normalization (-180 deg, 180 deg)
}

// Optimization for external DCM use. Calculate normalized components
}

### Activity

RT @gclue_akira: 柏の葉で走行させてるjetracerの中身　#instantNeRF #jetracer https://t.co/giVvuE4hP7
Jul 4
Cool web-based self-driving simulator. Click save when the AI does the right thing https://github.com/pncsoares/self-driving-car
Jul 4
RT @donkey_car: Human-scale Donkey Car! Hope this makes it to a @diyrobocars race https://www.youtube.com/watch?v=ZMaf031U8jg
Jun 25
RT @GrantEMoe: I won my first @diyrobocars @donkey_car virtual race! Many thanks to @chr1sa @EllerbachMaxime @tawnkramer and everyone who m…
Jun 13
RT @gclue_akira: JetRacerで自動走行したコースを、InstantNeRFで再構築。データセットは別々に収集 #jetracer #instantNeRT https://t.co/T8zjg3MFyO
Jun 13
RT @SmallpixelCar: SPC 3.0 Now the motor also works. This car is doable. I just need to design a deck to mount my compute and sensors. http…
Jun 13