diff --git a/spinnyboy_rust/src/bin/dc_driver/afroesc.rs b/spinnyboy_rust/src/bin/dc_driver/afroesc.rs new file mode 100644 index 0000000..2e463ce --- /dev/null +++ b/spinnyboy_rust/src/bin/dc_driver/afroesc.rs @@ -0,0 +1,34 @@ +#![no_std] + +use esp_hal::aes::Operation; +use esp_hal::mcpwm::PwmPeripheral; +use esp_hal::mcpwm::operator::PwmPin; +use esp_hal::mcpwm::operator::Operator; +use esp_hal::mcpwm::McPwm; +use esp_hal::peripherals::MCPWM0; + +pub struct AfroEsc<'a>{ + pub pwm_pin: &'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true> + +} +const MIN_THROTTLE:u16 = 1121; +const MAX_THROTTLE:u16 = 1421; +const GAP:u16 = (MAX_THROTTLE - MIN_THROTTLE)/100; + +impl AfroEsc<'_>{ + pub fn new<'a>(pwm_pin:&'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true>)-> AfroEsc<'a> { + AfroEsc{pwm_pin:pwm_pin} + } + pub fn set_timestamp(&mut self,value:u16){ + &self.pwm_pin.set_timestamp(value); + } + //range is from 1121 till 1421 + pub fn set_duty_percent(&mut self,value:u16){ + if value > 100 { + // failsafe! + &self.pwm_pin.set_timestamp(1055); + } + let new_timestamp = MIN_THROTTLE+value*GAP; + &self.pwm_pin.set_timestamp(new_timestamp); + } +} \ No newline at end of file diff --git a/spinnyboy_rust/src/bin/dc_driver/mod.rs b/spinnyboy_rust/src/bin/dc_driver/mod.rs new file mode 100644 index 0000000..c2a351f --- /dev/null +++ b/spinnyboy_rust/src/bin/dc_driver/mod.rs @@ -0,0 +1 @@ +pub mod afroesc; \ No newline at end of file diff --git a/spinnyboy_rust/src/bin/main.rs b/spinnyboy_rust/src/bin/main.rs index a4fc937..b621a30 100644 --- a/spinnyboy_rust/src/bin/main.rs +++ b/spinnyboy_rust/src/bin/main.rs @@ -15,6 +15,8 @@ use esp_hal::main; use esp_hal::time::{Duration, Instant}; use esp_hal::delay::Delay; use rtt_target::rprintln; +mod dc_driver; +use dc_driver::afroesc::AfroEsc; #[panic_handler] fn panic(_: &core::panic::PanicInfo) -> ! { loop {} @@ -48,16 +50,15 @@ fn main() -> ! { .timer_clock_with_frequency(19_999, PwmWorkingMode::Increase, Rate::from_hz(50)) .unwrap(); mcpwm.timer0.start(timer_clock_cfg); - - pwm_pin.set_timestamp(1000); + let mut esc = AfroEsc::new(&mut pwm_pin); + esc.set_timestamp(1000); delay.delay_millis(3000); - rprintln!("WUT?"); - pwm_pin.set_timestamp(1121); - delay.delay_millis(1000); - - pwm_pin.set_timestamp(1055); + esc.set_duty_percent(10); delay.delay_millis(3000); - + //1421 is I think the best, makes calculating the throttle easier + esc.set_timestamp(1055); + delay.delay_millis(3000); + // Example: Ramp from 0% to 50% throttle //the afro esc starts turning at roughly setup gets a PWM at //loop{ diff --git a/spinnyboy_rust/src/lib.rs b/spinnyboy_rust/src/lib.rs index 0c9ac1a..2e7f0d4 100644 --- a/spinnyboy_rust/src/lib.rs +++ b/spinnyboy_rust/src/lib.rs @@ -1 +1 @@ -#![no_std] +#![no_std] \ No newline at end of file