Trait advanced_pid::PidController

source ·
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§

source

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);
source

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();
update_controller(&mut controller, 0.0);

fn update_controller(controller: &mut impl PidController, actual: f32) -> f32 {
   controller.update(1.0, actual, 0.1)
}

Provided Methods§

source

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)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    let mut pid = VelPid::default();
    let config = PidConfig::new(0.8, 0.3, 0.2).with_limits(-1.2, 1.2);
    pid.reset_config(config);

    let target = 1.0;
    let mut actual = 0.0;

    let mut pre = Instant::now();
    loop {
        let now = Instant::now();
        let dt = now - pre;

        if dt > std::time::Duration::from_secs(1) {
            let output = pid.update(target, actual, dt.as_secs_f32());
            actual += (output - actual) / 8.0;
            println!("{:5.2}\t{:5.2}\t{:?}", actual, output, dt);
            pre = now;
        }
    }
}

Object Safety§

This trait is not object safe.

Implementors§