idsp::iir

Struct Pid

source
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>

source

pub fn period(&mut self, period: T) -> &mut Self

Sample period

§Arguments
  • period: Sample period in some units, e.g. SI seconds
source

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) units
  • input are input (x) units
  • time are sample period units, e.g. SI seconds
  • order 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 control
  • gain: Gain value
source

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 gain
  • limit: Gain limit
source

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<T: Clone> Clone for Pid<T>

source§

fn clone(&self) -> Pid<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Pid<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Float> Default for Pid<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

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>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T: PartialEq> PartialEq for Pid<T>

source§

fn eq(&self, other: &Pid<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd> PartialOrd for Pid<T>

source§

fn partial_cmp(&self, other: &Pid<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T> Serialize for Pid<T>
where T: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T: Copy> Copy for Pid<T>

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,