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
//! Extended redundancy comparators for safety-critical signal voting.
//!
//! Provides:
//! - [`DualChannelComparatorExt`] — 1oo2 (one-out-of-two) voting with tolerance
//!   tracking and configurable divergence trip threshold.
//! - [`TripleModularRedundancy`] — 2oo3 voting with outlier channel identification.
//!
//! All structures are `no_std` compatible and perform no heap allocation.

#![allow(dead_code)]

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

// ─────────────────────────────────────────────────────────────────────────────
// ComparatorConfig
// ─────────────────────────────────────────────────────────────────────────────

/// Configuration shared by both redundancy comparators.
///
/// Two tolerances are provided to handle measurements at different scales:
/// - `absolute_tolerance` — maximum permitted |ch_a − ch_b|.
/// - `relative_tolerance` — maximum permitted |ch_a − ch_b| / |ch_a + ch_b| * 2,
///   evaluated only when both channels are non-zero.
/// - `max_divergence_count` — number of consecutive divergent comparisons
///   required before a fault is declared.
#[derive(Debug, Clone, Copy)]
pub struct ComparatorConfig<S: ControlScalar> {
    /// Absolute tolerance (engineering-unit difference).
    pub absolute_tolerance: S,
    /// Relative tolerance (dimensionless fraction of the signal magnitude).
    pub relative_tolerance: S,
    /// Number of consecutive divergences required to declare a fault.
    pub max_divergence_count: u32,
}

impl<S: ControlScalar> ComparatorConfig<S> {
    /// Construct a new configuration.
    ///
    /// Panics in debug mode if `max_divergence_count` is zero; in production
    /// a zero count is treated as one (immediate trip on first divergence).
    pub fn new(absolute_tolerance: S, relative_tolerance: S, max_divergence_count: u32) -> Self {
        Self {
            absolute_tolerance,
            relative_tolerance,
            max_divergence_count: max_divergence_count.max(1),
        }
    }

    /// Returns `true` if `a` and `b` agree within both absolute and relative
    /// tolerances.
    ///
    /// Either tolerance alone is sufficient to declare agreement.
    pub fn within_tolerance(&self, a: S, b: S) -> bool {
        let abs_diff = (a - b).abs();
        if abs_diff <= self.absolute_tolerance {
            return true;
        }
        // Relative check — guard against division by near-zero
        let magnitude = (a.abs() + b.abs()) * S::HALF;
        if magnitude > S::ZERO {
            let rel_diff = abs_diff / magnitude;
            if rel_diff <= self.relative_tolerance {
                return true;
            }
        }
        false
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// CompareResult
// ─────────────────────────────────────────────────────────────────────────────

/// Outcome of a single comparison cycle.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CompareResult<S: Copy> {
    /// Both channels agree; the inner value is the voted/average output.
    Agree(S),
    /// Channels diverge but the trip threshold has not yet been reached.
    /// The values of channel A and channel B are included for logging.
    Diverged(S, S),
    /// Fault declared: the comparator has tripped.
    /// `channel` is 0 = A, 1 = B, 2 = unknown.
    Faulty(u8),
}

// ─────────────────────────────────────────────────────────────────────────────
// DualChannelComparatorExt  (1oo2)
// ─────────────────────────────────────────────────────────────────────────────

/// 1oo2 (one-out-of-two) comparator for safety-critical redundant channels.
///
/// Accepts two independent channel readings and produces an agreed output or
/// a fault signal if the channels diverge beyond configured tolerances for
/// more than `max_divergence_count` consecutive comparisons.
///
/// Once tripped, the comparator stays faulted until explicitly reset.
///
/// # Type parameter
/// `S` — any [`ControlScalar`] (typically `f64` for safety calculations).
#[derive(Debug, Clone, Copy)]
pub struct DualChannelComparatorExt<S: ControlScalar> {
    config: ComparatorConfig<S>,
    /// Running count of consecutive divergent comparisons.
    divergence_count: u32,
    /// Whether a fault has been declared.
    tripped: bool,
    /// Index of the suspected faulty channel (0=A, 1=B, 2=unknown).
    suspect_channel: u8,
}

impl<S: ControlScalar> DualChannelComparatorExt<S> {
    /// Create a new 1oo2 comparator with the given configuration.
    pub fn new(config: ComparatorConfig<S>) -> Self {
        Self {
            config,
            divergence_count: 0,
            tripped: false,
            suspect_channel: 2,
        }
    }

