- Mon Jul 25, 2011 8:53 pm
#130745
When I tilt the device left and right up and down, I see the accelerometer and gyro readings change as I would expect but when hold it flat and rotate it around the compass directions all the numbers seem to stay in about the same range (-650 to -680 or so). Suspicious that the magnetometer wasn't working or not being read correctly I got a fridge magnet and held it at the 3 axis and did see the numbers change to -4096 depending on where it was held for each of the axis so I'm pretty sure I'm getting the data correctly from the device.
Thinking perhaps the numbers were just only valid in a small range I went ahead and tried the following math to get the actual tilt adjusted heading:
Thinking perhaps the numbers were just only valid in a small range I went ahead and tried the following math to get the actual tilt adjusted heading:
Code: Select all
But I'm only getting headings in the range of -5 to 5 degrees. I don't have it on top of any metal, in fact it is about 4 or 5 inches above a small micro controller. Any advice or tips on getting better magnetometer readings? private double WrapAngle(double angle)
{
if(angle > pi)
angle -= (2*pi);
else if(angle < -pi)
angle += (2*pi);
else if (angle < 0)
angle += 2*pi;
return angle;
}
private double GetHeading(int bx, int by, int bz, int phi, int theta)
{
var Xh = bx * MathEx.Cos(theta) + by * MathEx.Sin(phi) * MathEx.Sin(theta) + bz * MathEx.Cos(phi) * MathEx.Sin(theta);
var Yh = by * MathEx.Cos(phi) - bz * MathEx.Sin(phi);
return WrapAngle((MathEx.Atan2(-Yh, Xh) + variation));
}
Debug.Print(GetHeading(stickIMU.imuData.MagnetX, stickIMU.imuData.MagnetY, stickIMU.imuData.MagnetZ, stickIMU.imuData.AccelXDegrees, stickIMU.imuData.AccelYDegrees).ToString());