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
//! A small PID controller library.
//!
//! This crate implements the classic independent PID formulation.
//!
//! # Introduction
//!
//! PID controllers are an integral part of control systems, and provide a way to
//! perform error correction. It's used to control things like throughput or
//! resource allocation: as the resource approaches capacity, the returned
//! correction decreases. And because it is aware of a time factor, it can deal
//! with rapid changes as well.
//!
//! # Loop Tuning
//!
//! However PID controllers are not a silver bullet: they are a tool in a wider
//! toolbox. To maximally benefit from them they need to be tuned to the
//! workload. This is done through three parameters: `proportional_gain`,
//! `integral_gain` and `derivative_gain`. Automated algorithms exist to tune
//! these parameters based on sample workloads, but those are out of scope for
//! this crate.
//!
//! [Read more on loop tuning](https://en.wikipedia.org/wiki/PID_controller#Loop_tuning).
//!
//! # No-std support
//!
//! `#[no_std]` support can be enabled by disabling the default crate-level
//! features. This disables the `Controller::update` method which automatically
//! calculates the time elapsed. Instead use the `Controller::update_elapsed`
//! method which takes an externally calculated `Duration`.
//!
//! # Examples
//!
//! ```no_run
//! use pid_lite::Controller;
//! use std::thread;
//! use std::time::Duration;
//!
//! let target = 80.0;
//! let mut controller = Controller::new(target, 0.25, 0.01, 0.01);
//!
//! loop {
//!     let correction = controller.update(measure());
//!     apply_correction(correction);
//!     thread::sleep(Duration::from_secs(1));
//! }
//! # fn measure() -> f64 { todo!() }
//! # fn apply_correction(_: f64) { todo!() }
//! ```

#![forbid(unsafe_code)]
#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs, future_incompatible, unreachable_pub, rust_2018_idioms)]

use core::time::Duration;
#[cfg(feature = "std")]
use std::time::Instant;

/// PID controller
///
/// The `target` param sets the value we want to reach. The
/// `proportional_gain`, `integral_gain` and `derivative_gain` parameters are all
/// tuning parameters.
///
/// # Examples
///
/// ```no_run
/// use pid_lite::Controller;
/// use std::thread;
/// use std::time::Duration;
///
/// let target = 80.0;
/// let mut controller = Controller::new(target, 0.5, 0.1, 0.2);
///
/// loop {
///     let correction = controller.update(measure());
///     apply_correction(correction);
///     thread::sleep(Duration::from_secs(1));
/// }
/// # fn measure() -> f64 { todo!() }
/// # fn apply_correction(_: f64) { todo!() }
/// ```
#[derive(Debug)]
pub struct Controller {
    target: f64,

    proportional_gain: f64,
    integral_gain: f64,
    derivative_gain: f64,

    error_sum: f64,
    last_error: f64,
    #[cfg(feature = "std")]
    last_instant: Option<Instant>,
}

impl Controller {
    /// Create a new instance of `Controller`.
    ///
    /// # Examples
    ///
    /// ```
    /// # #![allow(unused_assignments)]
    /// use pid_lite::Controller;
    ///
    /// let target = 80.0;
    /// let mut controller = Controller::new(target, 0.20, 0.02, 0.04);
    /// ```
    pub const fn new(
        target: f64,
        proportional_gain: f64,
        integral_gain: f64,
        derivative_gain: f64,
    ) -> Self {
        Self {
            target,
            proportional_gain,
            integral_gain,
            derivative_gain,
            error_sum: 0.0,
            last_error: 0.0,
            #[cfg(feature = "std")]
            last_instant: None,
        }
    }

    /// Get the target.
    ///
    /// # Examples
    ///
    /// ```
    /// # #![allow(unused_assignments)]
    /// use pid_lite::Controller;
    ///
    /// let target = 80.0;
    /// let mut controller = Controller::new(target, 0.20, 0.02, 0.04);
    /// assert_eq!(controller.target(), 80.0);
    /// ```
    pub const fn target(&self) -> f64 {
        self.target
    }

