Skip to main content

Pid

Struct Pid 

Source
pub struct Pid { /* private fields */ }
Expand description

Drives a measured value to a target by blending proportional, integral, and derivative terms.

This is the workhorse continuous controller behind “keep it here”: hold a heater at a temperature, a pump at a pressure, a motor at a speed. It sums three responses to the error (target minus measurement): the proportional term reacts to the error now, the integral term removes the steady offset the proportional term leaves behind, and the derivative term damps overshoot by reacting to how fast the error is changing. The gains kp, ki, and kd weight them. The output is clamped to a configurable range, and the integral is held back from winding up past that range while the output is saturated, the standard clamping anti-windup.

For the simplest on/off case (a fridge, a tank pump) reach for Thermostat instead; a PID is for a smooth, proportional actuator.

§Examples

use pamoja_kit::Pid;

// Proportional-only: the command is the gain times the error.
let mut pid = Pid::new(2.0, 0.0, 0.0);
assert_eq!(pid.update(10.0, 7.0, 1.0), 6.0); // error 3 times kp 2

Implementations§

Source§

impl Pid

Source

pub fn new(kp: f32, ki: f32, kd: f32) -> Self

Creates a PID controller with the given gains and no output limit.

§Arguments
  • kp - proportional gain.
  • ki - integral gain.
  • kd - derivative gain.
§Returns

A controller with a cleared history and unbounded output.

Source

pub fn with_limits(self, min: f32, max: f32) -> Self

Limits the output to [min, max], also bounding the integral so it cannot wind up beyond the range while the output is saturated.

§Arguments
  • min - the lowest output.
  • max - the highest output. If max is below min the two are swapped.
§Returns

The controller, for chaining after new.

Source

pub fn update(&mut self, setpoint: f32, measurement: f32, dt: f32) -> f32

Computes the control output for one time step.

§Arguments
  • setpoint - the target value.
  • measurement - the latest measured value.
  • dt - the time since the previous update, in the unit ki and kd assume. A value at or below zero skips the integral and derivative updates.
§Returns

The control output, clamped to the configured limits.

Source

pub fn reset(&mut self)

Clears the integral and derivative history.

Trait Implementations§

Source§

impl Clone for Pid

Source§

fn clone(&self) -> Pid

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Pid

Source§

impl Debug for Pid

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Pid

§

impl RefUnwindSafe for Pid

§

impl Send for Pid

§

impl Sync for Pid

§

impl Unpin for Pid

§

impl UnsafeUnpin for Pid

§

impl UnwindSafe for Pid

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.