ladrc-no-std 0.1.0

no_std LADRC controllers for embedded Rust
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
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
#![no_std]
#![doc = include_str!("../README.md")]

/// Floating-point type used by the crate.
///
/// `f32` keeps the implementation compact for microcontrollers and avoids
/// pulling in generic numeric traits.
pub type Float = f32;

const MIN_ABS_B0: Float = 1.0e-9;

/// Configuration validation error.
///
/// Constructors validate parameters before a controller is created. This avoids
/// silently running a loop with a zero sample time, zero plant gain, inverted
/// output limit, or non-finite coefficient.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigError {
    /// A value must be finite.
    NonFinite,
    /// The sample period must be positive.
    NonPositiveSamplePeriod,
    /// A bandwidth, gain, tracking speed, or delta must be positive.
    NonPositiveParameter,
    /// The plant input gain estimate `b0` is zero or too close to zero.
    ZeroPlantGain,
    /// The lower output limit is greater than the upper output limit.
    InvalidOutputLimit,
}

/// Optional control output clamp.
///
/// Use this to match the command range of the real actuator, for example
/// `0.0..1.0` for normalized heater power or `-1.0..1.0` for a bidirectional
/// motor command. The controller still reports the unsaturated value in its
/// update output, which is useful during tuning.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OutputLimit {
    /// Minimum allowed controller output.
    pub min: Float,
    /// Maximum allowed controller output.
    pub max: Float,
}

impl OutputLimit {
    /// Creates a new output clamp.
    #[inline]
    pub const fn new(min: Float, max: Float) -> Self {
        Self { min, max }
    }

    /// Applies the clamp to `value`.
    #[inline]
    pub fn apply(self, value: Float) -> Float {
        if value < self.min {
            self.min
        } else if value > self.max {
            self.max
        } else {
            value
        }
    }

    #[inline]
    fn validate(self) -> Result<(), ConfigError> {
        if !finite(self.min) || !finite(self.max) {
            return Err(ConfigError::NonFinite);
        }

        if self.min > self.max {
            return Err(ConfigError::InvalidOutputLimit);
        }

        Ok(())
    }
}

#[inline]
fn apply_limit(value: Float, limit: Option<OutputLimit>) -> Float {
    match limit {
        Some(limit) => limit.apply(value),
        None => value,
    }
}

#[inline]
fn validate_limit(limit: Option<OutputLimit>) -> Result<(), ConfigError> {
    match limit {
        Some(limit) => limit.validate(),
        None => Ok(()),
    }
}

#[inline]
fn finite(value: Float) -> bool {
    value.is_finite()
}

#[inline]
fn validate_sample_period(sample_period: Float) -> Result<(), ConfigError> {
    if !finite(sample_period) {
        return Err(ConfigError::NonFinite);
    }

    if sample_period <= 0.0 {
        return Err(ConfigError::NonPositiveSamplePeriod);
    }

    Ok(())
}

#[inline]
fn validate_time(time_seconds: Float) -> Result<(), ConfigError> {
    if !finite(time_seconds) {
        return Err(ConfigError::NonFinite);
    }

    Ok(())
}

#[inline]
fn elapsed_sample_period(
    now_seconds: Float,
    last_update_at: &mut Option<Float>,
    fallback_sample_period: Float,
) -> Result<Float, ConfigError> {
    validate_time(now_seconds)?;

    let sample_period = match *last_update_at {
        Some(previous) => {
            let elapsed = now_seconds - previous;
            validate_sample_period(elapsed)?;
            elapsed
        }
        None => fallback_sample_period,
    };

    *last_update_at = Some(now_seconds);
    Ok(sample_period)
}

#[inline]
fn elapsed_sample_period_millis(
    now_millis: u64,
    last_update_at_millis: &mut Option<u64>,
    fallback_sample_period: Float,
) -> Result<Float, ConfigError> {
    let sample_period = match *last_update_at_millis {
        Some(previous) => {
            let elapsed_millis = now_millis
                .checked_sub(previous)
                .ok_or(ConfigError::NonPositiveSamplePeriod)?;

            if elapsed_millis == 0 {
                return Err(ConfigError::NonPositiveSamplePeriod);
            }

            elapsed_millis as Float * 0.001
        }
        None => fallback_sample_period,
    };

    *last_update_at_millis = Some(now_millis);
    Ok(sample_period)
}

#[inline]
fn validate_positive(value: Float) -> Result<(), ConfigError> {
    if !finite(value) {
        return Err(ConfigError::NonFinite);
    }

    if value <= 0.0 {
        return Err(ConfigError::NonPositiveParameter);
    }

    Ok(())
}

#[inline]
fn validate_b0(b0: Float) -> Result<(), ConfigError> {
    if !finite(b0) {
        return Err(ConfigError::NonFinite);
    }

    if b0.abs() <= MIN_ABS_B0 {
        return Err(ConfigError::ZeroPlantGain);
    }

    Ok(())
}

