idsp::iir

Struct PidBuilder

Source
pub struct PidBuilder<T> { /* private fields */ }
Expand description

PID controller builder

Builds Biquad from action gains, gain limits, input offset and output limits.

let b: Biquad<f32> = PidBuilder::default()
    .period(1e-3)
    .gain(Action::I, 1e-3)
    .gain(Action::P, 1.0)
    .gain(Action::D, 1e2)
    .limit(Action::I, 1e3)
    .limit(Action::D, 1e1)
    .build()
    .into();

Implementations§

Source§

impl<T: Float> PidBuilder<T>

Source

pub fn order(&mut self, order: Order) -> &mut Self

Feedback term order

§Arguments
  • order: The maximum feedback term order.
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.)

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> = PidBuilder::default()
    .period(tau)
    .gain(Action::I, ki)
    .build()
    .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 PidBuilder::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> = PidBuilder::default()
    .gain(Action::I, 8.0)
    .limit(Action::I, ki_limit)
    .build()
    .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>(&self) -> [C; 5]
where C: Coefficient + AsPrimitive<T>, T: AsPrimitive<C>,

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 unused gain actions or for proportional action are ignored.

let i: Biquad<f32> = PidBuilder::default()
    .gain(Action::P, 3.0)
    .order(Order::P)
    .build()
    .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 PidBuilder<T>

Source§

fn clone(&self) -> PidBuilder<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 PidBuilder<T>

Source§

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

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

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

Source§

fn default() -> Self

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

impl<'de, T> Deserialize<'de> for PidBuilder<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 PidBuilder<T>

Source§

fn eq(&self, other: &PidBuilder<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 PidBuilder<T>

Source§

fn partial_cmp(&self, other: &PidBuilder<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 PidBuilder<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 PidBuilder<T>

Source§

impl<T> StructuralPartialEq for PidBuilder<T>

Auto Trait Implementations§

§

impl<T> Freeze for PidBuilder<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for PidBuilder<T>
where T: RefUnwindSafe,

§

impl<T> Send for PidBuilder<T>
where T: Send,

§

impl<T> Sync for PidBuilder<T>
where T: Sync,

§

impl<T> Unpin for PidBuilder<T>
where T: Unpin,

§

impl<T> UnwindSafe for PidBuilder<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 u8)

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