*: cargo fmt

This commit is contained in:
Serge Bazanski 2026-01-23 21:28:46 +01:00
parent 9948a76602
commit fe8942575a
7 changed files with 156 additions and 151 deletions

View file

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

@ -41,11 +41,11 @@ impl BitTicks {
Self { Self {
t0_h, t0_h,
t1_h, t1_h,
t0_l:t1_h, t0_l: t1_h,
t1_l:t0_h, t1_l: t0_h,
} }
} }
pub fn from_clk( pub fn from_clk(
clk_speed: u32, clk_speed: u32,
clk_divider: u8, clk_divider: u8,
@ -57,11 +57,10 @@ impl BitTicks {
let bit_ticks = (bit_period_us / tick_len).round() as u16; let bit_ticks = (bit_period_us / tick_len).round() as u16;
let t1_h = (speed.bit_times().t1_h / tick_len).round() as u16; let t1_h = (speed.bit_times().t1_h / tick_len).round() as u16;
let t0_h = (speed.bit_times().t0_h / tick_len).round() as u16; let t0_h = (speed.bit_times().t0_h / tick_len).round() as u16;
let mut bittick = Self::new(t1_h, t0_h); let mut bittick = Self::new(t1_h, t0_h);
bittick.t0_l = bit_ticks -t0_h; bittick.t0_l = bit_ticks - t0_h;
bittick.t1_l = bit_ticks - t1_h; bittick.t1_l = bit_ticks - t1_h;
bittick bittick
} }
} }
@ -140,8 +139,14 @@ impl<'a> DShot<'a> {
) -> Self { ) -> Self {
let clk_speed = clk_speed.unwrap_or(80_000_000); let clk_speed = clk_speed.unwrap_or(80_000_000);
let clk_divider = clk_divider.unwrap_or(1); let clk_divider = clk_divider.unwrap_or(1);
let bit_ticks = BitTicks::from_clk(clk_speed, clk_divider, speed.bit_times(),speed); let bit_ticks = BitTicks::from_clk(clk_speed, clk_divider, speed.bit_times(), speed);
rprint!("bit_ticks.t1_h:{},t1_l:{},t0_h:{},t0_l{}",bit_ticks.t1_h,bit_ticks.t1_l,bit_ticks.t0_h,bit_ticks.t0_l); rprint!(
"bit_ticks.t1_h:{},t1_l:{},t0_h:{},t0_l{}",
bit_ticks.t1_h,
bit_ticks.t1_l,
bit_ticks.t0_h,
bit_ticks.t0_l
);
Self { Self {
rx_channel: rx_channel, rx_channel: rx_channel,
tx_channel: tx_channel, tx_channel: tx_channel,
@ -171,7 +176,7 @@ impl<'a> DShot<'a> {
rprintln!("--------------------"); rprintln!("--------------------");
for i in 0..16 { for i in 0..16 {
let bit = (frame >> (15 - i)) & 1; let bit = (frame >> (15 - i)) & 1;
pulses[i] = if bit == 1 { pulses[i] = if bit == 1 {
rprint!("1"); rprint!("1");
PulseCode::new( PulseCode::new(
@ -195,7 +200,7 @@ impl<'a> DShot<'a> {
rprintln!(""); rprintln!("");
rprintln!("--------------------"); rprintln!("--------------------");
pulses[16] =0; pulses[16] = 0;
pulses pulses
} }

View file

@ -4,14 +4,10 @@ pub enum EscState {
Starting, Starting,
Running, Running,
Stopping, Stopping,
Stop Stop,
} }
//taken from: https://docs.arduino.cc/language-reference/en/functions/math/map/ //taken from: https://docs.arduino.cc/language-reference/en/functions/math/map/
//used for the ramp up of the spincoater //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 { 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;
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }
}

View file

@ -6,18 +6,18 @@
holding buffers for the duration of a data transfer." holding buffers for the duration of a data transfer."
)] )]
use alloc::string::ToString; use alloc::string::ToString;
use esp_hal::uart::{Config, Uart};
use esp_hal::clock::CpuClock; use esp_hal::clock::CpuClock;
use esp_hal::delay::Delay; use esp_hal::delay::Delay;
use esp_hal::main; use esp_hal::main;
use esp_hal::uart::{Config, Uart};
use esp_hal::gpio::Event; use esp_hal::gpio::Event;
use esp_hal::gpio::{Input, InputConfig}; use esp_hal::gpio::{Input, InputConfig};
use esp_hal::handler;
use esp_hal::mcpwm::operator::PwmPinConfig; use esp_hal::mcpwm::operator::PwmPinConfig;
use esp_hal::mcpwm::timer::PwmWorkingMode; use esp_hal::mcpwm::timer::PwmWorkingMode;
use esp_hal::time::Rate;
use esp_hal::rmt::{Rmt, TxChannelConfig, TxChannelCreator}; use esp_hal::rmt::{Rmt, TxChannelConfig, TxChannelCreator};
use esp_hal::handler; use esp_hal::time::Rate;
use core::cell::RefCell; use core::cell::RefCell;
use critical_section::Mutex; use critical_section::Mutex;
@ -36,10 +36,10 @@ use peripherals::nextion::Nextion;
use dc_driver::dshot::DShot; use dc_driver::dshot::DShot;
use dc_driver::dshot::DShotSpeed; use dc_driver::dshot::DShotSpeed;
use esp_hal::rmt::RxChannelCreator;
use esp_hal::rmt::RxChannelConfig;
use crate::dc_driver::dshot; use crate::dc_driver::dshot;
use crate::peripherals::ErrCommand; use crate::peripherals::ErrCommand;
use esp_hal::rmt::RxChannelConfig;
use esp_hal::rmt::RxChannelCreator;
#[panic_handler] #[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! { fn panic(_: &core::panic::PanicInfo) -> ! {
rprintln!("PANIC!"); rprintln!("PANIC!");
@ -57,8 +57,8 @@ const DEFAULT_SPIN_TIME: u32 = 10;
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description> // For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!(); esp_bootloader_esp_idf::esp_app_desc!();
use crate::peripherals::Command; use crate::peripherals::Command;
use esp_hal::gpio::Output;
use esp_hal::gpio::Level; use esp_hal::gpio::Level;
use esp_hal::gpio::Output;
use esp_hal::gpio::OutputConfig; use esp_hal::gpio::OutputConfig;
#[main] #[main]
@ -85,23 +85,34 @@ fn main() -> ! {
.timer_clock_with_frequency(19_999, PwmWorkingMode::Increase, Rate::from_hz(50)) .timer_clock_with_frequency(19_999, PwmWorkingMode::Increase, Rate::from_hz(50))
.unwrap(); .unwrap();
mcpwm.timer0.start(timer_clock_cfg); mcpwm.timer0.start(timer_clock_cfg);
//setup RMT //setup RMT
let freq = Rate::from_mhz(80); let freq = Rate::from_mhz(80);
let rmt = Rmt::new(peripherals.RMT, freq).expect("CAN NOT SET FREQUENCY"); let rmt = Rmt::new(peripherals.RMT, freq).expect("CAN NOT SET FREQUENCY");
let rx_config = RxChannelConfig::default().with_clk_divider(1); let rx_config = RxChannelConfig::default().with_clk_divider(1);
let tx_config = TxChannelConfig::default().with_clk_divider(1); let tx_config = TxChannelConfig::default().with_clk_divider(1);
let mut toggle_pin = Output::new(peripherals.GPIO2, Level::Low, OutputConfig::default()); let mut toggle_pin = Output::new(peripherals.GPIO2, Level::Low, OutputConfig::default());
let mut tx_channel = rmt.channel0.configure_tx(peripherals.GPIO23,tx_config ).expect("creation of TX_CHANNEL FAILED!"); let mut tx_channel = rmt
let mut rx_channel = rmt.channel3.configure_rx(peripherals.GPIO14,rx_config).unwrap(); .channel0
let mut dshot_esc = dshot::DShot::new(&mut rx_channel, &mut tx_channel, DShotSpeed::DShot600, Some(80_000_000), Some(1)); .configure_tx(peripherals.GPIO23, tx_config)
.expect("creation of TX_CHANNEL FAILED!");
let mut rx_channel = rmt
.channel3
.configure_rx(peripherals.GPIO14, rx_config)
.unwrap();
let mut dshot_esc = dshot::DShot::new(
&mut rx_channel,
&mut tx_channel,
DShotSpeed::DShot600,
Some(80_000_000),
Some(1),
);
rprintln!("SENDING RMT"); rprintln!("SENDING RMT");
loop{ loop {
delay.delay_millis(1000); delay.delay_millis(1000);
toggle_pin.set_high(); toggle_pin.set_high();
dshot_esc.write_throttle(2047,true); dshot_esc.write_throttle(2047, true);
toggle_pin.set_low(); toggle_pin.set_low();
} }
// rprintln!("RMT SENT!"); // rprintln!("RMT SENT!");
// let mut esc = AfroEsc::new(&mut pwm_pin);; // let mut esc = AfroEsc::new(&mut pwm_pin);;
@ -144,35 +155,35 @@ fn main() -> ! {
loop { loop {
if display.read_ready() { if display.read_ready() {
match display.read_command() { match display.read_command() {
Ok(Command::CommandSuccess) => { Ok(Command::CommandSuccess) => {
rprintln!("COMMAND SUCCESSFULLY executed"); rprintln!("COMMAND SUCCESSFULLY executed");
} }
Ok(Command::Start) => { Ok(Command::Start) => {
rprintln!("START"); rprintln!("START");
started = true; started = true;
}, }
Ok(Command::Stop) => { Ok(Command::Stop) => {
rprintln!("STOP"); rprintln!("STOP");
started = false; started = false;
// spincoater.stop(); // spincoater.stop();
}, }
Ok(Command::SetRpm(x)) => { Ok(Command::SetRpm(x)) => {
rprintln!("SET_RPM with {}", x); rprintln!("SET_RPM with {}", x);
rpm = x; rpm = x;
}, }
Ok(Command::SetTimer(x)) => { Ok(Command::SetTimer(x)) => {
rprintln!("SETTING TIMER {}", x); rprintln!("SETTING TIMER {}", x);
timer = x; timer = x;
}, }
Ok(Command::SendConfig) => { Ok(Command::SendConfig) => {
rprintln!("SEND CONFIG"); rprintln!("SEND CONFIG");
let command = format!("rpm.val={}",DEFAULT_TARGET_RPM); let command = format!("rpm.val={}", DEFAULT_TARGET_RPM);
display.send_command(command.to_string().as_bytes()); display.send_command(command.to_string().as_bytes());
}, }
Err(ErrCommand::NoValidCmd) => { Err(ErrCommand::NoValidCmd) => {
rprintln!(" NOT A VALID CMD!"); rprintln!(" NOT A VALID CMD!");
}, }
Err(ErrCommand::ReadError) =>{ Err(ErrCommand::ReadError) => {
rprintln!("READ FAILED!"); rprintln!("READ FAILED!");
} }
} }

View file

@ -1,15 +1,15 @@
pub mod nextion; pub mod nextion;
#[derive(Debug)] #[derive(Debug)]
pub enum Command{ pub enum Command {
SetRpm(u32), SetRpm(u32),
SetTimer(u32), SetTimer(u32),
Start, Start,
Stop, Stop,
SendConfig, SendConfig,
CommandSuccess CommandSuccess,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum ErrCommand{ pub enum ErrCommand {
NoValidCmd, NoValidCmd,
ReadError ReadError,
} }

View file

@ -1,9 +1,9 @@
use esp_hal::Blocking;
use esp_hal::uart::RxError; use esp_hal::uart::RxError;
use esp_hal::uart::{Uart}; use esp_hal::uart::Uart;
use esp_hal::{Blocking };
use rtt_target::rprintln; use rtt_target::rprintln;
use crate::peripherals::{ErrCommand,Command}; use crate::peripherals::{Command, ErrCommand};
enum UartStatemachine { enum UartStatemachine {
WaitingP, WaitingP,
@ -93,42 +93,32 @@ impl<'a> Nextion<'a> {
} }
} }
} }
pub fn read_command(&mut self) -> Result<Command,ErrCommand>{ pub fn read_command(&mut self) -> Result<Command, ErrCommand> {
let mut buf:[u8;8] = [0;8]; let mut buf: [u8; 8] = [0; 8];
let _read_bytes = match self.read_frame(&mut buf){ let _read_bytes = match self.read_frame(&mut buf) {
Ok(x) => x, Ok(x) => x,
Err(e) => {rprintln!("ERROR while reading"); Err(e) => {
return Err(ErrCommand::NoValidCmd); rprintln!("ERROR while reading");
} return Err(ErrCommand::NoValidCmd);
}
}; };
rprintln!("READ SUCCESS!:{:?}",buf); rprintln!("READ SUCCESS!:{:?}", buf);
match buf[0]{ match buf[0] {
01 => Ok(Command::Start),
01 => {Ok(Command::Start)}, 02 => Ok(Command::Stop),
02 =>{Ok(Command::Stop)},
03 => { 03 => {
let rpm = u32::from_le_bytes(buf[1..5].try_into().expect("failed to parse rpm!")); let rpm = u32::from_le_bytes(buf[1..5].try_into().expect("failed to parse rpm!"));
Ok(Command::SetRpm(rpm)) Ok(Command::SetRpm(rpm))
}, }
04 => { 04 => {
let time = u32::from_le_bytes(buf[1..5].try_into().expect("failed to parse time!")); let time = u32::from_le_bytes(buf[1..5].try_into().expect("failed to parse time!"));
Ok(Command::SetTimer(time)) Ok(Command::SetTimer(time))
} }
05 =>{ 05 => Ok(Command::SendConfig),
Ok(Command::SendConfig) 00 => Ok(Command::CommandSuccess),
} _ => Err(ErrCommand::NoValidCmd),
00 => { }
Ok(Command::CommandSuccess)
},
_ => {
Err(ErrCommand::NoValidCmd)
}
}
} }
pub fn read_ready(&mut self) -> bool { pub fn read_ready(&mut self) -> bool {
self.interface.read_ready() self.interface.read_ready()

View file

@ -1 +1 @@
#![no_std] #![no_std]