pub mod ladrc {
    //! Linear active disturbance rejection controllers.
    //!
    //! LADRC keeps the model small and estimates the unknown part of the plant
    //! as one total disturbance. The crate provides first-order and
    //! second-order controllers with a bandwidth-based configuration helper.
    //!
    //! Use [`LadrcFirstOrder`] for plants that are well represented by:
    //!
    //! ```text
    //! y' = f + b0 * u
    //! ```
    //!
    //! Use [`LadrcSecondOrder`] for plants that are well represented by:
    //!
    //! ```text
    //! y'' = f + b0 * u
    //! ```
    //!
    //! In both cases `f` is not modeled explicitly. The extended state observer
    //! estimates it as `disturbance`, and the control law subtracts that
    //! estimate before dividing by `b0`.
    //!
    //! Typical update order in an application:
    //!
    //! ```text
    //! read sensor -> controller.update(reference, measurement) -> write actuator
    //! ```

    use super::{
        apply_limit, elapsed_sample_period, elapsed_sample_period_millis, validate_b0,
        validate_limit, validate_positive, validate_sample_period, validate_time, ConfigError,
        Float, OutputLimit,
    };

    /// Estimated first-order LADRC state.
    #[derive(Debug, Clone, Copy, Default, PartialEq)]
    pub struct FirstOrderEstimate {
        /// Estimated process output.
        pub output: Float,
        /// Estimated total disturbance.
        pub disturbance: Float,
    }

    /// One first-order LADRC update result.
    #[derive(Debug, Clone, Copy, PartialEq)]
    pub struct FirstOrderOutput {
        /// Saturated control signal.
        pub control: Float,
        /// Control before output saturation.
        pub unsaturated_control: Float,
        /// Pure feedback term before disturbance compensation.
        pub feedback: Float,
        /// Estimated state after the observer update.
        pub estimate: FirstOrderEstimate,
    }

    /// First-order LADRC configuration.
    ///
    /// This configuration is for processes where the control command mostly
    /// changes the first derivative of the measured output:
    ///
    /// ```text
    /// y' = f + b0 * u
    /// ```
    ///
    /// Examples include temperature, pressure, flow, and motor speed loops.
    #[derive(Debug, Clone, Copy, PartialEq)]
    pub struct LadrcFirstOrderConfig {
        /// Nominal sampling period in seconds.
        ///
        /// Plain `update` uses this value directly. `update_at` uses it only
        /// for the first call when no previous timestamp is known yet.
        pub sample_period: Float,
        /// Estimated plant input gain from command to output rate.
        ///
        /// The value may be approximate, but the sign must match the real
        /// plant. A wrong sign usually makes the closed loop diverge.
        pub b0: Float,
        /// Controller feedback gain.
        pub kp: Float,
        /// First observer gain for output estimation.
        pub observer_beta1: Float,
        /// Second observer gain for total-disturbance estimation.
        pub observer_beta2: Float,
        /// Optional output clamp.
        pub output_limit: Option<OutputLimit>,
    }

    impl LadrcFirstOrderConfig {
        /// Creates a configuration from controller and observer bandwidths.
        ///
        /// The gain placement is `kp = wc`, `beta1 = 2 * wo`,
        /// `beta2 = wo^2`.
        ///
        /// `controller_bandwidth` controls the closed-loop response speed.
        /// `observer_bandwidth` controls how fast the observer estimates the
        /// total disturbance. A common first value is `observer_bandwidth`
        /// around three to five times `controller_bandwidth`.
        #[inline]
        pub fn from_bandwidth(
            sample_period: Float,
            b0: Float,
            controller_bandwidth: Float,
            observer_bandwidth: Float,
        ) -> Self {
            Self {
                sample_period,
                b0,
                kp: controller_bandwidth,
                observer_beta1: 2.0 * observer_bandwidth,
                observer_beta2: observer_bandwidth * observer_bandwidth,
                output_limit: None,
            }
        }

        /// Returns the same configuration with an output clamp.
        #[inline]
        pub const fn with_output_limit(mut self, limit: OutputLimit) -> Self {
            self.output_limit = Some(limit);
            self
        }

        /// Validates all parameters.
        pub fn validate(self) -> Result<(), ConfigError> {
            validate_sample_period(self.sample_period)?;
            validate_b0(self.b0)?;
            validate_positive(self.kp)?;
            validate_positive(self.observer_beta1)?;
            validate_positive(self.observer_beta2)?;
            validate_limit(self.output_limit)
        }
    }

    /// First-order linear active disturbance rejection controller.
    ///
    /// Call [`LadrcFirstOrder::update`] once per fixed sample period. The
    /// method reads no hardware; it only consumes the reference and measured
    /// output and returns the next actuator command.
    #[derive(Debug, Clone, Copy, PartialEq)]
    pub struct LadrcFirstOrder {
        config: LadrcFirstOrderConfig,
        z1: Float,
        z2: Float,
        last_control: Float,
        last_update_at: Option<Float>,
        last_update_at_millis: Option<u64>,
    }

