wip step modes

This commit is contained in:
hulthe committed 2026-10-25 22:42:30 +01:00
1 parent 48e2ddee69
commit 77ef8b75c0
2 files changed
+49 -9

No files matched your search

+4 -2
View File
@@ -1,2 +1,4 @@
pin_mappings = [0, 2, 3, 4]
sleep_time = { secs = 0, nanos = 7_000_000 }
pin_mappings = [17, 27, 22, 23]
sleep_time = { secs = 0, nanos = 5_000_000 }
step_mode = "OneTwo" # or "One" or "Two"
+45 -7
View File
@@ -9,6 +9,7 @@ use std::time::Duration;
pub struct Motor {
pins: MotorPins,
sleep_time: Duration,
step_mode: StepModePhase,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -17,6 +18,16 @@ pub struct MotorConfig {
/// The time in milliseconds between each tick of the stepper motor
pub sleep_time: Duration,
/// The time in milliseconds between each tick of the stepper motor
pub step_mode: StepModePhase,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum StepModePhase {
OneTwo,
Two,
One,
}
pub struct MotorPins {
@@ -52,18 +63,45 @@ impl Motor {
Ok(Motor {
sleep_time: config.sleep_time,
pins,
step_mode: config.step_mode,
})
}
pub fn spin_step(&mut self, direction: Direction) {
let mut pin_states = [High, Low, Low, Low];
let phases = match self.step_mode {
StepModePhase::One => &[
[Low, High, Low, Low],
[Low, Low, High, Low],
[Low, Low, Low, High],
[High, Low, Low, Low],
][..],
StepModePhase::Two => &[
[Low, High, High, Low],
[Low, Low, High, High],
[High, Low, Low, High],
[High, High, Low, Low],
],
StepModePhase::OneTwo => &[
[High, High, Low, Low],
[Low, High, Low, Low],
[Low, High, High, Low],
[Low, Low, High, Low],
[Low, Low, High, High],
[Low, Low, Low, High],
[High, Low, Low, High],
[High, Low, Low, Low],
],
};
for _ in 0..pin_states.len() {
self.pins.set(pin_states);
match direction {
Direction::Right => pin_states.rotate_right(1),
Direction::Left => pin_states.rotate_left(1),
}
match direction {
Direction::Left => self.spin_step_inner(phases.iter().copied().rev()),
Direction::Right => self.spin_step_inner(phases.iter().copied()),
}
}
fn spin_step_inner(&mut self, phases: impl Iterator<Item = [Level; 4]>) {
for levels in phases {
self.pins.set(levels);
sleep(self.sleep_time);
}
}