pub trait PidController {
// Required methods
fn new(config: PidConfig) -> Self;
fn update(&mut self, set_point: f32, actual: f32, dt: f32) -> f32;
// Provided method
fn reset_config(&mut self, config: PidConfig)
where Self: Sized { ... }
}
Expand description
PidController
is a trait that provides a standard interface for PID controllers.
It provides methods for creating a new controller Self::new()
, updating the controller Self::update()
, and resetting the controller’s configuration Self::reset_config()
.
Required Methods§
Sourcefn new(config: PidConfig) -> Self
fn new(config: PidConfig) -> Self
Creates a new controller with the specified configuration.
use advanced_pid::{prelude::*, Pid, PidConfig};
let config = PidConfig::new(1.0, 0.3, 0.1);
let controller = Pid::new(config);
Sourcefn update(&mut self, set_point: f32, actual: f32, dt: f32) -> f32
fn update(&mut self, set_point: f32, actual: f32, dt: f32) -> f32
Updates the controller with the specified set point, actual value, and time delta. Returns the controller output.
use advanced_pid::{prelude::*, Pid};
let mut controller = Pid::default();
let output = controller.update(1.0, 0.0, 0.1);
Provided Methods§
Sourcefn reset_config(&mut self, config: PidConfig)where
Self: Sized,
fn reset_config(&mut self, config: PidConfig)where
Self: Sized,
Resets the controller’s configuration to the specified configuration.
use advanced_pid::{prelude::*, Pid, PidConfig};
let mut controller = Pid::default();
let config = PidConfig::new(1.0, 0.3, 0.1);
controller.reset_config(config);
Examples found in repository?
examples/simulation_with_instant.rs (line 8)
5fn main() {
6 let mut pid = VelPid::default();
7 let config = PidConfig::new(0.8, 0.3, 0.2).with_limits(-1.2, 1.2);
8 pid.reset_config(config);
9
10 let target = 1.0;
11 let mut actual = 0.0;
12
13 let mut pre = Instant::now();
14 loop {
15 let now = Instant::now();
16 let duration = pre.elapsed();
17 if duration > Duration::from_secs(1) {
18 let sec = as_secs(duration);
19 let output = pid.update(target, actual, sec);
20 actual += (output - actual) / 8.0;
21 println!("{:5.2}\t{:5.2}\t{:?}", actual, output, duration);
22 pre = now;
23 }
24 }
25}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.