Compass.ino 595 B

1234567891011121314151617181920212223
  1. /* This file is part of the Razor AHRS Firmware */
  2. void Compass_Heading()
  3. {
  4. float mag_x;
  5. float mag_y;
  6. float cos_roll;
  7. float sin_roll;
  8. float cos_pitch;
  9. float sin_pitch;
  10. cos_roll = cos(roll);
  11. sin_roll = sin(roll);
  12. cos_pitch = cos(pitch);
  13. sin_pitch = sin(pitch);
  14. // Tilt compensated magnetic field X
  15. mag_x = magnetom[0] * cos_pitch + magnetom[1] * sin_roll * sin_pitch + magnetom[2] * cos_roll * sin_pitch;
  16. // Tilt compensated magnetic field Y
  17. mag_y = magnetom[1] * cos_roll - magnetom[2] * sin_roll;
  18. // Magnetic Heading
  19. MAG_Heading = atan2(-mag_y, mag_x);
  20. }