Skip to main content

flight_solver/rls/
types.rs

1//! Types specific to the RLS solvers.
2
3/// How an RLS update terminated.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[repr(u8)]
6pub enum ExitCode {
7    /// Normal update completed successfully.
8    Success = 0,
9    /// Covariance explosion detected — forgetting factor was temporarily increased.
10    ///
11    /// The update still completed, but with a modified λ to prevent
12    /// unbounded covariance growth. This typically indicates insufficient
13    /// excitation in the regressor signal.
14    CovarianceExplosion = 1,
15}
16
17/// Statistics returned after each RLS update step.
18#[derive(Debug, Clone, Copy)]
19pub struct UpdateStats {
20    /// How the update terminated.
21    pub exit_code: ExitCode,
22    /// Total number of samples processed (including this one).
23    pub samples: u32,
24}
25
26/// Numerical guard configuration for the standard (covariance-form) RLS.
27///
28/// These guards prevent numerical instability that arises from maintaining
29/// the covariance matrix `P` directly. The inverse QR-RLS does not need
30/// these guards because it works with the information matrix factor, where
31/// updates involve only additions (never subtractions).
32///
33/// Default values match the indiflight C reference implementation.
34#[derive(Debug, Clone, Copy)]
35pub struct CovarianceGuards {
36    /// Maximum allowed diagonal value in `P`.
37    ///
38    /// When any `P[i,i]` exceeds this threshold, the forgetting factor `λ`
39    /// is temporarily increased toward 1.0 to slow covariance growth.
40    ///
41    /// Default: `1e10` (matches `RLS_COV_MAX` in indiflight).
42    pub cov_max: f32,
43
44    /// Minimum threshold for trace/diagonal ratio checks.
45    ///
46    /// Guards against division by very small numbers in the order
47    /// decrement limiting logic.
48    ///
49    /// Default: `1e-10` (matches `RLS_COV_MIN` in indiflight).
50    pub cov_min: f32,
51
52    /// Maximum relative trace reduction per update step.
53    ///
54    /// Limits how much `trace(P)` can decrease in a single update,
55    /// preventing numerical instability from aggressive rank-1 updates.
56    /// A value of 0.1 means at most 10% of the trace can be removed
57    /// per step.
58    ///
59    /// Default: `0.1` (matches `RLS_MAX_P_ORDER_DECREMENT` in indiflight).
60    pub max_order_decrement: f32,
61}
62
63impl Default for CovarianceGuards {
64    fn default() -> Self {
65        Self {
66            cov_max: 1e10,
67            cov_min: 1e-10,
68            max_order_decrement: 0.1,
69        }
70    }
71}