minikalman 0.7.0

A microcontroller targeted Kalman filter implementation
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
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
//! # Control inputs for Regular Kalman Filters.

use crate::kalman::*;
use crate::matrix::*;
use core::marker::PhantomData;

/// A builder for a [`Control`] filter instances.
#[allow(clippy::type_complexity)]
pub struct ControlBuilder<B, U, Q, TempBQ> {
    _phantom: (
        PhantomData<B>,
        PhantomData<U>,
        PhantomData<Q>,
        PhantomData<TempBQ>,
    ),
}

impl<B, U, Q, TempBQ> ControlBuilder<B, U, Q, TempBQ> {
    /// Initializes a Kalman filter control instance.
    ///
    /// ## Arguments
    /// * `B` - The control transition matrix (`STATES` × `CONTROLS`).
    /// * `u` - The control vector (`CONTROLS` × `1`).
    /// * `Q` - The control covariance matrix (`CONTROLS` × `CONTROLS`).
    /// * `temp_BQ` - The temporary vector for B×Q calculation (`STATES` × `CONTROLS`).
    #[allow(non_snake_case, clippy::too_many_arguments, clippy::new_ret_no_self)]
    pub fn new<const STATES: usize, const CONTROLS: usize, T>(
        B: B,
        u: U,
        Q: Q,
        temp_BQ: TempBQ,
    ) -> Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
    where
        T: MatrixDataType,
        B: ControlMatrix<STATES, CONTROLS, T>,
        U: ControlVector<CONTROLS, T>,
        Q: ControlProcessNoiseCovarianceMatrix<CONTROLS, T>,
        TempBQ: TemporaryBQMatrix<STATES, CONTROLS, T>,
    {
        Control::<STATES, CONTROLS, T, _, _, _, _> {
            B,
            u,
            Q,
            temp_BQ,
            _phantom: Default::default(),
        }
    }
}

/// Control Filter structure.  See [`ControlBuilder`] for construction.
#[allow(non_snake_case, unused)]
pub struct Control<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ> {
    /// Control vector.
    u: U,

    /// Control matrix.
    ///
    /// See also [`Q`].
    B: B,

    /// Control covariance matrix.
    ///
    /// See also [`B`].
    Q: Q,

    /// B×Q-sized temporary matrix (number of states × number of controls).
    ///
    /// The backing field for this temporary MAY be aliased with temporary P.
    temp_BQ: TempBQ,