    impl LadrcFirstOrder {
        /// Creates a controller and validates the configuration.
        #[inline]
        pub fn new(config: LadrcFirstOrderConfig) -> Result<Self, ConfigError> {
            config.validate()?;
            Ok(Self {
                config,
                z1: 0.0,
                z2: 0.0,
                last_control: 0.0,
                last_update_at: None,
                last_update_at_millis: None,
            })
        }

        /// Returns the current configuration.
        #[inline]
        pub const fn config(&self) -> LadrcFirstOrderConfig {
            self.config
        }

        /// Returns the current observer estimate.
        #[inline]
        pub const fn estimate(&self) -> FirstOrderEstimate {
            FirstOrderEstimate {
                output: self.z1,
                disturbance: self.z2,
            }
        }

        /// Returns the control signal used by the previous observer update.
        #[inline]
        pub const fn last_control(&self) -> Float {
            self.last_control
        }

        /// Returns the timestamp stored by the last [`LadrcFirstOrder::update_at`]
        /// call.
        #[inline]
        pub const fn last_update_at(&self) -> Option<Float> {
            self.last_update_at
        }

        /// Returns the millisecond timestamp stored by the last
        /// [`LadrcFirstOrder::update_at_millis`] call.
        #[inline]
        pub const fn last_update_at_millis(&self) -> Option<u64> {
            self.last_update_at_millis
        }

        /// Resets the observer to a measured output and clears disturbance and
        /// control memory.
        #[inline]
        pub fn reset(&mut self, measurement: Float) {
            self.reset_with(measurement, 0.0, 0.0);
        }

        /// Resets the observer and initializes the timestamp used by
        /// [`LadrcFirstOrder::update_at`].
        ///
        /// Use this before enabling a variable-period loop. It prevents the
        /// first `update_at` call from falling back to the nominal
        /// `config.sample_period`.
        #[inline]
        pub fn reset_at(
            &mut self,
            now_seconds: Float,
            measurement: Float,
        ) -> Result<(), ConfigError> {
            validate_time(now_seconds)?;
            self.reset(measurement);
            self.last_update_at = Some(now_seconds);
            self.last_update_at_millis = None;
            Ok(())
        }

        /// Resets the observer and initializes the millisecond timestamp used
        /// by [`LadrcFirstOrder::update_at_millis`].
        ///
        /// This is the preferred timestamp API for HAL clocks that return
        /// integer milliseconds, such as `esp-hal`'s
        /// `Instant::now().duration_since_epoch().as_millis()`.
        #[inline]
        pub fn reset_at_millis(&mut self, now_millis: u64, measurement: Float) {
            self.reset(measurement);
            self.last_update_at_millis = Some(now_millis);
            self.last_update_at = None;
        }

        /// Resets all controller state.
        #[inline]
        pub fn reset_with(
            &mut self,
            estimated_output: Float,
            estimated_disturbance: Float,
            last_control: Float,
        ) {
            self.z1 = estimated_output;
            self.z2 = estimated_disturbance;
            self.last_control = last_control;
            self.last_update_at = None;
            self.last_update_at_millis = None;
        }

        /// Runs one LADRC sample.
        ///
        /// The extended state observer uses the previous saturated control
        /// signal, then the new control signal is computed and stored for the
        /// next call.
        ///
        /// `reference` and `measurement` must use the same units. The returned
        /// `control` uses the actuator units implied by `b0`.
        pub fn update(&mut self, reference: Float, measurement: Float) -> FirstOrderOutput {
            self.update_unchecked(self.config.sample_period, reference, measurement)
        }

        /// Runs one LADRC sample with an explicit period in seconds.
        ///
        /// Use this when the control loop period is not perfectly constant and
        /// the application already computed the elapsed time since the previous
        /// sample.
        pub fn update_with_period(
            &mut self,
            sample_period: Float,
            reference: Float,
            measurement: Float,
        ) -> Result<FirstOrderOutput, ConfigError> {
            validate_sample_period(sample_period)?;
            Ok(self.update_unchecked(sample_period, reference, measurement))
        }

        /// Runs one LADRC sample at a monotonic timestamp in seconds.
        ///
        /// The controller stores the previous timestamp and computes
        /// `sample_period = now_seconds - previous_now_seconds` internally.
        /// The first call uses the nominal `config.sample_period` because no
        /// previous timestamp exists yet. Call [`LadrcFirstOrder::reset_at`] to
        /// initialize the timestamp before the first variable-period update.
        pub fn update_at(
            &mut self,
            now_seconds: Float,
            reference: Float,
            measurement: Float,
        ) -> Result<FirstOrderOutput, ConfigError> {
            let sample_period = elapsed_sample_period(
                now_seconds,
                &mut self.last_update_at,
                self.config.sample_period,
            )?;
            Ok(self.update_unchecked(sample_period, reference, measurement))
        }

