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