Struct comfy_wgpu::Pid

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: T

Ideal setpoint to strive for.

§output_limit: T

Defines the overall output filter limit.

§kp: T

Proportional gain.

§ki: T

Integral gain.

§kd: T

Derivative gain.

§p_limit: T

Limiter for the proportional term: -p_limit <= P <= p_limit.

§i_limit: T

Limiter for the integral term: -i_limit <= I <= i_limit.

§d_limit: T

Limiter for the derivative term: -d_limit <= D <= d_limit.

Implementations§

§

impl<T> Pid<T>where T: FloatCore,

pub fn new(setpoint: impl Into<T>, output_limit: impl Into<T>) -> Pid<T>

Creates a new controller with the target setpoint and the output limit

To set your P, I, and D terms into this controller, please use the following builder methods:

pub fn p(&mut self, gain: impl Into<T>, limit: impl Into<T>) -> &mut Pid<T>

Sets the Self::p term for this controller.

pub fn i(&mut self, gain: impl Into<T>, limit: impl Into<T>) -> &mut Pid<T>

Sets the Self::i term for this controller.

pub fn d(&mut self, gain: impl Into<T>, limit: impl Into<T>) -> &mut Pid<T>

Sets the Self::d term for this controller.

pub fn setpoint(&mut self, setpoint: impl Into<T>) -> &mut Pid<T>

Sets the Pid::setpoint to target for this controller.

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().

pub fn reset_integral_term(&mut self)

Resets the integral term back to zero, this may drastically change the control output.

Trait Implementations§

§

impl<T> Clone for Pid<T>where T: Clone + FloatCore,

§

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
§

impl<T> Debug for Pid<T>where T: Debug + FloatCore,

§

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

Formats the value using the given formatter. Read more
§

impl<T> Ord for Pid<T>where T: Ord + FloatCore,

§

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

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
§

impl<T> PartialEq<Pid<T>> for Pid<T>where T: PartialEq<T> + FloatCore,

§

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

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

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

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

impl<T> PartialOrd<Pid<T>> for Pid<T>where T: PartialOrd<T> + FloatCore,

§

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

This method 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

This method 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

This method 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

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

impl<T> Copy for Pid<T>where T: Copy + FloatCore,

§

impl<T> Eq for Pid<T>where T: Eq + FloatCore,

§

impl<T> StructuralEq for Pid<T>where T: FloatCore,

§

impl<T> StructuralPartialEq for Pid<T>where T: FloatCore,

Auto Trait Implementations§

§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

impl<T, U> Into<U> for Twhere 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.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

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

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> Any for Twhere T: Any,

§

impl<T> CloneAny for Twhere T: Any + Clone,

§

impl<T> Component for Twhere T: Send + Sync + 'static,

§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,

source§

impl<T> SerializableAny for Twhere T: 'static + Any + Clone + Send + Sync,