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