Language: EN

libreria-asyncstepper-v2-0

AsyncStepper Library v2.0

We have updated the AsyncStepper library to a well-deserved version 2.0. The main novelty is to incorporate linear acceleration, although it has been completely rewritten to improve its use and performance.

We remind you that the AsyncStepper library allows non-blocking movement of a stepper motor, that is, allowing the processor to perform actions while moving one or more motors at different speeds. Basically, with AsyncStepper you can do whatever you want with a stepper motor, without having to worry about practically anything. You just have to call the Update() function frequently, and AsyncStepper takes care of doing the necessary calculations.

AsyncStepper stepper1(stepper_steps, stepper_dir_pin, stepper_step_pin);

void setup()
{
  Serial.begin(115200);

  stepper1.SetSpeedRpm(30);
  stepper1.RotateAngle(360.0f * 2 + 270.0f, AsyncStepper::CW);
}

void loop()
{
  stepper1.Update();
}

Ideally, you should call Update() before the next step of the motor. In case of very long waits, AsyncStepper will try to recover the remaining steps, but this is a scenario to avoid.

AsyncStepper is designed to be used with drivers such as A4988 or DRV8825. Alternatively, it can be controlled with another type of driver (or without a driver) by providing the callBack actionCW() and actionCCW() functions that contain, respectively, the necessary code to move a step clockwise and counterclockwise.

Additionally, a function OnFinish() can be provided, which runs at the end of a movement, and can be used to update the status in the program or, for example, chain actions.

stepper1.OnFinish = []()
{  
  stepper1.RotateAngle(360.0f * 2 + 270.0f, AsyncStepper::CCW);
};

User Manual

The AsyncStepper class can be instantiated as an object through one of its constructors,

AsyncStepper(uint16_t motorSteps, int pinDir, int pinStep)
AsyncStepper(uint16_t motorSteps, StepperCallback actionCW, StepperCallback actionCCW)

Using AsyncStepper

// Move a certain angle
void Rotate(float angleDelta, StepDirection direction);
void Rotate(float angleDelta, StepDirection direction, StepCallback callback);
  
// Move to a certain angle
void RotateToAngle(float angle, StepDirection direction, StepCallback callback);
void RotateToAngle(float angle, StepDirection direction);
 
// Move in a certain time in seconds
void RotateAngleInTime(float angle, float time, StepperDirection direction, StepperCallback onFinish = nullptr);
void RotateToAngleInTime(float angle, float time, StepperDirection direction, StepperCallback onFinish = nullptr);
 
// Move continuously
void RotateContinuos(StepDirection direction);

// Change speed and acceleration
void SetSpeed(long speed);
void SetSpeedRpm(float rpm);
void SetSpeedDegreesBySecond(float degreesBySecond);
void SetAcceleration(long acceleration);
void SetAcceleration(long acceleration, long deceleration);

// Stop the stepper motor
void Stop();
void Break();

// Update the position of the stepper motor
// This method must be called frequently from the main loop
bool Update();

// Get information
long GetRemainSteps();
unsigned long GetTimeTraveling();
float GetCurrentAngle();
long GetCurrentInterval();
long GetTravelCurrentStep();
long GetTravelSteps();
long GetAbsoluteStep();
long GetMaxSpeed();
long GetCurrentSpeed();
float GetCurrentSpeedRpm();
float GetCurrentSpeedDegreesBySecond();
float GetSpeedForMove(long steps, float time);
float GetTimeForMove(long steps);
float GetTimeForMove(long steps, unsigned long speed);

Installation

  • Download the latest version from GitHub
  • Unzip the file
  • Copy to your libraries folder (usually My Documents\Arduino\libraries)
  • Relaunch the Arduino IDE

github-full