safety commit

This commit is contained in:
Robert Schauklies 2026-01-18 16:56:54 +01:00
parent 5f9e718f86
commit 3e5a905544
2 changed files with 53 additions and 19 deletions

View file

@ -40,18 +40,27 @@ impl BitTicks {
Self {
t0_h,
t1_h,
t0_l: t1_h,
t1_l: t0_h,
t0_l:t1_h,
t1_l:t0_h,
}
}
pub fn from_clk(clk_speed: u32, clk_divider: u8, bit_times: BitTimes) -> Self {
pub fn from_clk(
clk_speed: u32,
clk_divider: u8,
bit_times: BitTimes,
speed: DShotSpeed,
) -> Self {
let tick_len = (1. / clk_speed as f32) * (clk_divider as f32) * 1_000_000.;
let bit_period_us = speed.bit_period_ns() as f32 / 1000.0;
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 t0_h = (speed.bit_times().t0_h / tick_len).round() as u16;
let mut bittick = Self::new(t1_h, t0_h);
bittick.t0_l = bit_ticks -t0_h;
bittick.t1_l = bit_ticks - t1_h;
bittick
let t1_h = (bit_times.t1_h / tick_len).round() as u16;
let t0_h = (bit_times.t0_h / tick_len).round() as u16;
Self::new(t1_h, t0_h)
}
}
@ -112,7 +121,6 @@ impl DShotSpeed {
}
#[allow(dead_code)]
#[derive(Debug)]
pub struct DShot<'a> {
rx_channel: &'a mut Channel<'static, esp_hal::Blocking, esp_hal::rmt::Rx>,
tx_channel: &'a mut Channel<'static, esp_hal::Blocking, esp_hal::rmt::Tx>,
@ -124,14 +132,14 @@ impl<'a> DShot<'a> {
pub fn new(
rx_channel: &'a mut Channel<'static, esp_hal::Blocking, esp_hal::rmt::Rx>,
tx_channel: &'a mut Channel<'static, esp_hal::Blocking, esp_hal::rmt::Tx>,
speed: DShotSpeed,
clk_speed: Option<u32>,
clk_divider: Option<u8>,
) -> Self {
let clk_speed = clk_speed.unwrap_or(80_000_000);
let clk_divider = clk_divider.unwrap_or(1);
let bit_ticks = BitTicks::from_clk(clk_speed, clk_divider, speed.bit_times());
let bit_ticks = BitTicks::from_clk(clk_speed, clk_divider, speed.bit_times(),speed);
Self {
rx_channel: rx_channel,
tx_channel: tx_channel,
@ -179,8 +187,13 @@ impl<'a> DShot<'a> {
};
}
// Add empty pulse to end of pulses frame
pulses[16] = 0;
pulses[16] = PulseCode::new(
Level::Low,
self.bit_ticks.t1_h * 2,
Level::Low,
0,
)
.into();
pulses
}
@ -199,7 +212,7 @@ impl<'a> DShot<'a> {
pub fn arm(&mut self, delay: &mut Delay) -> Result<(), &'static str> {
for _ in 0..100 {
self.write_throttle(0, false)?;
delay.delay_millis(20);
delay.delay_millis(50);
}
Ok(())