From 1cdee1303370bebca497bc4a05c3639c9f4b0bac Mon Sep 17 00:00:00 2001 From: Dario A Lencina-Talarico Date: Sun, 12 May 2024 23:50:35 -0400 Subject: [PATCH] Video pwm (#2) * creating video version * cleanup --- control-dc-motor/src/main.rs | 69 +++++++++++++++--------------------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/control-dc-motor/src/main.rs b/control-dc-motor/src/main.rs index a171a6d..abe72ab 100644 --- a/control-dc-motor/src/main.rs +++ b/control-dc-motor/src/main.rs @@ -1,67 +1,56 @@ use gpio_cdev::{Chip, LineRequestFlags}; -const GPIO17: u32 = 17; -const GPIO18: u32 = 18; - -// PWM Min duty cycle in ms -const PWM_FREQUENCY_HZ: f64 = 100.0; +const GPIO5: u32 = 5; +const GPIO6: u32 = 6; enum Direction { Left, Right, } +/** + * This program will turn a motor left and right every 5 seconds. + */ fn main() -> Result<(), gpio_cdev::Error> { - // Open the GPIO chip; usually, it's gpiochip0 for the main GPIO controller on a Raspberry Pi + // 1. Open the GPIO chip; it's gpiochip0 for the main GPIO controller on a Raspberry Pi let mut chip = Chip::new("/dev/gpiochip0")?; - let mut current_direction = Direction::Left; - let mut last_direction_change = std::time::Instant::now(); - let line17 = chip.get_line(GPIO17)?; - let line18 = chip.get_line(GPIO18)?; - + let line5 = chip.get_line(GPIO5)?; + let line6 = chip.get_line(GPIO6)?; // Initially both are off - let line17 = line17.request(LineRequestFlags::OUTPUT, 0, "pwm")?; - let line18 = line18.request(LineRequestFlags::OUTPUT, 0, "pwm")?; + let line5 = line5.request(LineRequestFlags::OUTPUT, 0, "pwm")?; + let line6 = line6.request(LineRequestFlags::OUTPUT, 0, "pwm")?; - let duty_cycle = 99.0; - let time_on_ms = (duty_cycle / 100.0) * (1.0 / PWM_FREQUENCY_HZ) * 1000.0; - let time_off_ms = ((100.0 - duty_cycle) / 100.0) * (1.0 / PWM_FREQUENCY_HZ) * 1000.0; + // 2. Initially the motor is turning left + let mut current_direction = Direction::Left; - println!("Time on: {} ms, Time off: {} ms", time_on_ms, time_off_ms); + // 3. Keep track of the last time we changed direction + let mut last_direction_change = std::time::Instant::now(); loop { - // Set the PWM frequency - // turn it on + // transform direction to GPIO signals match current_direction { Direction::Left => { - line17.set_value(1)?; - line18.set_value(0)?; + line5.set_value(1)?; + line6.set_value(0)?; } Direction::Right => { - line17.set_value(0)?; - line18.set_value(1)?; + line5.set_value(0)?; + line6.set_value(1)?; } } - std::thread::sleep(std::time::Duration::from_millis(time_on_ms as u64)); - // turn it off - line17.set_value(0)?; - line18.set_value(0)?; - std::thread::sleep(std::time::Duration::from_millis(time_off_ms as u64)); - // change direction + if last_direction_change.elapsed().as_secs() >= 5 { - match current_direction { - Direction::Left => { - current_direction = Direction::Right; - } - Direction::Right => { - current_direction = Direction::Left; - } - } + // Turn off the motor + line5.set_value(0)?; + line6.set_value(0)?; + + // Change direction + current_direction = match current_direction { + Direction::Left => Direction::Right, + Direction::Right => Direction::Left + }; last_direction_change = std::time::Instant::now(); - // let the motor stop for a while std::thread::sleep(std::time::Duration::from_secs(1)); } } - - Ok(()) }