I have a magnetometer from where I get X, Y and Z which point to the direction of the detectable magnetic fields.
I'm using these values to get the angle between X and Y using atan2. I am a bit suprised of the result because North is not "aligned" with South. In fact there is not an angle of 90° between any "axes" (N, S, E or W). I can reproduce it with my phone. Can someone explain me why ? Am I Wrong ? Am I missing something ?
In fact my real problem is that my angle is not "linear". For example, in the image below, between North and East, with atan2 I get 270° which is obviously wrong regarding to the direction.
Here below, what it looks like :
Edit : diagram starts with East : angle = 0°. South is draw when angle = 90°.
magnetic cardinal points
My magnetometer is a IIS2MDC mems from STMicroelectronic. I am getting values from registers with a nordic MCU (but it would be the same with a ST MCU)
Edit :
My algorithm is copied from this post : https://arduino.stackexchange.com/questions/18625/converting-three-axis-magnetometer-to-degrees
Here is my function which converts from X, Y and Z raw values to angles in degrees :
static double main_calculate_angle_degree(int16_t x, int16_t y){
double angle;
angle = atan2((double)y, (double)x);
if (angle >= 0) {
angle = angle * (180 / M_PI);
}
else {
angle = (angle + 2 * M_PI) * (180 / M_PI);
}
if(angle > 350){ // angle 360 = 0 because it is a circle, of course !
angle = 360 - angle;
}
return angle;
}
Edit 2 : my algorithm
I have an offset which makes x = 0 in default position. This offset is applied to all axes. I tried without this offset and it makes no difference !
When angle = 0° : X² + Y² = 2² + 35² = 1229 (X is the average of 5 X values and the same for Y) Then I rotate my mems of 90°, the angle displayed by my device is around 70° : X² + Y² = 300² + 103² = 100609
Edit 3 : a photo of my setup I'm using the dev board of an nrf MCU and the eval board of the mems. Both are attached together. I tried to move around the big dev board alone aroung the mems and it seems to have no impact.