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