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