A simple stepper motor driver library, supporting micro-stepping drivers such as the Pololu A4988 stepper driver carrier or the Sparkfun EasyDriver.

Dependents:   HangLiu_copy

Fork of StepperMotor by Matthew Else

Stepper.cpp

Committer:
kfforex
Date:
2016-11-03
Revision:
1:6fbe104c6f37
Parent:
0:52fb09e87581

File content as of revision 1:6fbe104c6f37:

#include "Stepper.h"
#include "mbed.h"

stepper::stepper(PinName _en, PinName _stepPin, PinName dir):en(_en),
    stepPin(_stepPin),
    direction(dir)
{
}

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();
    }
    else if(speed >0) {
        direction = 0;
        toggler.attach_us(this,&stepper::toggle_step,1000000/abs(speed)); 
    }
    else{
        direction = 1;
        toggler.attach_us(this,&stepper::toggle_step,1000000/abs(speed)); 
    }
    actualSpeed = speed; 
}

void stepper::enable(){
    en = 0;
}

void stepper::disable(){
    en = 1;
}

void stepper::toggle_step (){
    stepPin = !stepPin; 
}