# Vertical-card compass for the M5Stack CoreS3

Simulation of an aviation **vertical-card magnetic compass**, errors and everything — for the ESP32 (tested on M5Stack CoreS3).

> [!NOTE]
> Entirely vibe-coded with [Claude Code](https://claude.com/claude-code).

## Demo

[Demo and conversation on X](https://x.com/austinbirch/status/2086535581773828169)

https://github.com/user-attachments/assets/133d94dd-b265-4dea-931c-4b7b5167d159

## What's modeled

- **The card** — a mirrored vertical drum, as in a real direct-reading compass
  (headings increase to the _left_), rendered side-on with depth, curvature,
  and shadow.
- **A real heading** — tilt-compensated magnetometer, hard-iron calibration
  (least-squares sphere fit), gyro fusion, interference gating, and gyro
  coasting through magnetometer dropouts.
- **Pendulous card physics** — the errors aren't injected; the card
  hangs perpendicular to _apparent_ vertical, so accelerating or turning tilts
  it, and the Earth field's vertical component leaks into its reading. ANDS
  and UNOS emerge from the projection, scale with the measured local dip, and
  reverse in the southern hemisphere — automatically.
- **Card dynamics** — a damped torsional oscillator: swing, overshoot, settle.
- **Floating in fluid** — underdamped rock, bob, and sway; a lateral-g strip
  along the top edge (artistic license — real wet compasses don't have one).

## Realism backlog

Not modeled yet, in rough order of payoff: **fluid swirl error** (the damping
fluid spins up in a sustained turn and keeps dragging the card after rollout),
**pivot stiction** (cards settle a degree off and stay until jostled),
**static dip tilt** (the pendulous-weight vs magnet torque balance that tilts
the card toward north), **soft-iron (ellipsoid) calibration**, **vibration
dither**, and a **configurable demo dip** for southern-hemisphere or
equatorial demos.

## Hardware

An [M5Stack CoreS3](https://docs.m5stack.com/en/core/CoreS3) (ESP32-S3, BMI270
IMU + BMM150 magnetometer, 320x240 touch display) and a USB-C cable — nothing
else. Works flat on a desk or mounted vertically on a windscreen; the forward
axis is derived from gravity.

## Installing

Install [PlatformIO Core](https://platformio.org/install/cli)
(`brew install platformio` or `pip install platformio`; the VS Code extension
works too), connect the CoreS3, then:

```sh
pio run -t upload      # build + flash (first build takes a few minutes)
pio device monitor     # optional: watch the telemetry stream
```

Serial ports are auto-detected; override with `upload_port` in
`platformio.ini`.

## Usage

![Vertical card compass on the CoreS3](docs/screenshot.png)

- **Triple-tap** the screen for the settings menu (calibration, volume, debug
  overlay); settings persist in NVS.
- **Calibrate**: pick "mag cal", tumble the device slowly through all
  orientations until the bars go green, then tap to save — away from steel
  desks, speakers, and USB hubs. Until then it runs in demo mode.
- **Demonstrate the errors**: heading E or W, slide the device sharply
  forward — the card swings toward N (ANDS). Heading N, whip through a fast
  flat turn — the card lags (UNOS). The steeper your local dip, the bigger
  both errors.

## Tools

Scripts in `tools/` talk to the device over USB serial (macOS/Linux; port
auto-detected, set `COMPASS_PORT` to override):

- `serial-log.sh [seconds] [outfile]` — timestamped telemetry capture.
- `analyze-cal.py [logfile]` — sphere-fits a tumble capture and searches all
  48 signed axis permutations to verify the mag→accel mapping (needs numpy).
- `screenshot.sh [outfile.png]` — pulls the current frame as a PNG.

## License

MIT — see [LICENSE](LICENSE).
