Struct Controller

Source
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

Source

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);
Source

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);
Source

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);
Source

pub fn set_proportional_gain(&mut self, proportional_gain: f64)

Set the proportional gain

Source

pub fn set_integral_gain(&mut self, integral_gain: f64)

Set the integral gain

Source

pub fn set_derivative_gain(&mut self, derivative_gain: f64)

Set the derivative gain

Source

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.

Source

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.

Source

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();

Trait Implementations§

Source§

impl Debug for Controller

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.