| Crates.io | pid |
| lib.rs | pid |
| version | 4.1.0 |
| created_at | 2018-07-24 02:08:29.540761+00 |
| updated_at | 2025-03-28 00:46:48.111009+00 |
| description | A PID controller. |
| homepage | |
| repository | https://github.com/braincore/pid-rs |
| max_upload_size | |
| id | 75712 |
| size | 37,374 |
A proportional-integral-derivative (PID) controller.
setpoint/kp/ki/kd.
ki by storing the integration of
e(t) * ki(t) rather than only e(t).f32 or f64.no_std environments, such as embedded systems.serde Cargo feature, if you need Pid to implement
Serialize/Deserialize.use pid::Pid;
// Create a new proportional-only PID controller with a setpoint of 15
let mut pid = Pid::new(15.0, 100.0);
pid.p(10.0, 100.0);
// Input a measurement with an error of 5.0 from our setpoint
let output = pid.next_control_output(10.0);
// Show that the error is correct by multiplying by our kp
assert_eq!(output.output, 50.0); // <--
assert_eq!(output.p, 50.0);
// It won't change on repeat; the controller is proportional-only
let output = pid.next_control_output(10.0);
assert_eq!(output.output, 50.0); // <--
assert_eq!(output.p, 50.0);
// Add a new integral term to the controller and input again
pid.i(1.0, 100.0);
let output = pid.next_control_output(10.0);
// Now that the integral makes the controller stateful, it will change
assert_eq!(output.output, 55.0); // <--
assert_eq!(output.p, 50.0);
assert_eq!(output.i, 5.0);
// Add our final derivative term and match our setpoint target
pid.d(2.0, 100.0);
let output = pid.next_control_output(15.0);
// The output will now say to go down due to the derivative
assert_eq!(output.output, -5.0); // <--
assert_eq!(output.p, 0.0);
assert_eq!(output.i, 5.0);
assert_eq!(output.d, -10.0);
t(i) = t(i-1) + C)-limit <= term <= limit).There are several different formulations of PID controllers. This library uses the independent form:
C(t) = K_p \cdot e(t) + K_i \cdot \int{e(t)dt} - K_d \cdot \frac{dP(t)}{dt}
Where:
kp/ki/kd can be changed during operation and can therefore be a function
of time.
If you're interested in the dependent form, add your own logic that computes
kp/ki/kd using dead time, time constant, kc, or whatever else.
Licensed under either at your discretion: