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>
23 #include <cfg/compiler.h>
25 #include <algo/ramp.h>
27 // Forward declaration
30 ///< Special value for steps to move the motor continuously
31 #define STEPS_INFINITE_POSITIVE ((int16_t)0xFFFF)
32 #define STEPS_INFINITE_NEGATIVE ((int16_t)0x8FFF)
34 ///< Maximum value for stepper steps
35 #define MAX_STEPS 0x7FFF
37 ///< Default value -> no level sensor associated to the motor
38 #define MOTOR_NO_LEVEL_SENSOR 0xFFFF
40 ///< Default value -> no home sensor associated to the motor
41 #define MOTOR_NO_HOME_SENSOR 0xFFFF
43 ///< Default value for deafsteps in normal movement (no level sensor)
44 #define DEAFSTEPS_DEFAULT MAX_STEPS
46 ///< Out-of-band values for speed
48 #define SPEED_STOPPED 0xFFFF ///< motor is stopped
49 #define SPEED_HOMING 0xFFFE ///< motor is homing
52 // default values for steps inside and outside home sensor
53 #define MOTOR_INSIDE_HOME_STEPS 10
54 #define MOTOR_OUTSIDE_HOME_STEPS 40
56 // default value for home sensor tolerance
57 #define MOTOR_TOLERANCE_HOME_STEPS 2
59 // default value for consecutive error
60 #define MOTOR_CONSECUTIVE_ERROR_STEPS 3
62 // values for the home control enabling
63 enum MotorHomeSensorCheck
65 MOTOR_HOMESENSOR_NOCHECK = 0,
66 MOTOR_HOMESENSOR_INCHECK,
67 MOTOR_HOMESENSOR_OUTCHECK
70 // default value in ms for home procedure timeout
71 #define MOTOR_TIMEOUT_HOME 20000
78 DIR_POSITIVE = 1, ///< moving away from zero (which is the home)
79 DIR_NONE = 0, ///< no movement
80 DIR_NEGATIVE = -1 ///< moving towards towards zero (which is the home)
83 #define STEPPER_MAX_STATES 32
87 * Stepper state-machine conditions
91 MSTS_UNINIT, ///< stepper_init() not yet called
92 MSTS_RUN, ///< running
93 MSTS_IDLE, ///< waiting for a command
94 MSTS_PREIDLE, ///< waiting before going low-current
95 MSTS_PRERUN, ///< waiting after high-current
98 MSTS_PREINIT, ///< preparing to initialize ;-)
99 MSTS_INIT, ///< initializing home procedure
100 MSTS_ENTERING, ///< entering home sensor
101 MSTS_LEAVING, ///< moving away from home (inside the sensor)
102 MSTS_OUTHOME, ///< moving away from home (outside the sensor)
104 MSTS_ERROR, ///< error status
106 ///< Dummy entry to guarantee the right underlying size for the enum
107 MSTS_DUMMY_ALIGN = STEPPER_MAX_STATES - 1
110 ///< Pointer to a function handling a state of the FSM driving the motor
111 typedef enum StepperState (*fsm_state)(struct Stepper* );
113 ///< Pointer to a isr stepper function
114 typedef void (*stepper_isr_t)(struct Stepper* );
116 ///< Time for steppers motor
117 typedef uint16_t stepper_time_t;
120 * Stepper configuration
124 struct Ramp ramp; ///< Acceleration ramp
125 uint16_t pulse; ///< (clocks) Length of the clock pulse used to drive the motor
127 fsm_state states[STEPPER_MAX_STATES]; ///< Custom FSM states (or NULL for default handling)
129 int16_t stepsInHome; ///< Additional steps to do after home detection
130 int16_t stepsOutHome; ///< Additional steps to do leaving sensor in home procedure
131 uint16_t clocksHome; ///< Clock ticks for steps done when searching home
133 int16_t stepsTollOutHome; ///< tolerance steps leaving home sensor control while moving
134 int16_t stepsTollInHome; ///< tolerance steps leaving home sensor control while moving
136 int16_t timeoutHome; ///< timeout in ms in home procedure
138 uint8_t powerRun; ///< Vref voltage when motor runs (0-255)
139 uint8_t powerIdle; ///< Vref voltage when motor is idle (0-255)
141 uint16_t homeSensorIndex; ///< Home Sensor index in the sensor list
142 uint16_t levelSensorIndex; ///< Level Sensor index in the sensor list
146 bool homeInverted : 1; ///< True for inverted home sensor
147 bool halfStep : 1; ///< True for half-step mode
148 bool axisInverted : 1; ///< True if the CW/CCW are inverted from default
149 bool levelInverted : 1; ///< True for inverted level sensor
150 bool controlBit : 1; ///< Control bit status
151 bool controlMoveBit : 1; ///< Control bit status in movement
152 bool highcurrentBit : 1; ///< Mantain high current bit status
158 * Motor context structure
162 const struct StepperConfig *cfg; ///< Configuration of this stepper
163 fsm_state state; ///< Motor FSM state function
165 struct TimerCounter *timer; ///< HW timer bound to this motor
166 uint16_t index; ///< Index of the motor
168 volatile int16_t step; ///< Steps counter (used in interrupt)
169 volatile int16_t rampStep; ///< Current position in acceleration ramp (used in intrrupt)
170 #if RAMP_USE_FLOATING_POINT
171 float rampValue; ///< Nr of Ticks for current step in ramp
172 float rampClock; ///< Cumulative nr of ticks for current step in ramp
178 enum MotorDirection dir; ///< Current direction
179 uint8_t power; ///< Current power
181 uint16_t speed; ///< Timer compare value to reach
182 int16_t stepToReach; ///< Final position to reach when running
184 int16_t skipIrqs; ///< Counter used to skip IRQs (delay state changes)
185 int16_t changeCurrentIrqs; ///< Counter used to change current level (delay state changes)
187 int8_t enableCheckHome; ///< enable the home sensor control during movement
188 int8_t stepsErrorHome; ///< number of consecutive steps in error
189 int16_t stepsTollMax; ///< home sensor out max position
190 int16_t stepsTollMin; ///< home sensor in max position
192 int16_t stepsDeaf; ///< Position after which start the level check
193 int16_t stepsLevel; ///< Position of level contact
195 int16_t stepCircular; ///< Steps corresponding to 360 degrees (rotating motor)
199 void stepper_init(void);
200 void stepper_end(void);
201 struct Stepper *stepper_setup(int index, struct StepperConfig *cfg);
202 void stepper_disable(void);
203 void stepper_reset(struct Stepper *motor);
204 void stepper_home(struct Stepper *motor);
205 void stepper_setStep(struct Stepper *motor, int16_t step);
206 int16_t stepper_getStep(struct Stepper *motor);
207 int16_t stepper_move(struct Stepper *motor, int16_t step, uint16_t speed, int16_t deafstep);
208 void stepper_stop(struct Stepper *motor);
209 void stepper_break(struct Stepper *motor, enum StepperState state);
210 bool stepper_idle(struct Stepper *motor);
211 bool stepper_readHome(struct Stepper *motor);
212 bool stepper_readLevel(struct Stepper *motor);
213 void stepper_updateHalfStep(struct Stepper *motor);
214 void stepper_updateControlBit(struct Stepper *motor);
215 void stepper_updateControlMoveBit(struct Stepper *motor);
216 bool stepper_error(struct Stepper *motor);
217 bool stepper_inhome(struct Stepper *motor);
218 int16_t stepper_getLevelStep(struct Stepper *motor);
219 void stepper_set_stepCircular(struct Stepper *motor, int16_t steps);
220 int16_t stepper_get_stepCircular(struct Stepper *motor);
221 int16_t stepper_scaleSteps(struct Stepper *motor, int16_t dir);
223 #endif /* DRV_STEPPER_H */