    /// Compare channel A and channel B.
    ///
    /// Returns:
    /// - [`CompareResult::Agree`] — channels are within tolerance; the
    ///   output value is the average of the two channels.
    /// - [`CompareResult::Diverged`] — channels differ but trip count not yet
    ///   reached; the comparator remains un-tripped.
    /// - [`CompareResult::Faulty`] — trip count reached or already tripped;
    ///   the suspect channel index is included.
    pub fn compare(&mut self, ch_a: S, ch_b: S) -> CompareResult<S> {
        if self.tripped {
            return CompareResult::Faulty(self.suspect_channel);
        }

        if self.config.within_tolerance(ch_a, ch_b) {
            self.divergence_count = 0;
            let voted = (ch_a + ch_b) * S::HALF;
            CompareResult::Agree(voted)
        } else {
            self.divergence_count += 1;
            if self.divergence_count >= self.config.max_divergence_count {
                self.tripped = true;
                // Without a reference we cannot identify the faulty channel
                self.suspect_channel = 2;
                CompareResult::Faulty(self.suspect_channel)
            } else {
                CompareResult::Diverged(ch_a, ch_b)
            }
        }
    }

    /// Compare channel A and channel B with an external reference value.
    ///
    /// If the comparator trips, the channel that is *farther* from `reference`
    /// is identified as suspect.
    pub fn compare_with_ref(&mut self, ch_a: S, ch_b: S, reference: S) -> CompareResult<S> {
        let result = self.compare(ch_a, ch_b);
        if self.tripped {
            let da = (ch_a - reference).abs();
            let db = (ch_b - reference).abs();
            self.suspect_channel = if da > db { 0 } else { 1 };
            CompareResult::Faulty(self.suspect_channel)
        } else {
            result
        }
    }

    /// Whether the comparator has tripped (fault declared).
    pub fn is_tripped(&self) -> bool {
        self.tripped
    }

    /// Current consecutive divergence count.
    pub fn divergence_count(&self) -> u32 {
        self.divergence_count
    }

    /// Index of the suspected faulty channel (0=A, 1=B, 2=unknown).
    pub fn suspect_channel(&self) -> u8 {
        self.suspect_channel
    }

    /// Reset the comparator, clearing the trip flag and counters.
    pub fn reset(&mut self) {
        self.divergence_count = 0;
        self.tripped = false;
        self.suspect_channel = 2;
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// VoteResult
// ─────────────────────────────────────────────────────────────────────────────

/// Result of a 2oo3 majority vote.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VoteResult<S: Copy> {
    /// The majority-voted value (median of the three channels).
    pub voted_value: S,
    /// Index of the channel that differed from the majority, if any.
    /// - `None` — all three channels agree within tolerance.
    /// - `Some(0)` — channel A is the outlier.
    /// - `Some(1)` — channel B is the outlier.
    /// - `Some(2)` — channel C is the outlier.
    pub outlier_channel: Option<u8>,
}

// ─────────────────────────────────────────────────────────────────────────────
// TripleModularRedundancy  (2oo3)
// ─────────────────────────────────────────────────────────────────────────────

/// 2oo3 (two-out-of-three) Triple Modular Redundancy voter.
///
/// Compares three independent channel readings and returns the majority-voted
/// value.  The channel whose reading deviates from the majority is identified
/// as the outlier.
///
/// Consecutive outlier events on a single channel are counted; if they exceed
/// `config.max_divergence_count`, that channel is declared permanently faulty
/// until `reset()` is called.
#[derive(Debug, Clone, Copy)]
pub struct TripleModularRedundancy<S: ControlScalar> {
    config: ComparatorConfig<S>,
    /// Consecutive outlier count per channel [A, B, C].
    outlier_counts: [u32; 3],
    /// Whether each channel has been declared faulty.
    channel_faulted: [bool; 3],
}

impl<S: ControlScalar> TripleModularRedundancy<S> {
    /// Create a new TMR voter with the given configuration.
    pub fn new(config: ComparatorConfig<S>) -> Self {
        Self {
            config,
            outlier_counts: [0; 3],
            channel_faulted: [false; 3],
        }
    }

