Update preset.
[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  *
11  * \author Francesco Michelini <francesco.michelini@seacfi.com>
12  * \author Giovanni Bajo <rasky@develer.com>
13  * \author Bernie Innocenti <bernie@codewiz.org>
14  * \author Simone Zinanni <s.zinanni@develer.com>
15  * \author Daniele Basile <asterix@develer.com>
16  *
17  * $WIZ$ module_name = "stepper"
18  * $WIZ$ module_depends = "timer", "ramp"
19  * $WIZ$ module_configuration = "bertos/cfg/cfg_stepper.h"
20  * $WIZ$ module_hw = "bertos/hw/hw_stepper.h"
21  * $WIZ$ module_supports = "not avr"
22  */
23
24 #ifndef DRV_STEPPER_H
25 #define DRV_STEPPER_H
26
27 #include <cfg/compiler.h>
28
29 #include <algo/ramp.h>
30
31 // Forward declaration
32 struct Stepper;
33
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)
37
38 ///< Maximum value for stepper steps
39 #define MAX_STEPS                      0x7FFF
40
41 ///< Default value -> no level sensor associated to the motor
42 #define MOTOR_NO_LEVEL_SENSOR          0xFFFF
43
44 ///< Default value -> no home sensor associated to the motor
45 #define MOTOR_NO_HOME_SENSOR           0xFFFF
46
47 ///< Default value for deafsteps in normal movement (no level sensor)
48 #define DEAFSTEPS_DEFAULT              MAX_STEPS
49
50 ///< Out-of-band values for speed
51 //\{
52 #define SPEED_STOPPED                  0xFFFF    ///< motor is stopped
53 #define SPEED_HOMING                   0xFFFE    ///< motor is homing
54 //\}
55
56 // default values for steps inside and outside home sensor
57 #define MOTOR_INSIDE_HOME_STEPS        10
58 #define MOTOR_OUTSIDE_HOME_STEPS       40
59
60 // default value for home sensor tolerance
61 #define MOTOR_TOLERANCE_HOME_STEPS      2
62
63 // default value for consecutive error
64 #define MOTOR_CONSECUTIVE_ERROR_STEPS   3
65
66 // values for the home control enabling
67 enum MotorHomeSensorCheck
68 {
69         MOTOR_HOMESENSOR_NOCHECK = 0,
70         MOTOR_HOMESENSOR_INCHECK,
71         MOTOR_HOMESENSOR_OUTCHECK
72 };
73
74 // default value in ms for home procedure timeout
75 #define MOTOR_TIMEOUT_HOME              20000
76
77 /**
78  * Motor direction
79  */
80 enum MotorDirection
81 {
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)
85 };
86
87 #define STEPPER_MAX_STATES    32
88
89
90 /**
91  * Stepper state-machine conditions
92  */
93 enum StepperState
94 {
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
100
101 // Home procedure
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)
107
108         MSTS_ERROR,         ///< error status
109
110         ///< Dummy entry to guarantee the right underlying size for the enum
111         MSTS_DUMMY_ALIGN = STEPPER_MAX_STATES - 1
112 };
113
114 ///< Pointer to a function handling a state of the FSM driving the motor
115 typedef enum StepperState (*fsm_state)(struct Stepper* );
116
117 ///< Pointer to a isr stepper function
118 typedef void (*stepper_isr_t)(struct Stepper* );
119
120 ///< Time for steppers motor
121 typedef uint16_t stepper_time_t;
122
123 /**
124  * Stepper configuration
125  */
126 struct StepperConfig
127 {
128         struct Ramp ramp;             ///< Acceleration ramp
129         uint16_t pulse;               ///< (clocks) Length of the clock pulse used to drive the motor
130
131         fsm_state states[STEPPER_MAX_STATES];  ///< Custom FSM states (or NULL for default handling)
132
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
136
137     int16_t stepsTollOutHome;     ///< tolerance steps leaving home sensor control while moving
138     int16_t stepsTollInHome;      ///< tolerance steps leaving home sensor control while moving
139
140     int16_t timeoutHome;          ///< timeout in ms in home procedure
141
142         uint8_t powerRun;             ///< Vref voltage when motor runs (0-255)
143         uint8_t powerIdle;            ///< Vref voltage when motor is idle (0-255)
144
145         uint16_t homeSensorIndex;     ///< Home Sensor index in the sensor list
146         uint16_t levelSensorIndex;    ///< Level Sensor index in the sensor list
147
148         struct
149         {
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
157         } flags;
158 };
159
160
161 /**
162  * Motor context structure
163  */
164 struct Stepper
165 {
166         const struct StepperConfig *cfg; ///< Configuration of this stepper
167         fsm_state state;                 ///< Motor FSM state function
168
169         struct TimerCounter *timer;   ///< HW timer bound to this motor
170         uint16_t index;               ///< Index of the motor
171
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
177 #else
178         uint16_t rampValue;
179         uint32_t rampClock;
180 #endif
181
182         enum MotorDirection dir;      ///< Current direction
183         uint8_t power;                ///< Current power
184
185         uint16_t speed;               ///< Timer compare value to reach
186         int16_t stepToReach;          ///< Final position to reach when running
187
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)
190
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
195
196         int16_t stepsDeaf;            ///< Position after which start the level check
197         int16_t stepsLevel;           ///< Position of level contact
198
199         int16_t stepCircular;         ///< Steps corresponding to 360 degrees (rotating motor)
200 };
201
202
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);
226
227 #endif /* DRV_STEPPER_H */