    _phantom: PhantomData<T>,
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
{
    /// Returns the number of states.
    #[allow(unused)]
    pub const fn states(&self) -> usize {
        STATES
    }

    /// Returns the number of controls.
    #[allow(unused)]
    pub const fn controls(&self) -> usize {
        CONTROLS
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    U: 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.
    #[inline(always)]
    #[doc(alias = "kalman_get_control_vector")]
    pub fn control_vector(&self) -> &U {
        &self.u
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    U: 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.
    #[inline(always)]
    #[doc(alias = "kalman_get_control_vector")]
    pub fn control_vector_mut(&mut self) -> &mut U {
        &mut self.u
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    B: ControlMatrix<STATES, CONTROLS, T>,
{
    /// Gets a reference to the control transition matrix B.
    ///
    /// This matrix maps the control inputs to the state space, allowing the control vector to
    /// influence the state transition. It quantifies how the control inputs affect the state change.
    #[inline(always)]
    pub fn control_matrix(&self) -> &B {
        &self.B
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    B: ControlMatrixMut<STATES, CONTROLS, T>,
{
    /// Gets a mutable reference to the control transition matrix B.
    ///
    /// This matrix maps the control inputs to the state space, allowing the control vector to
    /// influence the state transition. It quantifies how the control inputs affect the state change.
    #[inline(always)]
    #[doc(alias = "kalman_get_control_matrix")]
    pub fn control_matrix_mut(&mut self) -> &mut B {
        &mut self.B
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    Q: ControlProcessNoiseCovarianceMatrix<CONTROLS, T>,
{
    /// Gets a reference to the control covariance matrix Q.
    ///
    /// This matrix represents the uncertainty in the state transition process, accounting for the
    /// randomness and inaccuracies in the model. It quantifies the expected variability in the
    /// state transition.
    #[inline(always)]
    #[doc(alias = "control_covariance")]
    pub fn process_noise_covariance(&self) -> &Q {
        &self.Q
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    Q: ControlProcessNoiseCovarianceMatrixMut<CONTROLS, T>,
{
    /// Gets a mutable reference to the control covariance matrix Q.
    ///
    /// This matrix represents the uncertainty in the state transition process, accounting for the
    /// randomness and inaccuracies in the model. It quantifies the expected variability in the
    /// state transition.
    #[inline(always)]
    #[doc(alias = "kalman_get_control_covariance")]
    #[doc(alias = "control_covariance_mut")]
    pub fn process_noise_covariance_mut(&mut self) -> &mut Q {
        &mut self.Q
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    U: ControlVector<CONTROLS, T>,
    B: ControlMatrix<STATES, CONTROLS, T>,
    Q: ControlProcessNoiseCovarianceMatrix<CONTROLS, T>,
    TempBQ: TemporaryBQMatrix<STATES, CONTROLS, T>,
    T: MatrixDataType,
{
    /// Applies a correction step to the provided state vector and covariance matrix.
    #[allow(non_snake_case)]
    pub fn apply_control<X, P>(&mut self, x: &mut X, P: &mut P)
    where
        X: StateVectorMut<STATES, T>,
        P: EstimateCovarianceMatrix<STATES, T>,
    {
        // matrices and vectors
        let P = P.as_matrix_mut();
        let x = x.as_matrix_mut();

        // matrices and vectors
        let u = self.u.as_matrix();
        let B = self.B.as_matrix();
        let Q = self.Q.as_matrix();

        if u.is_empty() || B.is_empty() {
            return;
        }

        // temporaries
        let BQ_temp = self.temp_BQ.as_matrix_mut();

        // Incorporate control with state
        // x = x + B*u
        B.multadd_rowvector(u, x);

        // P = P + B*Q*Bᵀ
        B.mult(Q, BQ_temp); // temp = B*Q
        BQ_temp.multadd_transb(B, P); // P += temp*Bᵀ
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ> KalmanFilterNumStates<STATES>
    for Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
{
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    KalmanFilterNumControls<CONTROLS> for Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
{
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    KalmanFilterControlVector<CONTROLS, T> for Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    U: ControlVector<CONTROLS, T>,
{
    type ControlVector = U;

    fn control_vector(&self) -> &Self::ControlVector {
        self.control_vector()
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    KalmanFilterControlVectorMut<CONTROLS, T> for Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    U: ControlVectorMut<CONTROLS, T>,
{
    type ControlVectorMut = U;

    fn control_vector_mut(&mut self) -> &mut Self::ControlVectorMut {
        self.control_vector_mut()
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    KalmanFilterControlTransition<STATES, CONTROLS, T>
    for Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    B: ControlMatrix<STATES, CONTROLS, T>,
{
    type ControlTransitionMatrix = B;

    fn control_matrix(&self) -> &Self::ControlTransitionMatrix {
        self.control_matrix()
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    KalmanFilterControlTransitionMut<STATES, CONTROLS, T>
    for Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    B: ControlMatrixMut<STATES, CONTROLS, T>,
{
    type ControlTransitionMatrixMut = B;

    fn control_matrix_mut(&mut self) -> &mut Self::ControlTransitionMatrixMut {
        self.control_matrix_mut()
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    KalmanFilterControlProcessNoiseCovariance<CONTROLS, T>
    for Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    Q: ControlProcessNoiseCovarianceMatrix<CONTROLS, T>,
{
    type ProcessNoiseCovarianceMatrix = Q;

    fn process_noise_covariance(&self) -> &Self::ProcessNoiseCovarianceMatrix {
        self.process_noise_covariance()
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    KalmanFilterControlProcessNoiseMut<CONTROLS, T>
    for Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    Q: ControlProcessNoiseCovarianceMatrixMut<CONTROLS, T>,
{
    type ProcessNoiseCovarianceMatrixMut = Q;

    fn process_noise_covariance_mut(&mut self) -> &mut Self::ProcessNoiseCovarianceMatrixMut {
        self.process_noise_covariance_mut()
    }
}

impl<const STATES: usize, const CONTROLS: usize, T, B, U, Q, TempBQ>
    KalmanFilterControlApplyToFilter<STATES, T> for Control<STATES, CONTROLS, T, B, U, Q, TempBQ>
where
    U: ControlVector<CONTROLS, T>,
    B: ControlMatrix<STATES, CONTROLS, T>,
    Q: ControlProcessNoiseCovarianceMatrix<CONTROLS, T>,
    TempBQ: TemporaryBQMatrix<STATES, CONTROLS, T>,
    T: MatrixDataType,
{
    #[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>,
    {
        self.apply_control(x, P)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_dummies::make_dummy_control;

    #[test]
    #[cfg(feature = "alloc")]
    fn test_mut_apply() {
        use crate::regular::builder::KalmanFilterBuilder;

        let builder = KalmanFilterBuilder::<3, f32>::default();
        let mut filter = builder.build();
        let mut control = builder.controls().build::<1>();

        filter.predict();
        filter.control(&mut control);
    }

    #[allow(non_snake_case)]
    #[test]
    #[cfg(feature = "alloc")]
    fn control_only() {
        use crate::matrix::MatrixMut;
        use assert_float_eq::*;

        use crate::prelude::{regular::RegularKalmanBuilder, BufferBuilder};

        const NUM_STATES: usize = 4;
        const NUM_CONTROLS: usize = 3;

        // System buffers.
        let x = BufferBuilder::state_vector_x::<NUM_STATES>().new();
        let A = BufferBuilder::system_matrix_A::<NUM_STATES>().new();
        let P = BufferBuilder::estimate_covariance_P::<NUM_STATES>().new();
        let Q_direct = BufferBuilder::direct_process_noise_covariance_Q::<NUM_STATES>().new();

        // Control buffers.
        let u = BufferBuilder::control_vector_u::<NUM_CONTROLS>().new();
        let B = BufferBuilder::control_matrix_B::<NUM_STATES, NUM_CONTROLS>().new();
        let Q_control = BufferBuilder::control_process_noise_covariance_Q::<NUM_CONTROLS>().new();

        // Filter temporaries.
        let temp_x = BufferBuilder::state_prediction_temp_x::<NUM_STATES>().new();
        let temp_P = BufferBuilder::temp_system_covariance_P::<NUM_STATES>().new();

        // Control temporaries
        let temp_BQ = BufferBuilder::temp_BQ::<NUM_STATES, NUM_CONTROLS>().new();

        let mut filter =
            RegularKalmanBuilder::new::<NUM_STATES, f32>(A, x, P, Q_direct, temp_x, temp_P);
        let mut control =
            ControlBuilder::new::<NUM_STATES, NUM_CONTROLS, f32>(B, u, Q_control, temp_BQ);

        // State transition is identity.
        filter.state_transition_mut().apply(|mat| {
            mat[0] = 1.0;
            mat[1] = 1.0;
            mat[2] = 1.0;
            mat[3] = 1.0;

            mat[NUM_STATES + 1] = 1.0;
            mat[2 * NUM_STATES + 2] = 1.0;
            mat[3 * NUM_STATES + 3] = 1.0;
        });

        // State covariance is identity.
        filter.estimate_covariance_mut().apply(|mat| {
            mat[0] = 1.0;
            mat[NUM_STATES + 1] = 1.0;
            mat[2 * NUM_STATES + 2] = 1.0;
            mat[3 * NUM_STATES + 3] = 1.0;
        });

        // Control applies linearly to state.
        control.control_matrix_mut().apply(|mat| {
            mat[NUM_CONTROLS] = 1.0;
            mat[2 * NUM_CONTROLS + 1] = 1.0;
            mat[3 * NUM_CONTROLS + 2] = 1.0;
        });

        // Control covariance is identity.
        control.process_noise_covariance_mut().apply(|mat| {
            mat[0] = 1.0;
            mat[NUM_CONTROLS + 1] = 1.0;
            mat[2 * NUM_CONTROLS + 2] = 1.0;
        });

        // Define some test control vector.
        control.control_vector_mut().apply(|vec| {
            vec.set_at(0, 0, 0.1);
            vec.set_at(1, 0, 1.0);
            vec.set_at(2, 0, 10.0);
        });

        // Sanity checks.
        assert_eq!(filter.states(), 4);
        assert_eq!(control.states(), 4);
        assert_eq!(control.controls(), 3);

        // First round, state vector is empty.
        let state = filter.state_vector().as_ref();
        assert_f32_near!(state[0], 0.0);
        assert_f32_near!(state[1], 0.0);
        assert_f32_near!(state[2], 0.0);
        assert_f32_near!(state[3], 0.0);

        // Predict one step - no controls, so no changes.
        filter.predict();
        let state = filter.state_vector().as_ref();
        assert_f32_near!(state[0], 0.0);
        assert_f32_near!(state[1], 0.0);
        assert_f32_near!(state[2], 0.0);
        assert_f32_near!(state[3], 0.0);

        // Predict one step (with controls).
        filter.predict();
        filter.control(&mut control);
        let state = filter.state_vector().as_ref();
        assert_f32_near!(state[0], 0.0);
        assert_f32_near!(state[1], 0.1);
        assert_f32_near!(state[2], 1.0);
        assert_f32_near!(state[3], 10.0);

        // Predict another step (with controls).
        filter.predict();
        filter.control(&mut control);
        let state = filter.state_vector().as_ref();
        assert_f32_near!(state[0], 11.1);
        assert_f32_near!(state[1], 0.2);
        assert_f32_near!(state[2], 2.0);
        assert_f32_near!(state[3], 20.0);
    }

    fn trait_impl<const STATES: usize, const CONTROLS: usize, T, M>(mut control: M) -> M
    where
        M: KalmanFilterControl<STATES, CONTROLS, T>
            + KalmanFilterControlTransitionMut<STATES, CONTROLS, T>,
    {
        assert_eq!(control.states(), STATES);
        assert_eq!(control.controls(), CONTROLS);

        let test_fn = || 42;

        let mut temp = 0;
        let mut test_fn_mut = || {
            temp += 0;
            42
        };

        let _vec = control.control_vector();
        let _vec = control.control_vector_mut();
        let _ = control
            .control_vector()
            .as_matrix()
            .inspect(|_vec| test_fn());
        let _ = control
            .control_vector()
            .as_matrix()
            .inspect(|_vec| test_fn_mut());
        control
            .control_vector_mut()
            .as_matrix_mut()
            .apply(|_vec| test_fn());
        control
            .control_vector_mut()
            .as_matrix_mut()
            .apply(|_vec| test_fn_mut());

        let _mat = control.control_matrix();
        let _mat = control.control_matrix_mut();
        let _ = control
            .control_matrix()
            .as_matrix()
            .inspect(|_mat| test_fn());
        let _ = control
            .control_matrix()
            .as_matrix()
            .inspect(|_mat| test_fn_mut());
        control
            .control_matrix_mut()
            .as_matrix_mut()
            .apply(|_mat| test_fn());
        control
            .control_matrix_mut()
            .as_matrix_mut()
            .apply(|_mat| test_fn_mut());

        let _mat = control.process_noise_covariance();
        let _mat = control.process_noise_covariance_mut();
        let _ = control
            .process_noise_covariance()
            .as_matrix()
            .inspect(|_mat| test_fn());
        let _ = control
            .process_noise_covariance()
            .as_matrix()
            .inspect(|_mat| test_fn_mut());
        control
            .process_noise_covariance_mut()
            .as_matrix_mut()
            .apply(|_mat| test_fn());
        control
            .process_noise_covariance_mut()
            .as_matrix_mut()
            .apply(|_mat| test_fn_mut());

        control
    }

    #[test]
    fn builder_simple() {
        let control = make_dummy_control();

        let mut control = trait_impl(control);
        assert_eq!(control.states(), 3);
        assert_eq!(control.controls(), 2);

        let test_fn = || 42;

        let mut temp = 0;
        let mut test_fn_mut = || {
            temp += 0;
            42
        };

        let _vec = control.control_vector();
        let _vec = control.control_vector_mut();
        let _ = control
            .control_vector()
            .as_matrix()
            .inspect(|_vec| test_fn());
        let _ = control
            .control_vector()
            .as_matrix()
            .inspect(|_vec| test_fn_mut());
        control
            .control_vector_mut()
            .as_matrix_mut()
            .apply(|_vec| test_fn());
        control
            .control_vector_mut()
            .as_matrix_mut()
            .apply(|_vec| test_fn_mut());

        let _mat = control.control_matrix();
        let _mat = control.control_matrix_mut();
        let _ = control
            .control_matrix()
            .as_matrix()
            .inspect(|_mat| test_fn());
        let _ = control
            .control_matrix()
            .as_matrix()
            .inspect(|_mat| test_fn_mut());
        control
            .control_matrix_mut()
            .as_matrix_mut()
            .apply(|_mat| test_fn());
        control
            .control_matrix_mut()
            .as_matrix_mut()
            .apply(|_mat| test_fn_mut());

        let _mat = control.process_noise_covariance();
        let _mat = control.process_noise_covariance_mut();
        let _ = control
            .process_noise_covariance()
            .as_matrix()
            .inspect(|_mat| test_fn());
        let _ = control
            .process_noise_covariance()
            .as_matrix()
            .inspect(|_mat| test_fn_mut());
        control
            .process_noise_covariance_mut()
            .as_matrix_mut()
            .apply(|_mat| test_fn());
        control
            .process_noise_covariance_mut()
            .as_matrix_mut()
            .apply(|_mat| test_fn_mut());
    }
}