Class library for a Stepper Motor to be able to Select amount of steps,set the time interval between steps, run stepper motor freely and stop the stepper motor.
Stepper.cpp
- Committer:
- Armand
- Date:
- 2017-03-03
- Revision:
- 0:b0c267f9cc05
- Child:
- 2:bfdd7246ac5e
File content as of revision 0:b0c267f9cc05:
#include "Stepper.h" #include "mbed.h" Stepper::Stepper(PinName Coil1, PinName Coil2, PinName Coil3, PinName Coil4) : _Coils(Coil1, Coil2, Coil3, Coil4) { _Coils = 0; stepcount = 0; Prevtime = 0; secinter = 0.5; lastsecinter = 0; StepDir = 1; stepping = 0; stepincr = 0; stepper_steps[0] = 0x09; stepper_steps[1] = 0x0A; stepper_steps[2] = 0x06; stepper_steps[3] = 0x05; } void Stepper::Run() { run = 1; stepping = 0; if(secinter != lastsecinter) { ticker.attach(this, &Stepper::stepper_isr,secinter); lastsecinter = secinter; } } void Stepper::Stop() { run = 0; stepping = 0; } void Stepper::Direction(int dir) { if(dir == 1) { StepDir = 1; } else if(dir == -1) { StepDir = -1; } } void Stepper::Interval(float sec) { secinter = sec; if(secinter != lastsecinter) { ticker.attach(this, &Stepper::stepper_isr,secinter); lastsecinter = secinter; } } void Stepper::Step(int steps) { Steps = steps; stepincr = 0; run = 0; stepping = 1; if(secinter != lastsecinter) { ticker.attach(this, &Stepper::stepper_isr,secinter); lastsecinter = secinter; } } void Stepper::stepper_isr() { if(run == 1) { _Coils = stepper_steps[stepcount]; stepcount = stepcount + StepDir; if(stepcount > 3) { stepcount = 0; } if(stepcount < 0) { stepcount = 3; } } if(stepping == 1 && stepincr < Steps) { _Coils = stepper_steps[stepcount]; stepcount = stepcount + StepDir; stepincr++; if(stepcount > 3) { stepcount = 0; } if(stepcount < 0) { stepcount = 3; } } }