    /// Set the target.
    ///
    /// # Examples
    ///
    /// ```
    /// # #![allow(unused_assignments)]
    /// use pid_lite::Controller;
    ///
    /// let target = 80.0;
    /// let mut controller = Controller::new(target, 0.20, 0.02, 0.04);
    /// controller.set_target(60.0);
    /// assert_eq!(controller.target(), 60.0);
    /// ```
    pub fn set_target(&mut self, target: f64) {
        self.target = target;
    }

    /// Push an entry into the controller.
    ///
    /// # Examples
    ///
    /// ```
    /// # #![allow(unused_assignments)]
    /// use pid_lite::Controller;
    ///
    /// let target = 80.0;
    /// let mut controller = Controller::new(target, 0.0, 0.0, 0.0);
    /// assert_eq!(controller.update(60.0), 0.0);
    /// ```
    ///
    /// # Panics
    ///
    /// This function may panic if the `time_delta` in millis no longer fits in
    /// an `f64`. This limit can be encountered when the PID controller is updated on the scale of
    /// hours, rather than on the scale of minutes to milliseconds.
    #[cfg(feature = "std")]
    #[must_use = "A PID controller does nothing if the correction is not applied"]
    pub fn update(&mut self, current_value: f64) -> f64 {
        let now = Instant::now();
        let elapsed = match self.last_instant {
            Some(last_time) => now.duration_since(last_time),
            None => Duration::from_millis(1),
        };
        self.last_instant = Some(now);
        self.update_elapsed(current_value, elapsed)
    }

    /// Push an entry into the controller with a time delta since the last update.
    ///
    /// The `time_delta` value will be rounded down to the closest millisecond
    /// with a minimum of 1 millisecond.
    ///
    /// # Examples
    ///
    /// ```
    /// # #![allow(unused_assignments)]
    /// use pid_lite::Controller;
    /// use std::time::Duration;
    ///
    /// let target = 80.0;
    /// let mut controller = Controller::new(target, 0.5, 0.1, 0.2);
    /// let dur = Duration::from_millis(2);
    /// assert_eq!(controller.update_elapsed(60.0, dur), 16.0);
    /// ```
    ///
    /// # Panics
    ///
    /// This function may panic if the `time_delta` in millis no longer fits in
    /// an `f64`. This limit can be encountered when the PID controller is updated on the scale of
    /// hours, rather than on the scale of minutes to milliseconds.
    #[must_use = "A PID controller does nothing if the correction is not applied"]
    pub fn update_elapsed(&mut self, current_value: f64, elapsed: Duration) -> f64 {
        let elapsed = (elapsed.as_millis() as f64).min(1.0);

        let error = self.target - current_value;
        let error_delta = (error - self.last_error) / elapsed;
        self.error_sum = self.error_sum + error * elapsed;
        self.last_error = error;

        let p = self.proportional_gain * error;
        let i = self.integral_gain * self.error_sum;
        let d = self.derivative_gain * error_delta;

        p + i + d
    }

    /// Reset the internal state.
    ///
    /// # Examples
    ///
    /// ```
    /// # #![allow(unused_assignments)]
    /// use pid_lite::Controller;
    /// use std::time::Duration;
    ///
    /// let target = 80.0;
    /// let mut controller = Controller::new(target, 0.0, 0.0, 0.0);
    /// let dur = Duration::from_secs(2);
    /// let correction = controller.update_elapsed(60.0, dur);
    ///
    /// controller.reset();
    /// ```
    pub fn reset(&mut self) {
        self.reset_inner();
    }

    #[cfg(feature = "std")]
    fn reset_inner(&mut self) {
        self.error_sum = 0.0;
        self.last_error = 0.0;
        self.last_instant = None;
    }

    #[cfg(not(feature = "std"))]
    pub fn reset_inner(&mut self) {
        self.error_sum = 0.0;
        self.last_error = 0.0;
    }
}

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

    #[test]
    fn base_correction() {
        let target = 80.0;
        let mut controller = Controller::new(target, 0.5, 0.1, 0.2);
        let dur = Duration::from_millis(2);
        assert_eq!(controller.update_elapsed(60.0, dur), 16.0);
    }

    #[test]
    #[cfg(feature = "std")]
    fn no_correction() {
        let target = 80.0;
        let mut controller = Controller::new(target, 0.0, 0.0, 0.0);
        assert_eq!(controller.update(60.0), 0.0);
    }
}