Skip to main content

Pid

Struct Pid 

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

Discrete-time PID controller with anti-windup and derivative filtering.

Uses derivative-on-measurement to avoid derivative kick on setpoint changes, trapezoidal integration for the integral term, and optional first-order low-pass filtering on the derivative term.

Anti-windup is provided via back-calculation: when output saturates, the integrator is corrected to prevent excessive windup.

§Example

use numeris::control::Pid;

// PID controller running at 100 Hz
let mut pid = Pid::new(1.0_f64, 0.5, 0.1, 0.01)
    .with_output_limits(-10.0, 10.0)
    .with_derivative_filter(0.01);

// Simulate a few ticks
let setpoint = 5.0;
let measurement = 0.0;
let output = pid.tick(setpoint, measurement);
assert!(output > 0.0); // positive correction to reach setpoint

Implementations§

Source§

impl<T: FloatScalar> Pid<T>

Source

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

Create a new PID controller with the given gains and time step.

§Panics

Panics if dt <= 0 or dt is not finite.

§Example
use numeris::control::Pid;

let pid = Pid::new(1.0_f64, 0.1, 0.05, 0.01);
assert_eq!(pid.gains(), (1.0, 0.1, 0.05));
Source

pub fn with_output_limits(self, min: T, max: T) -> Self

Set output clamping limits. Returns self for chaining.

§Panics

Panics if min >= max.

§Example
use numeris::control::Pid;

let pid = Pid::new(1.0_f64, 0.0, 0.0, 0.01)
    .with_output_limits(-5.0, 5.0);
Source

pub fn with_derivative_filter(self, tau: T) -> Self

Set derivative low-pass filter time constant. Returns self for chaining.

When tau > 0, the derivative term is filtered through a first-order IIR with alpha = dt / (tau + dt), smoothing out noise on the measurement signal. When tau == 0 (the default), no filtering is applied.

§Panics

Panics if tau < 0.

§Example
use numeris::control::Pid;

let pid = Pid::new(1.0_f64, 0.0, 0.5, 0.01)
    .with_derivative_filter(0.02);
Source

pub fn with_back_calculation_gain(self, kb: T) -> Self

Set the anti-windup back-calculation gain. Returns self for chaining.

By default, kb = ki / kp (or ki if kp == 0). Set to zero to disable anti-windup correction entirely.

§Example
use numeris::control::Pid;

let pid = Pid::new(1.0_f64, 0.5, 0.0, 0.01)
    .with_output_limits(-1.0, 1.0)
    .with_back_calculation_gain(2.0);
Source

pub fn tick(&mut self, setpoint: T, measurement: T) -> T

Process one time step and return the control output.

setpoint is the desired value; measurement is the current process value.

On the first tick after construction or reset(), the derivative and trapezoidal integration terms are zeroed to avoid startup transients.

§Example
use numeris::control::Pid;

let mut pid = Pid::new(2.0_f64, 0.0, 0.0, 0.01);
let u = pid.tick(10.0, 3.0); // error = 7, output = 2 * 7 = 14
assert!((u - 14.0).abs() < 1e-12);
Source

pub fn reset(&mut self)

Reset all internal state (integral, derivative, initialization flag).

Configuration (gains, limits, filter constants) is preserved.

§Example
use numeris::control::Pid;

let mut pid = Pid::new(1.0_f64, 1.0, 0.0, 0.01);
pid.tick(1.0, 0.0);
pid.reset();
assert_eq!(pid.integral(), 0.0);
Source

pub fn gains(&self) -> (T, T, T)

Return the current (kp, ki, kd) gains.

Source

pub fn set_gains(&mut self, kp: T, ki: T, kd: T)

Update the PID gains at runtime.

Does not reset internal state — the integrator and derivative filter continue from their current values.

Source

pub fn integral(&self) -> T

Return the current integrator value.

Source

pub fn set_integral(&mut self, value: T)

Manually set the integrator value.

Useful for bumpless transfer when switching between manual and automatic control modes.

Trait Implementations§

Source§

impl<T: Clone> Clone for Pid<T>

Source§

fn clone(&self) -> Pid<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Pid<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Copy> Copy for Pid<T>

Auto Trait Implementations§

§

impl<T> Freeze for Pid<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Pid<T>
where T: RefUnwindSafe,

§

impl<T> Send for Pid<T>
where T: Send,

§

impl<T> Sync for Pid<T>
where T: Sync,

§

impl<T> Unpin for Pid<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Pid<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Pid<T>
where T: UnwindSafe,

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.