pub struct Pid<T> { /* private fields */ }
Expand description
PID controller builder
Builds Biquad
from action gains, gain limits, input offset and output limits.
let b: Biquad<f32> = Pid::default()
.period(1e-3)
.gain(Action::Ki, 1e-3)
.gain(Action::Kp, 1.0)
.gain(Action::Kd, 1e2)
.limit(Action::Ki, 1e3)
.limit(Action::Kd, 1e1)
.build()
.unwrap()
.into();
Implementations§
source§impl<T: Float> Pid<T>
impl<T: Float> Pid<T>
sourcepub fn gain(&mut self, action: Action, gain: T) -> &mut Self
pub fn gain(&mut self, action: Action, gain: T) -> &mut Self
Gain for a given action
Gain units are output/input * time.powi(order)
where
output
are output (y
) unitsinput
are input (x
) unitstime
are sample period units, e.g. SI secondsorder
is the action order: the frequency exponent (-1
for integrating,0
for proportional, etc.)
Note that inverse time units correspond to angular frequency units. Gains are accurate in the low frequency limit. Towards Nyquist, the frequency response is warped.
Note that limit signs and gain signs should match.
let tau = 1e-3;
let ki = 1e-4;
let i: Biquad<f32> = Pid::default()
.period(tau)
.gain(Action::Ki, ki)
.build()
.unwrap()
.into();
let x0 = 5.0;
let y0 = i.update(&mut [0.0; 4], x0);
assert!((y0 / (x0 * tau * ki) - 1.0).abs() < 2.0 * f32::EPSILON);
§Arguments
action
: Action to controlgain
: Gain value
sourcepub fn limit(&mut self, action: Action, limit: T) -> &mut Self
pub fn limit(&mut self, action: Action, limit: T) -> &mut Self
Gain limit for a given action
Gain limit units are output/input
. See also Pid::gain()
.
Multiple gains and limits may interact and lead to peaking.
Note that limit signs and gain signs should match and that the default limits are positive infinity.
let ki_limit = 1e3;
let i: Biquad<f32> = Pid::default()
.gain(Action::Ki, 8.0)
.limit(Action::Ki, ki_limit)
.build()
.unwrap()
.into();
let mut xy = [0.0; 4];
let x0 = 5.0;
for _ in 0..1000 {
i.update(&mut xy, x0);
}
let y0 = i.update(&mut xy, x0);
assert!((y0 / (x0 * ki_limit) - 1.0f32).abs() < 1e-3);
§Arguments
action
: Action to limit in gainlimit
: Gain limit
sourcepub fn build<C: Coefficient + AsPrimitive<T>>(&self) -> Result<[C; 5], PidError>where
T: AsPrimitive<C>,
pub fn build<C: Coefficient + AsPrimitive<T>>(&self) -> Result<[C; 5], PidError>where
T: AsPrimitive<C>,
Perform checks, compute coefficients and return Biquad
.
No attempt is made to detect NaNs, non-finite gains, non-positive period, zero gain limits, or gain/limit sign mismatches. These will consequently result in NaNs/infinities, peaking, or notches in the Biquad coefficients.
Gain limits for zero gain actions or for proportional action are ignored.
let i: Biquad<f32> = Pid::default().gain(Action::Kp, 3.0).build().unwrap().into();
assert_eq!(i, Biquad::proportional(3.0));
§Panic
Will panic in debug mode on fixed point coefficient overflow.
Trait Implementations§
source§impl<'de, T> Deserialize<'de> for Pid<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Pid<T>where
T: Deserialize<'de>,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<T: PartialOrd> PartialOrd for Pid<T>
impl<T: PartialOrd> PartialOrd for Pid<T>
impl<T: Copy> Copy for Pid<T>
impl<T> StructuralPartialEq 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> 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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)