        /// Runs one LADRC sample at a monotonic timestamp in milliseconds.
        ///
        /// The controller computes `dt` with integer subtraction first, then
        /// converts only that short elapsed interval to seconds. This avoids
        /// losing millisecond precision after long uptime.
        pub fn update_at_millis(
            &mut self,
            now_millis: u64,
            reference: Float,
            measurement: Float,
        ) -> Result<FirstOrderOutput, ConfigError> {
            let sample_period = elapsed_sample_period_millis(
                now_millis,
                &mut self.last_update_at_millis,
                self.config.sample_period,
            )?;
            Ok(self.update_unchecked(sample_period, reference, measurement))
        }

        fn update_unchecked(
            &mut self,
            sample_period: Float,
            reference: Float,
            measurement: Float,
        ) -> FirstOrderOutput {
            self.update_observer(measurement, sample_period);

            let feedback = self.config.kp * (reference - self.z1);
            let unsaturated_control = (feedback - self.z2) / self.config.b0;
            let control = apply_limit(unsaturated_control, self.config.output_limit);
            self.last_control = control;

            FirstOrderOutput {
                control,
                unsaturated_control,
                feedback,
                estimate: self.estimate(),
            }
        }

        fn update_observer(&mut self, measurement: Float, sample_period: Float) {
            let e = self.z1 - measurement;
            let h = sample_period;

            self.z1 +=
                h * (self.z2 - self.config.observer_beta1 * e + self.config.b0 * self.last_control);
            self.z2 += h * (-self.config.observer_beta2 * e);
        }
    }

    /// Estimated second-order LADRC state.
    #[derive(Debug, Clone, Copy, Default, PartialEq)]
    pub struct SecondOrderEstimate {
        /// Estimated process output.
        pub position: Float,
        /// Estimated output derivative.
        pub velocity: Float,
        /// Estimated total disturbance.
        pub disturbance: Float,
    }

    /// One second-order LADRC update result.
    #[derive(Debug, Clone, Copy, PartialEq)]
    pub struct SecondOrderOutput {
        /// Saturated control signal.
        pub control: Float,
        /// Control before output saturation.
        pub unsaturated_control: Float,
        /// Pure feedback term before disturbance compensation.
        pub feedback: Float,
        /// Estimated state after the observer update.
        pub estimate: SecondOrderEstimate,
    }

    /// Second-order LADRC configuration.
    ///
    /// This configuration is for processes where the control command mostly
    /// changes the second derivative of the measured output:
    ///
    /// ```text
    /// y'' = f + b0 * u
    /// ```
    ///
    /// Examples include motor position, robot joint angle, gimbal angle, and
    /// linear actuator position loops.
    #[derive(Debug, Clone, Copy, PartialEq)]
    pub struct LadrcSecondOrderConfig {
        /// Nominal sampling period in seconds.
        ///
        /// Plain `update` uses this value directly. `update_at` uses it only
        /// for the first call when no previous timestamp is known yet.
        pub sample_period: Float,
        /// Estimated plant input gain from command to output acceleration.
        ///
        /// The value may be approximate, but the sign must match the real
        /// plant. A wrong sign usually makes the closed loop diverge.
        pub b0: Float,
        /// Proportional-like state feedback gain.
        pub kp: Float,
        /// Derivative-like state feedback gain.
        pub kd: Float,
        /// First observer gain for output estimation.
        pub observer_beta1: Float,
        /// Second observer gain for derivative estimation.
        pub observer_beta2: Float,
        /// Third observer gain for total-disturbance estimation.
        pub observer_beta3: Float,
        /// Optional output clamp.
        pub output_limit: Option<OutputLimit>,
    }

    impl LadrcSecondOrderConfig {
        /// Creates a configuration from controller and observer bandwidths.
        ///
        /// The gain placement is `kp = wc^2`, `kd = 2 * wc`,
        /// `beta1 = 3 * wo`, `beta2 = 3 * wo^2`, `beta3 = wo^3`.
        ///
        /// `controller_bandwidth` controls the closed-loop response speed.
        /// `observer_bandwidth` controls how fast the observer estimates the
        /// total disturbance. A common first value is `observer_bandwidth`
        /// around three to five times `controller_bandwidth`.
        #[inline]
        pub fn from_bandwidth(
            sample_period: Float,
            b0: Float,
            controller_bandwidth: Float,
            observer_bandwidth: Float,
        ) -> Self {
            Self {
                sample_period,
                b0,
                kp: controller_bandwidth * controller_bandwidth,
                kd: 2.0 * controller_bandwidth,
                observer_beta1: 3.0 * observer_bandwidth,
                observer_beta2: 3.0 * observer_bandwidth * observer_bandwidth,
                observer_beta3: observer_bandwidth * observer_bandwidth * observer_bandwidth,
                output_limit: None,
            }
        }

