Monday, February 9, 2026

New 10 Hz Marine Compass (Part 2 - Software)

I have made major changes to the software used in my marine compass.

First, all is done now in the Arduino environment. For this project, this allows the use of powerful libraries for the accelerometer and magnetometer breakouts. I also makes the code easier to read, and facilitates the editing and recompilation (for example, to apply new or revised calibration factors).

Also, I am now using a full 9-DOF implementation which also makes use of the gyroscope in the LSM6DSO chip. The result is based on a detailed analysis of the datasheets, in order to optimize the different configurations for sailboat dynamics.

The Arduino .ino file can be found here. In the Arduino IDE, the 2 following libraries are needed:
- STM32duino LSM6DSO by SRA
- STM32duino LIS3MDL by  AST

Following is a summary (produced with the help of ChatGPT) of the different design decisions that have been made during the development.

A much more detailed summary can be found in this .pdf file


A Marine-Optimized Tilt-Compensated Compass

Lightweight, robust heading for a sailboat

Designing a compass for a sailboat is very different from building one for a drone or a lab bench. Offshore, the boat is constantly rolling, pitching, yawing, vibrating, and occasionally passing near magnetic disturbances. A usable marine compass must remain smooth, stable, and trustworthy under all of that.

This implementation uses:

LSM6DSO (gyro + accelerometer)
LIS3MDL (magnetometer)
A lightweight complementary fusion structure
A 50 Hz control loop

The goal is not maximum dynamics — it’s stable, natural marine behavior.


Sensor Setup (Optimized for Sailboats)

Update Rates

Accel/Gyro: 52 Hz
Magnetometer: 10 Hz
Fusion loop: 50 Hz

Sailboat heading changes are slow (typically < 1 Hz). Higher rates only add noise and CPU load without benefit.


Gyroscope Filtering

LPF1 set to 9.7 Hz
LPF2 fixed at 16.6 Hz (hardware)

Why 9.7 Hz?

Because we only care about low-frequency yaw motion (helm input and waves). This setting:

Reduces vibration from hull and rigging
Stabilizes gyro integration
Improves sea-state detection
Does not reduce steering responsiveness

Magnetometer Configuration

ODR: 10 Hz
Mode: Ultra-High Performance
External 2nd-order low-pass ≈ 2 Hz

This:

Passes real helm motion
Rejects wave spikes
Reduces magnetic jitter
Keeps heading natural and smooth

Tilt Compensation

Roll and pitch are computed from the accelerometer and used to rotate the magnetic field into the horizontal plane before computing yaw.

This ensures correct heading even at 20–30° of heel — common under sail.


Fusion Strategy (Why Not Full AHRS?)

Instead of Mahony or Madgwick, this design uses a minimal complementary structure:

Gyro → short-term yaw stability
Magnetometer → long-term heading reference
Accelerometer → tilt reference

Advantages:

Predictable behavior
Very low CPU load
Runs on small microcontrollers
Easy to tune for marine dynamics

This is a deliberate choice: sailboats do not need aggressive high-dynamic fusion.


Adaptive Yaw Gain (Sea-State Aware)

A rolling RMS of gyro magnitude estimates boat motion.

Calm water → higher magnetic correction gain
Rough sea → reduced magnetic trust

This prevents wave-induced heading jitter while maintaining long-term accuracy.


Automatic Magnetic Anomaly Rejection

Magnetometer updates are rejected if:

Field magnitude deviates from expected norm
Heading jump exceeds plausible rate

During rejection, the system runs gyro-only temporarily and resumes correction smoothly once the field stabilizes.

This is essential for real onboard installations.


Why This Works

Because it matches:

The frequency content of sailboat motion
The noise characteristics of MEMS sensors
The computational limits of embedded hardware
The real-world behavior of marine environments

The result is not just a tilt-compensated compass.

It’s a marine-tuned heading system — smooth in swell, responsive to the helm, stable near interference, and efficient enough to run continuously onboard.