# Why is the angle between vector from accelerometer and compass not constant?

I'm using a gy80 board and reading the raw sensor data using the arduino serial. The angle between these two vectors are not constant. Shouldn't this angle be constant as the angle between north and gravity is constant?

Maybe need to read up on calibration procedures. Also.... need to be more specific about 'not constant'.

My understanding is that the vector from 3 axis compass points in the direction of the magnetic field and the vector from 3 axis accelerometer points in the direction of acceleration due to gravity. Since both these are constant, shouldn’t the angle between them also be constant.

And to find the angle, I read the data from the board through serial and used python. Here is the code

``````    import serial
import numpy as np

ser = serial.Serial('/dev/ttyACM1',9600)

def fn():
while True:
try:
global_down = np.array(data[0:3],dtype = np.float)
mag_down = np.linalg.norm(global_down)
global_down /= mag_down
global_north = np.array(data[6:9],dtype = np.float)
mag_north = np.linalg.norm(global_north)
global_north /= mag_north
print np.arccos(np.dot(global_north,global_down))*180/np.pi
except Exception,e:
print e
``````

This code prints different angles based on how I hold the board,its not constant as I thought it would be.
And even if I don’t calibrate the compass, the angle between these two vectors must be same right?

My understanding is that the vector from 3 axis compass points in the direction of the magnetic field and the vector from 3 axis accelerometer points in the direction of acceleration due to gravity. Since both these are constant, shouldn't the angle between them also be constant.

Earth's magnetic field and gravity are unrelated/uncorrelated as far as I know. So whichever direction a 3 axis magnetometer vector reading points is unrelated to the direction of a 3-axis accelerometer vector.

Say my gy80 board is flat on the table and the x axis is pointing north, y axis pointing west and z axis pointing upwards. Now, the uncalibrated acceleroter would read a value in the direction of z axis and the other two axis having zero component, acc = 0i + 0j -1k. Similarly, the compass vector reads a value in the direction of x axis, comp = 1i + 0j + 0k.
These vectors are perpendicular.

Now, say I rotate my board 90 degrees in the horizontal plane and make the x axis point east. In this case, the acceleration vector is still the same, acc = 0i + 0j -1k. However, the compass vector is comp = 0i + 1j + 0k.Again, these vectors are perpendicular.

This is my reason as to why they should be constant. The angles should always be 90. Even if I haven't accounted for declination, the angle may not be 90 degrees, but should still remain constant.

Yeah..... but regardless of what measurement device you use..... if you stand still at the same spot (away from the north or south pole), then the 'actual' direction of north vector is always perpendicular to the force vector due to gravity.

But, when you use a measurement device to measure the north/south field, the 'vector' RELATIVE to an initial reference measurement direction will certainly change if you rotate your measurement device. In other words..... relative to your initial reference direction, a 3-d cartesian coordinate measurement of 1,0,0 is clearly different from a measurement of 0,1,0. Although, we know that your measurement device still tells you which way 'north' is.

But, when you use a measurement device to measure the north/south field, the 'vector' RELATIVE to an initial reference measurement direction will certainly change if you rotate your measurement device.

So will the gravity vector. In my example the gravity vector doesn't change because I'm rotating about the gravity vector. It was a simple case I considered to explain my problem. The example can be extended to any rotation I guess.

In other words..... relative to your initial reference direction, a 3-d cartesian coordinate measurement of 1,0,0 is clearly different from a measurement of 0,1,0. Although, we know that your measurement device still tells you which way 'north' is.

That's kinda my point. The device tells me which way north is and which way down is in the new coordinate system after the rotation. Despite this, my angles are varying as the orientation changes.

I may have misunderstood what you tried to say, so please do clarify. Thanks.

It is like..... you are now facing toward a reference direction, which you will call '0 degrees', and straight in front of you is a bright light. Now you turn clockwise 45 degrees. The angle you are now facing is +45 degrees relative to the reference direction.

