Helper library for stepper motor control
Revision 0:2cfd64d210f3, committed 2015-06-29
- Comitter:
- SavinescuStefan
- Date:
- Mon Jun 29 07:47:45 2015 +0000
- Commit message:
- First commit.
Changed in this revision
steppercontroller.cpp | Show annotated file Show diff for this revision Revisions of this file |
steppercontroller.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 2cfd64d210f3 steppercontroller.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/steppercontroller.cpp Mon Jun 29 07:47:45 2015 +0000 @@ -0,0 +1,77 @@ + +#include "steppercontroller.h" + +StepperController::StepperController(PinName phaseA,PinName enA, PinName phaseB, PinName enB ): + phaseA(phaseA), enA(enA), phaseB(phaseB), enB(enB) +{ + this->enA.period(50e-6f); + this->enB.period(50e-6f); + pulseWidth = 0.1f; + state = 0; +} + +void StepperController::advance() +{ + state = (state + 4 + dir) & 3; + updateOutputs(); +} + +void StepperController::setPeriod(float period) +{ + enA.period(period); + enB.period(period); +} + +void StepperController::setPulseWidth(float pulseWidth) +{ + this->pulseWidth = pulseWidth; +} + +void StepperController::setDirection(Direction dir) +{ + this->dir = dir; +} + +void StepperController::updateOutputs() +{ + switch(state){ + case 0: + phaseA = 1; + enA = pulseWidth; + phaseB = 0; + enB = 0.0f; + break; + case 1: + phaseA = 0; + enA = pulseWidth; + phaseB = 0; + enB = 0.0f; + break; + + case 2: + phaseA = 0; + enA = 0.0f; + phaseB = 1; + enB = pulseWidth; + break; + + case 3: + phaseA = 0; + enA = 0.0f; + phaseB = 0; + enB = pulseWidth; + break; + + default: + phaseA = 0; + enA = 0.0; + phaseB = 0; + enB = 0.0; + } +} + + + + + + \ No newline at end of file
diff -r 000000000000 -r 2cfd64d210f3 steppercontroller.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/steppercontroller.h Mon Jun 29 07:47:45 2015 +0000 @@ -0,0 +1,26 @@ +#ifndef STEPPERCONTROLLER_H +#define STEPPERCONTROLLER_H +#include "mbed.h" + +class StepperController { + +public: + enum Direction {DirectionCW = 1, DirectionCCW = -1}; + StepperController(PinName phaseA,PinName enA, PinName phaseB, PinName enB ); + void advance(); + void setPeriod(float period); + void setPulseWidth(float width); + void setDirection(Direction dir); + +private: + int state; + float pulseWidth; + DigitalOut phaseA, phaseB; + PwmOut enA, enB; + Direction dir; + void updateOutputs(); + + +}; + + #endif \ No newline at end of file