pub struct Controller { /* private fields */ }
Expand description
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
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));
}
Implementations§
Source§impl Controller
impl Controller
Sourcepub const fn new(
target: f64,
proportional_gain: f64,
integral_gain: f64,
derivative_gain: f64,
) -> Self
pub const fn new( target: f64, proportional_gain: f64, integral_gain: f64, derivative_gain: f64, ) -> Self
Create a new instance of Controller
.
§Examples
use pid_lite::Controller;
let target = 80.0;
let mut controller = Controller::new(target, 0.20, 0.02, 0.04);
Sourcepub const fn target(&self) -> f64
pub const fn target(&self) -> f64
Get the target.
§Examples
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);
Sourcepub fn set_target(&mut self, target: f64)
pub fn set_target(&mut self, target: f64)
Set the target.
§Examples
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);
Sourcepub fn set_proportional_gain(&mut self, proportional_gain: f64)
pub fn set_proportional_gain(&mut self, proportional_gain: f64)
Set the proportional gain
Sourcepub fn set_integral_gain(&mut self, integral_gain: f64)
pub fn set_integral_gain(&mut self, integral_gain: f64)
Set the integral gain
Sourcepub fn set_derivative_gain(&mut self, derivative_gain: f64)
pub fn set_derivative_gain(&mut self, derivative_gain: f64)
Set the derivative gain
Sourcepub fn update(&mut self, current_value: f64) -> f64
pub fn update(&mut self, current_value: f64) -> f64
Push an entry into the controller.
§Examples
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.
Sourcepub fn update_elapsed(&mut self, current_value: f64, elapsed: Duration) -> f64
pub fn update_elapsed(&mut self, current_value: f64, elapsed: Duration) -> f64
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
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.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the internal state.
§Examples
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();