        /// Returns the same configuration with an output clamp.
        #[inline]
        pub const fn with_output_limit(mut self, limit: OutputLimit) -> Self {
            self.output_limit = Some(limit);
            self
        }

        /// Validates all parameters.
        pub fn validate(self) -> Result<(), ConfigError> {
            validate_sample_period(self.sample_period)?;
            validate_b0(self.b0)?;
            validate_positive(self.kp)?;
            validate_positive(self.kd)?;
            validate_positive(self.observer_beta1)?;
            validate_positive(self.observer_beta2)?;
            validate_positive(self.observer_beta3)?;
            validate_limit(self.output_limit)
        }
    }

    /// Second-order linear active disturbance rejection controller.
    ///
    /// This is the recommended default controller for position-like embedded
    /// plants. Call [`LadrcSecondOrder::update`] once per fixed sample period
    /// when the reference derivative is zero, or
    /// [`LadrcSecondOrder::update_with_rate`] when a trajectory generator
    /// provides both position and velocity references.
    #[derive(Debug, Clone, Copy, PartialEq)]
    pub struct LadrcSecondOrder {
        config: LadrcSecondOrderConfig,
        z1: Float,
        z2: Float,
        z3: Float,
        last_control: Float,
        last_update_at: Option<Float>,
        last_update_at_millis: Option<u64>,
    }

    impl LadrcSecondOrder {
        /// Creates a controller and validates the configuration.
        #[inline]
        pub fn new(config: LadrcSecondOrderConfig) -> Result<Self, ConfigError> {
            config.validate()?;
            Ok(Self {
                config,
                z1: 0.0,
                z2: 0.0,
                z3: 0.0,
                last_control: 0.0,
                last_update_at: None,
                last_update_at_millis: None,
            })
        }

        /// Returns the current configuration.
        #[inline]
        pub const fn config(&self) -> LadrcSecondOrderConfig {
            self.config
        }

        /// Returns the current observer estimate.
        #[inline]
        pub const fn estimate(&self) -> SecondOrderEstimate {
            SecondOrderEstimate {
                position: self.z1,
                velocity: self.z2,
                disturbance: self.z3,
            }
        }

        /// Returns the control signal used by the previous observer update.
        #[inline]
        pub const fn last_control(&self) -> Float {
            self.last_control
        }

        /// Returns the timestamp stored by the last [`LadrcSecondOrder::update_at`]
        /// call.
        #[inline]
        pub const fn last_update_at(&self) -> Option<Float> {
            self.last_update_at
        }

        /// Returns the millisecond timestamp stored by the last
        /// [`LadrcSecondOrder::update_at_millis`] call.
        #[inline]
        pub const fn last_update_at_millis(&self) -> Option<u64> {
            self.last_update_at_millis
        }

        /// Resets the observer to a measured output and clears derivative,
        /// disturbance, and control memory.
        #[inline]
        pub fn reset(&mut self, measurement: Float) {
            self.reset_with(measurement, 0.0, 0.0, 0.0);
        }

        /// Resets the observer and initializes the timestamp used by
        /// [`LadrcSecondOrder::update_at`].
        ///
        /// Use this before enabling a variable-period loop. It prevents the
        /// first `update_at` call from falling back to the nominal
        /// `config.sample_period`.
        #[inline]
        pub fn reset_at(
            &mut self,
            now_seconds: Float,
            measurement: Float,
        ) -> Result<(), ConfigError> {
            validate_time(now_seconds)?;
            self.reset(measurement);
            self.last_update_at = Some(now_seconds);
            self.last_update_at_millis = None;
            Ok(())
        }

        /// Resets the observer and initializes the millisecond timestamp used
        /// by [`LadrcSecondOrder::update_at_millis`].
        ///
        /// This is the preferred timestamp API for HAL clocks that return
        /// integer milliseconds, such as `esp-hal`'s
        /// `Instant::now().duration_since_epoch().as_millis()`.
        #[inline]
        pub fn reset_at_millis(&mut self, now_millis: u64, measurement: Float) {
            self.reset(measurement);
            self.last_update_at_millis = Some(now_millis);
            self.last_update_at = None;
        }

        /// Resets all controller state.
        #[inline]
        pub fn reset_with(
            &mut self,
            estimated_position: Float,
            estimated_velocity: Float,
            estimated_disturbance: Float,
            last_control: Float,
        ) {
            self.z1 = estimated_position;
            self.z2 = estimated_velocity;
            self.z3 = estimated_disturbance;
            self.last_control = last_control;
            self.last_update_at = None;
            self.last_update_at_millis = None;
        }

