kinavis 1.0.1

A Rust library for solving common maritime navigation tasks.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Standard observations for the estimator.
//!
//! Each type implements [`Observation`] for one reading kind: position, ground
//! velocity, heading, speed through water. A receiver fix becomes a
//! [`PositionObservation`] and optionally a [`VelocityObservation`] on the
//! caller's side via [`PositionObservation::from_fix`] and
//! [`VelocityObservation::from_fix`]; the estimator never sees the fix.
//!
//! A position is observed in its own local frame: the observation anchors a
//! frame at the reported point and predicts the state's displacement from it,
//! so the measurement is zero and the innovation is the vector from estimate to
//! report, in metres. The observation needs no knowledge of the state anchor
//! and works in metres, not degrees.

use crate::angle::{wrap180, TrueCourse};
use crate::estimation::{
    GatingPolicy, JacobianRow, Observation, ObservationJacobian, ObservationNoise,
    ObservationVector,
};
use crate::event::SensorId;
use crate::geodesy::{Ellipsoid, GeodeticPoint, Height};
use crate::gnss::GnssFix;
use crate::local::{LocalFrame, Ned, Vector3};
use crate::math;
use crate::position::Position;
use crate::state::StateComponent::{CurrentEast, CurrentNorth, Heading, SpeedThroughWater};
use crate::state::{NavigationState, StateComponent};
use crate::time::{Instant, Utc};
use crate::units::{Angle, Distance, Speed};
use kinavis_kernel::error::Result;

/// χ² threshold exceeded with probability 0.1 % for one element: default 1-dof
/// gate.
pub const GATE_ONE_IN_A_THOUSAND_1DOF: f64 = 10.83;

/// Same for two elements.
pub const GATE_ONE_IN_A_THOUSAND_2DOF: f64 = 13.82;

/// Position from a receiver or a manual fix.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PositionObservation {
    taken_at: Instant<Utc>,
    position: Position,
    sigma_north: Distance,
    sigma_east: Distance,
    gate: GatingPolicy,
    sensor: SensorId,
}

impl PositionObservation {
    /// Position with isotropic 1σ uncertainty.
    #[must_use]
    pub fn new(taken_at: Instant<Utc>, position: Position, sigma: Distance) -> Self {
        Self {
            taken_at,
            position,
            sigma_north: sigma,
            sigma_east: sigma,
            gate: GatingPolicy::reject_above(GATE_ONE_IN_A_THOUSAND_2DOF),
            sensor: SensorId::named("position"),
        }
    }

    /// Position with separate north and east uncertainties.
    #[must_use]
    pub fn with_sigmas(mut self, north: Distance, east: Distance) -> Self {
        self.sigma_north = north;
        self.sigma_east = east;
        self
    }

    /// Sets the gate.
    #[must_use]
    pub const fn with_gate(mut self, gate: GatingPolicy) -> Self {
        self.gate = gate;
        self
    }

    /// Sets the source identifier; differently named receivers are judged
    /// separately.
    #[must_use]
    pub const fn from_sensor(mut self, sensor: SensorId) -> Self {
        self.sensor = sensor;
        self
    }

    /// Position of a fix, with its horizontal accuracy as sigma, or `fallback`
    /// if none.
    #[must_use]
    pub fn from_fix(fix: &GnssFix, fallback: Distance) -> Self {
        Self::new(
            fix.taken_at(),
            fix.position(),
            fix.horizontal_accuracy().unwrap_or(fallback),
        )
    }

    /// Reported position.
    #[must_use]
    pub const fn position(&self) -> Position {
        self.position
    }
}

impl Observation for PositionObservation {
    fn sensor(&self) -> SensorId {
        self.sensor
    }

    fn taken_at(&self) -> Instant<Utc> {
        self.taken_at
    }

    /// Zero: the observation is its own origin.
    fn measured(&self) -> ObservationVector {
        ObservationVector::new(&[0.0, 0.0]).unwrap_or(ObservationVector::EMPTY)
    }

    /// State displacement from the reported position, north and east, m.
    fn predict(&self, state: &NavigationState) -> Result<ObservationVector> {
        let here = GeodeticPoint::new(self.position, Height::above_ellipsoid(Distance::ZERO));
        let frame = LocalFrame::at(here, &Ellipsoid::WGS84)?;
        let there = GeodeticPoint::new(state.position(), Height::above_ellipsoid(Distance::ZERO));
        let displacement: Vector3<Ned, Distance> = frame.ned_of(there)?;
        ObservationVector::new(&[displacement.north().metres(), displacement.east().metres()])
    }

