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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
//! # Kalman Filters for Embedded Targets
//!
//! This is the Rust port of the [kalman-clib](https://github.com/sunsided/kalman-clib/) library,
//! a microcontroller targeted Kalman filter implementation. It can use [`micromath`](https://docs.rs/micromath)
//! for square root and reciprocal calculations on `no_std`; [`libm`](https://docs.rs/libm) is supported as well.
//!
//! This implementation uses statically allocated buffers for all matrix operations. Due to lack
//! of `const` generics for array allocations in Rust, this crate also provides helper macros
//! to create the required arrays (see e.g. [`impl_buffer_A`]).
//!
//! If allocation is available (via `std` or `alloc` crate features), the [`KalmanFilterBuilder`](regular::builder::KalmanFilterBuilder) can be
//! used to quickly create a [`RegularKalman`](regular::RegularKalman) filter instance with all necessary buffers, alongside
//! [`Control`](regular::Control) and [`Observation`](regular::RegularObservation) instances.
//! Similar types exist for Extended Kalman Filters.
//!
//! ## Crate Features
//!
//! * `std` - Disabled by default. Disables the `no_std` configuration attribute (enabling `std` support).
//! * `alloc` - Enables allocation support for builder types.
//! * `libm` - Enables [libm](https://crates.io/crates/libm) support.
//! * `micromath` - Enables [micromath](https://crates.io/crates/micromath) support.
//! * `fixed` - Enables fixed-point support via the [fixed](https://crates.io/crates/fixed) crate.
//! * `unsafe` - Enables some unsafe pointer operations. Disabled by default; when turned off,
//! compiles the crate as `#![forbid(unsafe)]`.
//! * `nalgebra` - Enables [nalgebra](https://crates.io/crates/nalgebra) support. For major parts,
//! must be used in conjunction with `unsafe`.
//!
//! ## Example
//!
//! On `std` or `alloc` crates, the [`KalmanFilterBuilder`](regular::builder::KalmanFilterBuilder) is enabled. An overly simplified example
//! for setting up and operating the Kalman Filter could look like this:
//!
//! ```no_run
//! use minikalman::regular::builder::KalmanFilterBuilder;
//! use minikalman::prelude::MatrixMut;
//!
//! const NUM_STATES: usize = 3;
//! const NUM_CONTROLS: usize = 2;
//! const NUM_OBSERVATIONS: usize = 1;
//!
//! let builder = KalmanFilterBuilder::<NUM_STATES, f32>::default();
//! let mut filter = builder.build();
//! let mut control = builder.controls().build::<NUM_CONTROLS>();
//! let mut measurement = builder.observations().build::<NUM_OBSERVATIONS>();
//!
//! // Set up the system dynamics, control matrices, observation matrices, ...
//!
//! // Filter!
//! loop {
//! // Update your control vector(s).
//! control.control_vector_mut().apply(|u| {
//! u[0] = 0.0;
//! u[1] = 1.0;
//! });
//!
//! // Update your measurement vectors.
//! measurement.measurement_vector_mut().apply(|z| {
//! z[0] = 42.0;
//! });
//!
//! // Update prediction (without controls).
//! filter.predict();
//!
//! // Apply any controls to the prediction.
//! filter.control(&mut control);
//!
//! // Apply any measurements.
//! filter.correct(&mut measurement);
//!
//! // Access the state
//! let state = filter.state_vector();
//! let covariance = filter.estimate_covariance();
//! }
//! ```
//!
//! ## Extended Kalman Filters
//!
//! The general setup remains the same, however the `predict` and `correct` methods are
//! replaced with their nonlinear counterparts:
//!
//! ```no_run
//! use minikalman::extended::builder::KalmanFilterBuilder;
//! use minikalman::prelude::MatrixMut;
//!
//! const NUM_STATES: usize = 3;
//! const NUM_CONTROLS: usize = 2;
//! const NUM_OBSERVATIONS: usize = 1;
//!
//! let builder = KalmanFilterBuilder::<NUM_STATES, f32>::default();
//! let mut filter = builder.build();
//! let mut measurement = builder.observations().build::<NUM_OBSERVATIONS>();
//!
//! // Set up the system dynamics, control matrices, observation matrices, ...
//!
//! // Filter!
//! loop {
//! // Obtain the control values.
//! let control_value = 1.0;
//!
//! // Update prediction using nonlinear transfer function.
//! filter.predict_nonlinear(|current, next| {
//! next[0] = current[0] * current[0];
//! next[1] = current[1].sin() * control_value;
//! });
//!
//! // Update your measurement vectors.
//! measurement.measurement_vector_mut().apply(|z| {
//! z[0] = 42.0;
//! });
//!
//! // Apply any measurements using a nonlinear measurement function.
//! filter.correct_nonlinear(&mut measurement, |state, observation| {
//! observation[0] = state[0].cos() + state[1].sin();
//! });
//!
//! // Access the state
//! let state = filter.state_vector();
//! let covariance = filter.estimate_covariance();
//! }
//! ```
//!
//! ## `no_std` Example
//!
//! Systems without the liberty of heap allocation may make use of the provided helper macros
//! to wire up new types. This comes at the cost of potentially confusing IDEs due to recursive
//! macro expansion, so buyer beware. In the example below, types are set up as arrays bound
//! to `static mut` variables.
//!
//! ```
//! # #![allow(non_snake_case)]
//! # #![allow(non_upper_case_globals)]
//! use minikalman::buffers::types::*;
//! use minikalman::prelude::*;
//! use minikalman::regular::{RegularKalmanBuilder, RegularObservationBuilder};
//!
//! const NUM_STATES: usize = 3;
//! const NUM_OBSERVATIONS: usize = 1;
//!
//! // System buffers.
//! impl_buffer_x!(static mut gravity_x, NUM_STATES, f32, 0.0);
//! impl_buffer_A!(static mut gravity_A, NUM_STATES, f32, 0.0);
//! impl_buffer_P!(static mut gravity_P, NUM_STATES, f32, 0.0);
//! impl_buffer_Q_direct!(static mut gravity_Q, NUM_STATES, f32, 0.0);
//!
//! // Observation buffers.
//! impl_buffer_z!(static mut gravity_z, NUM_OBSERVATIONS, f32, 0.0);
//! impl_buffer_H!(static mut gravity_H, NUM_OBSERVATIONS, NUM_STATES, f32, 0.0);
//! impl_buffer_R!(static mut gravity_R, NUM_OBSERVATIONS, f32, 0.0);
//! impl_buffer_y!(static mut gravity_y, NUM_OBSERVATIONS, f32, 0.0);
//! impl_buffer_S!(static mut gravity_S, NUM_OBSERVATIONS, f32, 0.0);
//! impl_buffer_K!(static mut gravity_K, NUM_STATES, NUM_OBSERVATIONS, f32, 0.0);
//!
//! // Filter temporaries.
//! impl_buffer_temp_x!(static mut gravity_temp_x, NUM_STATES, f32, 0.0);
//! impl_buffer_temp_P!(static mut gravity_temp_P, NUM_STATES, f32, 0.0);
//!
//! // Observation temporaries.
//! impl_buffer_temp_S_inv!(static mut gravity_temp_S_inv, NUM_OBSERVATIONS, f32, 0.0);
//!
//! // Observation temporaries.
//! impl_buffer_temp_HP!(static mut gravity_temp_HP, NUM_OBSERVATIONS, NUM_STATES, f32, 0.0);
//! impl_buffer_temp_PHt!(static mut gravity_temp_PHt, NUM_STATES, NUM_OBSERVATIONS, f32, 0.0);
//! impl_buffer_temp_KHP!(static mut gravity_temp_KHP, NUM_STATES, f32, 0.0);
//!
//! let mut filter = RegularKalmanBuilder::new::<NUM_STATES, f32>(
//! StateTransitionMatrixMutBuffer::from(unsafe { gravity_A.as_mut_slice() }),
//! StateVectorBuffer::from(unsafe { gravity_x.as_mut_slice() }),
//! EstimateCovarianceMatrixBuffer::from(unsafe { gravity_P.as_mut_slice() }),
//! DirectProcessNoiseCovarianceMatrixBuffer::from(unsafe { gravity_Q.as_mut_slice() }),
//! PredictedStateEstimateVectorBuffer::from(unsafe { gravity_temp_x.as_mut_slice() }),
//! TemporaryStateMatrixBuffer::from(unsafe { gravity_temp_P.as_mut_slice() }),
//! );
//!
//! let mut measurement = RegularObservationBuilder::new::<NUM_STATES, NUM_OBSERVATIONS, f32>(
//! ObservationMatrixMutBuffer::from(unsafe { gravity_H.as_mut_slice() }),
//! MeasurementVectorBuffer::from(unsafe { gravity_z.as_mut_slice() }),
//! MeasurementNoiseCovarianceMatrixBuffer::from(unsafe { gravity_R.as_mut_slice() }),
//! InnovationVectorBuffer::from(unsafe { gravity_y.as_mut_slice() }),
//! InnovationCovarianceMatrixBuffer::from(unsafe { gravity_S.as_mut_slice() }),
//! KalmanGainMatrixBuffer::from(unsafe { gravity_K.as_mut_slice() }),
//! TemporaryResidualCovarianceInvertedMatrixBuffer::from(unsafe {
//! gravity_temp_S_inv.as_mut_slice()
//! }),
//! TemporaryHPMatrixBuffer::from(unsafe { gravity_temp_HP.as_mut_slice() }),
//! TemporaryPHTMatrixBuffer::from(unsafe { gravity_temp_PHt.as_mut_slice() }),
//! TemporaryKHPMatrixBuffer::from(unsafe { gravity_temp_KHP.as_mut_slice() }),
//! );
//! ```
//!
//! After that, the `filter` and `measurement` variables can be used similar to the example above.
// only enables the `doc_cfg` feature when
// the `docsrs` configuration attribute is defined
// Enable `no_std` if the `no_std` crate feature is enabled.
// Forbid unsafe code unless the `unsafe` crate feature is explicitly enabled.
extern crate alloc;
extern crate core;
pub use crateBufferBuilder;
/// Re-export `num_traits`.
pub use num_traits;
/// Regular Kalman Filters
/// Extended Kalman Filters (EKF)
/// Unscented Kalman Filters (UKF)
/// Exports all macros and common types.
/// Sizes a buffer fitting the state transition matrix (`num_states` × `num_states`).
///
/// ## Arguments
/// * `num_states` - The number of states describing the system.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// assert_eq!(size_buffer_A!(NUM_STATES), 9);
/// ```
/// Sizes a buffer fitting the state covariance matrix (`num_states` × `num_states`).
///
/// ## Arguments
/// * `num_states` - The number of states describing the system.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// assert_eq!(size_buffer_P!(NUM_STATES), 9);
/// ```
/// Sizes a buffer fitting the state vector (`num_states` × `1`).
///
/// ## Arguments
/// * `num_states` - The number of states describing the system.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// assert_eq!(size_buffer_x!(NUM_STATES), 3);
/// ```
/// Sizes a buffer fitting the direct process noise matrix (`num_states` × `num_states`).
///
/// ## Arguments
/// * `num_states` - The number of states describing the system.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// assert_eq!(size_buffer_Q_direct!(NUM_STATES), 9);
/// ```
/// Sizes a buffer fitting the control vector (`num_controls` × `1`).
///
/// ## Arguments
/// * `num_controls` - The number of states describing the system.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_CONTROLS: usize = 1;
/// assert_eq!(size_buffer_u!(NUM_CONTROLS), 1);
/// ```
/// Sizes a buffer fitting the control transition matrix (`num_states` × `num_controls`).
///
/// ## Arguments
/// * `num_states` - The number of states describing the system.
/// * `num_controls` - The number of controls.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// const NUM_CONTROLS: usize = 1;
/// assert_eq!(size_buffer_B!(NUM_STATES, NUM_CONTROLS), 3);
/// ```
/// Sizes a buffer fitting the control process noise matrix (`num_controls` × `num_controls`).
///
/// ## Arguments
/// * `num_controls` - The number of control inputs to the system.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_CONTROLS: usize = 1;
/// assert_eq!(size_buffer_Q_control!(NUM_CONTROLS), 1);
/// ```
/// Sizes a buffer fitting the measurement vector z (`num_measurements` × `1`).
///
/// ## Arguments
/// * `num_measurements` - The number of measurements.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_OBSERVATIONS: usize = 1;
/// assert_eq!(size_buffer_z!(NUM_OBSERVATIONS), 1);
/// ```
/// Sizes a buffer fitting the measurement transformation matrix (`num_measurements` × `num_states`).
///
/// ## Arguments
/// * `num_measurements` - The number of measurements.
/// * `num_states` - The number of states.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// const NUM_OBSERVATIONS: usize = 1;
/// assert_eq!(size_buffer_H!(NUM_OBSERVATIONS, NUM_STATES), 3);
/// ```
/// Sizes a buffer fitting the measurement uncertainty matrix (`num_measurements` × `num_measurements`).
///
/// ## Arguments
/// * `num_measurements` - The number of measurements.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_OBSERVATIONS: usize = 1;
/// assert_eq!(size_buffer_R!(NUM_OBSERVATIONS), 1);
/// ```
/// Sizes a buffer fitting the innovation vector (`num_measurements` × `1`).
///
/// ## Arguments
/// * `num_measurements` - The number of measurements.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_OBSERVATIONS: usize = 1;
/// assert_eq!(size_buffer_y!(NUM_OBSERVATIONS), 1);
/// ```
/// Sizes a buffer fitting the innovation covariance matrix (`num_measurements` × `num_measurements`).
///
/// ## Arguments
/// * `num_measurements` - The number of measurements.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_OBSERVATIONS: usize = 1;
/// assert_eq!(size_buffer_S!(NUM_OBSERVATIONS), 1);
/// ```
/// Sizes a buffer fitting the Kalman gain matrix (`num_states` × `num_measurements`).
///
/// ## Arguments
/// * `num_states` - The number of states.
/// * `num_measurements` - The number of measurements.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// const NUM_OBSERVATIONS: usize = 1;
/// assert_eq!(size_buffer_K!(NUM_STATES, NUM_OBSERVATIONS), 3);
/// ```
/// Sizes a buffer fitting the temporary x predictions (`num_states` × `1`).
///
/// ## Arguments
/// * `num_states` - The number of states.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// assert_eq!(size_buffer_temp_x!(NUM_STATES), 3);
/// ```
/// Sizes a buffer fitting the temporary P matrix (`num_states` × `num_states`).
///
/// ## Arguments
/// * `num_states` - The number of states.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// assert_eq!(size_buffer_temp_P!(NUM_STATES), 9);
/// ```
/// Sizes a buffer fitting the temporary B×Q matrix (`num_states` × `num_controls`).
///
/// ## Arguments
/// * `num_states` - The number of states.
/// * `num_controls` - The number of controls.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// const NUM_CONTROLS: usize = 1;
/// assert_eq!(size_buffer_temp_BQ!(NUM_STATES, NUM_CONTROLS), 3);
/// ```
/// Sizes a buffer fitting the temporary S-inverted (`num_measurements` × `num_measurements`).
///
/// ## Arguments
/// * `num_measurements` - The number of measurements.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_OBSERVATIONS: usize = 1;
/// assert_eq!(size_buffer_temp_S_inv!(NUM_OBSERVATIONS), 1);
/// ```
/// Sizes a buffer fitting the temporary H×P matrix (`num_measurements` × `num_states`).
///
/// ## Arguments
/// * `num_measurements` - The number of measurements.
/// * `num_states` - The number of states.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// const NUM_OBSERVATIONS: usize = 1;
/// assert_eq!(size_buffer_temp_HP!(NUM_OBSERVATIONS, NUM_STATES), 3);
/// ```
/// Sizes a buffer fitting the temporary P×H^-1 buffer (`num_states` × `num_measurements`).
///
/// ## Arguments
/// * `num_states` - The number of states.
/// * `num_measurements` - The number of measurements.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// const NUM_OBSERVATIONS: usize = 1;
/// assert_eq!(size_buffer_temp_PHt!(NUM_STATES, NUM_OBSERVATIONS), 3);
/// ```
/// Sizes a buffer fitting the temporary K×(H×P) buffer (`num_states` × `num_states`).
///
/// ## Arguments
/// * `num_states` - The number of states.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// assert_eq!(size_buffer_temp_KHP!(NUM_STATES), 9);
/// ```
/// Sizes a buffer fitting the sigma point matrix (`num_states` × `2*num_states+1`).
///
/// ## Arguments
/// * `num_states` - The number of states describing the system.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// assert_eq!(size_buffer_sigma_points!(NUM_STATES), 21);
/// ```
/// Sizes a buffer fitting the sigma weights vector (`2*num_states+1`).
///
/// ## Arguments
/// * `num_states` - The number of states describing the system.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// assert_eq!(size_buffer_sigma_weights!(NUM_STATES), 7);
/// ```
/// Sizes a buffer fitting the observed sigma points (`num_observations` × `2*num_states+1`).
///
/// ## Arguments
/// * `num_observations` - The number of observations.
/// * `num_states` - The number of states describing the system.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_OBSERVATIONS: usize = 2;
/// const NUM_STATES: usize = 3;
/// assert_eq!(size_buffer_sigma_observed!(NUM_OBSERVATIONS, NUM_STATES), 14);
/// ```
/// Sizes a buffer fitting the cross-covariance matrix (`num_states` × `num_observations`).
///
/// ## Arguments
/// * `num_states` - The number of states.
/// * `num_observations` - The number of observations.
///
/// ## Example
/// ```
/// # use minikalman::*;
/// const NUM_STATES: usize = 3;
/// const NUM_OBSERVATIONS: usize = 2;
/// assert_eq!(size_buffer_cross_covariance!(NUM_STATES, NUM_OBSERVATIONS), 6);
/// ```