        /// Runs one LADRC sample with zero reference velocity.
        ///
        /// Use this for setpoint control where the desired target is a position
        /// or angle and the desired final velocity is zero.
        #[inline]
        pub fn update(&mut self, reference: Float, measurement: Float) -> SecondOrderOutput {
            self.update_with_rate(reference, 0.0, measurement)
        }

        /// Runs one LADRC sample with zero reference velocity and an explicit
        /// period in seconds.
        ///
        /// Use this when the control loop period is not perfectly constant and
        /// the application already computed the elapsed time since the previous
        /// sample.
        #[inline]
        pub fn update_with_period(
            &mut self,
            sample_period: Float,
            reference: Float,
            measurement: Float,
        ) -> Result<SecondOrderOutput, ConfigError> {
            self.update_with_period_and_rate(sample_period, reference, 0.0, measurement)
        }

        /// Runs one LADRC sample with zero reference velocity at a monotonic
        /// timestamp in seconds.
        ///
        /// The controller stores the previous timestamp and computes
        /// `sample_period = now_seconds - previous_now_seconds` internally.
        /// The first call uses the nominal `config.sample_period` because no
        /// previous timestamp exists yet. Call [`LadrcSecondOrder::reset_at`] to
        /// initialize the timestamp before the first variable-period update.
        #[inline]
        pub fn update_at(
            &mut self,
            now_seconds: Float,
            reference: Float,
            measurement: Float,
        ) -> Result<SecondOrderOutput, ConfigError> {
            self.update_at_with_rate(now_seconds, reference, 0.0, measurement)
        }

        /// Runs one LADRC sample with zero reference velocity at a monotonic
        /// timestamp in milliseconds.
        ///
        /// The controller computes `dt` with integer subtraction first, then
        /// converts only that short elapsed interval to seconds. This avoids
        /// losing millisecond precision after long uptime.
        #[inline]
        pub fn update_at_millis(
            &mut self,
            now_millis: u64,
            reference: Float,
            measurement: Float,
        ) -> Result<SecondOrderOutput, ConfigError> {
            self.update_at_millis_with_rate(now_millis, reference, 0.0, measurement)
        }

        /// Runs one LADRC sample with an explicit reference velocity.
        ///
        /// The extended state observer uses the previous saturated control
        /// signal, then the new control signal is computed and stored for the
        /// next call.
        ///
        /// Use this when an external trajectory generator provides both
        /// reference position and reference velocity.
        pub fn update_with_rate(
            &mut self,
            reference: Float,
            reference_rate: Float,
            measurement: Float,
        ) -> SecondOrderOutput {
            self.update_unchecked(
                self.config.sample_period,
                reference,
                reference_rate,
                measurement,
            )
        }

        /// Runs one LADRC sample with explicit period and explicit reference
        /// velocity.
        ///
        /// Use this when an external trajectory generator provides both
        /// reference position and reference velocity, and the application
        /// already computed the elapsed time since the previous sample.
        pub fn update_with_period_and_rate(
            &mut self,
            sample_period: Float,
            reference: Float,
            reference_rate: Float,
            measurement: Float,
        ) -> Result<SecondOrderOutput, ConfigError> {
            validate_sample_period(sample_period)?;
            Ok(self.update_unchecked(sample_period, reference, reference_rate, measurement))
        }

        /// Runs one LADRC sample at a monotonic timestamp with explicit
        /// reference velocity.
        ///
        /// The controller stores the previous timestamp and computes the sample
        /// period internally. Call [`LadrcSecondOrder::reset_at`] before the
        /// first call if you do not want to use the nominal sample period for
        /// the first update.
        pub fn update_at_with_rate(
            &mut self,
            now_seconds: Float,
            reference: Float,
            reference_rate: Float,
            measurement: Float,
        ) -> Result<SecondOrderOutput, ConfigError> {
            let sample_period = elapsed_sample_period(
                now_seconds,
                &mut self.last_update_at,
                self.config.sample_period,
            )?;
            Ok(self.update_unchecked(sample_period, reference, reference_rate, measurement))
        }

        /// Runs one LADRC sample at a monotonic millisecond timestamp with
        /// explicit reference velocity.
        ///
        /// This is useful with HAL clocks that expose integer milliseconds.
        pub fn update_at_millis_with_rate(
            &mut self,
            now_millis: u64,
            reference: Float,
            reference_rate: Float,
            measurement: Float,
        ) -> Result<SecondOrderOutput, ConfigError> {
            let sample_period = elapsed_sample_period_millis(
                now_millis,
                &mut self.last_update_at_millis,
                self.config.sample_period,
            )?;
            Ok(self.update_unchecked(sample_period, reference, reference_rate, measurement))
        }