    /// Identity on the position components: the observation frame and the state
    /// frame are parallel well within noise for any displacement the gate
    /// passes.
    fn jacobian(&self, _: &NavigationState) -> Result<ObservationJacobian> {
        ObservationJacobian::new()
            .with_row(JacobianRow::new().with(StateComponent::North, 1.0))?
            .with_row(JacobianRow::new().with(StateComponent::East, 1.0))
    }

    fn noise(&self) -> ObservationNoise {
        sigmas(&[self.sigma_north.metres(), self.sigma_east.metres()])
    }

    fn gate(&self) -> GatingPolicy {
        self.gate
    }
}

/// Ground velocity (receiver course and speed).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VelocityObservation {
    taken_at: Instant<Utc>,
    velocity: Vector3<Ned, Speed>,
    sigma: Speed,
    gate: GatingPolicy,
    sensor: SensorId,
}

impl VelocityObservation {
    /// Velocity with the same 1σ on each component.
    #[must_use]
    pub fn new(taken_at: Instant<Utc>, velocity: Vector3<Ned, Speed>, sigma: Speed) -> Self {
        Self {
            taken_at,
            velocity,
            sigma,
            gate: GatingPolicy::reject_above(GATE_ONE_IN_A_THOUSAND_2DOF),
            sensor: SensorId::named("velocity over ground"),
        }
    }

    /// Course and speed over ground.
    #[must_use]
    pub fn from_track(
        taken_at: Instant<Utc>,
        course: TrueCourse,
        speed: Speed,
        sigma: Speed,
    ) -> Self {
        let radians = math::to_radians(course.degrees());
        Self::new(
            taken_at,
            Vector3::new(
                speed * math::cos(radians),
                speed * math::sin(radians),
                Speed::ZERO,
            ),
            sigma,
        )
    }

    /// Velocity of a fix, if it reports both course and speed.
    #[must_use]
    pub fn from_fix(fix: &GnssFix, sigma: Speed) -> Option<Self> {
        Some(Self::from_track(
            fix.taken_at(),
            fix.course_over_ground()?,
            fix.speed_over_ground()?,
            sigma,
        ))
    }

    /// Sets the gate.
    #[must_use]
    pub const fn with_gate(mut self, gate: GatingPolicy) -> Self {
        self.gate = gate;
        self
    }

    /// Sets the source identifier; differently named receivers are judged
    /// separately.
    #[must_use]
    pub const fn from_sensor(mut self, sensor: SensorId) -> Self {
        self.sensor = sensor;
        self
    }
}

impl Observation for VelocityObservation {
    fn sensor(&self) -> SensorId {
        self.sensor
    }

    fn taken_at(&self) -> Instant<Utc> {
        self.taken_at
    }

    fn measured(&self) -> ObservationVector {
        vector(&[
            self.velocity.north().metres_per_second(),
            self.velocity.east().metres_per_second(),
        ])
    }

    /// `u (cos ψ, sin ψ) + c`.
    fn predict(&self, state: &NavigationState) -> Result<ObservationVector> {
        let velocity = state.velocity_over_ground();
        ObservationVector::new(&[
            velocity.north().metres_per_second(),
            velocity.east().metres_per_second(),
        ])
    }

    fn jacobian(&self, state: &NavigationState) -> Result<ObservationJacobian> {
        let heading = math::to_radians(state.heading().degrees());
        let speed = state.speed_through_water().metres_per_second();
        let (sin, cos) = (math::sin(heading), math::cos(heading));
        ObservationJacobian::new()
            .with_row(
                JacobianRow::new()
                    .with(Heading, -speed * sin)
                    .with(SpeedThroughWater, cos)
                    .with(CurrentNorth, 1.0),
            )?
            .with_row(
                JacobianRow::new()
                    .with(Heading, speed * cos)
                    .with(SpeedThroughWater, sin)
                    .with(CurrentEast, 1.0),
            )
    }

    fn noise(&self) -> ObservationNoise {
        let sigma = self.sigma.metres_per_second();
        sigmas(&[sigma, sigma])
    }

    fn gate(&self) -> GatingPolicy {
        self.gate
    }
}

/// Heading from a gyrocompass or corrected magnetic compass.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HeadingObservation {
    taken_at: Instant<Utc>,
    heading: TrueCourse,
    sigma: Angle,
    gate: GatingPolicy,
    sensor: SensorId,
}

impl HeadingObservation {
    /// Heading with 1σ uncertainty.
    #[must_use]
    pub fn new(taken_at: Instant<Utc>, heading: TrueCourse, sigma: Angle) -> Self {
        Self {
            taken_at,
            heading,
            sigma,
            gate: GatingPolicy::reject_above(GATE_ONE_IN_A_THOUSAND_1DOF),
            sensor: SensorId::named("heading"),
        }
    }