Now, if you forget about the original reference direction, and then you assume that the direction that you are FACING is the new reference, then the bright light is located at -45 degrees (relative to the direction that you are facing). If you then rotate clockwise an additional 45 degrees, then the light will be at -90 degrees (relative to the direction that you're facing). The light is always at the same actual location in space. But the angle that you're 'measuring' (in order to keep track of the light) changes when you rotate. This means...... if you have a hypothetical device that is always able to tell you the angle of the light (relative to the direction you're facing), then sure.... the 'measured' angle will certainly change when you rotate. Afterall, it's a measured angle relative to the direction you're facing.

The example you gave does not fully answer my question.

If my intention is to just measure the angle between my sensor orientation and north, or just the sensor orientation and down, your example will help.

However, my problem in terms your example would be something like this:

There are two lights glowing and not just one: one in front of me and another below me. If I rotate like you said, 45 degrees, the light doesn't move and with respect to my new position, it is at -45 degrees. If I again move 45 degrees relative to the new position, I'm at 90 degrees or the light is at -90 degrees. And in these movements, the light at the bottom is not changing its angle, Its 90 degrees throughout.

This same movements can be extended to the vertical direction. So, say I'm looking up at an angle of 45 degrees. Again, this rotation would cause the horizontal bulb to move.

Now, what I'm calculating is the relative position of these two bulbs. So, no matter how I tilt or move my head, the relative position of these two bulbs should not change. This is what I don't understand.

Thanks.

Now, what I'm calculating is the relative position of these two bulbs. So, no matter how I tilt or move my head, the relative position of these two bulbs should not change. This is what I don't understand.

Thanks.

Ok.... I see. Maybe you need to check out 3-D coordinate transformations...... maybe focusing on "changing" 3-D coordinate systems.

http://www.math.tau.ac.il/~dcor/Graphics/cg-slides/geom3d.pdf

Maybe read the part that says "Given the XYZ orthogonal coordinate system, find a transformation, M, that maps XYZ to an arbitrary orthogonal system UVW."

Interesting. It looks like what I've been trying to do in my project.
I want to generate XYZ(north,down,west) from the sensors and UVW is the coordinate frame local to sensor. UVW is not my concern as of now.
The XYZ generation is what is the problem. The down vector works fine. The north vector is the problem. If I find out whats wrong, I can generate the XYZ using simple vector cross products.
This is the guide I used and tried to implement it.

specifically "Part 4. DCM complimentary filter algorithm using 6DOF or 9DOF IMU sensors" .

Any other way to generate XYZ will also be helpful.

Thanks.

MEMS compasses are awful. You only get something vaguely useful if and only if:

1. You properly calibrate for soft and hard iron errors - even then there will be errors.
2. Don’t use magnetic components nearby on the board (ie steel-containing resistors, capacitors
within a cm or two, note most have steel end-caps)
3. Use away from any ferrous metals (ie outside away from buildings, not in a steel box!) - this applies to all compasses

Also they produce a lot of noise, several degrees worth typically.

Having said they are awful, they are the best you can do in a small package (assuming household
rather than government budget is available).

Even after considering all these factors, I can’t explain why there is so much variation in the angles I calculated. I could share the raw values and the angles calculated if that would help.They from as low as 30 to about 120. And if I hold the board at one position, the angles don’t jump around much.

Thanks.

This means you need to first make sure that your mathematical mapping or calculations are correct. So use mathematical simulations through matlab or something.... to ensure your method works in simulated scenarios.

Then you need to look at measurements from your measurement devices to ensure that the readings are accurate and that they make sense. Assess the noise level. Determine if the noise will cause issues with your computations.

Also.... if your simulation starts with a true north direction and a gravity force direction at a particular location.... then that's certainly fixed. If your computations in your coordinate system do not reflect this .... then the issue is with your computations...since you already know what is expected in advance. But if your computations are influenced by incorrect or bad data.... then that is something else to consider.

You need to see have correct computations and good input data in order to determine the angle between two measured vectors. You also know that the centres of the magnetometer system and the accelerometer system (in a real MPU device) are not located at the same physical spot - just in case.

Even after considering all these factors, I can’t explain why there is so much variation in the angles I calculated. I could share the raw values and the angles calculated if that would help.They from as low as 30 to about 120. And if I hold the board at one position, the angles don’t jump around much.

Thanks.

Have you done compensation? MEMS magnetometers out of the box are not usable, they must be
calibrated in-situ.

Have you done compensation? MEMS magnetometers out of the box are not usable, they must be
calibrated in-situ.

By that, do you mean "tilt" compensation or the sensor calibration.
I haven't done anything to the sensor values and am just reading the raw sensor data.

I was under the impression that tilt compensation was not needed for a 3 axis compass as all three axis can detect magnetic field.

But, I guess calibrating the sensor might improve my results. Maybe the three axis are not equally sensitive to the magnetic field or maybe they have different offsets. I will try that soon.

So use mathematical simulations through matlab or something.... to ensure your method works in simulated scenarios.

I have never done this before and have no idea where to start. Any simple guides that you know off? I found something called openrave which I can use with python and octave and has sensor modules. I could use that I guess?

Thanks.

By that, do you mean "tilt" compensation or the sensor calibration.
I haven't done anything to the sensor values and am just reading the raw sensor data.

The important question is ..... have you done sensor calibration of any kind? Looks like you're saying that you are just reading raw values from the sensor, hence uncalibrated - which suggests you haven't done any sensor calibration. Sensor calibration is absolutely necessary for these kinds of devices.

But, I guess calibrating the sensor might improve my results.

Calibration is totally necessary.

I have never done this before and have no idea where to start. Any simple guides that you know off? I found something called openrave which I can use with python and octave and has sensor modules. I could use that I guess?

You can use such software you mentioned - as long as you can conveniently use it to model your system (or the mathematics of your system). So, for the coordinate system you use, you could choose two vectors in the system. And you can know in advance what the angle is between those vectors. Then pretend that the data associated with those two vectors are 'measured', and then use that data as input to your software....then see if your software is able to calculate the correct (expected) angle between those two vectors. It's using computers and software to make sure that your maths processing is correct.

Importantly, the MPU devices generally cannot be used straight away. They need to be calibrated. So if you haven't calibrated, then that could be part of the reason for your computations not making sense. Part of the reason. Also, just follow the usual procedure of using devices - any device that you aren't familiar with..... which is - read up on it....read the manual and as much information about it as you can. Don't assume things. What's happening here is that you appear to be assuming something (such as incorrectly assuming you can use raw uncalibrated sensor data for measurements of angles).

If you haven't calibrated the magnetometer it will be way out - you always have to calibrate
MEMS magnetometers to get anything vaguely like sensible readings.

And never expose them to strong magnetic fields, you'll permanently magnetize the pole
pieces in the sensor.

The best procedure for calibrating magnetometers (absolutely essential) is Sailboat Instruments: Improved magnetometer calibration (Part 1)

Works for accelerometers too, if the sensor is held still for each data point.

Let us know if you have questions after going through the material.