Friday, August 22, 2014

A low-cost marine compass (Part 3 – Calibration)

When connected to a terminal program through the FTDI breakout at 115200 baud, the compass displays the results formatted as NMEA sentences. This is the default operating mode (Mode 4 in the software).

Two calibration helper modes are available:
        Mode 1 : Accelerometer  calibration (type M1<ENTER>)
        Mode 2 : Magnetometer calibration (type M2<ENTER>)

These calibration modes are used to produce the raw data files required by the calibration software Magneto. In these modes, the compass stops displaying results until it receives an ‘x’ character (type x<ENTER> in the terminal program).

In Mode 1 (Accelerometer), it will then displays 64 consecutive (x,y,z) filtered raw data (at the 200 Hz rate), then pauses until it receives another ‘x’ character.

In Mode 2 (Magnetometer), it will displays 16 consecutive (x,y,z) raw data (at the 6.5 Hz rate), then pauses until it receives another ‘x’ character.

By logging the results to a text file in the terminal program, it is then possible to create the Magneto input files, by moving the compass around and pressing the ‘x’ character at each new position.

I recommend moving the compass with roughly equal angle steps around three perpendicular axis. For the accelerometer, you can use any support (metallic or not) to help position the compass, but it is very important to wait until the compass is completely at rest (no vibration) before hitting the ‘x’ character.

For the magnetometer, try to use the lightest non-magnetic support (wood or plastic), and use the ribbon cable to keep away the FTDI brekout and especially the laptop (all this in a magnetic clean environment!).

The 2 raw data files can then be processed by the Magneto software. The Pro Mini source file can then be edited with the new values and recompiled.

Unfortunately, this is not the end of the game if we want to be rigorous. We will have to look for misalignments between three independent reference frames: the compass base, the accelerometer plan and the magnetometer plan, with a final offset correction when compared to a known heading.

But the compass should already be quite usable, as these last corrections will be quite small compared to what has been accomplished.

A low-cost marine compass (Part 1 - Hardware)
A low-cost marine compass (Part 2 - Software)

Wednesday, August 20, 2014

A low-cost marine compass (Part 2 – Software)

The problem with low-cost sensors is that the accelerometer outputs are very noisy (much less so for the magnetometer).

The LSM303D sensor is one of 2 magnetic sensors supported by the PX4 development team. The solution they have chosen to reduce the noise in the accelerometer outputs is to apply a low-pass software filter to the measurements, in addition to the hardware low-pass filter built into the chip. In this project, I have ported their LSM303D SPI driver and 2-pole software filter to the WinAVR environment.

I have also kept the strategy adopted for my Hi-Res compass, which is to use a higher sampling rate for the accelerometer, and take the average result in the calculation performed after each magnetometer measurement.

I have chosen a 6.25 Hz sampling rate for the LSM303D magnetometer, to insure that it will operate in its hi-resolution mode. The accelerometer sampling rate is set at 200 Hz, and the cut-off frequency of the software low-pass filter is set at 40 Hz.

Here is a general explanation of the code behaviour during normal operation.

The Pro Mini continually reads triple raw values (x,y,z) from the accelerometer. For each axis, it feeds the raw value to the software low-pass filter, which spits out a new filtered value for each axis. The filtered values are accumulated and counted.

Each time the LSM303D magnetometer has a new set of 3 raw values available (x,y,z), it fires an interrupt on the Pro Mini. When this happens, the Pro Mini applies the calibration correction to the magnetometer values. It calculates the average of the accelerometer filtered values since the last interrupt, and applies the calibration correction. From this set of 6 values, it calculates the tilt-compensated heading, and the heel and pitch angles. It formats the results in 2 NMEA sentences that are sent to the serial port (115200 baud), 6.25 times per second. 

The source code for the WinAVR environment can be found here. I am using Programmer’s Notepad to compile the code and program the Pro Mini through the Arduino bootloader. 

This implies that the calibration factors have already been found since they appear as constants in the code. The software has other modes of operation useful for developing these calibration factors, which will be presented in Part 3 of this series.

A low-cost marine compass (Part 1 – Hardware
A low-cost marine compass (Part 3 - Calibration

Tuesday, August 19, 2014

A low-cost marine compass (Part 1 – Hardware)

For an electronic compass, the accuracy depends more on the quality of the calibration that on the price of the components. Here is the design of a low-cost compass developed around this idea.

The compass is based on the LSM303D chip which combines a 3-axis accelerometer and a 3-axis magnetometer. I used the Pololu carrier board featuring this chip (9.95 US$).

The other component of the compass is an Arduino Pro Mini 328 – 3.3V/8Mhz from Sparkfun (9.95 US$). The Pro Mini communicates with the LSM303D through the SPI bus, using drivers adapted from the PX4 Development Team. The 2 components are hardwired together to keep a compact form.

The following picture illustrates the small compass besides my Hi-Resolution custom compass previously described on this blog.

Here is the wiring diagram for SPI communication. The FTDI breakout is used to program the Pro Mini and to read the LSM303D raw data during the calibration step.

The long ribbon cable is required during calibration so that the compass can be moved around at a safe distance from the breakout and the laptop to avoid magnetic interference. After calibration, the ribbon cable is used to power the compass (3.3V) and to transmit the tilt-compensated heading and the heel angle through the serial link to whatever instrument that needs it.

A low-cost marine compass (Part 2 – Software) 
A low-cost marine compass (Part 3 - Calibration)