Add different step modes for better torque
This commit is contained in:
2 files changed
+49
-9
No files matched your search
+4
-2
@@ -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
@@ -9,6 +9,7 @@ use std::time::Duration;
|
|||||||
pub struct Motor {
|
pub struct Motor {
|
||||||
pins: MotorPins,
|
pins: MotorPins,
|
||||||
sleep_time: Duration,
|
sleep_time: Duration,
|
||||||
|
step_mode: StepModePhase,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
@@ -17,6 +18,16 @@ pub struct MotorConfig {
|
|||||||
|
|
||||||
/// The time in milliseconds between each tick of the stepper motor
|
/// The time in milliseconds between each tick of the stepper motor
|
||||||
pub sleep_time: Duration,
|
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 {
|
pub struct MotorPins {
|
||||||
@@ -52,18 +63,45 @@ impl Motor {
|
|||||||
Ok(Motor {
|
Ok(Motor {
|
||||||
sleep_time: config.sleep_time,
|
sleep_time: config.sleep_time,
|
||||||
pins,
|
pins,
|
||||||
|
step_mode: config.step_mode,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spin_step(&mut self, direction: Direction) {
|
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() {
|
match direction {
|
||||||
self.pins.set(pin_states);
|
Direction::Left => self.spin_step_inner(phases.iter().copied().rev()),
|
||||||
match direction {
|
Direction::Right => self.spin_step_inner(phases.iter().copied()),
|
||||||
Direction::Right => pin_states.rotate_right(1),
|
}
|
||||||
Direction::Left => pin_states.rotate_left(1),
|
}
|
||||||
}
|
|
||||||
|
fn spin_step_inner(&mut self, phases: impl Iterator<Item = [Level; 4]>) {
|
||||||
|
for levels in phases {
|
||||||
|
self.pins.set(levels);
|
||||||
sleep(self.sleep_time);
|
sleep(self.sleep_time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user