A simple stepper motor driver library, supporting micro-stepping drivers such as the Pololu A4988 stepper driver carrier or the Sparkfun EasyDriver.
Fork of StepperMotor by
Revision 1:6fbe104c6f37, committed 2016-11-03
- Comitter:
- kfforex
- Date:
- Thu Nov 03 17:14:37 2016 +0000
- Parent:
- 0:52fb09e87581
- Commit message:
- before publish
Changed in this revision
Stepper.cpp | Show annotated file Show diff for this revision Revisions of this file |
Stepper.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 52fb09e87581 -r 6fbe104c6f37 Stepper.cpp --- a/Stepper.cpp Sun Feb 24 16:23:02 2013 +0000 +++ b/Stepper.cpp Thu Nov 03 17:14:37 2016 +0000 @@ -1,41 +1,45 @@ #include "Stepper.h" #include "mbed.h" -stepper::stepper(PinName _en, PinName ms1, PinName ms2, PinName ms3, PinName _stepPin, PinName dir):en(_en), - microstepping(ms1, ms2, ms3), +stepper::stepper(PinName _en, PinName _stepPin, PinName dir):en(_en), stepPin(_stepPin), direction(dir) { } -void stepper::step(int microstep, int dir, float speed) -{ - if (microstep == 1) { - microstepping = 0; - } else if (microstep <= 4) { - microstepping = microstep / 2; - } else if (microstep > 4) { - microstepping = (microstep / 2) - 1; +void stepper::step(float tspeed){ + + float speed; + //max acceleration limiting + if ((actualSpeed - tspeed) > MAX_ACCEL) + speed = actualSpeed - MAX_ACCEL; + else if ((actualSpeed - tspeed) < -MAX_ACCEL) + speed = actualSpeed + MAX_ACCEL; + else + speed = tspeed; + + if (speed == 0) { + toggler.detach(); } - if (dir == 1) { + else if(speed >0) { direction = 0; - } else if (dir == 0) { - direction = 1; + toggler.attach_us(this,&stepper::toggle_step,1000000/abs(speed)); } - - // Step... - stepPin = 1; - wait(1/speed); - stepPin = 0; - wait(1/speed); + else{ + direction = 1; + toggler.attach_us(this,&stepper::toggle_step,1000000/abs(speed)); + } + actualSpeed = speed; } -void stepper::enable() -{ +void stepper::enable(){ en = 0; } -void stepper::disable() -{ +void stepper::disable(){ en = 1; +} + +void stepper::toggle_step (){ + stepPin = !stepPin; } \ No newline at end of file
diff -r 52fb09e87581 -r 6fbe104c6f37 Stepper.h --- a/Stepper.h Sun Feb 24 16:23:02 2013 +0000 +++ b/Stepper.h Thu Nov 03 17:14:37 2016 +0000 @@ -3,17 +3,19 @@ #endif #include "mbed.h" +#define MAX_ACCEL 500 -class stepper -{ +class stepper{ public: - stepper(PinName _en, PinName ms1, PinName ms2, PinName ms3, PinName _stepPin, PinName dir); - void step(int microstep, int dir, float speed); + stepper(PinName _en, PinName _stepPin, PinName dir); + void step(float speed); void enable(); void disable(); + void toggle_step(); private: + float actualSpeed; DigitalOut en; - BusOut microstepping; DigitalOut stepPin; DigitalOut direction; + Ticker toggler; }; \ No newline at end of file