Struct comfy_core::Pid
source · pub struct Pid<T>where
T: FloatCore,{
pub setpoint: T,
pub output_limit: T,
pub kp: T,
pub ki: T,
pub kd: T,
pub p_limit: T,
pub i_limit: T,
pub d_limit: T,
/* private fields */
}Expand description
Adjustable proportional-integral-derivative (PID) controller.
Examples
This controller provides a builder pattern interface which allows you to pick-and-choose which PID inputs you’d like to use during operation. Here’s what a basic proportional-only controller could look like:
use pid::Pid;
// Create limited controller
let mut p_controller = Pid::new(15.0, 100.0);
p_controller.p(10.0, 100.0);
// Get first output
let p_output = p_controller.next_control_output(400.0);This controller would give you set a proportional controller to 10.0 with a target of 15.0 and an output limit of 100.0 per output iteration. The same controller with a full PID system built in looks like:
use pid::Pid;
// Create full PID controller
let mut full_controller = Pid::new(15.0, 100.0);
full_controller.p(10.0, 100.0).i(4.5, 100.0).d(0.25, 100.0);
// Get first output
let full_output = full_controller.next_control_output(400.0);This next_control_output method is what’s used to input new values into the controller to tell it what the current state of the system is. In the examples above it’s only being used once, but realistically this will be a hot method. Please see ControlOutput for examples of how to handle these outputs; it’s quite straight forward and mirrors the values of this structure in some ways.
The last item of note is that these p, i, and d methods can be used during operation which lets you add and/or modify these controller values if need be.
Fields§
§setpoint: TIdeal setpoint to strive for.
output_limit: TDefines the overall output filter limit.
kp: TProportional gain.
ki: TIntegral gain.
kd: TDerivative gain.
p_limit: TLimiter for the proportional term: -p_limit <= P <= p_limit.
i_limit: TLimiter for the integral term: -i_limit <= I <= i_limit.
d_limit: TLimiter for the derivative term: -d_limit <= D <= d_limit.
Implementations§
source§impl<T> Pid<T>where
T: FloatCore,
impl<T> Pid<T>where T: FloatCore,
sourcepub fn p(&mut self, gain: impl Into<T>, limit: impl Into<T>) -> &mut Pid<T>
pub fn p(&mut self, gain: impl Into<T>, limit: impl Into<T>) -> &mut Pid<T>
Sets the Self::p term for this controller.
sourcepub fn i(&mut self, gain: impl Into<T>, limit: impl Into<T>) -> &mut Pid<T>
pub fn i(&mut self, gain: impl Into<T>, limit: impl Into<T>) -> &mut Pid<T>
Sets the Self::i term for this controller.
sourcepub fn d(&mut self, gain: impl Into<T>, limit: impl Into<T>) -> &mut Pid<T>
pub fn d(&mut self, gain: impl Into<T>, limit: impl Into<T>) -> &mut Pid<T>
Sets the Self::d term for this controller.
sourcepub fn setpoint(&mut self, setpoint: impl Into<T>) -> &mut Pid<T>
pub fn setpoint(&mut self, setpoint: impl Into<T>) -> &mut Pid<T>
Sets the Pid::setpoint to target for this controller.
sourcepub fn next_control_output(&mut self, measurement: T) -> ControlOutput<T>
pub fn next_control_output(&mut self, measurement: T) -> ControlOutput<T>
Given a new measurement, calculates the next control output.
Panics
- If a setpoint has not been set via
update_setpoint().
sourcepub fn reset_integral_term(&mut self)
pub fn reset_integral_term(&mut self)
Resets the integral term back to zero, this may drastically change the control output.
Trait Implementations§
source§impl<T> Ord for Pid<T>where
T: Ord + FloatCore,
impl<T> Ord for Pid<T>where T: Ord + FloatCore,
source§impl<T> PartialEq<Pid<T>> for Pid<T>where
T: PartialEq<T> + FloatCore,
impl<T> PartialEq<Pid<T>> for Pid<T>where T: PartialEq<T> + FloatCore,
source§impl<T> PartialOrd<Pid<T>> for Pid<T>where
T: PartialOrd<T> + FloatCore,
impl<T> PartialOrd<Pid<T>> for Pid<T>where T: PartialOrd<T> + FloatCore,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more