Skip to main content

first_order_temperature/
first_order_temperature.rs

1use ladrc_no_std::{LadrcFirstOrder, LadrcFirstOrderConfig, OutputLimit};
2
3fn main() -> Result<(), ladrc_no_std::ConfigError> {
4    let dt = 0.01;
5    let target_temperature = 55.0;
6
7    let config = LadrcFirstOrderConfig::from_bandwidth(
8        dt, 2.0, // one unit of heater command gives roughly +2 C/s initially
9        0.8, 4.0,
10    )
11    .with_output_limit(OutputLimit::new(0.0, 1.0));
12
13    let mut controller = LadrcFirstOrder::new(config)?;
14    let mut temperature = 20.0;
15    let ambient = 20.0;
16    let mut control = 0.0;
17
18    for step in 0..6_000 {
19        let out = controller.update(target_temperature, temperature);
20        control = out.control;
21
22        let open_window_disturbance = if step > 3_000 { -0.35 } else { 0.0 };
23        let plant_cooling = -0.04 * (temperature - ambient);
24        let plant_heating = 2.0 * control;
25
26        temperature += dt * (plant_cooling + plant_heating + open_window_disturbance);
27    }
28
29    println!("target temperature: {target_temperature:.2} C");
30    println!("final temperature:  {temperature:.2} C");
31    println!("final command:      {:.1} %", control * 100.0);
32
33    Ok(())
34}