Language: EN

libreria-arduino-asyncservo

Arduino AsyncServo Library

Arduino library that allows non-blocking movement of a servo. This creates a servo with a certain “asynchronous” behavior.

The AsyncServo class implements a servo whose movements are timed, instead of being blocking. The objective of the library is to be able to move one or more servos in a project, even at different speeds, while not preventing the execution of other tasks in the main control loop.

An Asyncservo object is instantiated through its constructor, similar to that of a conventional servo. The position can be indicated by three actions,

  • Write, receives the angle in tenths of a degree.
  • WriteDegrees, receives the angle in degrees.
  • WriteMicroseconds, receives the angle in microseconds.

The default option Write is used in tenths of a degree because it provides superior precision to indicate the angles in degrees with WriteDegree, but without adding the complexity of indicating microseconds with WriteMicroseconds.

The “asynchronous” behavior is achieved through the Move and MoveDegress functions, which receive the desired position for the servo and the displacement time.

To update the servo’s position, it is necessary to call the Update function, which must be called frequently from the main control loop.

Optionally, the Move and MoveDegress methods allow a final callback function, which is useful for forming chained movements and loops.

Additionally, there are other functions to improve the use of a servo in a real project. These functions are SetInput and SetOutput, which establish the input and output range for the servo, and help calibrate the servo. This is very useful in the case of articulated robots. For example, it is possible to map the values of -90°, 0°, and 90° to 0.1°, 90.5°, and 179.9°.

User Manual

The AsyncServo class can be instantiated as an object through its constructor, and is updated with the Update() method.

// Constructor
AsyncServo();

// Attach the AsyncServo to an I/O pin
uint8_t Attach(int pin);
void Detach();

// Update the servo's position
// This method must be called frequently from the main loop
void Update();

// Set the input and output range mapping
void SetInput(int low, int mid, int high);
void SetOutput(int low, int mid, int high);

// Instantly move the servo
void write(int dec);
void WriteDegree(int deg);
void WriteMicroseconds(int uS);

// Servo status
int Read();
int16_t GetCurrentPosition() const;

// Move the servo in a timed ("asynchronous") manner
void Move(int dec, int timeMs);
void Move(int dec, int timeMs, ServoAction finish);
void MoveDegrees(int deg, int timeMs);
void MoveDegrees(int deg, int timeMs, ServoAction finish);
void Stop();

// Callback when a timed movement ends
ServoAction Finish;

Examples

The AsyncServo library includes the following examples to illustrate its use.

  • Sweep: Example that shows how to create a rocking effect with AsyncServo
#include "AsyncServoLib.h"

AsyncServo asyncServo;

void setup()
{
  Serial.begin(115200);
  Serial.println("Restart");
  
  asyncServo.Attach(9);
  asyncServo.SetOutput(500, 1500, 2500);

  step1();
}

void step1()
{
  asyncServo.Move(1350, 4000, step2);
}

void step2()
{
  asyncServo.Move(450, 4000, step1);
}

void loop()
{
  asyncServo.Update();
}
  • TwoServos: Example that extends the Sweep example to two servos
#include "AsyncServoLib.h"

AsyncServo asyncServo09;
AsyncServo asyncServo10;

void setup()
{
  Serial.begin(115200);
  Serial.println("Restart");
  
  asyncServo09.Attach(9);
  asyncServo10.Attach(10);

  asyncServo09.SetOutput(500, 1500, 2500);
  asyncServo10.SetOutput(500, 1500, 2500);
  delay(1000);
  asyncServo10.write(450);
  delay(1000);
  asyncServo10.write(1350);
  delay(1000);
  asyncServo10.write(900);
  delay(1000);

  step1();
  step4();
}

void step1()
{
  asyncServo10.Move(1350, 2000, step2);
}

void step2()
{
  asyncServo10.Move(450, 2000, step1);
}

void step4()
{
  asyncServo09.Move(1350, 4000, step5);
}

void step5()
{
  asyncServo09.Move(450, 4000, step4);
}

void loop()
{
  delay(10);
  asyncServo09.Update();
  asyncServo10.Update();
}

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