Magnetometer array reader & dipole magnet localizer
magsensor is a demo codebase. It reads a 9-sensor magnetometer array via serial and localizes a dipole magnet in real time, with a live 3D visualization GUI.
Prerequisites: uv, and of course, a magnetometer array :)
Clone this repo and install dependencies:
git clone https://github.com/zixingjiang/magsensor
cd magsensor
# install with optional extras as needed
uv sync # core dependencies
uv sync --extra gui # with GUI
uv sync --extra examples # to run the example
uv sync --extra dev # everythingUsage:
uv run magsensor --help # show available commands; also works on subcommands (e.g. magsensor read --help)
uv run magsensor read # read raw sensor data from the array
uv run magsensor calibrate # calibrate sensor bias offsets
uv run magsensor localize # localize a dipole magnet from current readings
uv run magsensor gui # launch the all-in-one GUILocalizing a dipole magnet from a magnetometer array is formulated as a nonlinear least-squares problem: find the 5-DoF magnet pose (position and heading) that minimizes the difference between the measured field and the analytical dipole model.
The magnetic field at position
where
Let
The localizer solves for
This codebase provides two complementary solvers:
| Solver | Description |
|---|---|
| SQP (Sequential Quadratic Programming) | Gradient-based optimizer using CasADi's sqpmethod. Default solver. |
| PIA (Particle Importance Analysis) | Random particle sampling followed by Gram-Schmidt Orthogonal Least Squares. Global search, less susceptible to local minima. |
Note: This is an unofficial implementation of the PIA algorithm. See XuRuoyuRoy/Particle-Importance-Analysis for the official version.
Both solvers support solving with the moment magnitude
The magnetometer array (a) contains 9 LIS3MDL sensors arranged in a
The codebase includes a toy demo showing how dipole‑magnet localization can support ultrasound‑guided needle insertion. By tracking a magnet mounted on the needle, its position relative to the ultrasound scan plane is estimated, enabling an overlay on the ultrasound image that helps keep the needle aligned with the scan plane.
To run the example:
uv run examples/needle_overlay/3dview.py examples/needle_overlay/config.yaml # 3D illustration
uv run examples/needle_overlay/overlay.py examples/needle_overlay/config.yaml # ultrasound image overlay
uv run examples/needle_overlay/simulation.py examples/needle_overlay/config.yaml # simulation without ultrasound


