pub struct PidController<F, const NUM_SAMPLES: usize>
where F: Fn(Vec3<f32>, Vec3<f32>) -> f32,
{ pub kp: f32, pub ki: f32, pub kd: f32, pub sp: Vec3<f32>, pv_samples: [(f64, Vec3<f32>); NUM_SAMPLES], pv_idx: usize, integral_error: f64, e: F, }
Expand description

PID controllers are used for automatically adapting nonlinear controls (like buoyancy for airships) to target specific outcomes (i.e. a specific height)

Fields§

§kp: f32

The coefficient of the proportional term

§ki: f32

The coefficient of the integral term

§kd: f32

The coefficient of the derivative term

§sp: Vec3<f32>

The setpoint that the process has as its goal

§pv_samples: [(f64, Vec3<f32>); NUM_SAMPLES]§pv_idx: usize§integral_error: f64§e: F

Implementations§

§

impl<F, const NUM_SAMPLES: usize> PidController<F, NUM_SAMPLES>
where F: Fn(Vec3<f32>, Vec3<f32>) -> f32,

pub fn new( kp: f32, ki: f32, kd: f32, sp: Vec3<f32>, time: f64, e: F, ) -> PidController<F, NUM_SAMPLES>

Constructs a PidController with the specified weights, setpoint, starting time, and error function

pub fn add_measurement(&mut self, time: f64, pv: Vec3<f32>)

Adds a measurement of the process variable to the ringbuffer

pub fn calc_err(&self) -> f32

The amount to set the control variable to is a weighed sum of the proportional error, the integral error, and the derivative error. https://en.wikipedia.org/wiki/PID_controller#Mathematical_form

pub fn proportional_err(&self) -> f32

The proportional error is the error function applied to the set point and the most recent process variable measurement

pub fn integral_err(&self) -> f32

The integral error is the error function integrated over all previous values, updated per point. The trapezoid rule for numerical integration was chosen because it’s fairly easy to calculate and sufficiently accurate. https://en.wikipedia.org/wiki/Trapezoidal_rule#Uniform_grid

pub fn derivative_err(&self) -> f32

The derivative error is the numerical derivative of the error function based on the most recent 2 samples. Using more than 2 samples might improve the accuracy of the estimate of the derivative, but it would be an estimate of the derivative error further in the past. https://en.wikipedia.org/wiki/Numerical_differentiation#Finite_differences

Trait Implementations§

§

impl<F, const NUM_SAMPLES: usize> Clone for PidController<F, NUM_SAMPLES>
where F: Clone + Fn(Vec3<f32>, Vec3<f32>) -> f32,

§

fn clone(&self) -> PidController<F, NUM_SAMPLES>

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<F, const NUM_SAMPLES: usize> Debug for PidController<F, NUM_SAMPLES>
where F: Fn(Vec3<f32>, Vec3<f32>) -> f32,

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<F, const NUM_SAMPLES: usize> Freeze for PidController<F, NUM_SAMPLES>
where F: Freeze,

§

impl<F, const NUM_SAMPLES: usize> RefUnwindSafe for PidController<F, NUM_SAMPLES>
where F: RefUnwindSafe,

§

impl<F, const NUM_SAMPLES: usize> Send for PidController<F, NUM_SAMPLES>
where F: Send,

§

impl<F, const NUM_SAMPLES: usize> Sync for PidController<F, NUM_SAMPLES>
where F: Sync,

§

impl<F, const NUM_SAMPLES: usize> Unpin for PidController<F, NUM_SAMPLES>
where F: Unpin,

§

impl<F, const NUM_SAMPLES: usize> UnwindSafe for PidController<F, NUM_SAMPLES>
where F: 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§

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

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

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> Same for T

§

type Output = T

Should always be Self
§

impl<Context> SubContext<Context> for Context

§

fn sub_context(self) -> Context

source§

impl<T> ToOwned for T
where 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
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Event for T
where T: Send + Sync + 'static,

§

impl<T> Resource for T
where T: Any + Send + Sync,

§

impl<T> Storable for T
where T: Send + Sync + 'static,