Magnetometer calibration. Electronic Compass and Arduino

Magnetometer calibration. Electronic Compass and Arduino

Daniil Zhuk
MARG Landmark 1 - a module that allows you to navigate in space. It contains a gyrotachometer (measures angular velocity), an accelerometer (measures acceleration, including caused by the force of gravity of the earth) and a magnetometer (works like a compass, allowing you to determine the position of the module relative to the ground).

The readings of each of these sensors can be read separately. The module consists of two chips. The first contains a gyrotachometer (ST L3G4200D), the second has a magnetometer and an accelerometer (ST LSM303DLM). All three sensors are three-axis, so MARG Landmark 1 has nine degrees of freedom (9-DOF IMU sensor).

Working with the MARG Landmark-1 and Arduino module:
First, connect the module with the Arduino:

Arduino Uno:
  • GND - to the pin of the same name;
  • 5V - to the pin of the same name;
  • SCL - to the same pin or A5;
  • SDA - to the same pin or A4.
Arduino Mega:
  • GND - to the pin of the same name;
  • 5V - to the pin of the same name;
  • SCL - to the same pin or D21;
  • SDA - to the same pin or D20.

For the sensors that form the basis of the module, there are different libraries: for the L3G4200D and LSM303DLMB, you can find examples of sketches of work with each of the sensors.

Download both libraries. In the sample folder for LSM303DLM, find the “Calibrate” sketch. It is used to calibrate the magnetometer. Sew the Arduino with this sketch and launch the port monitor. Rotate the module in different directions. At the end of the calibration, the desired values ​​will be displayed. Write them out.

To determine the position of the module in space, the library “Transformation” is used.

To determine the exact position it is not enough just to summarize the readings of the gyrotachometer. The speed of the module may not be constant, and the gyro-tachometer may give not quite accurate readings.

To obtain accurate readings are used landmarks, over time remain the same: the vector of gravity and magnetic field. For orientation with the help of all three sensors, different algorithms are used, the “Transformation” library uses a Majwick filter.

Send it to the libraries folder, which is located in the folder with the Arduino IDE development environment. Now open the development environment and open the “Serial” example from the examples of the downloaded library. If you have not calibrated the magnetometer yet - do it.

Now load the sketch into the Arduino and open the port monitor. When Arduino starts working, the board with sensors must be strictly parallel to the ground, that is, its Z-axis must be aligned with the direction of gravity.

This will eliminate errors in the module. If at the start the module was in a different position, correct it and restart the Arduino.

The library is included in the sketch with the line:

#include <Transformation.h>
The transformation class contains the following methods:

  • calibrate — magnetometer calibration. It takes the parameters obtained during the calibration of the magnetometer: the minimum and maximum values ​​on each axis in the order of X, Y, Z. The method must be called before the module starts working.
  • init - initialization. It calculates the position of the module in space, eliminates minor errors and fixes its position. I called once when the module starts.
  • fix - fix the position. After calling it, the next getFixed method will indicate the new fixed position of the board. If you do not call the fix method, the board position fixed by the init method will be fixed.
  • getFixed - getting the relative position of the module.
  • getAbsolute - getting the absolute position of the module. The absolute position is measured in the corners of Euler, as well as fixed.
  • calculate - this method calculates the position of the module. His call is needed every time before taking the readings.

Add a comment

* Comments must be approved before being displayed.