    /// Sets the gate.
    #[must_use]
    pub const fn with_gate(mut self, gate: GatingPolicy) -> Self {
        self.gate = gate;
        self
    }

    /// Sets the source identifier; differently named sensors are judged
    /// separately.
    #[must_use]
    pub const fn from_sensor(mut self, sensor: SensorId) -> Self {
        self.sensor = sensor;
        self
    }
}

impl Observation for HeadingObservation {
    fn sensor(&self) -> SensorId {
        self.sensor
    }

    fn taken_at(&self) -> Instant<Utc> {
        self.taken_at
    }

    fn measured(&self) -> ObservationVector {
        vector(&[math::to_radians(self.heading.degrees())])
    }

    fn predict(&self, state: &NavigationState) -> Result<ObservationVector> {
        ObservationVector::new(&[math::to_radians(state.heading().degrees())])
    }

    fn jacobian(&self, _: &NavigationState) -> Result<ObservationJacobian> {
        ObservationJacobian::new().with_row(JacobianRow::new().with(StateComponent::Heading, 1.0))
    }

    fn noise(&self) -> ObservationNoise {
        sigmas(&[self.sigma.radians()])
    }

    fn gate(&self) -> GatingPolicy {
        self.gate
    }

    /// Difference wrapped to `[−π, π)`: 359° vs predicted 1° is −2°, not 358°.
    fn innovation(&self, predicted: &ObservationVector) -> Result<ObservationVector> {
        let measured = math::to_radians(self.heading.degrees());
        let predicted = predicted.get(0).unwrap_or(0.0);
        let difference = math::to_radians(wrap180(math::to_degrees(measured - predicted)));
        ObservationVector::new(&[difference])
    }
}

/// Speed through the water from a log.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SpeedThroughWaterObservation {
    taken_at: Instant<Utc>,
    speed: Speed,
    sigma: Speed,
    gate: GatingPolicy,
    sensor: SensorId,
}

impl SpeedThroughWaterObservation {
    /// Speed with 1σ uncertainty.
    #[must_use]
    pub fn new(taken_at: Instant<Utc>, speed: Speed, sigma: Speed) -> Self {
        Self {
            taken_at,
            speed,
            sigma,
            gate: GatingPolicy::reject_above(GATE_ONE_IN_A_THOUSAND_1DOF),
            sensor: SensorId::named("speed through water"),
        }
    }

    /// Sets the gate.
    #[must_use]
    pub const fn with_gate(mut self, gate: GatingPolicy) -> Self {
        self.gate = gate;
        self
    }

    /// Sets the source identifier; differently named sensors are judged
    /// separately.
    #[must_use]
    pub const fn from_sensor(mut self, sensor: SensorId) -> Self {
        self.sensor = sensor;
        self
    }
}

impl Observation for SpeedThroughWaterObservation {
    fn sensor(&self) -> SensorId {
        self.sensor
    }

    fn taken_at(&self) -> Instant<Utc> {
        self.taken_at
    }

    fn measured(&self) -> ObservationVector {
        vector(&[self.speed.metres_per_second()])
    }

    fn predict(&self, state: &NavigationState) -> Result<ObservationVector> {
        ObservationVector::new(&[state.speed_through_water().metres_per_second()])
    }

    fn jacobian(&self, _: &NavigationState) -> Result<ObservationJacobian> {
        ObservationJacobian::new()
            .with_row(JacobianRow::new().with(StateComponent::SpeedThroughWater, 1.0))
    }

    fn noise(&self) -> ObservationNoise {
        sigmas(&[self.sigma.metres_per_second()])
    }

    fn gate(&self) -> GatingPolicy {
        self.gate
    }
}

/// Observation vector from values already finite and few; cannot fail for them.
/// Falls back to empty (rejected by the estimator) instead of panicking.
fn vector(values: &[f64]) -> ObservationVector {
    ObservationVector::new(values).unwrap_or(ObservationVector::EMPTY)
}

/// Noise from validated sigmas. A zero sigma (allowed by the units) becomes the
/// smallest positive variance, since a noiseless observation makes the update
/// singular.
fn sigmas(values: &[f64]) -> ObservationNoise {
    let floor = |sigma: &f64| sigma.max(1e-9);
    let mut floored = [0.0; 4];
    for (slot, sigma) in floored.iter_mut().zip(values) {
        *slot = floor(sigma);
    }
    ObservationNoise::sigmas(floored.get(..values.len().min(4)).unwrap_or(&[]))
        .unwrap_or(ObservationNoise::UNIT)
}