+++
title = 'Sensor Fusion for Autonomous Underwater Vehicles'
description = 'Combining data from multiple sensors to make them all more useful!'
date = 2025-02-27T07:17:00-05:00
draft = false
categories = ['USM', 'magnetics']
tags = ['data science', 'KarstTech', 'UUV', 'sensor fusion']
+++

## Getting Started
Several months ago I was referred to the University of Southern Mississippi [Marine Research Center](https://www.usm.edu/ocean-enterprise/marine-research-center.php) by a coworker. They wanted some assistance from someone with a physics and data science background to work on an autonomous underwater vehicle for the purpose of developing a magnetic sensing platform.

The work sounded interesting, so I joined the team as a contractor. Fast forward several months and we are making fantastic progress!

The work began with using data acquired from high quality magnetic sensors including from [QUSPIN](https://quspin.com/), and processing the data to detect magnetic objects of interest. This magnetic sensing becomes a very difficult problem due to a combination of factors:
1.  Magnetic fields fall off as the cube of the distance from the source (rather than the inverse square law that we all know from electromagnetic fields like light). This makes for a much more challenging problem since the size of the signals we are looking for are TINY compared to the noise in the data once we get to any significant distance from the source.
2.  The Earth's magnetic field, while weak compared to some magnets that we use in everyday life, is very strong due to it's very large size and therefore slow falloff compared to small targets.
3.  The targets of interest are passive ferromagnetic objects, meaning their field is created by becoming magnetized by the Earth's magnetic field. These induced magnetic fields are very weak compared to the Earth's magnetic field.
4.  The sensing platform is moving, and so the vehicle's orientation, and its vector magnetic sensor readings are changing rapidly. Imbalances in the readings between vector components due to sensor imperfections, and due to the magnetic properties of the vehicle itself make this motion difficult to filter out.

![Test Vehicle](surface-vehicle.png "Test Vehicle")

## Sensor Fusion
Before we can even attempt to detect targets, we need to convert the vehicle reference frame to the Earth's reference frame. This is a non-trivial problem because the vehicle is free to rotate around the z-axis (up/down), can pitch and roll due to surface conditions and for an underwater vehicle it can move and orient itself in virtually any direction. In addition to the vehicle orientation quickly changing, the Earth's magnetic field is very complex, and varies from location to location on the surface of the Earth.

![Earth's Magnetic Field](earth-geomag.webp "Earth's Magnetic Field (courtesy of [Wikipedia](https://en.wikipedia.org/wiki/Earth%27s_magnetic_field))")

While the Earth's scale magnetic field is complex, many people assume that on the local scale, the magnetic field is constant. This is not the case, and local variations can be large enough to throw off the vehicle's orientation estimate. In a surface vehicle, this can be mitigated by using GPS to correct for long-term drift of the compass / magnetic measurements, but underwater it becomes much more complicated without a detailed map of local magnetic anomalies. Designing a method for planning underwater missions which can correct for these local variations is a problem that is being addressed in the near future, but in short it involves some calibration using surface data, and then using that calibration to improve underwater navigation.

![Local Magnetic Field Variations](gulfport-geomag.png "Local Magnetic Field Variations near Gulfport, MS")

The sensor fusion process attempts to take the best aspects of each sensor and combine them in a way that provides the most accurate estimate of the vehicle's orientation and position. For gyroscopes, which are very accurate at measuring short-term rotations, but suffer from drift over time, we can use the magnetometer to correct for the drift. We can use accelerometers to correct for quick movements, and to measure the long term orientation relative to gravity, while using GPS to provide long term movement and position data. Magnetometers are immune to the errors introduced into accelerometers by quick movements, especially in surface waves, and so they provide a very accurate measurement of the vehicle's orientation, but are subject to small errors due to local magnetic anomalies, and due to magnetic interference from the vehicle itself. Work is underway to integrate doppler velocity data from a DVL sensor to improve the vehicle's estimate of its position and minimize drift from GPS reference during extended underwater missions.

The exact method used to fuse the data is highly platform specific and needs to be tuned for the physical characteristics of the vehicle, and its sensors. The diagram below shows the general process, but the exact implementation details will not be discussed here.


{{< mermaid >}}
flowchart TD
    subgraph "Input Sensors"
        direction TB
        GPS["GPS + RTK
        (Position Data)
        Note: Unavailable when underwater"]:::inputNode
        QTFM["QTFM Magnetics
        (X, Y, Z Magnetic Field)"]:::inputNode
        IMU["Inertial Measurement Unit (IMU)"]:::inputNode
        IMU_GYRO["IMU Gyroscope
        (Roll, Pitch, Yaw Rates)
        Only shows rate of change"]:::inputNode
        IMU_ACCEL["IMU Accelerometer
        (X, Y, Z Acceleration)
        Note: Affected by quick movements"]:::inputNode
        IMU_MAG["IMU Magnetometer
        (X, Y, Z Magnetic Field)
        Note: Affected by nearby magnetic fields"]:::inputNode
    end

    subgraph "Sensor Fusion Process"
        direction TB
        SF["Sensor Fusion Algorithm"]:::processingNode
        GD["Geomagnetic Declination Correction
        Note: Declination varies by location & time"]:::processingNode
        OC["Orientation Correction"]:::processingNode
        FILTER["Signal Filtering"]:::processingNode
    end

    subgraph "Output Data"
        direction TB
        POS["Geographic Position
        (Earth Coordinates)"]:::outputNode
        ROT["Platform Orientation
        (Roll, Pitch, Yaw)"]:::outputNode
        MAG_CORR["Orientation Corrected
        Magnetic Data"]:::outputNode
        TARGET["Target Detection
        & Localization"]:::outputNode
    end
    
    classDef inputNode fill:#d4f1f9,stroke:#05386B,color:black
    classDef processingNode fill:#f9e79f,stroke:#b7950b,color:black
    classDef outputNode fill:#c8e6c9,stroke:#2e7d32,color:black
    
    IMU --- IMU_GYRO
    IMU --- IMU_ACCEL
    IMU --- IMU_MAG
    
    GPS --> SF
    QTFM --> SF
    IMU_GYRO --> SF
    IMU_ACCEL --> SF
    IMU_MAG --> SF
    
    SF --> GD
    GD --> OC
    OC --> FILTER
    
    SF --> POS
    SF --> ROT
    FILTER --> MAG_CORR
    MAG_CORR --> TARGET
{{< /mermaid >}}

Once the vehicle knows its own position and orientation through sensor fusion, we can use that information to convert the location of detected objects near the vehicle into the Earth's reference frame. This allows combining measurements from multiple passes over the same area, by multiple vehicles, or even on different days.

