In addition to the location of the boat, we need to know which way it is pointing so we need a compass. Electronic compasses work by measuring the flux of the earth's magnetic field and computing the direction of the horizontal maximum. Tilting the compass changes the field strengths so it also needs to know the orientation of the chip relative to the earth's gravitational field so it needs accelerometers too. Rather surprisingly all this is quite cheaply available in chip form. I chose the CMPS10 board from Devantech, using its I2C interface.
I used the Adafuit I2C module again and wrote a class along the same lines as the BMP085 class I use in the barometer. This is much simpler since all data is directly available in registers:
A simple test script for this module:
The script has to be run under sudo: sudo python compass.py
The bearing values are repeatable with a range of about +- 0.5 degrees and are a good match to the direction on a nearly phyiscal compass. Pitch and roll work as expected, returning rotation in degrees around the North axis of the chip. It will be interesting to differentiate these values on board to record the strength and period of the boat's motion.
A compass suffers from two types of error: deviation and variation. Deviation is due to local magnetic fields from elctrical equipment or iron. The CMPS10 has a calibration mode to counter these errors if they can't be minimised by careful location. The limited range of I2C reduces the ability to site the compass away for distrubance so this may require a relay. Variation (or declination) is due to changes in the difference between the direction of the magnetic pole and true North with position on the earth's surface and time . This could either be entered manually or computed from tables or one of the models of the magnetic field. For our purposes, an editable configuration file would be sufficient.
According to the NGDC online calculator, the variation in Bristol today is -2.2148 so to convert from compass bearing to True North we need to add this value.
This leads to an improved module for the compass with methods for both magnetic and true bearings. I have also used properties where possible and renamed the accelerometer property:
Now to get a battery power-pack and see how this, combined with the GPS receiver, would work in motion. A test project would be to download the data from my Gloucester Road project (a bit neglected this summer) and get it to tell me about each shop as I walk past.