LSM303 vector question


I am having some trouble with my LSM303DLH. I calibrated, i can read heading and all is well, but:

I do something like:

currentHeading = compass.heading((LSM303::vector){0,-1,0});

I understand {0,-1,0} is the orientiation vector, so we can put the board in arbitary orientation.

I noticed:

{0,-1,0} - {0,-1,-1} and {0,-1,1}

gives nearly the same results in my configuration. my sensor is mounted 90degree sideways.

Could someone explain the {0,-1,0} part? My vector math is not good, so i am having some trouble. by trial and error, I found that 0 does not do anything, and this thing can be in 3^3 states total.

the problem I am having is when the bot is stationary, all is fine. but if we are turning to a heading, once in a while I get errorful values.

For example:

h180 <- command to turn to 180 deg
0 3 1 359 3 <- last 5 readings
3 177 180 clock 65 (current heading, angular dist, target heading, turnng clock or counterclock wise, motor power)

3 1 359 3 344
344 -164 180 counter 61
1 359 3 344 28
28 152 180 clock 58
359 3 344 28 352
352 -172 180 counter 63

when the robot turns from 0 to 90 or 90 to 180 the code works perfect. when the bot turns 180d sharp, the first few readings jitter in sign, and this makes the robot jitter when turning. it always turns to correct heading, but when it does sharp turns, it jitters.

Any ideas recomendations greatly appreciated.



I am not sure what you are trying to show here, did you do some vector subtraction in before the and?

If you are going 180 degrees, it is hard to decide which way to go around when the path is the same amount around in either direction.

A compass reading is just number of degrees away from north. With the magnetometer-accelerometer pair, we get a vector of where north is. The heading vector is just a way of converting this north vector into a number of degrees away from north. You can arbitrarily choose the heading vector.

Imagine you are using a smartphone running the a traditional compass app. It shows, on the screen, a circle that is rotated based on how many degrees away from north the top of your phone is. In this case, the phone is using a heading vector that is parallel to the screen and points up out the top of the phone. When you tip phone up so it is almost vertical, it gets confused. How can it show which way is north when the plane of display is perpendicular to the North vector?

Now imagine that, instead of a traditional compass app, you use one that draws a 3D arrow on the screen that points north. In this case, you do not need a heading vector. You aren’t trying to collapse the information you have to a single degree measurement; you are just drawing the vector.

I like to use a geometrical explanation for how the heading works on our compass program. Imagine you have a sheet of paper with two perpendicular arrows that point north and east. Now use a pencil as your heading vector. Put the eraser at the point where your north and east arrows originated and point the tip in the direction of your heading vector. Now, imagine a line that is perpendicular to the paper and goes through the tip. Between the eraser and the point where the imaginary line intersects the paper is a new vector that is in the plane of the paper. The angle the heading function reports is the angle between this new vector and the north arrow on the paper.

- Ryan

Hello Ryan,

I understood the 3d vector part. However I am asking:

currentHeading = compass.heading((LSM303::vector){0,-1,0});

what is the explanation / documentation of {0,-1,0} - I have read the code, and it is doing some kind of translation - so I thought the {0,-1,0} defines some sort of orientation of the compass, so the heading vector can be calculated.

However there is no documentation on this on the driver page. For example, giving {0,1,0} or {0,-1,0} will result in different readings. how am i supposed to compose this, depending on the orientation of the compass module???

best regards,

{0,-1,0} was an arbitrary choice based on how I had the board oriented with respect to the output LCD I was using when I was writing the code. It is a vector in the coordinate system of the board, so, yes, it depends on the orientation of the board. You can choose that vector based on where you want your robot’s “pencil” to be. By the way, in some situations it makes sense to dynamically update the vector.

- Ryan

In other words: the code defines the heading as the direction in which the negative Y axis of the LSM303 is pointing, since {0,-1,0} represents the negative Y axis (the vector is in the form {X,Y,Z}). If you want something else to define the heading, you would change that vector; for example, if you want the heading to be defined by the direction of the positive Z axis, you could use {0,0,1}.

- Kevin