oxictl 0.1.1

Pure Rust Real-Time Control Systems Framework
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
//! Active Disturbance Rejection Control (ADRC).
//!
//! ADRC estimates the total disturbance (including unmodeled dynamics and external
//! disturbances) through an Extended State Observer (ESO), then cancels it via
//! feed-forward. The remaining system behaves as a chain of integrators that is
//! easily controlled by a PD/proportional law.
//!
//! References:
//! - Han, J. (2009). "From PID to Active Disturbance Rejection Control."
//!   IEEE Transactions on Industrial Electronics, 56(3), 900-906.
//! - Gao, Z. (2006). "Scaling and Bandwidth-Parameterization Based Controller
//!   Tuning." Proceedings of the American Control Conference.

use crate::core::scalar::ControlScalar;

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

/// Errors that can occur when constructing or updating ADRC controllers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdrcError {
    /// Observer bandwidth must be strictly positive.
    NonPositiveObserverBandwidth,
    /// Controller bandwidth must be strictly positive.
    NonPositiveControllerBandwidth,
    /// The control gain parameter α must be strictly positive.
    NonPositiveAlpha,
    /// Sampling period `dt` must be strictly positive.
    NonPositiveDt,
}

impl core::fmt::Display for AdrcError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            AdrcError::NonPositiveObserverBandwidth => {
                f.write_str("observer bandwidth must be positive")
            }
            AdrcError::NonPositiveControllerBandwidth => {
                f.write_str("controller bandwidth must be positive")
            }
            AdrcError::NonPositiveAlpha => f.write_str("alpha must be positive"),
            AdrcError::NonPositiveDt => f.write_str("dt must be positive"),
        }
    }
}

// ---------------------------------------------------------------------------
// Second-order ADRC
// ---------------------------------------------------------------------------

/// Second-order Active Disturbance Rejection Controller.
///
/// Plant model (assumed): ÿ = f(y, ẏ, d, t) + b·u
/// where `f` is the total disturbance (unknown) and `b` is the approximate
/// high-frequency gain.
///
/// The ESO augments the state with `z3 = f`, yielding:
/// ```text
///   ż1 = z2
///   ż2 = z3 + b·u
///   ż3 = ḟ   (treated as zero — slowly varying)
/// ```
/// with observer gains tuned by bandwidth ω_o: β1 = 3ω_o, β2 = 3ω_o², β3 = ω_o³.
///
/// The control law is:
/// ```text
///   u0 = kp·(r - z1) + kd·(ṙ - z2)   with kp = ω_c², kd = 2ω_c
///   u  = (u0 - z3) / b
/// ```
///
/// # Type parameters
/// - `S`: numeric scalar type (f32 or f64).
#[derive(Debug, Clone, Copy)]
pub struct SecondOrderAdrc<S: ControlScalar> {
    /// Observer state: estimated output z1.
    pub z1: S,
    /// Observer state: estimated output derivative z2.
    pub z2: S,
    /// Observer state: estimated total disturbance z3.
    pub z3: S,

    /// Observer bandwidth ω_o (rad/s).
    omega_o: S,
    /// Controller bandwidth ω_c (rad/s).
    omega_c: S,
    /// Approximate high-frequency gain b.
    b: S,

    // Pre-computed observer gains
    beta1: S,
    beta2: S,
    beta3: S,
    // Pre-computed controller gains
    kp: S,
    kd: S,

    /// Sampling period (s).
    dt: S,
}

impl<S: ControlScalar> SecondOrderAdrc<S> {
    /// Construct a second-order ADRC.
    ///
    /// # Arguments
    /// - `omega_o`: ESO bandwidth (rad/s). Typically 3–10× `omega_c`.
    /// - `omega_c`: closed-loop controller bandwidth (rad/s).
    /// - `b`: approximate high-frequency gain of the plant.
    /// - `dt`: discrete sampling period (s).
    pub fn new(omega_o: S, omega_c: S, b: S, dt: S) -> Result<Self, AdrcError> {
        if omega_o <= S::ZERO {
            return Err(AdrcError::NonPositiveObserverBandwidth);
        }
        if omega_c <= S::ZERO {
            return Err(AdrcError::NonPositiveControllerBandwidth);
        }
        if b <= S::ZERO {
            return Err(AdrcError::NonPositiveAlpha);
        }
        if dt <= S::ZERO {
            return Err(AdrcError::NonPositiveDt);
        }

        let three = S::from_f64(3.0);
        let beta1 = three * omega_o;
        let beta2 = three * omega_o * omega_o;
        let beta3 = omega_o * omega_o * omega_o;

        let kp = omega_c * omega_c;
        let kd = S::TWO * omega_c;

        Ok(Self {
            z1: S::ZERO,
            z2: S::ZERO,
            z3: S::ZERO,
            omega_o,
            omega_c,
            b,
            beta1,
            beta2,
            beta3,
            kp,
            kd,
            dt,
        })
    }

