pub struct PidController<F, const NUM_SAMPLES: usize>{
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>
impl<F, const NUM_SAMPLES: usize> PidController<F, NUM_SAMPLES>
pub fn new(
kp: f32,
ki: f32,
kd: f32,
sp: Vec3<f32>,
time: f64,
e: F,
) -> PidController<F, NUM_SAMPLES>
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>)
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
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
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
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
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>
impl<F, const NUM_SAMPLES: usize> Clone for PidController<F, NUM_SAMPLES>
§fn clone(&self) -> PidController<F, NUM_SAMPLES>
fn clone(&self) -> PidController<F, NUM_SAMPLES>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto 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> 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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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