Use configuration file instead appconfig. Reformat. Clean up. Remove CVS logs.
[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
25 #include <algo/ramp.h>
26
27 // Forward declaration
28 struct Stepper;
29
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)
33
34 ///< Maximum value for stepper steps
35 #define MAX_STEPS                      0x7FFF
36
37 ///< Default value -> no level sensor associated to the motor
38 #define MOTOR_NO_LEVEL_SENSOR          0xFFFF
39
40 ///< Default value -> no home sensor associated to the motor
41 #define MOTOR_NO_HOME_SENSOR           0xFFFF
42
43 ///< Default value for deafsteps in normal movement (no level sensor)
44 #define DEAFSTEPS_DEFAULT              MAX_STEPS
45
46 ///< Out-of-band values for speed
47 //\{
48 #define SPEED_STOPPED                  0xFFFF    ///< motor is stopped
49 #define SPEED_HOMING                   0xFFFE    ///< motor is homing
50 //\}
51
52 // default values for steps inside and outside home sensor
53 #define MOTOR_INSIDE_HOME_STEPS        10
54 #define MOTOR_OUTSIDE_HOME_STEPS       40
55
56 // default value for home sensor tolerance
57 #define MOTOR_TOLERANCE_HOME_STEPS      2
58
59 // default value for consecutive error
60 #define MOTOR_CONSECUTIVE_ERROR_STEPS   3
61
62 // values for the home control enabling
63 enum MotorHomeSensorCheck
64 {
65         MOTOR_HOMESENSOR_NOCHECK = 0,
66         MOTOR_HOMESENSOR_INCHECK,
67         MOTOR_HOMESENSOR_OUTCHECK
68 };
69
70 // default value in ms for home procedure timeout
71 #define MOTOR_TIMEOUT_HOME              20000
72
73 /**
74  * Motor direction
75  */
76 enum MotorDirection
77 {
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)
81 };
82
83 #define STEPPER_MAX_STATES    32
84
85
86 /**
87  * Stepper state-machine conditions
88  */
89 enum StepperState
90 {
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
96
97 // Home procedure
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)
103
104         MSTS_ERROR,         ///< error status
105
106         ///< Dummy entry to guarantee the right underlying size for the enum
107         MSTS_DUMMY_ALIGN = STEPPER_MAX_STATES - 1
108 };
109
110 ///< Pointer to a function handling a state of the FSM driving the motor
111 typedef enum StepperState (*fsm_state)(struct Stepper* );
112
113 ///< Pointer to a isr stepper function
114 typedef void (*stepper_isr_t)(struct Stepper* );
115
116 ///< Time for steppers motor
117 typedef uint16_t stepper_time_t;
118
119 /**
120  * Stepper configuration
121  */
122 struct StepperConfig
123 {
124         struct Ramp ramp;             ///< Acceleration ramp
125         uint16_t pulse;               ///< (clocks) Length of the clock pulse used to drive the motor
126
127         fsm_state states[STEPPER_MAX_STATES];  ///< Custom FSM states (or NULL for default handling)
128
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
132
133     int16_t stepsTollOutHome;     ///< tolerance steps leaving home sensor control while moving
134     int16_t stepsTollInHome;      ///< tolerance steps leaving home sensor control while moving
135
136     int16_t timeoutHome;          ///< timeout in ms in home procedure
137
138         uint8_t powerRun;             ///< Vref voltage when motor runs (0-255)
139         uint8_t powerIdle;            ///< Vref voltage when motor is idle (0-255)
140
141         uint16_t homeSensorIndex;     ///< Home Sensor index in the sensor list
142         uint16_t levelSensorIndex;    ///< Level Sensor index in the sensor list
143
144         struct
145         {
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
153         } flags;
154 };
155
156
157 /**
158  * Motor context structure
159  */
160 struct Stepper
161 {
162         const struct StepperConfig *cfg; ///< Configuration of this stepper
163         fsm_state state;                 ///< Motor FSM state function
164
165         struct TimerCounter *timer;   ///< HW timer bound to this motor
166         uint16_t index;               ///< Index of the motor
167
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
173 #else
174         uint16_t rampValue;
175         uint32_t rampClock;
176 #endif
177
178         enum MotorDirection dir;      ///< Current direction
179         uint8_t power;                ///< Current power
180
181         uint16_t speed;               ///< Timer compare value to reach
182         int16_t stepToReach;          ///< Final position to reach when running
183
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)
186
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
191
192         int16_t stepsDeaf;            ///< Position after which start the level check
193         int16_t stepsLevel;           ///< Position of level contact
194
195         int16_t stepCircular;         ///< Steps corresponding to 360 degrees (rotating motor)
196 };
197
198
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);
222
223 #endif /* DRV_STEPPER_H */