PID Controller for Rust
A proportional-integral-derivative (PID) controller.
Features
- Visibility into individual contribution of P, I, and D terms which often need to be logged for later analysis and parameter tuning.
- Output limits on a per term basis.
- Three-term control output limit.
- Mitigation of integral windup using integral term limit.
- Mitigation of derivative kick by using the derivative of the measurement rather than the derivative of the error.
- On-the-fly changes to
setpoint
/kp
/ki
/kd
.- Mitigation of output jumps when changing
ki
by storing the integration ofe(t) * ki(t)
rather than onlye(t)
.
- Mitigation of output jumps when changing
- Generic float type parameter to support
f32
orf64
. - Support for
no_std
environments, such as embedded systems. - Optional support for Serde. Enable the
serde
Cargo feature, if you needPid
to implementSerialize
/Deserialize
.
Example
extern crate pid;
use Pid;
Assumptions
- Measurements occur at equal spacing. (
t(i) = t(i-1) + C
) - Output limits per term are symmetric around 0 (
-limit <= term <= limit
).
Formulation
There are several different formulations of PID controllers. This library uses the independent form:
where:
- C(t) = control output, the output to the actuator.
- P(t) = process variable, the measured value.
- e(t) = error = S(t) - P(t)
- S(t) = set point, the desired target for the process variable.
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.
Todo
- Helper for (auto-)tuning by detecting frequency & amplitude of oscillations.