pub struct StepperController {
steps_per_revolution: u32,
current_steps: i32,
target_steps: i32,
}
impl StepperController {
pub fn new(steps_per_revolution: u32) -> Self {
Self {
steps_per_revolution,
current_steps: 0,
target_steps: 0,
}
}
pub fn set_desired_angle(&mut self, angle: f32) {
let resolution = 360.0 / self.steps_per_revolution as f32;
let steps = (angle / resolution) as i32;
self.target_steps = steps;
self.current_steps = steps;
}
pub fn apply_step(&mut self) {
if self.current_steps < 0 {
self.current_steps += 1 } else if self.current_steps > 0 {
self.current_steps -= 1
}
if self.current_steps == 0 {
self.target_steps = 0;
}
}
pub fn steps_per_revolution(&self) -> u32 {
self.steps_per_revolution
}
pub fn is_reversed(&self) -> bool {
self.target_steps < 0
}
pub fn needs_movement(&self) -> bool {
self.current_steps != 0
}
pub fn current_steps(&self) -> i32 {
self.current_steps
}
pub fn reset(&mut self) {
self.target_steps = 0;
self.current_steps = 0;
}
}