Struct pid_lite::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 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.