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
//! A tiny `no_std` PID controller library.
//!
//! This crate implements the classic windowed PID loop over abstract data type.
//!
//! # Introduction
//!
//! A proportional-integral-derivative controller is a control loop mechanism
//! employing feedback that is widely used in industrial control systems
//! and a variety of other applications requiring continuously modulated control.
//!
//! # Examples
//!
//! ```no_run
//! use pid_loop::PID;
//!
//! let target = 42.0;
//! let mut controller = PID::<f32, 1>::new(2.0, 0.7, 0.3, 0.0, 0.0);
//! loop {
//!     let correction = controller.next(target, measure());
//!     apply_correction(correction);
//!     sleep();
//! }
//!
//! fn sleep() { todo!() }
//! fn measure() -> f32 { todo!() }
//! fn apply_correction(_: f32) { todo!() }
//! ```

#![no_std]
#![deny(nonstandard_style, future_incompatible, rust_2018_idioms)]
use core::ops::*;

/// PID controller
///
/// # Examples
///
/// ```no_run
/// use pid_loop::PID;
///
/// let target = 42.0;
/// let mut controller = PID::<f64, 1>::new(2.0, 0.7, 0.3, 0.0, 0.0);
/// let correction = controller.next(target, measure());
///
/// fn measure() -> f64 { todo!() }
/// ```
pub struct PID<F, const W: usize> {
    /// Proportional gain.
    pub kp: F,
    /// Integral gain.
    pub ki: F,
    /// Derivative gain.
    pub kd: F,
    /// Feed forward gain
    pub kf: F,
    /// Velocity
    pub kv: F,
    last_sp: F,
    last_error_idx: usize,
    errors: [F; W],
    err_history: F,
}

impl<F, const W: usize> PID<F, W>
where
    F: Default + Add<Output = F> + Sub<Output = F> + Mul<Output = F> + PartialOrd + Copy,
{
    /// Create a new instance of `PID`.
    ///
    /// # Examples
    ///
    /// ```
    /// #![allow(unused_assignments)]
    /// use pid_loop::PID;
    ///
    /// let mut controller = PID::<f32, 1>::new(0.7, 0.034, 0.084, 0.1, 0.0);
    /// ```
    pub fn new(
        kp: impl Into<F>,
        ki: impl Into<F>,
        kd: impl Into<F>,
        kf: impl Into<F>,
        kv: impl Into<F>,
    ) -> Self {
        assert!(W > 0);
        Self {
            kp: kp.into(),
            ki: ki.into(),
            kd: kd.into(),
            kf: kf.into(),
            kv: kv.into(),
            last_sp: F::default(),
            errors: [F::default(); W],
            last_error_idx: usize::default(),
            err_history: F::default(),
        }
    }

    /// Reset controller internal state.
    ///
    /// # Examples
    ///
    /// ```
    /// #![allow(unused_assignments)]
    /// use pid_loop::PID;
    ///
    /// let target = 30.0;
    /// let mut controller = PID::<f32, 1>::new(0.7, 0.034, 0.084, 0.1, 0.0);
    /// controller.next(target, 42.0);
    /// controller.reset();
    ///
    /// ```
    pub fn reset(&mut self) {
        self.last_sp = F::default();
        self.last_error_idx = usize::default();
        self.errors = [F::default(); W];
    }

    /// Push next measurement into the controller and return correction.
    ///
    /// # Examples
    ///
    /// ```
    /// #![allow(unused_assignments)]
    /// use pid_loop::PID;
    ///
    /// let target = 30.0;
    /// let mut controller = PID::<f64, 1>::new(0.7, 0.034, 0.084, 0.1, 0.1);
    /// let correction = controller.next(target, 42.0);
    /// ```
    pub fn next(&mut self, sp: impl Into<F>, fb: impl Into<F>) -> F {
        let sp = sp.into();
        let fb = fb.into();
        let error = sp - fb;

        let error_delta = error - self.errors[self.last_error_idx];
        self.push_error(error);

        let sp_delta = sp - self.last_sp;
        self.last_sp = sp;

        let p = self.kp * error;
        let i = self.ki * self.err_history;
        let d = self.kd * error_delta;
        let f = self.kf * sp_delta;
        let v = self.kv * fb;

        p + i + d + f + v
    }

    fn push_error(&mut self, error: F) {
        self.last_error_idx += 1;
        if self.last_error_idx >= W {
            self.last_error_idx = 0;
        }

        self.err_history = self.err_history - self.errors[self.last_error_idx];
        self.err_history = self.err_history + error;
        self.errors[self.last_error_idx] = error;
    }
}