    /// Reset observer states to zero.
    pub fn reset(&mut self) {
        self.z1 = S::ZERO;
        self.z2 = S::ZERO;
        self.z3 = S::ZERO;
    }

    /// Reset observer states to given initial conditions.
    pub fn reset_to(&mut self, y0: S, dy0: S) {
        self.z1 = y0;
        self.z2 = dy0;
        self.z3 = S::ZERO;
    }

    /// Run one update step.
    ///
    /// # Arguments
    /// - `y`: current plant output (measured).
    /// - `r`: reference (set-point).
    /// - `dr`: reference derivative (set to `S::ZERO` if not available).
    /// - `u_prev`: control input applied at the *previous* step.
    ///
    /// # Returns
    /// The control input `u` to apply at the current step.
    pub fn update(&mut self, y: S, r: S, dr: S, u_prev: S) -> S {
        // ESO prediction error
        let e_obs = self.z1 - y;

        // Euler integration of ESO
        let dz1 = self.z2 - self.beta1 * e_obs;
        let dz2 = self.z3 + self.b * u_prev - self.beta2 * e_obs;
        let dz3 = -self.beta3 * e_obs;

        self.z1 += dz1 * self.dt;
        self.z2 += dz2 * self.dt;
        self.z3 += dz3 * self.dt;

        // PD control on virtual (disturbance-free) double integrator
        let u0 = self.kp * (r - self.z1) + self.kd * (dr - self.z2);

        // Cancel estimated disturbance
        (u0 - self.z3) / self.b
    }

    /// Observer bandwidth.
    pub fn omega_o(&self) -> S {
        self.omega_o
    }

    /// Controller bandwidth.
    pub fn omega_c(&self) -> S {
        self.omega_c
    }

    /// Estimated total disturbance (z3).
    pub fn disturbance_estimate(&self) -> S {
        self.z3
    }
}

// ---------------------------------------------------------------------------
// First-order ADRC
// ---------------------------------------------------------------------------

/// First-order Active Disturbance Rejection Controller.
///
/// Plant model (assumed): ẏ = f(y, d, t) + b·u
///
/// ESO (2 states):
/// ```text
///   ż1 = z2 + b·u   (estimated output)
///   ż2 = ḟ          (estimated total disturbance)
/// ```
/// Observer gains: β1 = 2ω_o, β2 = ω_o².
///
/// Control law (P + disturbance cancellation):
/// ```text
///   u0 = ω_c · (r - z1)
///   u  = (u0 - z2) / b
/// ```
///
/// # Type parameters
/// - `S`: numeric scalar type (f32 or f64).
#[derive(Debug, Clone, Copy)]
pub struct FirstOrderAdrc<S: ControlScalar> {
    /// Observer state: estimated output z1.
    pub z1: S,
    /// Observer state: estimated total disturbance z2.
    pub z2: S,

    /// Observer bandwidth ω_o (rad/s).
    omega_o: S,
    /// Controller bandwidth ω_c (rad/s).
    omega_c: S,
    /// Approximate high-frequency gain b.
    b: S,

    // Pre-computed gains
    beta1: S,
    beta2: S,

    /// Sampling period (s).
    dt: S,
}

impl<S: ControlScalar> FirstOrderAdrc<S> {
    /// Construct a first-order ADRC.
    ///
    /// # Arguments
    /// - `omega_o`: ESO bandwidth (rad/s).
    /// - `omega_c`: closed-loop bandwidth (rad/s).
    /// - `b`: approximate high-frequency gain of the plant.
    /// - `dt`: discrete sampling period (s).
    pub fn new(omega_o: S, omega_c: S, b: S, dt: S) -> Result<Self, AdrcError> {
        if omega_o <= S::ZERO {
            return Err(AdrcError::NonPositiveObserverBandwidth);
        }
        if omega_c <= S::ZERO {
            return Err(AdrcError::NonPositiveControllerBandwidth);
        }
        if b <= S::ZERO {
            return Err(AdrcError::NonPositiveAlpha);
        }
        if dt <= S::ZERO {
            return Err(AdrcError::NonPositiveDt);
        }

        let two = S::TWO;
        let beta1 = two * omega_o;
        let beta2 = omega_o * omega_o;

        Ok(Self {
            z1: S::ZERO,
            z2: S::ZERO,
            omega_o,
            omega_c,
            b,
            beta1,
            beta2,
            dt,
        })
    }

