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:
- None — not on a closing track, or beyond the awareness window.
- Awareness — silent on-chart attention. Cross + hover label, no audio. “Watch this one.”
- Alarm — audible klaxon + red-blink marker + always-on label. “Act now.”
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:
- A dashed line from own boat to where own boat will be at the CPA timestamp (own CPA point).
- A dashed line from the target to where it will be at the same timestamp (target CPA point).
- 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:
- Click anywhere in the banner — dismisses the current alarm. If the threat is still active on the next tick, the banner comes back.
- Snooze 10 m — mutes this specific MMSI for 10 minutes. Another dangerous vessel will still trigger the alarm. The snooze is in-memory only, cleared by a page reload — a new watch is a fresh start.
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:
- Pick a local origin at the midpoint of the two vessels, project both
positions to metres using
metresPerDegLon = 111_320 · cos(midLat). - Build 2-D velocity vectors from (COG, SOG):
v = (sin(COG) · SOG, cos(COG) · SOG). - Relative position
dp = own - target; relative velocitydv = vₒ − vₜ. - Minimise
|dp + dv·t|²analytically:t* = −(dp · dv) / (dv · dv). - If
t* < 0, the CPA has already happened — vessels are diverging, returnnull. - 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:
- Both stationary — return
null(no approach is happening). - Parallel courses at same speed —
|dv|² ≈ 0; return the current separation withTCPA = 0(they’re already at their minimum distance).
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:
- SOG stays below 0.514 m/s (~1 kn) for 60 s straight → vessel is moored and skipped by the CPA loop.
- SOG climbs back above the threshold → clock resets, vessel becomes a candidate again immediately.
- Vessel drops off AIS range →
Cleanup(activeContexts)evicts its entry so the dictionary doesn’t grow without bound.
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:
AisStore.GetVessels()— a cached array rebuilt only when the backing dictionary version changed.- For each vessel: early-out if position / COG / SOG is null, then the moored
check (O(1) dictionary lookup), then the snooze check (same), then
Cpa.Compute(constant work). First match raises the alarm and exits.
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:
- C#
Utilities/Cpa.cs— used byServices/AlarmManager(viaCpaAlarmRule) to drive the banner, and byMap.razor.BuildVesselListto sort the vessel list. - JS
geoMath.js:computeCpa— used byupdateAisTargetsto colour targets and draw crossing lines. Covered by Node’snode:testsuite ingeoMath.test.js.
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
- Polar regions — equirectangular projection assumes
cos(latitude)is well away from zero. Above ~75° latitude the east-west distance scaling starts to drift; CPA distances become unreliable though the “converging yes/no” answer stays correct. If you’re sailing the NW Passage, file an issue. - Constant-velocity assumption — both vessels are assumed to hold current course and speed. In reality a vessel manoeuvring defeats the forecast. This is the same assumption every AIS/ARPA system makes, including commercial ECDIS.
- Radar ARPA via Mayara. When Mayara
is feeding
radars.<radarId>.targets.<targetId>.*, those targets are routed intoAisStoreunder a synthesisedradar.<rid>.<tid>context and run through the same CPA / snooze / banner pipeline as AIS vessels- they render as outline triangles to keep them visually distinct on the chart but participate in the same alarm logic.
- No buddy-list integration yet — the
BuddyListApidetectssbender9/signalk-buddylist-plugin, but the UI doesn’t yet mark buddies specially or exempt them from CPA alarms. On the roadmap.
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 |