From 6d0c43e38cd074b18bd2e3fcf4dace0b38fa02a1 Mon Sep 17 00:00:00 2001 From: jpfbastos Date: Wed, 27 Nov 2024 11:31:28 +0000 Subject: [PATCH] fixed typo and added comment before main --- boards/stm32l432kc/src/bin/levitation.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/boards/stm32l432kc/src/bin/levitation.rs b/boards/stm32l432kc/src/bin/levitation.rs index e8bb67f..9271038 100644 --- a/boards/stm32l432kc/src/bin/levitation.rs +++ b/boards/stm32l432kc/src/bin/levitation.rs @@ -32,6 +32,15 @@ const GAIN_CURRENT: PidGain = PidGain{ }; +/* +For the lev control, we need to chain 2 PIDs together and output a PWM signal. The first one takes in a height and +outputs a current, the second one takes in a current and outputs a voltage, which we the use to calculate duty cycle of +the PWM signal. Outside the loop, we initialise the pids and the board pins for PWM. We also need max_duty to represent +our voltage output as a fraction of the max duty cycle. In the loop, there is a timer which measures the time between +the signal being set and us taking readings from the sensors as part of the PID calculations. The PID calculations are +then performed, and the duty cycle is set, and timer restarted. +*/ + #[embassy_executor::main] async fn main(_spawner: Spawner) { @@ -43,21 +52,22 @@ async fn main(_spawner: Spawner) { let green_light = PwmPin::new_ch2(p.PB3, OutputType::PushPull); // TODOLater change to actual pin (this is just for testing) let mut pwm = SimplePwm::new(p.TIM2, None, Some(green_light), None, None, hz(2000), CountingMode::EdgeAlignedUp); // TODOLater change to actual pin (this is just for testing) - pwm.enable(Channel::Ch2); - + let max_duty = pwm.get_max_duty() as f32; + + pwm.enable(Channel::Ch2); let mut time_start = Instant::now().as_micros() as f32; loop { let actual_height = 0.7; // TODOLater we'll get that from a sensor - - let dt = (Instant::now().as_micros() as f32) - time_start; // this gets the timeframe between the last change in the pwm signal for the PID - let target_curent = (pid_height.update(TARGET_HEIGHT, actual_height, dt)).min(MAX_CURRENT); // takes in height -> outputs current target (within boundaries) + let actual_current = 1.0; // TODOLater we'll get that from a sensor + + let dt = (Instant::now().as_micros() as f32) - time_start; // this gets the timeframe between the last change in the pwm signal for the PID - let actual_current = 1.0; // TODOLater we'll get that from a sensor + let target_current = (pid_height.update(TARGET_HEIGHT, actual_height, dt)).min(MAX_CURRENT); // takes in height -> outputs current target (within boundaries) let required_voltage = (pid_current.update(target_current, actual_current, dt)).min(MAX_VOLTAGE); // takes in current -> outputs voltage (within boundaries)