Compare commits
2 commits
07e6e6ec4b
...
afd1a61bfe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
afd1a61bfe | ||
|
|
266f09c4b1 |
2 changed files with 68 additions and 13 deletions
|
|
@ -6,11 +6,12 @@ use esp_hal::mcpwm::operator::PwmPin;
|
|||
use esp_hal::mcpwm::operator::Operator;
|
||||
use esp_hal::mcpwm::McPwm;
|
||||
use esp_hal::peripherals::MCPWM0;
|
||||
|
||||
use esp_hal::delay::Delay;
|
||||
pub struct AfroEsc<'a>{
|
||||
pub pwm_pin: &'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true>
|
||||
|
||||
}
|
||||
const ARMING_SEQUENCE:u16 = 1055;
|
||||
const MIN_THROTTLE:u16 = 1121;
|
||||
const MAX_THROTTLE:u16 = 1421;
|
||||
const GAP:u16 = (MAX_THROTTLE - MIN_THROTTLE)/100;
|
||||
|
|
@ -18,7 +19,11 @@ const GAP:u16 = (MAX_THROTTLE - MIN_THROTTLE)/100;
|
|||
impl AfroEsc<'_>{
|
||||
//this is a little hacky tbh
|
||||
pub fn new<'a>(pwm_pin:&'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true>)-> AfroEsc<'a> {
|
||||
AfroEsc{pwm_pin:pwm_pin}
|
||||
let mut esc = AfroEsc{pwm_pin:pwm_pin};
|
||||
let delay = Delay::new();
|
||||
esc.set_arming_sequence();
|
||||
delay.delay_millis(3000);
|
||||
esc
|
||||
}
|
||||
pub fn set_timestamp(&mut self,value:u16){
|
||||
&self.pwm_pin.set_timestamp(value);
|
||||
|
|
@ -32,4 +37,8 @@ impl AfroEsc<'_>{
|
|||
let new_timestamp = MIN_THROTTLE+value*GAP;
|
||||
&self.pwm_pin.set_timestamp(new_timestamp);
|
||||
}
|
||||
pub fn set_arming_sequence(&mut self){
|
||||
&self.set_timestamp(1055);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -11,19 +11,30 @@ use esp_hal::uart::{Config, Uart};
|
|||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::main;
|
||||
|
||||
use esp_hal::gpio::Event;
|
||||
use esp_hal::gpio::{Input, InputConfig};
|
||||
use esp_hal::mcpwm::operator::PwmPinConfig;
|
||||
use esp_hal::mcpwm::timer::PwmWorkingMode;
|
||||
use esp_hal::time::Rate;
|
||||
|
||||
use esp_hal::handler;
|
||||
|
||||
use core::cell::RefCell;
|
||||
use esp_hal::time::{Duration, Instant};
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
|
||||
use critical_section::Mutex;
|
||||
use rtt_target::rprintln;
|
||||
mod dc_driver;
|
||||
|
||||
use dc_driver::afroesc::AfroEsc;
|
||||
#[panic_handler]
|
||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
//
|
||||
static BUTTON: Mutex<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None));
|
||||
extern crate alloc;
|
||||
|
||||
// This creates a default app-descriptor required by the esp-idf bootloader.
|
||||
|
|
@ -47,7 +58,6 @@ fn main() -> ! {
|
|||
let mut mcpwm = esp_hal::mcpwm::McPwm::new(peripherals.MCPWM0, clock_config);
|
||||
mcpwm.operator0.set_timer(&mcpwm.timer0);
|
||||
let pin = peripherals.GPIO18;
|
||||
|
||||
let mut pwm_pin = mcpwm
|
||||
.operator0
|
||||
.with_pin_a(pin, PwmPinConfig::UP_ACTIVE_HIGH);
|
||||
|
|
@ -55,19 +65,29 @@ fn main() -> ! {
|
|||
.timer_clock_with_frequency(19_999, PwmWorkingMode::Increase, Rate::from_hz(50))
|
||||
.unwrap();
|
||||
mcpwm.timer0.start(timer_clock_cfg);
|
||||
let mut esc = AfroEsc::new(&mut pwm_pin);
|
||||
esc.set_timestamp(1000);
|
||||
delay.delay_millis(3000);
|
||||
esc.set_duty_percent(10);
|
||||
delay.delay_millis(3000);
|
||||
// let mut esc = AfroEsc::new(&mut pwm_pin);
|
||||
// esc.set_timestamp(1000);
|
||||
// delay.delay_millis(3000);
|
||||
// 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);
|
||||
// esc.set_timestamp(1055);
|
||||
// delay.delay_millis(3000);
|
||||
//gpio 9 is the button
|
||||
let input_config = InputConfig::default().with_pull(esp_hal::gpio::Pull::Up);
|
||||
//let emergency_button = peripherals.GPIO9;
|
||||
let mut emergency_button = Input::new(peripherals.GPIO9, input_config);
|
||||
critical_section::with(|cs| {
|
||||
emergency_button.listen(Event::FallingEdge);
|
||||
BUTTON.borrow_ref_mut(cs).replace(emergency_button)
|
||||
});
|
||||
let mut io = esp_hal::gpio::Io::new(peripherals.IO_MUX);
|
||||
io.set_interrupt_handler(handler);
|
||||
|
||||
let mut uart0 = Uart::new(peripherals.UART0, Config::default().with_baudrate(9600))
|
||||
.unwrap()
|
||||
.with_rx(peripherals.GPIO9)
|
||||
.with_tx(peripherals.GPIO10);
|
||||
.with_rx(peripherals.GPIO10)
|
||||
.with_tx(peripherals.GPIO11);
|
||||
uart0.write(b"page page1");
|
||||
uart0.write(b"\xff");
|
||||
uart0.write(b"\xff");
|
||||
|
|
@ -97,3 +117,29 @@ fn main() -> ! {
|
|||
|
||||
// for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0/examples/src/bin
|
||||
}
|
||||
|
||||
#[handler]
|
||||
// #[ram]
|
||||
fn handler() {
|
||||
|
||||
|
||||
if critical_section::with(|cs| {
|
||||
BUTTON
|
||||
.borrow_ref_mut(cs)
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.is_interrupt_set()
|
||||
}) {
|
||||
rprintln!("Button was the source of the interrupt");
|
||||
} else {
|
||||
rprintln!("Button was not the source of the interrupt");
|
||||
}
|
||||
|
||||
critical_section::with(|cs| {
|
||||
BUTTON
|
||||
.borrow_ref_mut(cs)
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.clear_interrupt()
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue