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 2Implementations§
Source§impl Pid
impl Pid
Sourcepub fn with_limits(self, min: f32, max: f32) -> Self
pub fn with_limits(self, min: f32, max: f32) -> Self
Sourcepub fn update(&mut self, setpoint: f32, measurement: f32, dt: f32) -> f32
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 unitkiandkdassume. A value at or below zero skips the integral and derivative updates.
§Returns
The control output, clamped to the configured limits.