Skip to content

Commit

Permalink
Video pwm (#2)
Browse files Browse the repository at this point in the history
* creating video version

* cleanup
  • Loading branch information
darioalessandro authored May 13, 2024
1 parent 2825617 commit 1cdee13
Showing 1 changed file with 29 additions and 40 deletions.
69 changes: 29 additions & 40 deletions control-dc-motor/src/main.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 1cdee13

Please sign in to comment.