using the way of the arduino map function

This commit is contained in:
Robert Schauklies 2026-01-14 23:31:36 +01:00
parent 443da0edcf
commit 1a5c58c31c
2 changed files with 40 additions and 12 deletions

View file

@ -1,14 +1,9 @@
#![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;
use esp_hal::delay::Delay;
use crate::dc_driver::EscState;
pub struct AfroEsc<'a>{
pub pwm_pin: &'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true>
pub pwm_pin: &'a mut PwmPin<'a, esp_hal::peripherals::MCPWM0<'a>, 0,true>,
state: crate::dc_driver::EscState,
}
const ARMING_SEQUENCE:u16 = 1055;
@ -19,13 +14,14 @@ 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> {
let mut esc = AfroEsc{pwm_pin:pwm_pin};
let mut esc = AfroEsc{pwm_pin:pwm_pin,state:EscState::Starting};
let delay = Delay::new();
esc.set_arming_sequence();
esc.send_arming_sequence();
delay.delay_millis(3000);
esc
}
pub fn set_timestamp(&mut self,value:u16){
self.pwm_pin.set_timestamp(value);
}
//range is from 1121 till 1421
@ -37,8 +33,24 @@ impl AfroEsc<'_>{
let new_timestamp = MIN_THROTTLE+value*GAP;
self.pwm_pin.set_timestamp(new_timestamp);
}
pub fn set_arming_sequence(&mut self){
pub fn send_arming_sequence(&mut self){
self.set_timestamp(1055);
}
fn set_state(&mut self, state:EscState){
self.state = state;
}
pub fn get_state(self) -> EscState{
self.state
}
pub fn map_duty(&mut self, x:i32){
if x <=0 {
self.pwm_pin.set_timestamp(1055);
}
else{
let duty:u16 = crate::dc_driver::arduino_map(x, 0, 100, MIN_THROTTLE.into(), MAX_THROTTLE.into()).try_into().expect("INTEGER TOO LARGE");
self.pwm_pin.set_timestamp(duty);
}
}
}

View file

@ -1 +1,17 @@
pub mod afroesc;
pub enum EscState {
Starting,
Running,
Stopping,
Stop
}
//taken from: https://docs.arduino.cc/language-reference/en/functions/math/map/
//used for the ramp up of the spincoater
pub fn arduino_map(x:i32, in_min:i32, in_max:i32, out_min:i32, out_max:i32) -> i32 {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}