Wednesday, May 18, 2011

Tilt Sensor

We have a 3-axis accelerometer to go along with the 3-axis compass. Its actually all on the same circuit-board that is smaller than a stamp. The idea is to comebine the accelerometer and compass readings to give us a tilt-compensated compass. As mentioned in my last post, the magnetic field isn't a 2-dimensional thing, it has components in all 3 directions, so when you tilt a compass, it will give you incorrect readings (that is why the needle on any hand compass you buy has some room to level itself to gravity). Using the accelerometer tells us how much the boat is tilting and we can therefore compensate for that tilt when looking at the compass readings. That is going to require a bit of math before we get to that point, though.

There was already some code written to get the accelerometer talking to the microcontroller (debugging output goes from there to a computer). Much like the compass, the outputs need to be adjusted and normalized. Again, I'm modifying them to read -1000 to +1000 (+1000 is equal to 1-g) on each axis. The raw outputs for each axis are approximately:
X-axis: -256 to 270
Y-axis: -262 to 264
Z-axis: -267 to 242

After completing the output modification, I decided to calculate the angle that the sensor is tilted. On one axis that can be done as pitch=asin(x-reading/gravity). Since I scaled the readings to be 1000 for 1-g, gravity can be replaced by 1000 in the equation above. The same can be done for roll angle using the y-axis.

I was a bit worried about noise in the readings, but it turns out to not really be an issue. When I'm looking at the scaled results and holding it as steady as possible on a table, it does have some variability. For instance, it might range from 482 to 490. Due to the the Sine function, when you are close to zero, the sensor reading needs to change a relatively large amount to change even one degree. As the angle gets closer to 90 degrees, small changes in the reading produce large changes in angle. Realistically, we don't care too much about the compass reading if the boat is pitched 90 degrees downwards. We'd have bigger problems in that case.

Waves and other things might affect the acceleration somewhat and therefore the calculated angle and compass heading, but I'm hoping that we can take readings frequently and average them over time to smooth out those errors.

It works pretty nicely now. The readings for moderate tilt angles (up to 60 degrees at least) are pretty smooth and steady. Tilted at 45 degrees and held on a table or something solid, it will vary less than one degree, which seems good enough to me. When you get close to 90 degrees, it gets a bit wierd, but that is expected. Again, I'm not too worried about being able to precisely measure 86 or 89 degrees of roll.

It seems like I should have gotten more done tonight, but I was having problems with the microcontroller to computer interface, so that slowed me down a bit. For some reason, there does not appear to be any good way to output floating point numbers (decimals) to the computer. I looked it up online and other people had the same problem. It will, however, output the integer portion of a floating point number, so I ended up just multiplying the angles by 10 so I could see the first decimal place. Not a problem when this is actually in operation on the sailboat, but its annoying for testing and debugging the software.

Writing the blog posts also takes up a bit of time, but since I have a poor memory, I suspect some of these posts will be useful to look back on in the future.

No comments:

Post a Comment