kaptan-actuator: improve config abstractions
Signed-off-by: Onur Özkan <work@onurozkan.dev>
This commit is contained in:
1 parent
117e1abe97
commit
07a8038857
7 files changed
+136
-146
No files matched your search
@@ -1,11 +1,48 @@
|
||||
//! DShot framing, timing, and arming policy.
|
||||
|
||||
use crate::ArmingDuration;
|
||||
use core::sync::atomic::{AtomicBool, AtomicU16, Ordering};
|
||||
|
||||
pub const MIN_THROTTLE: u16 = 48;
|
||||
pub const MOTOR_STOP: u16 = 0;
|
||||
pub const FRAME_SYMBOLS: usize = 18;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct Config {
|
||||
rate: Rate,
|
||||
armed_idle_throttle: Throttle,
|
||||
arming_duration: ArmingDuration,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub const fn new(
|
||||
rate: Rate,
|
||||
armed_idle_throttle: Throttle,
|
||||
arming_duration: ArmingDuration,
|
||||
) -> Option<Self> {
|
||||
if rate.frames_for_millis(arming_duration.milliseconds()) == 0 {
|
||||
return None;
|
||||
}
|
||||
Some(Self {
|
||||
rate,
|
||||
armed_idle_throttle,
|
||||
arming_duration,
|
||||
})
|
||||
}
|
||||
|
||||
pub const fn rate(self) -> Rate {
|
||||
self.rate
|
||||
}
|
||||
|
||||
pub const fn armed_idle_throttle(self) -> Throttle {
|
||||
self.armed_idle_throttle
|
||||
}
|
||||
|
||||
pub const fn arming_duration(self) -> ArmingDuration {
|
||||
self.arming_duration
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Rate {
|
||||
Dshot150,
|
||||
@@ -186,7 +223,8 @@ pub const fn symbols(packet: u16, zero_high: u16, one_high: u16) -> [u16; FRAME_
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{ArmSequence, FrameValue, Rate, Throttle, Timing, packet, symbols};
|
||||
use super::{ArmSequence, Config, FrameValue, Rate, Throttle, Timing, packet, symbols};
|
||||
use crate::ArmingDuration;
|
||||
|
||||
#[test]
|
||||
fn packet_matches_standard_dshot_vectors() {
|
||||
@@ -219,4 +257,11 @@ mod tests {
|
||||
assert!(Timing::from_timer_clock(rate.timer_base_hz(), rate).is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requires_an_arming_interval_with_frames() {
|
||||
let duration = ArmingDuration::from_millis(2_000).unwrap();
|
||||
let throttle = Throttle::new(300).unwrap();
|
||||
assert!(Config::new(Rate::Dshot600, throttle, duration).is_some());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user