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