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
Stepper.cpp@1:6fbe104c6f37, 2016-11-03 (annotated)
- Committer:
- kfforex
- Date:
- Thu Nov 03 17:14:37 2016 +0000
- Revision:
- 1:6fbe104c6f37
- Parent:
- 0:52fb09e87581
before publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
melse | 0:52fb09e87581 | 1 | #include "Stepper.h" |
melse | 0:52fb09e87581 | 2 | #include "mbed.h" |
melse | 0:52fb09e87581 | 3 | |
kfforex | 1:6fbe104c6f37 | 4 | stepper::stepper(PinName _en, PinName _stepPin, PinName dir):en(_en), |
melse | 0:52fb09e87581 | 5 | stepPin(_stepPin), |
melse | 0:52fb09e87581 | 6 | direction(dir) |
melse | 0:52fb09e87581 | 7 | { |
melse | 0:52fb09e87581 | 8 | } |
melse | 0:52fb09e87581 | 9 | |
kfforex | 1:6fbe104c6f37 | 10 | void stepper::step(float tspeed){ |
kfforex | 1:6fbe104c6f37 | 11 | |
kfforex | 1:6fbe104c6f37 | 12 | float speed; |
kfforex | 1:6fbe104c6f37 | 13 | //max acceleration limiting |
kfforex | 1:6fbe104c6f37 | 14 | if ((actualSpeed - tspeed) > MAX_ACCEL) |
kfforex | 1:6fbe104c6f37 | 15 | speed = actualSpeed - MAX_ACCEL; |
kfforex | 1:6fbe104c6f37 | 16 | else if ((actualSpeed - tspeed) < -MAX_ACCEL) |
kfforex | 1:6fbe104c6f37 | 17 | speed = actualSpeed + MAX_ACCEL; |
kfforex | 1:6fbe104c6f37 | 18 | else |
kfforex | 1:6fbe104c6f37 | 19 | speed = tspeed; |
kfforex | 1:6fbe104c6f37 | 20 | |
kfforex | 1:6fbe104c6f37 | 21 | if (speed == 0) { |
kfforex | 1:6fbe104c6f37 | 22 | toggler.detach(); |
melse | 0:52fb09e87581 | 23 | } |
kfforex | 1:6fbe104c6f37 | 24 | else if(speed >0) { |
melse | 0:52fb09e87581 | 25 | direction = 0; |
kfforex | 1:6fbe104c6f37 | 26 | toggler.attach_us(this,&stepper::toggle_step,1000000/abs(speed)); |
melse | 0:52fb09e87581 | 27 | } |
kfforex | 1:6fbe104c6f37 | 28 | else{ |
kfforex | 1:6fbe104c6f37 | 29 | direction = 1; |
kfforex | 1:6fbe104c6f37 | 30 | toggler.attach_us(this,&stepper::toggle_step,1000000/abs(speed)); |
kfforex | 1:6fbe104c6f37 | 31 | } |
kfforex | 1:6fbe104c6f37 | 32 | actualSpeed = speed; |
melse | 0:52fb09e87581 | 33 | } |
melse | 0:52fb09e87581 | 34 | |
kfforex | 1:6fbe104c6f37 | 35 | void stepper::enable(){ |
melse | 0:52fb09e87581 | 36 | en = 0; |
melse | 0:52fb09e87581 | 37 | } |
melse | 0:52fb09e87581 | 38 | |
kfforex | 1:6fbe104c6f37 | 39 | void stepper::disable(){ |
melse | 0:52fb09e87581 | 40 | en = 1; |
kfforex | 1:6fbe104c6f37 | 41 | } |
kfforex | 1:6fbe104c6f37 | 42 | |
kfforex | 1:6fbe104c6f37 | 43 | void stepper::toggle_step (){ |
kfforex | 1:6fbe104c6f37 | 44 | stepPin = !stepPin; |
melse | 0:52fb09e87581 | 45 | } |