stepper.h

Go to the documentation of this file.
00001 
00020 #ifndef DRV_STEPPER_H
00021 #define DRV_STEPPER_H
00022 
00023 #include <cfg/compiler.h>
00024 #include <algo/ramp.h>
00025 
00026 // Forward declaration
00027 struct Stepper;
00028 
00030 #define STEPS_INFINITE_POSITIVE        ((int16_t)0xFFFF)
00031 #define STEPS_INFINITE_NEGATIVE        ((int16_t)0x8FFF)
00032 
00034 #define MAX_STEPS                      0x7FFF
00035 
00037 #define MOTOR_NO_LEVEL_SENSOR          0xFFFF
00038 
00040 #define MOTOR_NO_HOME_SENSOR           0xFFFF
00041 
00043 #define DEAFSTEPS_DEFAULT              MAX_STEPS
00044 
00046 //\{
00047 #define SPEED_STOPPED                  0xFFFF    
00048 #define SPEED_HOMING                   0xFFFE    
00049 //\}
00050 
00051 // default values for steps inside and outside home sensor
00052 #define MOTOR_INSIDE_HOME_STEPS        10
00053 #define MOTOR_OUTSIDE_HOME_STEPS       40
00054 
00055 // default value for home sensor tolerance
00056 #define MOTOR_TOLERANCE_HOME_STEPS      2
00057 
00058 // default value for consecutive error
00059 #define MOTOR_CONSECUTIVE_ERROR_STEPS   3
00060 
00061 // values for the home control enabling
00062 enum MotorHomeSensorCheck
00063 {
00064     MOTOR_HOMESENSOR_NOCHECK = 0,
00065     MOTOR_HOMESENSOR_INCHECK,
00066     MOTOR_HOMESENSOR_OUTCHECK
00067 };
00068 
00069 // default value in ms for home procedure timeout
00070 #define MOTOR_TIMEOUT_HOME              20000
00071 
00075 enum MotorDirection
00076 {
00077     DIR_POSITIVE = 1,       
00078     DIR_NONE = 0,           
00079     DIR_NEGATIVE = -1       
00080 };
00081 
00082 #define STEPPER_MAX_STATES    32
00083 
00084 
00088 enum StepperState
00089 {
00090     MSTS_UNINIT,        
00091     MSTS_RUN,           
00092     MSTS_IDLE,          
00093     MSTS_PREIDLE,       
00094     MSTS_PRERUN,        
00095 
00096 // Home procedure
00097     MSTS_PREINIT,       
00098     MSTS_INIT,          
00099     MSTS_ENTERING,      
00100     MSTS_LEAVING,       
00101     MSTS_OUTHOME,       
00102 
00103     MSTS_ERROR,         
00104 
00106     MSTS_DUMMY_ALIGN = STEPPER_MAX_STATES - 1
00107 };
00108 
00110 typedef enum StepperState (*fsm_state)(struct Stepper* );
00111 
00113 typedef void (*stepper_isr_t)(struct Stepper* );
00114 
00116 typedef uint16_t stepper_time_t;
00117 
00121 struct StepperConfig
00122 {
00123     struct Ramp ramp;             
00124     uint16_t pulse;               
00125 
00126     fsm_state states[STEPPER_MAX_STATES];  
00127 
00128     int16_t stepsInHome;          
00129     int16_t stepsOutHome;         
00130     uint16_t clocksHome;          
00131 
00132     int16_t stepsTollOutHome;     
00133     int16_t stepsTollInHome;      
00134 
00135     int16_t timeoutHome;          
00136 
00137     uint8_t powerRun;             
00138     uint8_t powerIdle;            
00139 
00140     uint16_t homeSensorIndex;     
00141     uint16_t levelSensorIndex;    
00142 
00143     struct
00144     {
00145         bool homeInverted : 1;    
00146         bool halfStep : 1;        
00147         bool axisInverted : 1;    
00148         bool levelInverted : 1;   
00149         bool controlBit : 1;      
00150         bool controlMoveBit : 1;  
00151         bool highcurrentBit : 1;  
00152     } flags;
00153 };
00154 
00155 
00159 struct Stepper
00160 {
00161     const struct StepperConfig *cfg; 
00162     fsm_state state;                 
00163 
00164     struct TimerCounter *timer;   
00165     uint16_t index;               
00166 
00167     volatile int16_t step;        
00168     volatile int16_t rampStep;    
00169 #if RAMP_USE_FLOATING_POINT
00170     float rampValue;              
00171     float rampClock;              
00172 #else
00173     uint16_t rampValue;
00174     uint32_t rampClock;
00175 #endif
00176 
00177     enum MotorDirection dir;      
00178     uint8_t power;                
00179 
00180     uint16_t speed;               
00181     int16_t stepToReach;          
00182 
00183     int16_t skipIrqs;            
00184     int16_t changeCurrentIrqs;   
00185 
00186     int8_t  enableCheckHome;      
00187     int8_t  stepsErrorHome;       
00188     int16_t stepsTollMax;         
00189     int16_t stepsTollMin;         
00190 
00191     int16_t stepsDeaf;            
00192     int16_t stepsLevel;           
00193 
00194     int16_t stepCircular;         
00195 };
00196 
00197 
00198 void stepper_init(void);
00199 void stepper_end(void);
00200 struct Stepper *stepper_setup(int index, struct StepperConfig *cfg);
00201 void stepper_disable(void);
00202 void stepper_reset(struct Stepper *motor);
00203 void stepper_home(struct Stepper *motor);
00204 void stepper_setStep(struct Stepper *motor, int16_t step);
00205 int16_t stepper_getStep(struct Stepper *motor);
00206 int16_t stepper_move(struct Stepper *motor, int16_t step, uint16_t speed, int16_t deafstep);
00207 void stepper_stop(struct Stepper *motor);
00208 void stepper_break(struct Stepper *motor, enum StepperState state);
00209 bool stepper_idle(struct Stepper *motor);
00210 bool stepper_readHome(struct Stepper *motor);
00211 bool stepper_readLevel(struct Stepper *motor);
00212 void stepper_updateHalfStep(struct Stepper *motor);
00213 void stepper_updateControlBit(struct Stepper *motor);
00214 void stepper_updateControlMoveBit(struct Stepper *motor);
00215 bool stepper_error(struct Stepper *motor);
00216 bool stepper_inhome(struct Stepper *motor);
00217 int16_t stepper_getLevelStep(struct Stepper *motor);
00218 void stepper_set_stepCircular(struct Stepper *motor, int16_t steps);
00219 int16_t stepper_get_stepCircular(struct Stepper *motor);
00220 int16_t stepper_scaleSteps(struct Stepper *motor, int16_t dir);
00221 
00222 #endif /* DRV_STEPPER_H */