syact/act/
stepper.rs

1use syunit::*;
2
3use crate::data::MicroSteps;
4use crate::math::movements::DefinedActuator;
5use crate::{StepperConst, SyncActuator, SyncActuatorGroup, StepperConfig};
6
7// Public imports
8    pub use syact_macros::StepperActuatorGroup;
9// 
10
11// Submodules
12    mod builder;
13    pub use builder::{BuilderError, DriveMode, StepperBuilder, StartStopBuilder, ComplexBuilder};
14
15    mod ctrl;
16    pub use ctrl::{StepperController, GenericPWM, ControllerError};
17
18    mod motor;
19// 
20
21// #####################
22// #   Stepper-Types   #
23// #####################
24    /// Default `Stepper` type for high-performance systems (high resolution stepper)
25    pub type Stepper<S, D> = motor::StepperMotor<StartStopBuilder, GenericPWM<S, D>>;
26
27    /// Complex `Stepper` type for high-performance systems with greater speed but greater risk and higher calculation complexity
28    pub type ComplexStepper<S, D> = motor::StepperMotor<ComplexBuilder, GenericPWM<S, D>>;
29// 
30
31// Stepper traits
32    /// A component based on a stepper motor
33    pub trait StepperActuator : SyncActuator + DefinedActuator {
34        /// Returns the constants of the stepper motor
35        fn consts(&self) -> &StepperConst;
36
37        // Config
38            /// Returns a reference to the `StepperConfig` used by the stepper motor
39            fn config(&self) -> &StepperConfig;
40
41            /// Set the config used by the Stepper motor
42            fn set_config(&mut self, config : StepperConfig) -> Result<(), BuilderError>;
43        // 
44
45        // Microstepping
46            /// The amount of microsteps in a full step
47            fn microsteps(&self) -> MicroSteps;
48
49            /// Set the amount of microsteps in a full step
50            fn set_microsteps(&mut self, micro : MicroSteps) -> Result<(), BuilderError>;
51        //
52
53        // Steps
54            /// The angular distance of a step considering microstepping
55            fn step_ang(&self) -> Delta;
56        // 
57    }
58
59    /// A group of stepper motor based components
60    pub trait StepperActuatorGroup<T, const C : usize> : SyncActuatorGroup<T, C> 
61    where 
62        T: StepperActuator + ?Sized + 'static
63    {
64        /// Set the configuration for multiple stepper motors
65        fn set_config(&mut self, config : StepperConfig) {
66            self.for_each_mut(|comp, _| {
67                comp.set_config(config.clone())
68            });
69        }
70
71        /// Returns the amount of microsteps every component uses
72        fn microsteps(&self) -> [MicroSteps; C] {
73            self.for_each(|comp, _| {
74                comp.microsteps()
75            })
76        }
77
78        /// Sets the amount of microsteps for each motor 
79        fn set_micro(&mut self, micro : [MicroSteps; C]) -> Result<(), BuilderError> {
80            self.try_for_each_mut(|comp, index| {
81                comp.set_microsteps(micro[index])
82            })?;
83            Ok(())
84        }
85    }
86//