2

I have been trying to calculate the compass heading using the magnetometer in an MPU9250 sensor. But the result is not making sense to me. I am completely new in this and do not have much understanding in how magnetometers work and just trying to find resources in the internet to make it work. I am calculating the atan2 of the y and x axis readings of the sensor to find out the angle:

float mx = imu.mag_x_ut();
float my = imu.mag_y_ut();
float mz = imu.mag_z_ut();

float heading = atan2(my, mx) * 180 / PI;

Serial.print(mx); Serial.print(","); Serial.print(my); Serial.print(","); Serial.print(mz); Serial.print(","); Serial.println(heading);

My understanding is that when the x axis of my magnetometer faces the north direction the value of heading should be 0, for east 90, south 180 and then for west 270. But my calculations are nowhere near the expected results. I am not even getting a zero reading in any directon. I am attaching the screenshots of the direction reading :

This is when x axis points to north. x pointed to north

Rotated x axis towards east. x pointed to east

x towards south. x pointed to south

x towards west. x pointed to west

I have also added the raw readings from the magnetometer represnted by mx, my and mz. Is something wrong with my sensor or there is some misunderstanding from my part.

EDIT: Readings after hard iron offset compensation.

after hard iron offset

Sayantan Das
  • 123
  • 6

1 Answers1

3

Consider calibrating your magnetometer. Off the shelf, almost all magnetometers require calibration before making use of their data. The offset (commonly referred to as Hard Iron) and the magnitude (commonly referred to as Soft Iron) need to be found for each of the 3 sensors (X, Y & Z) for individual magnetometers.

As there is no accelerometer in this design let us simplify the problem and assume the magnetometer will always be used on a flat surface with its Z axis normal to the surface of the Earth. Let us also assume the X and Y sensors in the magnetometer are perfectly orthogonal to one another.

To calibrate the magnetometer remove metal objects from the local vicinity and avoid large metal object such as cars by 10s of meters. Sample the X and Y sensors many times as these sensors point toward and away from north. Find the maximum and minimum X and Y values.

Find the offset calibration value by adding the minimum and maximum values then divining by 2. Find the magnitude calibration value by arbitrarily picking one axis and adjusting the magnitude of the other to match. Picking X, we subtract the X minimum from the X maximum to find the X range. We do the same for Y to find the Y range. Then we assume the X magnitude calibration value to be 1 and the Y magnitude calibration value to be x-range divided by y-range.

Once calibration is done, to get calibrated X and Y values we subtract the X and Y offset respectively. Then we multiply the offset adjusted Y value by the Y magnitude calibration value.

Note, this type of calibrated compass is difficult to carry around by hand because it may not be held level with the surface of the earth. Also, because the inclination of the magnetic field points deeper into the earth the more one travels away from the equator, it will require re-calibration when traveling to a different latitude.

Calibration calculations based on data in question:

X max to min is 42 to -22 so offset is 10 and range is 64
Y max to min is 120 to 48 so offset is 84 and range is 72
So Y magnitude calibration is 64/72 or 0.889

Data when sensors are pointed East:

Y ~48 & X ~11 
bearing = atan2(((48 - Y_offset) * Y_magnitude), ((11 - X_offset) * X_magnitude)) 
bearing = atan2(((48 - 84) * 0.889), ((11 - 10) * 1))
bearing = atan2(((-36) * 0.889), (1 * 1))
bearing = atan2(-32.004, 1)
bearing = -1.540 rad = -88.236 degrees

If instead of -88.236 degrees the value of approximately 90 degrees is desired, change the sign of the Y sensor as part of the calibration process.

The C programming function atan2 is an enhanced form of the math arc tan operation. The C programming function atan2 will calculate to a full 2-Pi circle. To verify how this works use this online atan2 calculator.

st2000
  • 7,513
  • 2
  • 13
  • 19