diff --git a/firmware/src/main.rs b/firmware/src/main.rs index ca64956..61eaa93 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -5,21 +5,24 @@ use embassy_executor::Spawner; use embassy_time::Timer; use esp_backtrace as _; use esp_hal::{ - - clock::Clocks, - gpio::{Io, Level, Output}, - mcpwm::PeripheralClockConfig, - timer::timg::TimerGroup, -}; -use esp_println::println; - clock::Clocks, gpio::{Io, Level, Output}, mcpwm::PeripheralClockConfig, timer::timg::TimerGroup }; use esp_println::println; -use esp_wifi::current_millis; +use fugit::RateExtU32; +use esp_hal::mcpwm::McPwm; +use esp_hal::mcpwm::timer::PwmWorkingMode; +use esp_hal::mcpwm::operator::PwmPinConfig; + +#[esp_hal_embassy::main] +async fn main(_spawner: Spawner) { + esp_println::logger::init_logger_from_env(); + let peripherals = esp_hal::init(esp_hal::Config::default()); + let timg0 = TimerGroup::new(peripherals.TIMG0); + esp_hal_embassy::init(timg0.timer0); + + let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - let mut led = Output::new(io.pins.gpio17, Level::Low); let mut led = Output::new(io.pins.gpio18, Level::Low); // add these let motor_hi_pin = io.pins.gpio13; @@ -40,76 +43,67 @@ use esp_wifi::current_millis; PwmPinConfig::UP_ACTIVE_HIGH, ); - let cur_motor_value = 255; - motor_hi.set_timestamp(cur_motor_value); + motor_hi.set_timestamp(180); motor_lo.set_timestamp(0); - - // Toggle LED - led.toggle(); - - if increasing && l_r { - duty_cycle -= step; - println!("fast!"); - - if duty_cycle <= 0 { - increasing = false; - } - motor_hi.set_timestamp(255 - duty_cycle); - motor_lo.set_timestamp(duty_cycle); - } else if !increasing && l_r { - println!("slow"); - duty_cycle += step; - if duty_cycle >= 60 { - increasing = true; - l_r = false + let mut status = 0;//speed up 1 and slowing down 0 + let mut direction = 0;//spin sirection. + let mut pin13= 255; + let mut pin14 = 0; + let maximum =255; + let minimum = 180; + + loop { + //println!("Hello, World!"); + //led.toggle(); + //Timer::after_millis(1_000).await; + + + if status == 0 && direction == 0 { + pin13 -= 1; + if pin13 <= minimum { + direction = 1; + status = 1; + pin13 = 0; + pin14 = 180; + Timer::after_millis(50).await; } - motor_lo.set_timestamp(duty_cycle); - motor_hi.set_timestamp(255 - duty_cycle); - } else if increasing && !l_r { - println!("away fast!"); - duty_cycle -= step; - if duty_cycle <= 0 { - increasing = false; - } - motor_hi.set_timestamp(duty_cycle); - motor_lo.set_timestamp(255 - duty_cycle); - } - else if !increasing && !l_r { - println!("away slow!"); - duty_cycle += step; - if duty_cycle >= 60 { - increasing = true; - l_r = true; + if status == 1 && direction == 1 { + pin14 += 1; + if pin14 >= maximum { + direction = 1; + status = 0; } - motor_hi.set_timestamp(duty_cycle); - motor_lo.set_timestamp(255 - duty_cycle); + } + if status == 0 && direction == 1 { + pin14 -= 1; + if pin14 <= minimum { + direction = 0; + status = 1; + pin14 = 0; + pin13 = 180; + Timer::after_millis(50).await; + } } - Timer::after_millis(500).await; + if status == 1 && direction == 0 { + pin13 += 1; + if pin13 >= maximum { + direction = 0; + status = 0; + } + } + motor_hi.set_timestamp(pin13); + motor_lo.set_timestamp(pin14); - // println!("Hello, World!"); - // led.toggle(); - // Timer::after_millis(1_000).await; + - for cur_motor_value in (0..=255).rev() { - motor_hi.set_timestamp(cur_motor_value); - motor_lo.set_timestamp(0); - Timer::after_millis(10).await; - } - Timer::after_millis(100).await; + Timer::after_millis(50).await; - for cur_motor_value in (0..=255).rev() { - motor_hi.set_timestamp(0); - motor_lo.set_timestamp(cur_motor_value); - Timer::after_millis(10).await; - } - - Timer::after_millis(100).await; } }