    /// Reset observer states to zero.
    pub fn reset(&mut self) {
        self.z1 = S::ZERO;
        self.z2 = S::ZERO;
    }

    /// Reset observer to a known initial output.
    pub fn reset_to(&mut self, y0: S) {
        self.z1 = y0;
        self.z2 = S::ZERO;
    }

    /// Run one update step.
    ///
    /// # Arguments
    /// - `y`: measured output.
    /// - `r`: reference.
    /// - `u_prev`: control input applied at the previous step.
    ///
    /// # Returns
    /// Control input for the current step.
    pub fn update(&mut self, y: S, r: S, u_prev: S) -> S {
        let e_obs = self.z1 - y;

        let dz1 = self.z2 + self.b * u_prev - self.beta1 * e_obs;
        let dz2 = -self.beta2 * e_obs;

        self.z1 += dz1 * self.dt;
        self.z2 += dz2 * self.dt;

        let u0 = self.omega_c * (r - self.z1);
        (u0 - self.z2) / self.b
    }

    /// Observer bandwidth.
    pub fn omega_o(&self) -> S {
        self.omega_o
    }

    /// Controller bandwidth.
    pub fn omega_c(&self) -> S {
        self.omega_c
    }

    /// Estimated total disturbance.
    pub fn disturbance_estimate(&self) -> S {
        self.z2
    }
}

// ---------------------------------------------------------------------------
// ESO standalone
// ---------------------------------------------------------------------------

/// Standalone second-order Extended State Observer.
///
/// Useful when you want to separate observation from control, or use the
/// disturbance estimate in a different control scheme.
#[derive(Debug, Clone, Copy)]
pub struct ExtendedStateObserver<S: ControlScalar> {
    /// Estimated output.
    pub z1: S,
    /// Estimated output derivative.
    pub z2: S,
    /// Estimated total disturbance.
    pub z3: S,

    beta1: S,
    beta2: S,
    beta3: S,
    b: S,
    dt: S,
}

impl<S: ControlScalar> ExtendedStateObserver<S> {
    /// Create a second-order ESO parameterised by observer bandwidth ω_o.
    pub fn new(omega_o: S, b: S, dt: S) -> Result<Self, AdrcError> {
        if omega_o <= S::ZERO {
            return Err(AdrcError::NonPositiveObserverBandwidth);
        }
        if b <= S::ZERO {
            return Err(AdrcError::NonPositiveAlpha);
        }
        if dt <= S::ZERO {
            return Err(AdrcError::NonPositiveDt);
        }

        let three = S::from_f64(3.0);
        Ok(Self {
            z1: S::ZERO,
            z2: S::ZERO,
            z3: S::ZERO,
            beta1: three * omega_o,
            beta2: three * omega_o * omega_o,
            beta3: omega_o * omega_o * omega_o,
            b,
            dt,
        })
    }

    /// Update ESO with current measurement and applied input.
    pub fn update(&mut self, y: S, u: S) {
        let e = self.z1 - y;
        let dz1 = self.z2 - self.beta1 * e;
        let dz2 = self.z3 + self.b * u - self.beta2 * e;
        let dz3 = -self.beta3 * e;
        self.z1 += dz1 * self.dt;
        self.z2 += dz2 * self.dt;
        self.z3 += dz3 * self.dt;
    }

    /// Returns (z1, z2, z3).
    pub fn states(&self) -> (S, S, S) {
        (self.z1, self.z2, self.z3)
    }

