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 setpointImplementations§
Source§impl<T: FloatScalar> Pid<T>
impl<T: FloatScalar> Pid<T>
Sourcepub fn with_output_limits(self, min: T, max: T) -> Self
pub fn with_output_limits(self, min: T, max: T) -> Self
Sourcepub fn with_derivative_filter(self, tau: T) -> Self
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);Sourcepub fn with_back_calculation_gain(self, kb: T) -> Self
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);Sourcepub fn tick(&mut self, setpoint: T, measurement: T) -> T
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);Sourcepub fn reset(&mut self)
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);Sourcepub fn set_gains(&mut self, kp: T, ki: T, kd: T)
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.
Sourcepub fn set_integral(&mut self, value: T)
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.