        fn update_unchecked(
            &mut self,
            sample_period: Float,
            reference: Float,
            reference_rate: Float,
            measurement: Float,
        ) -> SecondOrderOutput {
            self.update_observer(measurement, sample_period);

            let feedback = self.config.kp * (reference - self.z1)
                + self.config.kd * (reference_rate - self.z2);
            let unsaturated_control = (feedback - self.z3) / self.config.b0;
            let control = apply_limit(unsaturated_control, self.config.output_limit);
            self.last_control = control;

            SecondOrderOutput {
                control,
                unsaturated_control,
                feedback,
                estimate: self.estimate(),
            }
        }

        fn update_observer(&mut self, measurement: Float, sample_period: Float) {
            let e = self.z1 - measurement;
            let h = sample_period;

            self.z1 += h * (self.z2 - self.config.observer_beta1 * e);
            self.z2 +=
                h * (self.z3 - self.config.observer_beta2 * e + self.config.b0 * self.last_control);
            self.z3 += h * (-self.config.observer_beta3 * e);
        }
    }

    /// Conventional alias for the second-order LADRC controller.
    pub type Ladrc = LadrcSecondOrder;
}

pub use ladrc::{
    FirstOrderEstimate, FirstOrderOutput, Ladrc, LadrcFirstOrder, LadrcFirstOrderConfig,
    LadrcSecondOrder, LadrcSecondOrderConfig, SecondOrderEstimate, SecondOrderOutput,
};

