update
Stepper.h@0:f2a3b0be279d, 2013-04-15 (annotated)
- Committer:
- mederic
- Date:
- Mon Apr 15 11:29:15 2013 +0000
- Revision:
- 0:f2a3b0be279d
- Child:
- 1:a456aa3935ca
Published as library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mederic | 0:f2a3b0be279d | 1 | #ifndef STEPPER_H |
mederic | 0:f2a3b0be279d | 2 | #define STEPPER_H |
mederic | 0:f2a3b0be279d | 3 | |
mederic | 0:f2a3b0be279d | 4 | #include "mbed.h" |
mederic | 0:f2a3b0be279d | 5 | /** Stepper class. |
mederic | 0:f2a3b0be279d | 6 | * Used for drive Stepper motor |
mederic | 0:f2a3b0be279d | 7 | * |
mederic | 0:f2a3b0be279d | 8 | * Example: |
mederic | 0:f2a3b0be279d | 9 | * @code |
mederic | 0:f2a3b0be279d | 10 | * #include "mbed.h" |
mederic | 0:f2a3b0be279d | 11 | * #include "Stepper.h" |
mederic | 0:f2a3b0be279d | 12 | * |
mederic | 0:f2a3b0be279d | 13 | * DigitalIn home(p20); |
mederic | 0:f2a3b0be279d | 14 | * Stepper mot(p21,p22); |
mederic | 0:f2a3b0be279d | 15 | * |
mederic | 0:f2a3b0be279d | 16 | * int main() |
mederic | 0:f2a3b0be279d | 17 | * { |
mederic | 0:f2a3b0be279d | 18 | * mot.setSpeed(1200); |
mederic | 0:f2a3b0be279d | 19 | * mot.setAcceleration(4000); |
mederic | 0:f2a3b0be279d | 20 | * mot.setDeceleration(4000); |
mederic | 0:f2a3b0be279d | 21 | * |
mederic | 0:f2a3b0be279d | 22 | * mot.rotate(CW); |
mederic | 0:f2a3b0be279d | 23 | * while(!home); |
mederic | 0:f2a3b0be279d | 24 | * mot.stop(); |
mederic | 0:f2a3b0be279d | 25 | * mot.setPositionZero(); |
mederic | 0:f2a3b0be279d | 26 | * |
mederic | 0:f2a3b0be279d | 27 | * mot.goesTo(1600); |
mederic | 0:f2a3b0be279d | 28 | * while(!mot.stopped()); |
mederic | 0:f2a3b0be279d | 29 | * |
mederic | 0:f2a3b0be279d | 30 | * while(1) |
mederic | 0:f2a3b0be279d | 31 | * { |
mederic | 0:f2a3b0be279d | 32 | * |
mederic | 0:f2a3b0be279d | 33 | * } |
mederic | 0:f2a3b0be279d | 34 | * } |
mederic | 0:f2a3b0be279d | 35 | * @endcode |
mederic | 0:f2a3b0be279d | 36 | */ |
mederic | 0:f2a3b0be279d | 37 | class Stepper |
mederic | 0:f2a3b0be279d | 38 | { |
mederic | 0:f2a3b0be279d | 39 | public: |
mederic | 0:f2a3b0be279d | 40 | /** Create Stepper instance connected to pin clk & dir |
mederic | 0:f2a3b0be279d | 41 | * @param clk pin to connect at clk/step output |
mederic | 0:f2a3b0be279d | 42 | * @param dir pin to connect at dir output |
mederic | 0:f2a3b0be279d | 43 | */ |
mederic | 0:f2a3b0be279d | 44 | Stepper(PinName clk, PinName dir); |
mederic | 0:f2a3b0be279d | 45 | |
mederic | 0:f2a3b0be279d | 46 | /** Moving the motor relative to a reference position (set by setPositionZero()) |
mederic | 0:f2a3b0be279d | 47 | * @param position steps from position zero |
mederic | 0:f2a3b0be279d | 48 | */ |
mederic | 0:f2a3b0be279d | 49 | void goesTo(int position); |
mederic | 0:f2a3b0be279d | 50 | |
mederic | 0:f2a3b0be279d | 51 | /** Moving the motor for given steps |
mederic | 0:f2a3b0be279d | 52 | * @param steps number of steps to go(>0 CW ; <0 CCW) |
mederic | 0:f2a3b0be279d | 53 | */ |
mederic | 0:f2a3b0be279d | 54 | void move(int steps); |
mederic | 0:f2a3b0be279d | 55 | |
mederic | 0:f2a3b0be279d | 56 | /** Moving the motor until user stop |
mederic | 0:f2a3b0be279d | 57 | * @param direction rotation Stepper::CC or Stepper::CCW |
mederic | 0:f2a3b0be279d | 58 | */ |
mederic | 0:f2a3b0be279d | 59 | void rotate(bool direction); |
mederic | 0:f2a3b0be279d | 60 | |
mederic | 0:f2a3b0be279d | 61 | /** Stop the motor as fast as possible |
mederic | 0:f2a3b0be279d | 62 | */ |
mederic | 0:f2a3b0be279d | 63 | void stop(void); |
mederic | 0:f2a3b0be279d | 64 | |
mederic | 0:f2a3b0be279d | 65 | /**Set Rotation Speed |
mederic | 0:f2a3b0be279d | 66 | * @param speed [steps/sec] |
mederic | 0:f2a3b0be279d | 67 | */ |
mederic | 0:f2a3b0be279d | 68 | void setSpeed(float speed); |
mederic | 0:f2a3b0be279d | 69 | |
mederic | 0:f2a3b0be279d | 70 | /**Set Acceleration ramp |
mederic | 0:f2a3b0be279d | 71 | * @param acc acceleration [steps/sec²] (0 = infinite acceleration 1st step @ max speed) |
mederic | 0:f2a3b0be279d | 72 | */ |
mederic | 0:f2a3b0be279d | 73 | void setAcceleration(float acc); |
mederic | 0:f2a3b0be279d | 74 | |
mederic | 0:f2a3b0be279d | 75 | /**Set Deceleration ramp |
mederic | 0:f2a3b0be279d | 76 | * @param dec deceleration [steps/sec²] (0 = infinite deceleration last step @ max speed) |
mederic | 0:f2a3b0be279d | 77 | */ |
mederic | 0:f2a3b0be279d | 78 | void setDeceleration(float dec); |
mederic | 0:f2a3b0be279d | 79 | |
mederic | 0:f2a3b0be279d | 80 | /**Give Origin for motor absolute movement |
mederic | 0:f2a3b0be279d | 81 | */ |
mederic | 0:f2a3b0be279d | 82 | void setPositionZero(void); |
mederic | 0:f2a3b0be279d | 83 | |
mederic | 0:f2a3b0be279d | 84 | /**Check if motor is stopped |
mederic | 0:f2a3b0be279d | 85 | * @returns true if motor stopped |
mederic | 0:f2a3b0be279d | 86 | */ |
mederic | 0:f2a3b0be279d | 87 | bool stopped(void); |
mederic | 0:f2a3b0be279d | 88 | |
mederic | 0:f2a3b0be279d | 89 | /**Get absolute position from origin |
mederic | 0:f2a3b0be279d | 90 | * @returns position [steps] from origin set by setPositionZero() |
mederic | 0:f2a3b0be279d | 91 | */ |
mederic | 0:f2a3b0be279d | 92 | int getPosition(void); |
mederic | 0:f2a3b0be279d | 93 | |
mederic | 0:f2a3b0be279d | 94 | /**Get Acceleration |
mederic | 0:f2a3b0be279d | 95 | * @returns acceleration [steps/sec²] |
mederic | 0:f2a3b0be279d | 96 | */ |
mederic | 0:f2a3b0be279d | 97 | float getAcceleration(void); |
mederic | 0:f2a3b0be279d | 98 | |
mederic | 0:f2a3b0be279d | 99 | /**Get Deceleration |
mederic | 0:f2a3b0be279d | 100 | * @returns deceleration [steps/sec²] |
mederic | 0:f2a3b0be279d | 101 | */ |
mederic | 0:f2a3b0be279d | 102 | float getDeceleration(void); |
mederic | 0:f2a3b0be279d | 103 | |
mederic | 0:f2a3b0be279d | 104 | /**Get Speed |
mederic | 0:f2a3b0be279d | 105 | * @returns speed [steps/sec] |
mederic | 0:f2a3b0be279d | 106 | */ |
mederic | 0:f2a3b0be279d | 107 | float getSpeed(void); |
mederic | 0:f2a3b0be279d | 108 | |
mederic | 0:f2a3b0be279d | 109 | /**Enum for direction |
mederic | 0:f2a3b0be279d | 110 | * CW=true or 1; CCW=false or 0 |
mederic | 0:f2a3b0be279d | 111 | */ |
mederic | 0:f2a3b0be279d | 112 | typedef enum {CW=1,CCW=0} direction; |
mederic | 0:f2a3b0be279d | 113 | |
mederic | 0:f2a3b0be279d | 114 | protected: |
mederic | 0:f2a3b0be279d | 115 | void run(void); //step generator thread |
mederic | 0:f2a3b0be279d | 116 | unsigned int nTo(float speed,float acc); |
mederic | 0:f2a3b0be279d | 117 | |
mederic | 0:f2a3b0be279d | 118 | private: |
mederic | 0:f2a3b0be279d | 119 | float _acc; //Acceleration [step/s²] |
mederic | 0:f2a3b0be279d | 120 | float _dec; //Decceleration [step/s²] |
mederic | 0:f2a3b0be279d | 121 | float _spd; //Speed [step/s] |
mederic | 0:f2a3b0be279d | 122 | unsigned int _steps; //nbr total of steps per mov |
mederic | 0:f2a3b0be279d | 123 | DigitalOut _clk; //output clock pin |
mederic | 0:f2a3b0be279d | 124 | DigitalOut _dir; //output dir pin |
mederic | 0:f2a3b0be279d | 125 | Timeout _timer; //step timer |
mederic | 0:f2a3b0be279d | 126 | enum {STOP,ACCEL,CRUISE,DECEL}_state; //Motor state |
mederic | 0:f2a3b0be279d | 127 | unsigned int _dt0; //initial delay [µs] |
mederic | 0:f2a3b0be279d | 128 | unsigned int _dtmin; //delay minimum [µs] |
mederic | 0:f2a3b0be279d | 129 | int _pos; //motor position |
mederic | 0:f2a3b0be279d | 130 | }; |
mederic | 0:f2a3b0be279d | 131 | |
mederic | 0:f2a3b0be279d | 132 | #endif |