Here is a reference to install a third-party library into our Arduino IDE.
Create an object of UARTServo after include UARTServo.h.
#include "UARTServo.h"
UARTServo servo;
Initialize this Object in setup(), we use the begin() function:
void begin(HardwareSerial* serial = &Serial, unsigned long baud = BAUD_RATE);
Initializes the UART servo library and communication settings, it should be placed in function setup(). The first parameter is a pointer to the class HardwareSerial, default value is Serial. The second parameter is transmission rate, default value is BAUD_RATE.
void setup()
{
servo.begin(&Serial, 115200);
}
- Polling Update the data of the UARTServo object.
void loop()
{
servo.update();
}
Detect status of the specified servo, if the servo is online, it will send back its number.
// Ping the servo with id 1, and response id into pingCallback();
servo.ping(1, pingCallback);
// Ping the servo with id 2 without callback function,
// but it makes no sense to do so.
servo.ping(2, NULL);
// Callback function for ping(),
// if the specified servo exists, its id will be treated as a
// parameter passed into this function.
void pingCallback(byte id)
{
// TODO: Do something after ping command responsed.
}
Read the current angle of the servo.
// Read angle from the servo with id 1, and response parameters into readAngleCallback();
servo.readAngle(1, readAngleCallback);
// Callback function for readAngle().
void readAngleCallback(byte id, int degree)
{
// TODO: Do something after readAngle command responsed.
}
The UART servo has three motion modes, which are wheel mode, angle mode, and damping mode.
The function spin() set the servo to spin mode, and spin by given parameters. It has the following parameters in order:
- id : Servo ID.
- method : Spin direction | Spin behavior.
- Spin direction : See the macros SPIN_CLOCKWISE and SPIN_COUNTERCLOCKWISE.
- Spin behavior : See the macros SPIN_STOP, SPIN_START, SPIN_BY_CYCLE, and SPIN_BY_TIME.
- speed : Spin speed(unit: degree/sec.).
- value :
- By cycle : The number of turns to be spined(unit: rounds.);
- By time : The scheduled time to spin(unit: micro second).
- callback : Callback function. The parameters in order are servo ID(byte), and result(byte; 1:success, 0:fail).
void spin(byte id, byte method, unsigned int speed = 0, unsigned int value = 0, void(*callback)(byte, byte) = NULL);
For example:
// Specify #3 servo to start turning clockwise, 360 degrees per second.
servo.spin(3, SPIN_CLOCKWISE | SPIN_START, 360);
// Specify #5 servo to start turning counterclockwise 10 times, 90 degrees per second.
servo.spin(5, SPIN_COUNTERCLOCKWISE | SPIN_BY_CYCLE, 90, 100);
// Specify #9 servo to start turning clockwise, 180 degrees per second for five seconds.
servo.spin(9, SPIN_CLOCKWISE | SPIN_BY_TIME, 180, 5000);
But how can we stop the servo?
// Use the following code to stop the #1 servo.
servo.spin(1, SPIN_STOP);
// We can also use the function stop() to stop the #1 servo.
servo.stop(1);
In fact, the above methods can stop the servos in all three modes.
The function rotate() set the servo to rotate mode, and roate by given parameters. It has the following parameters in order:
- id : Servo ID.
- angle : Specify the angle of rotation(unit: 0.1 degree)
- interval : Motion interval.(unit: micro second)
- power : Power output of the servo in this mode, if it is zero or greater than the power protection value, adjust to the power protection value.
- callback : Callback function. The parameters in order are Servo ID(byte), and result(byte; 1:success, 0:fail).
void rotate(byte id, int angle, unsigned int interval, unsigned int power = 0, void(*callback)(byte, byte) = NULL);
For example:
// It takes two seconds to turn the #1 servo to -135 degree position.
servo.rotate(1, -1350, 2000);
The function damping() set the servo to damping mode, and specify the power output to the servo to resist external force. It has the following parameters in order:
- id : Servo ID.
- power : Power output of the servo in this mode, if it is zero or greater than the power protection value, adjust to the power protection value.
- callback : Callback function. The parameters in order are Servo ID(byte), and result(byte; 1:success, 0:fail).
void damping(byte id, unsigned int power = 0, void(*callback)(byte, byte) = NULL);
Online manual(HTML format).