#[cfg(test)]
extern crate std;

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

    fn assert_close(actual: Float, expected: Float, tolerance: Float) {
        assert!(
            (actual - expected).abs() <= tolerance,
            "actual={actual}, expected={expected}, tolerance={tolerance}"
        );
    }

    #[test]
    fn output_limit_clamps_values() {
        let limit = OutputLimit::new(-2.0, 3.0);

        assert_eq!(limit.apply(-4.0), -2.0);
        assert_eq!(limit.apply(1.0), 1.0);
        assert_eq!(limit.apply(5.0), 3.0);
    }

    #[test]
    fn validates_bad_configurations() {
        let bad_b0 = ladrc::LadrcSecondOrderConfig::from_bandwidth(0.001, 0.0, 10.0, 50.0);
        assert_eq!(
            ladrc::LadrcSecondOrder::new(bad_b0).unwrap_err(),
            ConfigError::ZeroPlantGain
        );

        let bad_h = ladrc::LadrcFirstOrderConfig::from_bandwidth(0.0, 1.0, 10.0, 50.0);
        assert_eq!(
            ladrc::LadrcFirstOrder::new(bad_h).unwrap_err(),
            ConfigError::NonPositiveSamplePeriod
        );

        let bad_limit = ladrc::LadrcFirstOrderConfig::from_bandwidth(0.001, 1.0, 10.0, 50.0)
            .with_output_limit(OutputLimit::new(1.0, -1.0));
        assert_eq!(
            ladrc::LadrcFirstOrder::new(bad_limit).unwrap_err(),
            ConfigError::InvalidOutputLimit
        );
    }

    #[test]
    fn ladrc_bandwidth_tuning_sets_expected_gains() {
        let first = ladrc::LadrcFirstOrderConfig::from_bandwidth(0.001, 2.0, 12.0, 40.0);
        assert_close(first.kp, 12.0, 1.0e-6);
        assert_close(first.observer_beta1, 80.0, 1.0e-6);
        assert_close(first.observer_beta2, 1_600.0, 1.0e-3);

        let second = ladrc::LadrcSecondOrderConfig::from_bandwidth(0.001, 2.0, 12.0, 40.0);
        assert_close(second.kp, 144.0, 1.0e-6);
        assert_close(second.kd, 24.0, 1.0e-6);
        assert_close(second.observer_beta1, 120.0, 1.0e-6);
        assert_close(second.observer_beta2, 4_800.0, 1.0e-3);
        assert_close(second.observer_beta3, 64_000.0, 1.0e-2);
    }

    #[test]
    fn first_order_ladrc_tracks_with_constant_disturbance() {
        let dt = 0.001;
        let config = ladrc::LadrcFirstOrderConfig::from_bandwidth(dt, 1.0, 18.0, 80.0)
            .with_output_limit(OutputLimit::new(-20.0, 20.0));
        let mut controller = ladrc::LadrcFirstOrder::new(config).unwrap();

        let mut y = 0.0;
        for _ in 0..5_000 {
            let output = controller.update(1.0, y);
            let disturbance = 0.35;
            let y_dot = -0.45 * y + output.control + disturbance;
            y += dt * y_dot;
        }

        assert_close(y, 1.0, 0.03);
        assert_close(controller.estimate().output, y, 0.03);
    }

    #[test]
    fn second_order_ladrc_tracks_with_model_error_and_disturbance() {
        let dt = 0.001;
        let config = ladrc::LadrcSecondOrderConfig::from_bandwidth(dt, 1.0, 14.0, 70.0)
            .with_output_limit(OutputLimit::new(-30.0, 30.0));
        let mut controller = ladrc::LadrcSecondOrder::new(config).unwrap();

        let mut y = 0.0;
        let mut v = 0.0;

        for _ in 0..7_000 {
            let output = controller.update(1.0, y);
            let acceleration = -1.6 * v - 2.0 * y + output.control + 0.4;
            v += dt * acceleration;
            y += dt * v;
        }

        assert_close(y, 1.0, 0.04);
        assert_close(v, 0.0, 0.08);
        assert_close(controller.estimate().position, y, 0.04);
    }

    #[test]
    fn first_order_ladrc_accepts_explicit_variable_period() {
        let config = ladrc::LadrcFirstOrderConfig::from_bandwidth(0.001, 1.0, 18.0, 80.0)
            .with_output_limit(OutputLimit::new(-20.0, 20.0));
        let mut controller = ladrc::LadrcFirstOrder::new(config).unwrap();
        let periods = [0.0007, 0.0012, 0.0009, 0.0015, 0.0010];

        let mut y = 0.0;
        for step in 0..5_000 {
            let dt = periods[step % periods.len()];
            let output = controller.update_with_period(dt, 1.0, y).unwrap();
            let disturbance = 0.35;
            let y_dot = -0.45 * y + output.control + disturbance;
            y += dt * y_dot;
        }

        assert_close(y, 1.0, 0.04);
    }

    #[test]
    fn second_order_ladrc_update_at_handles_variable_periods() {
        let config = ladrc::LadrcSecondOrderConfig::from_bandwidth(0.001, 1.0, 14.0, 70.0)
            .with_output_limit(OutputLimit::new(-30.0, 30.0));
        let mut controller = ladrc::LadrcSecondOrder::new(config).unwrap();
        let periods = [0.0007, 0.0013, 0.0009, 0.0011, 0.0015];

        let mut now = 0.0;
        let mut y = 0.0;
        let mut v = 0.0;
        controller.reset_at(now, y).unwrap();

        for step in 0..7_000 {
            let dt = periods[step % periods.len()];
            now += dt;
            let output = controller.update_at(now, 1.0, y).unwrap();
            let acceleration = -1.6 * v - 2.0 * y + output.control + 0.4;
            v += dt * acceleration;
            y += dt * v;
        }

        assert_close(y, 1.0, 0.05);
        assert_close(v, 0.0, 0.10);
    }

    #[test]
    fn second_order_ladrc_update_at_millis_handles_large_uptime() {
        let config = ladrc::LadrcSecondOrderConfig::from_bandwidth(0.001, 1.0, 14.0, 70.0)
            .with_output_limit(OutputLimit::new(-30.0, 30.0));
        let mut controller = ladrc::LadrcSecondOrder::new(config).unwrap();
        let periods_ms = [1_u64, 2, 1, 1, 2];

        let mut now_ms = 50_000_000_u64;
        let mut y = 0.0;
        let mut v = 0.0;
        controller.reset_at_millis(now_ms, y);

        for step in 0..6_000 {
            let dt_ms = periods_ms[step % periods_ms.len()];
            let dt = dt_ms as Float * 0.001;
            now_ms += dt_ms;

            let output = controller.update_at_millis(now_ms, 1.0, y).unwrap();
            let acceleration = -1.6 * v - 2.0 * y + output.control + 0.4;
            v += dt * acceleration;
            y += dt * v;
        }

        assert_close(y, 1.0, 0.05);
        assert_close(v, 0.0, 0.10);
        assert_eq!(controller.last_update_at_millis(), Some(now_ms));
    }

    #[test]
    fn update_at_rejects_non_monotonic_time() {
        let config = ladrc::LadrcSecondOrderConfig::from_bandwidth(0.001, 1.0, 14.0, 70.0);
        let mut controller = ladrc::LadrcSecondOrder::new(config).unwrap();
        controller.reset_at(1.0, 0.0).unwrap();

        assert_eq!(
            controller.update_at(1.0, 1.0, 0.0).unwrap_err(),
            ConfigError::NonPositiveSamplePeriod
        );
        assert_eq!(
            controller.update_at(0.9, 1.0, 0.0).unwrap_err(),
            ConfigError::NonPositiveSamplePeriod
        );
    }

    #[test]
    fn update_at_millis_rejects_non_monotonic_time() {
        let config = ladrc::LadrcSecondOrderConfig::from_bandwidth(0.001, 1.0, 14.0, 70.0);
        let mut controller = ladrc::LadrcSecondOrder::new(config).unwrap();
        controller.reset_at_millis(1_000, 0.0);

        assert_eq!(
            controller.update_at_millis(1_000, 1.0, 0.0).unwrap_err(),
            ConfigError::NonPositiveSamplePeriod
        );
        assert_eq!(
            controller.update_at_millis(999, 1.0, 0.0).unwrap_err(),
            ConfigError::NonPositiveSamplePeriod
        );
    }
}