4 * Copyright 2004, 2008 Develer S.r.l. (http://www.develer.com/)
8 * \brief Driver to control stepper motor
12 * \author Francesco Michelini <francesco.michelini@seacfi.com>
13 * \author Giovanni Bajo <rasky@develer.com>
14 * \author Bernie Innocenti <bernie@codewiz.org>
15 * \author Simone Zinanni <s.zinanni@develer.com>
16 * \author Daniele Basile <asterix@develer.com>
18 * $WIZ$ module_name = "stepper"
19 * $WIZ$ module_depends = "timer", "ramp"
20 * $WIZ$ module_configuration = "bertos/cfg/cfg_stepper.h"
27 #include <cfg/compiler.h>
29 #include <algo/ramp.h>
31 // Forward declaration
34 ///< Special value for steps to move the motor continuously
35 #define STEPS_INFINITE_POSITIVE ((int16_t)0xFFFF)
36 #define STEPS_INFINITE_NEGATIVE ((int16_t)0x8FFF)
38 ///< Maximum value for stepper steps
39 #define MAX_STEPS 0x7FFF
41 ///< Default value -> no level sensor associated to the motor
42 #define MOTOR_NO_LEVEL_SENSOR 0xFFFF
44 ///< Default value -> no home sensor associated to the motor
45 #define MOTOR_NO_HOME_SENSOR 0xFFFF
47 ///< Default value for deafsteps in normal movement (no level sensor)
48 #define DEAFSTEPS_DEFAULT MAX_STEPS
50 ///< Out-of-band values for speed
52 #define SPEED_STOPPED 0xFFFF ///< motor is stopped
53 #define SPEED_HOMING 0xFFFE ///< motor is homing
56 // default values for steps inside and outside home sensor
57 #define MOTOR_INSIDE_HOME_STEPS 10
58 #define MOTOR_OUTSIDE_HOME_STEPS 40
60 // default value for home sensor tolerance
61 #define MOTOR_TOLERANCE_HOME_STEPS 2
63 // default value for consecutive error
64 #define MOTOR_CONSECUTIVE_ERROR_STEPS 3
66 // values for the home control enabling
67 enum MotorHomeSensorCheck
69 MOTOR_HOMESENSOR_NOCHECK = 0,
70 MOTOR_HOMESENSOR_INCHECK,
71 MOTOR_HOMESENSOR_OUTCHECK
74 // default value in ms for home procedure timeout
75 #define MOTOR_TIMEOUT_HOME 20000
82 DIR_POSITIVE = 1, ///< moving away from zero (which is the home)
83 DIR_NONE = 0, ///< no movement
84 DIR_NEGATIVE = -1 ///< moving towards towards zero (which is the home)
87 #define STEPPER_MAX_STATES 32
91 * Stepper state-machine conditions
95 MSTS_UNINIT, ///< stepper_init() not yet called
96 MSTS_RUN, ///< running
97 MSTS_IDLE, ///< waiting for a command
98 MSTS_PREIDLE, ///< waiting before going low-current
99 MSTS_PRERUN, ///< waiting after high-current
102 MSTS_PREINIT, ///< preparing to initialize ;-)
103 MSTS_INIT, ///< initializing home procedure
104 MSTS_ENTERING, ///< entering home sensor
105 MSTS_LEAVING, ///< moving away from home (inside the sensor)
106 MSTS_OUTHOME, ///< moving away from home (outside the sensor)
108 MSTS_ERROR, ///< error status
110 ///< Dummy entry to guarantee the right underlying size for the enum
111 MSTS_DUMMY_ALIGN = STEPPER_MAX_STATES - 1
114 ///< Pointer to a function handling a state of the FSM driving the motor
115 typedef enum StepperState (*fsm_state)(struct Stepper* );
117 ///< Pointer to a isr stepper function
118 typedef void (*stepper_isr_t)(struct Stepper* );
120 ///< Time for steppers motor
121 typedef uint16_t stepper_time_t;
124 * Stepper configuration
128 struct Ramp ramp; ///< Acceleration ramp
129 uint16_t pulse; ///< (clocks) Length of the clock pulse used to drive the motor
131 fsm_state states[STEPPER_MAX_STATES]; ///< Custom FSM states (or NULL for default handling)
133 int16_t stepsInHome; ///< Additional steps to do after home detection
134 int16_t stepsOutHome; ///< Additional steps to do leaving sensor in home procedure
135 uint16_t clocksHome; ///< Clock ticks for steps done when searching home
137 int16_t stepsTollOutHome; ///< tolerance steps leaving home sensor control while moving
138 int16_t stepsTollInHome; ///< tolerance steps leaving home sensor control while moving
140 int16_t timeoutHome; ///< timeout in ms in home procedure
142 uint8_t powerRun; ///< Vref voltage when motor runs (0-255)
143 uint8_t powerIdle; ///< Vref voltage when motor is idle (0-255)
145 uint16_t homeSensorIndex; ///< Home Sensor index in the sensor list
146 uint16_t levelSensorIndex; ///< Level Sensor index in the sensor list
150 bool homeInverted : 1; ///< True for inverted home sensor
151 bool halfStep : 1; ///< True for half-step mode
152 bool axisInverted : 1; ///< True if the CW/CCW are inverted from default
153 bool levelInverted : 1; ///< True for inverted level sensor
154 bool controlBit : 1; ///< Control bit status
155 bool controlMoveBit : 1; ///< Control bit status in movement
156 bool highcurrentBit : 1; ///< Mantain high current bit status
162 * Motor context structure
166 const struct StepperConfig *cfg; ///< Configuration of this stepper
167 fsm_state state; ///< Motor FSM state function
169 struct TimerCounter *timer; ///< HW timer bound to this motor
170 uint16_t index; ///< Index of the motor
172 volatile int16_t step; ///< Steps counter (used in interrupt)
173 volatile int16_t rampStep; ///< Current position in acceleration ramp (used in intrrupt)
174 #if RAMP_USE_FLOATING_POINT
175 float rampValue; ///< Nr of Ticks for current step in ramp
176 float rampClock; ///< Cumulative nr of ticks for current step in ramp
182 enum MotorDirection dir; ///< Current direction
183 uint8_t power; ///< Current power
185 uint16_t speed; ///< Timer compare value to reach
186 int16_t stepToReach; ///< Final position to reach when running
188 int16_t skipIrqs; ///< Counter used to skip IRQs (delay state changes)
189 int16_t changeCurrentIrqs; ///< Counter used to change current level (delay state changes)
191 int8_t enableCheckHome; ///< enable the home sensor control during movement
192 int8_t stepsErrorHome; ///< number of consecutive steps in error
193 int16_t stepsTollMax; ///< home sensor out max position
194 int16_t stepsTollMin; ///< home sensor in max position
196 int16_t stepsDeaf; ///< Position after which start the level check
197 int16_t stepsLevel; ///< Position of level contact
199 int16_t stepCircular; ///< Steps corresponding to 360 degrees (rotating motor)
203 void stepper_init(void);
204 void stepper_end(void);
205 struct Stepper *stepper_setup(int index, struct StepperConfig *cfg);
206 void stepper_disable(void);
207 void stepper_reset(struct Stepper *motor);
208 void stepper_home(struct Stepper *motor);
209 void stepper_setStep(struct Stepper *motor, int16_t step);
210 int16_t stepper_getStep(struct Stepper *motor);
211 int16_t stepper_move(struct Stepper *motor, int16_t step, uint16_t speed, int16_t deafstep);
212 void stepper_stop(struct Stepper *motor);
213 void stepper_break(struct Stepper *motor, enum StepperState state);
214 bool stepper_idle(struct Stepper *motor);
215 bool stepper_readHome(struct Stepper *motor);
216 bool stepper_readLevel(struct Stepper *motor);
217 void stepper_updateHalfStep(struct Stepper *motor);
218 void stepper_updateControlBit(struct Stepper *motor);
219 void stepper_updateControlMoveBit(struct Stepper *motor);
220 bool stepper_error(struct Stepper *motor);
221 bool stepper_inhome(struct Stepper *motor);
222 int16_t stepper_getLevelStep(struct Stepper *motor);
223 void stepper_set_stepCircular(struct Stepper *motor, int16_t steps);
224 int16_t stepper_get_stepCircular(struct Stepper *motor);
225 int16_t stepper_scaleSteps(struct Stepper *motor, int16_t dir);
227 #endif /* DRV_STEPPER_H */