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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
use crate::kalman::*;
use crate::prelude::{AsMatrix, AsMatrixMut};
/// A Kalman Filter.
pub trait KalmanFilter<const STATES: usize, T>:
KalmanFilterNumStates<STATES>
+ KalmanFilterStateVectorMut<STATES, T>
+ KalmanFilterStateTransition<STATES, T>
+ KalmanFilterEstimateCovarianceMut<STATES, T>
+ KalmanFilterPredict<STATES, T>
+ KalmanFilterApplyControl<STATES, T>
+ KalmanFilterUpdate<STATES, T>
{
}
/// An Extended Kalman Filter.
pub trait ExtendedKalmanFilter<const STATES: usize, T>:
KalmanFilterNumStates<STATES>
+ KalmanFilterStateVectorMut<STATES, T>
+ ExtendedKalmanFilterStateTransition<STATES, T>
+ KalmanFilterEstimateCovarianceMut<STATES, T>
+ KalmanFilterNonlinearPredict<STATES, T>
+ KalmanFilterNonlinearUpdate<STATES, T>
{
}
/// An Unscented Kalman Filter.
///
/// This trait is implemented automatically for types satisfying all required sub-traits.
/// See [`UnscentedKalman`](crate::unscented::UnscentedKalman) for the concrete implementation.
pub trait UnscentedKalmanFilter<const STATES: usize, const NUM_SIGMA: usize, T>:
KalmanFilterNumStates<STATES>
+ KalmanFilterStateVectorMut<STATES, T>
+ KalmanFilterEstimateCovarianceMut<STATES, T>
+ KalmanFilterSigmaPointPredict<STATES, T>
+ KalmanFilterSigmaPointCorrect<STATES, NUM_SIGMA, T>
+ KalmanFilterUnscentedParams<T>
{
}
/// Auto-implementation of [`UnscentedKalmanFilter`] for types that implement all necessary traits.
impl<const STATES: usize, const NUM_SIGMA: usize, T, Filter>
UnscentedKalmanFilter<STATES, NUM_SIGMA, T> for Filter
where
Filter: KalmanFilterNumStates<STATES>
+ KalmanFilterStateVectorMut<STATES, T>
+ KalmanFilterEstimateCovarianceMut<STATES, T>
+ KalmanFilterSigmaPointPredict<STATES, T>
+ KalmanFilterSigmaPointCorrect<STATES, NUM_SIGMA, T>
+ KalmanFilterUnscentedParams<T>,
{
}
/// A Kalman filter control input.
pub trait KalmanFilterControl<const STATES: usize, const CONTROLS: usize, T>:
KalmanFilterNumStates<STATES>
+ KalmanFilterNumControls<CONTROLS>
+ KalmanFilterControlVectorMut<CONTROLS, T>
+ KalmanFilterControlTransition<STATES, CONTROLS, T>
+ KalmanFilterControlProcessNoiseMut<CONTROLS, T>
{
}
/// A Kalman Filter observation or measurement.
pub trait KalmanFilterObservation<const STATES: usize, const OBSERVATIONS: usize, T>:
KalmanFilterNumStates<STATES>
+ KalmanFilterNumObservations<OBSERVATIONS>
+ KalmanFilterObservationVectorMut<OBSERVATIONS, T>
+ KalmanFilterObservationTransformation<STATES, OBSERVATIONS, T>
+ KalmanFilterMeasurementNoiseCovarianceMut<OBSERVATIONS, T>
+ KalmanFilterObservationCorrectFilter<STATES, T>
{
}
/// An Extended Kalman Filter observation or measurement.
pub trait ExtendedKalmanFilterObservation<const STATES: usize, const OBSERVATIONS: usize, T>:
KalmanFilterNumStates<STATES>
+ KalmanFilterNumObservations<OBSERVATIONS>
+ KalmanFilterObservationVectorMut<OBSERVATIONS, T>
+ KalmanFilterMeasurementNoiseCovarianceMut<OBSERVATIONS, T>
+ ExtendedKalmanFilterObservationTransformation<STATES, OBSERVATIONS, T>
+ KalmanFilterNonlinearObservationCorrectFilter<STATES, OBSERVATIONS, T>
{
}
/// Auto-implementation of [`KalmanFilter`] for types that implement all necessary traits.
impl<const STATES: usize, T, Filter> KalmanFilter<STATES, T> for Filter where
Filter: KalmanFilterNumStates<STATES>
+ KalmanFilterStateVectorMut<STATES, T>
+ KalmanFilterStateTransition<STATES, T>
+ KalmanFilterEstimateCovarianceMut<STATES, T>
+ KalmanFilterPredict<STATES, T>
+ KalmanFilterApplyControl<STATES, T>
+ KalmanFilterUpdate<STATES, T>
{
}
/// Auto-implementation of [`ExtendedKalmanFilter`] for types that implement all necessary traits.
impl<const STATES: usize, T, Filter> ExtendedKalmanFilter<STATES, T> for Filter where
Filter: KalmanFilterNumStates<STATES>
+ KalmanFilterStateVectorMut<STATES, T>
+ ExtendedKalmanFilterStateTransition<STATES, T>
+ KalmanFilterEstimateCovarianceMut<STATES, T>
+ KalmanFilterNonlinearPredict<STATES, T>
+ KalmanFilterNonlinearUpdate<STATES, T>
{
}
/// Auto-implementation of [`KalmanFilterControl`] for types that implement all necessary traits.
impl<const STATES: usize, const CONTROLS: usize, T, Control>
KalmanFilterControl<STATES, CONTROLS, T> for Control
where
Control: KalmanFilterNumStates<STATES>
+ KalmanFilterNumControls<CONTROLS>
+ KalmanFilterControlVectorMut<CONTROLS, T>
+ KalmanFilterControlTransition<STATES, CONTROLS, T>
+ KalmanFilterControlProcessNoiseMut<CONTROLS, T>
+ KalmanFilterControlApplyToFilter<STATES, T>,
{
}
/// Auto-implementation of [`KalmanFilterObservation`] for types that implement all necessary traits.
impl<const STATES: usize, const OBSERVATIONS: usize, T, Observation>
KalmanFilterObservation<STATES, OBSERVATIONS, T> for Observation
where
Observation: KalmanFilterNumStates<STATES>
+ KalmanFilterNumObservations<OBSERVATIONS>
+ KalmanFilterObservationVectorMut<OBSERVATIONS, T>
+ KalmanFilterObservationTransformation<STATES, OBSERVATIONS, T>
+ KalmanFilterMeasurementNoiseCovarianceMut<OBSERVATIONS, T>
+ KalmanFilterObservationCorrectFilter<STATES, T>,
{
}
/// Auto-implementation of [`ExtendedKalmanFilterObservation`] for types that implement all necessary traits.
impl<const STATES: usize, const OBSERVATIONS: usize, T, Observation>
ExtendedKalmanFilterObservation<STATES, OBSERVATIONS, T> for Observation
where
Observation: KalmanFilterNumStates<STATES>
+ KalmanFilterNumObservations<OBSERVATIONS>
+ KalmanFilterObservationVectorMut<OBSERVATIONS, T>
+ KalmanFilterMeasurementNoiseCovarianceMut<OBSERVATIONS, T>
+ ExtendedKalmanFilterObservationTransformation<STATES, OBSERVATIONS, T>
+ KalmanFilterNonlinearObservationCorrectFilter<STATES, OBSERVATIONS, T>,
{
}
pub trait KalmanFilterNumStates<const STATES: usize> {
/// The number of states.
const NUM_STATES: usize = STATES;
/// Returns the number of states.
fn states(&self) -> usize {
STATES
}
}
pub trait KalmanFilterPredict<const STATES: usize, T> {
/// Performs the time update / prediction step.
///
/// This call assumes that the control covariance and variables are already set in the filter structure.
#[doc(alias = "kalman_predict")]
fn predict(&mut self);
}
pub trait KalmanFilterNonlinearPredict<const STATES: usize, T>:
KalmanFilterStateVectorMut<STATES, T>
{
/// The type of observation vector to fill.
type NextStateVector: AsMatrixMut<STATES, 1, T>;
/// Performs the nonlinear time update / prediction step.
///
/// ## Extended Kalman Filter
/// This function assumes the state transition Jacobian was set up correctly using
/// [`state_transition`](KalmanFilterStateTransition::state_transition).
fn predict_nonlinear<F>(&mut self, state_transition: F)
where
F: FnMut(
&<Self as KalmanFilterStateVectorMut<STATES, T>>::StateVectorMut,
&mut Self::NextStateVector,
);
}
pub trait KalmanFilterApplyControl<const STATES: usize, T> {
/// Performs the measurement update step.
///
/// ## Extended Kalman Filters
/// In an Extended Kalman Filter, this method is meaningless. Use the
/// [`predict_nonlinear`](KalmanFilterNonlinearPredict::predict_nonlinear) function set instead.
///
/// ## Arguments
/// * `measurement` - The measurement to update the state prediction with.
fn control<I>(&mut self, control: &mut I)
where
I: KalmanFilterControlApplyToFilter<STATES, T>;
}
pub trait KalmanFilterUpdate<const STATES: usize, T> {
/// Performs the measurement update step.
///
/// ## Arguments
/// * `measurement` - The measurement to update the state prediction with.
fn correct<M>(&mut self, measurement: &mut M)
where
M: KalmanFilterObservationCorrectFilter<STATES, T>;
}
pub trait KalmanFilterNonlinearUpdate<const STATES: usize, T>:
KalmanFilterStateVectorMut<STATES, T>
{
/// Performs the measurement update step.
///
/// ## Arguments
/// * `measurement` - The measurement to update the state prediction with.
fn correct_nonlinear<M, F, const OBSERVATIONS: usize>(
&mut self,
measurement: &mut M,
observation: F,
) where
M: KalmanFilterNonlinearObservationCorrectFilter<STATES, OBSERVATIONS, T>,
F: FnMut(
&<Self as KalmanFilterStateVectorMut<STATES, T>>::StateVectorMut,
&mut M::ObservationVector,
);
}
pub trait KalmanFilterStateVector<const STATES: usize, T> {
type StateVector: StateVector<STATES, T>;
/// Gets a reference to the state vector x.
///
/// The state vector represents the internal state of the system at a given time.
/// It contains all the necessary information to describe the system's current situation.
fn state_vector(&self) -> &Self::StateVector;
}
pub trait KalmanFilterStateVectorMut<const STATES: usize, T>:
KalmanFilterStateVector<STATES, T>
{
type StateVectorMut: StateVectorMut<STATES, T>;
/// Gets a reference to the state vector x.
///
/// The state vector represents the internal state of the system at a given time.
/// It contains all the necessary information to describe the system's current situation.
#[doc(alias = "kalman_get_state_vector")]
fn state_vector_mut(&mut self) -> &mut Self::StateVectorMut;
}
/// Provides access to the state transition matrix.
///
/// ## (Regular) Kalman Filters
/// This matrix describes how the state vector evolves from one time step to the next in the
/// absence of control inputs. It defines the relationship between the previous state and the
/// current state, accounting for the inherent dynamics of the system.
pub trait KalmanFilterStateTransition<const STATES: usize, T> {
type StateTransitionMatrix: StateTransitionMatrix<STATES, T>;
/// Gets a reference to the state transition matrix A/F.
///
/// ## (Regular) Kalman Filters
/// This matrix describes how the state vector evolves from one time step to the next in the
/// absence of control inputs. It defines the relationship between the previous state and the
/// current state, accounting for the inherent dynamics of the system.
fn state_transition(&self) -> &Self::StateTransitionMatrix;
}
pub trait KalmanFilterStateTransitionMut<const STATES: usize, T>:
KalmanFilterStateTransition<STATES, T>
{
type StateTransitionMatrixMut: StateTransitionMatrixMut<STATES, T>;
/// Gets a reference to the state transition matrix A/Fn
///
/// This matrix describes how the state vector evolves from one time step to the next in the
/// absence of control inputs. It defines the relationship between the previous state and the
/// current state, accounting for the inherent dynamics of the system.
#[doc(alias = "kalman_get_state_transition")]
fn state_transition_mut(&mut self) -> &mut Self::StateTransitionMatrixMut;
}
/// Provides access to the Jacobian of the state transition matrix.
///
/// ## Extended Kalman Filters
/// In Extended Kalman Filters, this matrix is treated as the Jacobian of the state
/// transition matrix, i.e. the derivative of the state transition matrix with respect
/// to the state vector.
pub trait ExtendedKalmanFilterStateTransition<const STATES: usize, T> {
type StateTransitionMatrix: StateTransitionMatrix<STATES, T>;
/// Gets a reference to the Jacobian of the state transition matrix A/F.
///
/// ## Extended Kalman Filters
/// When predicting using [`predict_nonlinear`](KalmanFilterNonlinearPredict::predict_nonlinear),
/// this matrix is treated as the Jacobian of the state transition matrix, i.e. the derivative
/// of the state transition matrix with respect to the state vector.
fn state_transition_jacobian(&self) -> &Self::StateTransitionMatrix;
}
pub trait ExtendedKalmanFilterStateTransitionMut<const STATES: usize, T>:
ExtendedKalmanFilterStateTransition<STATES, T>
{
type StateTransitionMatrixMut: StateTransitionMatrixMut<STATES, T>;
/// Gets a reference to the Jacobian of the state transition matrix A/F.
///
/// This matrix describes how the state vector evolves from one time step to the next in the
/// absence of control inputs. It defines the relationship between the previous state and the
/// current state, accounting for the inherent dynamics of the system.
///
/// ## Extended Kalman Filters
/// When predicting using [`predict_nonlinear`](KalmanFilterNonlinearPredict::predict_nonlinear),
/// this matrix is treated as the Jacobian of the state transition matrix, i.e. the derivative
/// of the state transition matrix with respect to the state vector.
fn state_transition_jacobian_mut(&mut self) -> &mut Self::StateTransitionMatrixMut;
}
pub trait KalmanFilterEstimateCovariance<const STATES: usize, T> {
type EstimateCovarianceMatrix: EstimateCovarianceMatrix<STATES, T>;
/// Gets a reference to the estimate covariance matrix P.
///
/// This matrix represents the estimate covariance. It quantifies the uncertainty in
/// the state estimate, providing a measure of how much the state estimate is expected
/// to vary. This matrix offers a measure of confidence in the estimate by indicating
/// the degree of variability and uncertainty associated with the predicted state.
#[doc(alias = "system_covariance")]
fn estimate_covariance(&self) -> &Self::EstimateCovarianceMatrix;
}
pub trait KalmanFilterEstimateCovarianceMut<const STATES: usize, T>:
KalmanFilterEstimateCovariance<STATES, T>
{
type EstimateCovarianceMatrixMut: EstimateCovarianceMatrix<STATES, T>;
/// Gets a mutable reference to the estimate covariance matrix P.
///
/// This matrix represents the estimate covariance. It quantifies the uncertainty in
/// the state estimate, providing a measure of how much the state estimate is expected
/// to vary. This matrix offers a measure of confidence in the estimate by indicating
/// the degree of variability and uncertainty associated with the predicted state.
#[doc(alias = "kalman_get_system_covariance")]
#[doc(alias = "system_covariance_mut")]
fn estimate_covariance_mut(&mut self) -> &mut Self::EstimateCovarianceMatrixMut;
}
pub trait KalmanFilterDirectProcessNoiseCovariance<const CONTROLS: usize, T> {
type ProcessNoiseCovarianceMatrix: DirectProcessNoiseCovarianceMatrix<CONTROLS, T>;
/// Gets a reference to the direct process noise matrix Q.
///
/// This matrix represents the direct process noise covariance. It quantifies the
/// uncertainty introduced by inherent system dynamics and external disturbances,
/// providing a measure of how much the true state is expected to deviate from the
/// predicted state due to these process variations.
fn direct_process_noise(&self) -> &Self::ProcessNoiseCovarianceMatrix;
}
pub trait KalmanFilterDirectProcessNoiseMut<const CONTROLS: usize, T>:
KalmanFilterDirectProcessNoiseCovariance<CONTROLS, T>
{
type ProcessNoiseCovarianceMatrixMut: DirectProcessNoiseCovarianceMatrixMut<CONTROLS, T>;
/// Gets a mutable reference to the direct process noise matrix Q.
///
/// This matrix represents the direct process noise covariance. It quantifies the
/// uncertainty introduced by inherent system dynamics and external disturbances,
/// providing a measure of how much the true state is expected to deviate from the
/// predicted state due to these process variations.
fn direct_process_noise_mut(&mut self) -> &mut Self::ProcessNoiseCovarianceMatrixMut;
}
pub trait KalmanFilterNumControls<const CONTROLS: usize> {
/// The number of controls.
const NUM_CONTROLS: usize = CONTROLS;
/// Returns the number of controls.
fn controls(&self) -> usize {
CONTROLS
}
}
pub trait KalmanFilterControlApplyToFilter<const STATES: usize, T> {
/// Applies a control to the state.
///
/// ## Arguments
/// * `x` - The state vector.
/// * `P` - The system covariance matrix.
#[allow(non_snake_case)]
fn apply_to<X, P>(&mut self, x: &mut X, P: &mut P)
where
X: StateVectorMut<STATES, T>,
P: EstimateCovarianceMatrix<STATES, T>;
}
pub trait KalmanFilterControlVector<const CONTROLS: usize, T> {
type ControlVector: ControlVector<CONTROLS, T>;
/// Gets a reference to the control vector u.
///
/// The control vector contains the external inputs to the system that can influence its state.
/// These inputs might include forces, accelerations, or other actuations applied to the system.
fn control_vector(&self) -> &Self::ControlVector;
}
pub trait KalmanFilterControlVectorMut<const CONTROLS: usize, T>:
KalmanFilterControlVector<CONTROLS, T>
{
type ControlVectorMut: ControlVectorMut<CONTROLS, T>;
/// Gets a mutable reference to the control vector u.
///
/// The control vector contains the external inputs to the system that can influence its state.
/// These inputs might include forces, accelerations, or other actuations applied to the system.
#[doc(alias = "kalman_get_control_vector")]
fn control_vector_mut(&mut self) -> &mut Self::ControlVectorMut;
}
pub trait KalmanFilterControlTransition<const STATES: usize, const CONTROLS: usize, T> {
type ControlTransitionMatrix: ControlMatrix<STATES, CONTROLS, T>;
/// Gets a reference to the control transition matrix B.
///
/// This matrix represents the control input model. It defines how the control inputs
/// influence the state evolution from one time step to the next. The matrix \( B \)
/// is used to incorporate the effect of control inputs into the state transition,
/// allowing the model to account for external controls applied to the system.
fn control_matrix(&self) -> &Self::ControlTransitionMatrix;
}
pub trait KalmanFilterControlTransitionMut<const STATES: usize, const CONTROLS: usize, T>:
KalmanFilterControlTransition<STATES, CONTROLS, T>
{
type ControlTransitionMatrixMut: ControlMatrixMut<STATES, CONTROLS, T>;
/// Gets a mutable reference to the control transition matrix B.
///
/// This matrix represents the control input model. It defines how the control inputs
/// influence the state evolution from one time step to the next. The matrix \( B \)
/// is used to incorporate the effect of control inputs into the state transition,
/// allowing the model to account for external controls applied to the system.
#[doc(alias = "kalman_get_control_matrix")]
fn control_matrix_mut(&mut self) -> &mut Self::ControlTransitionMatrixMut;
}
#[doc(alias = "KalmanFilterControlCovariance")]
pub trait KalmanFilterControlProcessNoiseCovariance<const CONTROLS: usize, T> {
type ProcessNoiseCovarianceMatrix: ControlProcessNoiseCovarianceMatrix<CONTROLS, T>;
/// Gets a reference to the control process noise matrix Q.
///
/// This matrix represents the control process noise covariance. It quantifies the
/// uncertainty introduced by the control inputs, reflecting how much the true state
/// is expected to deviate from the predicted state due to noise and variations
/// in the control process. The matrix is used as B×Q×Bᵀ, where B
/// represents the control input model, and Q is the process noise covariance (this matrix).
#[doc(alias = "control_covariance")]
fn process_noise_covariance(&self) -> &Self::ProcessNoiseCovarianceMatrix;
}
pub trait KalmanFilterControlProcessNoiseMut<const CONTROLS: usize, T>:
KalmanFilterControlProcessNoiseCovariance<CONTROLS, T>
{
type ProcessNoiseCovarianceMatrixMut: ControlProcessNoiseCovarianceMatrixMut<CONTROLS, T>;
/// Gets a mutable reference to the control process noise matrix Q.
///
/// This matrix represents the control process noise covariance. It quantifies the
/// uncertainty introduced by the control inputs, reflecting how much the true state
/// is expected to deviate from the predicted state due to noise and variations
/// in the control process. The matrix is used as B×Q×Bᵀ, where B
/// represents the control input model, and Q is the process noise covariance (this matrix).
#[doc(alias = "kalman_get_control_covariance")]
#[doc(alias = "control_covariance_mut")]
fn process_noise_covariance_mut(&mut self) -> &mut Self::ProcessNoiseCovarianceMatrixMut;
}
pub trait KalmanFilterNumObservations<const OBSERVATIONS: usize> {
/// The number of measurements.
const NUM_OBSERVATIONS: usize = OBSERVATIONS;
/// Returns the number of controls.
fn observations(&self) -> usize {
OBSERVATIONS
}
}
/// The observation function for a regular Kalman Filter.
///
/// ## Extended Kalman Filters
/// See [`KalmanFilterNonlinearObservationCorrectFilter`].
pub trait KalmanFilterObservationCorrectFilter<const STATES: usize, T> {
/// Performs the measurement update step.
///
/// ## Arguments
/// * `x` - The state vector.
/// * `P` - The system covariance matrix.
#[allow(non_snake_case)]
fn correct<X, P>(&mut self, x: &mut X, P: &mut P)
where
X: StateVectorMut<STATES, T>,
P: EstimateCovarianceMatrix<STATES, T>;
}
/// The observation function for an Extended Kalman Filter.
pub trait KalmanFilterNonlinearObservationCorrectFilter<
const STATES: usize,
const OBSERVATIONS: usize,
T,
>
{
/// The type of observation vector to fill.
type ObservationVector: AsMatrixMut<OBSERVATIONS, 1, T>;
/// Performs the nonlinear measurement update step for Extended Kalman Filters.
///
/// ## Extended Kalman Filters
/// This function expects the observation transformation Jacobian to be set up correctly
/// using e.g. [`observation_matrix_mut`](KalmanFilterObservationTransformationMut::observation_matrix_mut).
///
/// ## Arguments
/// * `x` - The state vector.
/// * `P` - The system covariance matrix.
/// * `observations` - The nonlinear observation function.
#[allow(non_snake_case)]
fn correct_nonlinear<X, P, F>(&mut self, x: &mut X, P: &mut P, observation: F)
where
X: StateVectorMut<STATES, T>,
P: EstimateCovarianceMatrix<STATES, T>,
F: FnMut(&X, &mut Self::ObservationVector);
}
pub trait KalmanFilterMeasurementVector<const OBSERVATIONS: usize, T> {
type MeasurementVector: MeasurementVector<OBSERVATIONS, T>;
/// Gets a reference to the measurement vector z.
///
/// The measurement vector represents the observed measurements from the system.
/// These measurements are typically taken from sensors and are used to update the state estimate.
fn measurement_vector(&self) -> &Self::MeasurementVector;
}
pub trait KalmanFilterObservationVectorMut<const OBSERVATIONS: usize, T>:
KalmanFilterMeasurementVector<OBSERVATIONS, T>
{
type MeasurementVectorMut: MeasurementVectorMut<OBSERVATIONS, T>;
/// Gets a mutable reference to the measurement vector z.
///
/// The measurement vector represents the observed measurements from the system.
/// These measurements are typically taken from sensors and are used to update the state estimate.
#[doc(alias = "kalman_get_measurement_vector")]
fn measurement_vector_mut(&mut self) -> &mut Self::MeasurementVectorMut;
}
pub trait KalmanFilterObservationTransformation<const STATES: usize, const OBSERVATIONS: usize, T> {
type ObservationTransformationMatrix: ObservationMatrix<OBSERVATIONS, STATES, T>;
/// Gets a reference to the measurement transformation matrix H.
///
/// This matrix maps the state vector into the measurement space, relating the state of the
/// system to the observations or measurements. It defines how each state component contributes
/// to the measurement.
fn observation_matrix(&self) -> &Self::ObservationTransformationMatrix;
}
pub trait KalmanFilterObservationTransformationMut<
const STATES: usize,
const OBSERVATIONS: usize,
T,
>: KalmanFilterObservationTransformation<STATES, OBSERVATIONS, T>
{
type ObservationTransformationMatrixMut: ObservationMatrixMut<OBSERVATIONS, STATES, T>;
/// Gets a mutable reference to the measurement transformation matrix H.
///
/// This matrix maps the state vector into the measurement space, relating the state of the
/// system to the observations or measurements. It defines how each state component contributes
/// to the measurement.
#[doc(alias = "kalman_get_measurement_transformation")]
#[doc(alias = "measurement_transformation_mut")]
fn observation_matrix_mut(&mut self) -> &mut Self::ObservationTransformationMatrixMut;
}
pub trait ExtendedKalmanFilterObservationTransformation<
const STATES: usize,
const OBSERVATIONS: usize,
T,
>
{
type ObservationTransformationJacobianMatrix: ObservationMatrix<OBSERVATIONS, STATES, T>;
/// Gets a reference to the Jacobian of the measurement transformation matrix H.
///
/// This matrix maps the state vector into the measurement space, relating the state of the
/// system to the observations or measurements. It defines how each state component contributes
/// to the measurement.
///
/// ## Extended Kalman Filters
/// When correcting using [`correct_nonlinear`](KalmanFilterNonlinearObservationCorrectFilter::correct_nonlinear),
/// this matrix is treated as the Jacobian of the observation matrix, i.e. the derivative of
/// the measurement function with respect to the state vector.
fn observation_jacobian_matrix(&self) -> &Self::ObservationTransformationJacobianMatrix;
}
pub trait ExtendedKalmanFilterObservationTransformationMut<
const STATES: usize,
const OBSERVATIONS: usize,
T,
>: ExtendedKalmanFilterObservationTransformation<STATES, OBSERVATIONS, T>
{
type ObservationTransformationJacobianMatrixMut: ObservationMatrixMut<OBSERVATIONS, STATES, T>;
/// Gets a mutable reference to the Jacobian of the measurement transformation matrix H.
///
/// This matrix maps the state vector into the measurement space, relating the state of the
/// system to the observations or measurements. It defines how each state component contributes
/// to the measurement.
///
/// ## Extended Kalman Filters
/// When correcting using [`correct_nonlinear`](KalmanFilterNonlinearObservationCorrectFilter::correct_nonlinear),
/// this matrix is treated as the Jacobian of the observation matrix, i.e. the derivative of
/// the measurement function with respect to the state vector.
#[doc(alias = "kalman_get_measurement_transformation")]
#[doc(alias = "measurement_transformation_mut")]
fn observation_jacobian_matrix_mut(
&mut self,
) -> &mut Self::ObservationTransformationJacobianMatrixMut;
}
pub trait KalmanFilterMeasurementNoiseCovariance<const OBSERVATIONS: usize, T> {
type MeasurementNoiseCovarianceMatrix: MeasurementNoiseCovarianceMatrix<OBSERVATIONS, T>;
/// Gets a reference to the process noise matrix R.
///
/// This matrix represents the uncertainty in the measurements, accounting for sensor noise and
/// inaccuracies. It quantifies the expected variability in the measurement process.
fn measurement_noise_covariance(&self) -> &Self::MeasurementNoiseCovarianceMatrix;
}
pub trait KalmanFilterMeasurementNoiseCovarianceMut<const OBSERVATIONS: usize, T>:
KalmanFilterMeasurementNoiseCovariance<OBSERVATIONS, T>
{
type MeasurementNoiseCovarianceMatrixMut: MeasurementNoiseCovarianceMatrix<OBSERVATIONS, T>;
/// Gets a mutable reference to the process noise matrix R.
///
/// This matrix represents the uncertainty in the measurements, accounting for sensor noise and
/// inaccuracies. It quantifies the expected variability in the measurement process.
#[doc(alias = "kalman_get_process_noise")]
fn measurement_noise_covariance_mut(
&mut self,
) -> &mut Self::MeasurementNoiseCovarianceMatrixMut;
}
/// UKF parameters (alpha, beta, kappa).
pub trait KalmanFilterUnscentedParams<T> {
/// Spread parameter (default 1e-3).
fn alpha(&self) -> T;
/// Distribution knowledge parameter (2.0 for Gaussian).
fn beta(&self) -> T;
/// Secondary scaling parameter (default 0.0).
fn kappa(&self) -> T;
/// Computed lambda: alpha^2 * (n + kappa) - n.
fn lambda(&self, n: usize) -> T;
}
/// Mutable UKF parameters.
pub trait KalmanFilterUnscentedParamsMut<T>: KalmanFilterUnscentedParams<T> {
fn set_alpha(&mut self, alpha: T);
fn set_beta(&mut self, beta: T);
fn set_kappa(&mut self, kappa: T);
}
/// Nonlinear prediction using sigma points (Unscented Kalman Filter).
pub trait KalmanFilterSigmaPointPredict<const STATES: usize, T>:
KalmanFilterStateVectorMut<STATES, T>
{
/// The type of the next state vector (used for sigma point propagation).
type NextStateVector: AsMatrixMut<STATES, 1, T>;
/// Performs the nonlinear time update / prediction step.
/// The closure receives each sigma point and should modify it in-place
/// to produce the predicted sigma point.
fn predict_sigma_point<F>(&mut self, state_transition: F)
where
F: FnMut(&mut Self::NextStateVector);
}
/// Nonlinear correction using sigma points (Unscented Kalman Filter).
pub trait KalmanFilterSigmaPointCorrect<const STATES: usize, const NUM_SIGMA: usize, T>:
KalmanFilterStateVectorMut<STATES, T>
{
/// The type for propagated sigma points.
type SigmaPropagated: AsMatrix<STATES, NUM_SIGMA, T>;
/// Performs the nonlinear correction step using sigma points.
fn correct_sigma_point<M, F, const OBSERVATIONS: usize>(
&mut self,
measurement: &mut M,
observation: F,
) where
M: KalmanFilterUnscentedObservationCorrectFilter<STATES, OBSERVATIONS, NUM_SIGMA, T>,
F: FnMut(&Self::SigmaPropagated, &mut M::ObservedSigmaPoints);
}
/// Observation correct filter for Unscented Kalman Filter.
pub trait KalmanFilterUnscentedObservationCorrectFilter<
const STATES: usize,
const OBSERVATIONS: usize,
const NUM_SIGMA: usize,
T,
>
{
/// The type for observed sigma points.
type ObservedSigmaPoints: AsMatrixMut<OBSERVATIONS, NUM_SIGMA, T>;
/// Performs the correction given pre-populated observed sigma points.
///
/// The closure receives predicted sigma points and is responsible for
/// populating the observed sigma points before correction proceeds.
#[allow(non_snake_case)]
fn correct_with_observed<X, P, SP, F, W>(
&mut self,
x: &mut X,
P: &mut P,
sigma_predicted: &SP,
weights: &W,
lambda: T,
observation: F,
) where
X: StateVectorMut<STATES, T>,
P: EstimateCovarianceMatrix<STATES, T>,
SP: AsMatrix<STATES, NUM_SIGMA, T>,
W: AsMatrix<NUM_SIGMA, 1, T>,
F: FnMut(&SP, &mut Self::ObservedSigmaPoints);
}