Skip to the content.

Collision detection

OnaPlotter runs a proper Closest-Point-of-Approach (CPA) model rather than a proximity beeper. It classifies every AIS target into one of three bands:

The two thresholds (alarm + awareness) replace the older single “guard zone” radius. Awareness is the wider band; alarm is the inner, strict band.

What you see on the map

                      target (red, in alarm tier)
                          ▼
                          ▲  ← 60 s fading trail
       ┌─────────── "0.34 nm · T-8m"  ← midpoint label
       │                  ▲
       │                  │  dashed red from target to its CPA point
       ▼                  ▼
   own-CPA ●          target-CPA ●
       ▲
       │  dashed red from own boat to its CPA point
       │
       ●  ← own boat (magenta arrow)

Own boat — magenta arrow pointing along heading. A dashed pink vector ahead shows a configurable look-ahead at current SOG/COG. Optional helm-configured distance rings (Settings → Display → Distance rings) act as measurement scaffolding; they have no alarm semantics.

AIS vessels — triangles coloured by ship type. A 60-second fading trail shows recent positions. Vessels in the alarm tier turn red and blink with an always-visible label; vessels in the awareness tier turn amber with a hover label only.

Crossing-situation lines — drawn when a target’s projected CPA falls into either tier:

  1. A dashed line from own boat to where own boat will be at the CPA timestamp (own CPA point).
  2. A dashed line from the target to where it will be at the same timestamp (target CPA point).
  3. A midpoint label showing the CPA distance in nautical miles and TCPA as minutes.

Red for alarm-tier targets, amber for awareness-tier targets.

The alarm

The top banner surfaces the most pressing threat. For CPA it reads:

CPA BOAT NAME: CPA 0.34nm in 8min

Two buttons:

Audio is orchestrated by wwwroot/js/platform/audioAlert.js and uses different pulse rates for danger (1 s, used by CPA-alarm and SHALLOW) and warn (3 s, used by WIND SHIFT). A single looped tone so it’s obvious to a sleeping captain. Awareness-tier targets never trigger audio.

Tuning

Settings → Alarms:

Setting Default What it does
CPA & TCPA Alarm 0.1 nm / 30 min Audible alarm fires when projected CPA is below the distance AND TCPA is inside the window.
CPA & TCPA Awareness 1.0 nm / 30 min Silent on-chart cross + hover label. Setter clamps awareness ≥ alarm so the alarm always sits inside an awareness band.
Depth Alarm 2 m Overrides CPA in the banner priority order.
Wind Shift Alarm 30° / 10 min TWD change over the lookback, advisory-level (warn).

All persisted to localStorage via IAppSettings; they take effect live — no reload needed. The classifier runs server-side (Cpa.ClassifyThreat inside AisLayerController.BuildSnapshot); the JS layer just reads the precomputed cpaThreat field on each vessel payload.

How the math works (OnaPlotter/Utilities/Cpa.cs)

For short-range crossings — which is every scenario we care about — a local equirectangular projection is accurate to a fraction of a percent. Polar regions are the exception (see the Limitations section below).

Given two vessels with positions in degrees and (COG, SOG) in (radians from north, m/s), the math is:

  1. Pick a local origin at the midpoint of the two vessels, project both positions to metres using metresPerDegLon = 111_320 · cos(midLat).
  2. Build 2-D velocity vectors from (COG, SOG): v = (sin(COG) · SOG, cos(COG) · SOG).
  3. Relative position dp = own - target; relative velocity dv = vₒ − vₜ.
  4. Minimise |dp + dv·t|² analytically: t* = −(dp · dv) / (dv · dv).
  5. If t* < 0, the CPA has already happened — vessels are diverging, return null.
  6. Otherwise plug t* back in: cpa = |dp + dv·t*| (metres → nm by dividing by 1852), tcpa = t* (seconds → minutes by dividing by 60).

Two edge cases:

The SOG stationary threshold is 0.1 m/s (~0.2 kn).

Auto-muting moored vessels

A harbour tug drifting on its lines, a fishing boat jogging on station, or a ferry waiting for a berth should not keep waking the captain. MooredVesselTracker watches every AIS vessel:

The dwell is short enough (60 s vs. earlier 120 s) that anchored neighbours stop chattering the CPA alarm within a minute of settling, but long enough that a vessel briefly slowing through a turn doesn’t get silently exempted from the projection. See OnaPlotter.Tests/MooredVesselTrackerTests.cs.

Performance

The alarm pipeline is debounced to 1 Hz even if SignalK deltas arrive at 3+ Hz. Work per tick:

On a busy coastline with 40 AIS targets that’s ~40 constant-time checks per second — negligible even on a Pi.

What’s drawn in JavaScript

The Leaflet side (wwwroot/js/leafletInterop.js) owns the visual crossing lines so they can update on every position fix without Blazor re-renders. C# and JS each have their own copy of the CPA math:

Both implementations are ported from the same derivation and exercised by parallel test suites. The JS copy cannot call into Blazor during the AIS push, which happens at 3 Hz on a fast delta feed.

Limitations

Where things live

Concern File
CPA math + ClassifyThreat (C#) OnaPlotter/Utilities/Cpa.cs
CPA math (JS) OnaPlotter/wwwroot/js/geoMath.js (computeCpa)
Alarm + awareness thresholds OnaPlotter/Services/AppSettingsService.cs (CpaAlarmNm, TcpaAlarmMin, CpaAwarenessNm, TcpaAwarenessMin)
Moored auto-mute OnaPlotter/Services/MooredVesselTracker.cs
Alarm pipeline (C#) OnaPlotter/Services/AlarmManager.cs (Evaluate, DismissAsync, SnoozeActiveAsync, SnoozeAsync)
CPA alarm rule OnaPlotter/Services/Alarms/CpaAlarmRule.cs
Chip classifier (C#) OnaPlotter/Services/Map/AisLayerController.cs (ComputeVesselThreat, ThreatToWireString)
Audio (JS) OnaPlotter/wwwroot/js/platform/audioAlert.js
Chip rendering + CPA lines OnaPlotter/wwwroot/js/aisLayer.js
Distance rings (helm scaffold) OnaPlotter/wwwroot/js/aisLayer.js (setDistanceRings)
Settings UI OnaPlotter/Components/Pages/Settings.razor
Tests OnaPlotter.Tests/CpaTests.cs, MooredVesselTrackerTests.cs, geoMath.test.js