    /// Reset all states to zero.
    pub fn reset(&mut self) {
        self.z1 = S::ZERO;
        self.z2 = S::ZERO;
        self.z3 = S::ZERO;
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

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

    const DT: f64 = 0.001;

    /// Simple first-order plant: ẏ = -y + u + disturbance
    /// Discretised (Euler): y[k+1] = y[k] + dt*(-y[k] + u[k] + d)
    fn step_first_order(y: f64, u: f64, d: f64) -> f64 {
        y + DT * (-y + u + d)
    }

    /// Second-order plant: ÿ = -0.5ẏ + u + d
    /// State: [y, ẏ]
    fn step_second_order(state: [f64; 2], u: f64, d: f64) -> [f64; 2] {
        let dy = state[1];
        let ddy = -0.5 * state[1] + u + d;
        [state[0] + DT * dy, state[1] + DT * ddy]
    }

    #[test]
    fn first_order_adrc_invalid_params() {
        assert!(FirstOrderAdrc::<f64>::new(0.0, 10.0, 1.0, DT).is_err());
        assert!(FirstOrderAdrc::<f64>::new(50.0, 0.0, 1.0, DT).is_err());
        assert!(FirstOrderAdrc::<f64>::new(50.0, 10.0, 0.0, DT).is_err());
        assert!(FirstOrderAdrc::<f64>::new(50.0, 10.0, 1.0, 0.0).is_err());
    }

    #[test]
    fn second_order_adrc_invalid_params() {
        assert!(SecondOrderAdrc::<f64>::new(0.0, 10.0, 1.0, DT).is_err());
        assert!(SecondOrderAdrc::<f64>::new(100.0, 0.0, 1.0, DT).is_err());
        assert!(SecondOrderAdrc::<f64>::new(100.0, 10.0, -1.0, DT).is_err());
        assert!(SecondOrderAdrc::<f64>::new(100.0, 10.0, 1.0, -DT).is_err());
    }

    #[test]
    fn first_order_adrc_tracks_reference_with_disturbance() {
        let mut ctrl = FirstOrderAdrc::<f64>::new(50.0, 10.0, 1.0, DT).expect("valid params");
        let r = 1.0_f64;
        let disturbance = 0.3_f64; // constant disturbance

        let mut y = 0.0_f64;
        let mut u = 0.0_f64;
        for _ in 0..5000 {
            let u_new = ctrl.update(y, r, u);
            y = step_first_order(y, u, disturbance);
            u = u_new;
        }

        // Should track reference within 5% despite disturbance
        assert!(
            (y - r).abs() < 0.05,
            "output={:.4} should be near reference={}",
            y,
            r
        );
    }

    #[test]
    fn second_order_adrc_tracks_reference() {
        let mut ctrl = SecondOrderAdrc::<f64>::new(80.0, 10.0, 1.0, DT).expect("valid params");
        let r = 1.0_f64;
        let disturbance = 0.5_f64;

        let mut state = [0.0_f64; 2];
        let mut u = 0.0_f64;
        for _ in 0..8000 {
            let u_new = ctrl.update(state[0], r, 0.0, u);
            state = step_second_order(state, u, disturbance);
            u = u_new;
        }

        assert!(
            (state[0] - r).abs() < 0.05,
            "output={:.4} should converge to reference={}",
            state[0],
            r
        );
    }

    #[test]
    fn eso_disturbance_estimate_converges() {
        // Use ADRC first-order controller with a disturbance:
        // Plant: ẏ = d (pure integrator with disturbance d=2).
        // The first-order ADRC (which uses a 2-state ESO) should estimate d.
        let omega_o = 80.0_f64;
        let omega_c = 20.0_f64;
        let b = 1.0_f64;
        let d = 2.0_f64; // constant disturbance to estimate

        let mut ctrl = FirstOrderAdrc::<f64>::new(omega_o, omega_c, b, DT).expect("valid params");

        let mut y = 0.0_f64;
        let mut u = 0.0_f64;
        let r = 1.0_f64;

        for _ in 0..6000 {
            let u_new = ctrl.update(y, r, u);
            // Plant: ẏ = b·u + d
            y += DT * (b * u + d);
            u = u_new;
        }

        // After many steps the ADRC should have converged; output near reference
        assert!(
            (y - r).abs() < 0.1,
            "output y={:.4} should be near r={} despite disturbance d={}",
            y,
            r,
            d
        );
        // ESO second state (z2) estimates the lumped disturbance
        let d_est = ctrl.disturbance_estimate();
        assert!(
            d_est.abs() > 0.5,
            "disturbance estimate z2={:.4} should be significantly non-zero",
            d_est
        );
    }

    #[test]
    fn second_order_adrc_reset() {
        let mut ctrl = SecondOrderAdrc::<f64>::new(100.0, 20.0, 1.0, DT).expect("valid params");

        // Run for a while
        for i in 0..100 {
            let _ = ctrl.update(i as f64 * 0.01, 1.0, 0.0, 0.0);
        }

        ctrl.reset();
        assert_eq!(ctrl.z1, 0.0);
        assert_eq!(ctrl.z2, 0.0);
        assert_eq!(ctrl.z3, 0.0);
    }

    #[test]
    fn first_order_adrc_f32() {
        let mut ctrl =
            FirstOrderAdrc::<f32>::new(50.0, 10.0, 1.0, 0.001).expect("valid f32 params");
        let u = ctrl.update(0.0_f32, 1.0_f32, 0.0_f32);
        // Should produce a non-NaN finite control signal
        assert!(u.is_finite());
    }
}