    /// Perform a 2oo3 majority vote over channels A, B, and C.
    ///
    /// The voted value is the median of the three readings.  The channel
    /// farthest from the median (if it exceeds the configured tolerance) is
    /// flagged as the outlier.
    ///
    /// Returns `Err` if two or more channels have been permanently faulted
    /// (majority cannot be established).
    pub fn vote(&mut self, ch_a: S, ch_b: S, ch_c: S) -> Result<VoteResult<S>, TmrError> {
        let faulted_count = self.channel_faulted.iter().filter(|&&f| f).count();
        if faulted_count >= 2 {
            return Err(TmrError::MajorityLost);
        }

        let values = [ch_a, ch_b, ch_c];
        let voted = median3(ch_a, ch_b, ch_c);

        // Identify the channel with the largest deviation from the median
        let deviations = [
            (ch_a - voted).abs(),
            (ch_b - voted).abs(),
            (ch_c - voted).abs(),
        ];

        // Find the channel with the maximum deviation
        let mut max_dev = S::ZERO;
        let mut max_idx: u8 = 0;
        for (i, &dev) in deviations.iter().enumerate() {
            if dev > max_dev {
                max_dev = dev;
                max_idx = i as u8;
            }
        }

        let outlier_channel: Option<u8> = if !self
            .config
            .within_tolerance(values[max_idx as usize], voted)
        {
            // Update outlier count for the suspect channel
            let idx = max_idx as usize;
            self.outlier_counts[idx] += 1;
            // Reset counts for the other two channels
            for j in 0..3usize {
                if j != idx {
                    self.outlier_counts[j] = 0;
                }
            }
            if self.outlier_counts[idx] >= self.config.max_divergence_count {
                self.channel_faulted[idx] = true;
            }
            Some(max_idx)
        } else {
            // All three channels agree: reset all outlier counts
            self.outlier_counts = [0; 3];
            None
        };

        Ok(VoteResult {
            voted_value: voted,
            outlier_channel,
        })
    }

    /// Whether a specific channel index (0=A, 1=B, 2=C) has been declared faulted.
    pub fn is_channel_faulted(&self, channel: u8) -> bool {
        let idx = channel as usize;
        if idx < 3 {
            self.channel_faulted[idx]
        } else {
            false
        }
    }

    /// Fault flags for all three channels [A, B, C].
    pub fn channel_faults(&self) -> [bool; 3] {
        self.channel_faulted
    }

    /// Consecutive outlier counts for all three channels [A, B, C].
    pub fn outlier_counts(&self) -> [u32; 3] {
        self.outlier_counts
    }

    /// Reset all state (clear faults, counters).
    pub fn reset(&mut self) {
        self.outlier_counts = [0; 3];
        self.channel_faulted = [false; 3];
    }
}

/// Errors from TMR voting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TmrError {
    /// Two or more channels are permanently faulted; majority cannot be established.
    MajorityLost,
}

impl core::fmt::Display for TmrError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            TmrError::MajorityLost => {
                write!(f, "TMR: majority lost — two or more channels faulted")
            }
        }
    }
}

/// Compute the median of three values without sorting.
fn median3<S: ControlScalar>(a: S, b: S, c: S) -> S {
    // Branchless median: max(min(a,b), min(max(a,b),c))
    let lo = if a < b { a } else { b };
    let hi = if a > b { a } else { b };
    let mid = if hi < c { hi } else { c };
    if lo > mid {
        lo
    } else {
        mid
    }
}

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

    // ── ComparatorConfig::within_tolerance ──────────────────────────────────

    #[test]
    fn within_absolute_tolerance() {
        // abs_tol = 0.1, rel_tol = 0.001 (very tight)
        // diff = 0.05 < 0.1 → agrees by absolute tolerance
        let cfg = ComparatorConfig::new(0.1_f64, 0.001_f64, 3);
        assert!(cfg.within_tolerance(10.0, 10.05));
        // diff = 0.2 > 0.1; rel = 0.2/10.1 ≈ 0.0198 > 0.001 → both fail → diverged
        assert!(!cfg.within_tolerance(10.0, 10.2));
    }

    #[test]
    fn within_relative_tolerance() {
        // abs_tol = 0.05, rel_tol = 0.05
        // abs diff = 0.2 > 0.05 → absolute fails
        // magnitude = 10.1, rel = 0.2/10.1 ≈ 0.0198 < 0.05 → relative passes
        let cfg = ComparatorConfig::new(0.05_f64, 0.05_f64, 3);
        assert!(cfg.within_tolerance(10.0, 10.2));
        // Very large difference: rel = 10.0/15.0 ≈ 0.667 > 0.05 → both fail
        assert!(!cfg.within_tolerance(10.0, 20.0));
    }

    // ── DualChannelComparatorExt ────────────────────────────────────────────

    #[test]
    fn dual_channels_agree() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 3);
        let mut cmp = DualChannelComparatorExt::new(cfg);
        let result = cmp.compare(5.0, 5.05);
        assert!(matches!(result, CompareResult::Agree(_)));
        if let CompareResult::Agree(v) = result {
            assert!((v - 5.025).abs() < 1e-10);
        }
        assert!(!cmp.is_tripped());
    }

    #[test]
    fn dual_channels_diverge_below_threshold() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 3);
        let mut cmp = DualChannelComparatorExt::new(cfg);
        // Two divergences — trip threshold is 3
        let r1 = cmp.compare(0.0, 10.0);
        let r2 = cmp.compare(0.0, 10.0);
        assert!(matches!(r1, CompareResult::Diverged(_, _)));
        assert!(matches!(r2, CompareResult::Diverged(_, _)));
        assert!(!cmp.is_tripped());
        assert_eq!(cmp.divergence_count(), 2);
    }

    #[test]
    fn dual_channels_trip_after_threshold() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 3);
        let mut cmp = DualChannelComparatorExt::new(cfg);
        for _ in 0..3 {
            cmp.compare(0.0, 10.0);
        }
        assert!(cmp.is_tripped());
        // Subsequent calls return Faulty
        assert!(matches!(cmp.compare(0.0, 10.0), CompareResult::Faulty(_)));
    }

    #[test]
    fn dual_channels_trip_identifies_suspect_with_ref() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 1);
        let mut cmp = DualChannelComparatorExt::new(cfg);
        // ch_a = 10.0 (correct), ch_b = 20.0 (faulty), ref = 10.0
        let result = cmp.compare_with_ref(10.0_f64, 20.0, 10.0);
        assert!(cmp.is_tripped());
        assert_eq!(cmp.suspect_channel(), 1); // ch_b is farther from reference
        assert!(matches!(result, CompareResult::Faulty(1)));
    }

    #[test]
    fn dual_reset_clears_state() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 1);
        let mut cmp = DualChannelComparatorExt::new(cfg);
        cmp.compare(0.0, 10.0); // trip immediately
        assert!(cmp.is_tripped());
        cmp.reset();
        assert!(!cmp.is_tripped());
        assert_eq!(cmp.divergence_count(), 0);
        assert!(matches!(cmp.compare(5.0, 5.0), CompareResult::Agree(_)));
    }

    #[test]
    fn divergence_count_resets_on_agreement() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 5);
        let mut cmp = DualChannelComparatorExt::new(cfg);
        cmp.compare(0.0, 10.0); // diverge: count=1
        cmp.compare(0.0, 10.0); // diverge: count=2
        cmp.compare(5.0, 5.0); // agree:   count=0
        assert_eq!(cmp.divergence_count(), 0);
        assert!(!cmp.is_tripped());
    }

    // ── TripleModularRedundancy ─────────────────────────────────────────────

    #[test]
    fn tmr_all_channels_agree() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 3);
        let mut tmr = TripleModularRedundancy::new(cfg);
        let result = tmr.vote(5.0, 5.05, 4.98).unwrap();
        assert!(result.outlier_channel.is_none());
        // Median of [5.0, 5.05, 4.98] = 5.0
        assert!((result.voted_value - 5.0).abs() < 1e-9);
    }

    #[test]
    fn tmr_identifies_outlier_channel() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 3);
        let mut tmr = TripleModularRedundancy::new(cfg);
        // Channel C = 100.0 is the outlier
        let result = tmr.vote(5.0_f64, 5.05, 100.0).unwrap();
        assert_eq!(result.outlier_channel, Some(2)); // channel C (index 2)
                                                     // Median = 5.05
        assert!((result.voted_value - 5.05).abs() < 1e-9);
    }

    #[test]
    fn tmr_outlier_a_identified() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 3);
        let mut tmr = TripleModularRedundancy::new(cfg);
        // Channel A = 100.0 is the outlier
        let result = tmr.vote(100.0_f64, 5.0, 5.05).unwrap();
        assert_eq!(result.outlier_channel, Some(0)); // channel A
    }

    #[test]
    fn tmr_channel_faulted_after_repeated_outliers() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 3);
        let mut tmr = TripleModularRedundancy::new(cfg);
        // Channel C consistently outlying
        for _ in 0..3 {
            let _ = tmr.vote(5.0_f64, 5.05, 100.0);
        }
        assert!(tmr.is_channel_faulted(2));
        assert!(!tmr.is_channel_faulted(0));
        assert!(!tmr.is_channel_faulted(1));
    }

    #[test]
    fn tmr_majority_lost_when_two_channels_faulted() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 1);
        let mut tmr = TripleModularRedundancy::new(cfg);
        // Fault channel A
        let _ = tmr.vote(100.0_f64, 5.0, 5.0);
        assert!(tmr.is_channel_faulted(0));
        // Fault channel B by making it the outlier
        let _ = tmr.vote(5.0_f64, 100.0, 5.0);
        assert!(tmr.is_channel_faulted(1));
        // Now two channels faulted — majority lost
        let result = tmr.vote(5.0_f64, 5.0, 5.0);
        assert_eq!(result, Err(TmrError::MajorityLost));
    }

    #[test]
    fn tmr_reset_clears_faults() {
        let cfg = ComparatorConfig::new(0.1_f64, 0.01_f64, 1);
        let mut tmr = TripleModularRedundancy::new(cfg);
        let _ = tmr.vote(100.0_f64, 5.0, 5.0); // fault A
        assert!(tmr.is_channel_faulted(0));
        tmr.reset();
        assert!(!tmr.is_channel_faulted(0));
        assert_eq!(tmr.outlier_counts(), [0; 3]);
    }

    #[test]
    fn median3_correctness() {
        assert_eq!(median3(1.0_f64, 2.0, 3.0), 2.0);
        assert_eq!(median3(3.0_f64, 1.0, 2.0), 2.0);
        assert_eq!(median3(2.0_f64, 3.0, 1.0), 2.0);
        assert_eq!(median3(5.0_f64, 5.0, 5.0), 5.0);
        assert_eq!(median3(1.0_f64, 1.0, 100.0), 1.0);
    }
}