Servo¶
Overview¶
MoonBot Kit Controller Module can be connected up to four Servo Module 。 In Arduino library, we provide Servo library. Through this library, you can control one or more servos to move.
Servo library inherit Arduino basic servo driver class Servo
.
Except for basic Servo
class function, we also provide functions like servo calibration, several servos move together.
In MoonBot.h
header file, we provide four variables m_servo[kServo1]
m_servo[kServo2]
m_servo[kServo3]
m_servo[kServo4]
to drive corresponding servo ports in controller module.
Basic Application¶
There is a basic application of servos.
#include <MoonBot.h>
int pos;
void setup() {
m_servo[kServo1].attach(kServo1, true); // attaches servo on servo port 1, and reverse directions
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
m_servo[kServo1].write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
m_servo[kServo1].write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Note
Initial function of servos is changed to attach(moonbot_servo_t servo_port, bool reverse)
, and original function uint8_t attach(int pin)
is not supported anymore.
Servos move together¶
We provide void MoonBotServo::setTargetAngle()
and MoonBotServo::moveAllServoToTarget()
functions to make servos move togehther.
#include <MoonBot.h>
void setup() {
for (int i = 0; i < kServoNum; ++i) {
m_servo[i].attach((moonbot_servo_t)i); // attaches servo
}
}
void loop() {
// in steps of 1 degree
for (int i = 0; i < kServoNum; ++i) {
m_servo[i].setTargetAngle(180, 1); // set all servo to go to position in variable '180', speed 1 degree per pulse(20ms)
}
MoonBotServo::moveAllServoToTarget(); // move all servo to target angle
for (int i = 0; i < kServoNum; ++i) {
m_servo[i].setTargetAngle(0, 1); // set all servo to go to position in variable '0', speed 1 degree per pulse(20ms)
}
MoonBotServo::moveAllServoToTarget(); // move all servo to target angle
}
Note
When using MoonBotServo::moveAllServoToTarget();
default parameter, the function will wait for all servos finish moving and stopping.
When parameter is not 0, it will stop when time is over, and feed back whether moving is finished.
Function``bool isMoving(void);`` can be used every certain time to check the status.
while (!MoonBotServo::moveAllServoToTarget(0)) {
// Check whether servos are moving.
for (int i = 0; i < kServoNum; ++i) {
if (!m_servo[i].isMoving()) {
// when servos stop, print the status.
Serial.print("Servo");
Serial.print(i);
Serial.println(" Stopped.");
}
}
}
Serial.println("All Servo Stopped.");
When using COM monitor, information will be received as below.
Servo1 Stopped.
...
Servo1 Stopped.
Servo2 Stopped.
...
Servo2 Stopped.
Servo3 Stopped.
...
Servo3 Stopped.
All Servo Stopped.
Servo Calibration¶
MoonBot Kit Servo library provide servo calibration function that can correct the offset of servos.
m_servo[kServo1].correction(-2); //Calibrate servo 1 downwards for 2°
API Reference - Servo¶
Header File¶
Enum¶
- enum moonbot_servo_t
servo port type
value:
- kServo1
- kServo2
- kServo3
- kServo4
- kServoNum
servo port number
Class¶
- class MoonBotServo
MoonBot Kit servo driver library
- Member function
- uint8_t attach(moonbot_servo_t servo_port, bool reverse = MOONBOT_SERVO_REVERSE);
Initialise servo to servo ports.
- Parameter
servo_port
reverse
- Return
NOT_A_PORT
Servo port is invalid, and other initialization is right.
- uint8_t attach(moonbot_servo_t servo_port, int min, int max, bool reverse = MOONBOT_SERVO_REVERSE);
Initialise servo to servo ports, and set its moving range.
- Parameter
servo_port
:servo portmin
:minimum degree of servomax
:max degree of servoreverse
:reverse servo direction
- Return
NOT_A_PORT
Servo port is invalid, and other initialization is right.
- void detach(void);
Detach servo and port
- void write(int value);
Write servo angle
- parameter
value
:angle value range0~180°
- int read(void);
Read current servo degree
- Return
current degree
- void reverse(bool state);
Reverse servo direction
- parameter
state
: Statustrue
Direction is reversed
- void setTargetAngle(int angle, unsigned int speed = 1);
Initialise servos.It should be used together with``static bool moveAllServoToTarget()`` .
- parameter
angle
: Initialised anglespeed
: degree of every pulse
- void stop(void);
stop servos
- void power(bool state);
open or close servo power.
- parameter
state
:status of servo power,true
means open
- void correction(int angle_offset);
Servo calibration
- parameter
angle_offset
:Calibrate the angle. Range:±90°
- bool isMoving(void);
Read moving status.
- Return
true
Servo is moving
- bool isPowerOverload(void);
Detect whether current is overload.
- Return
true
Power is overload
- Static member function
- static bool moveAllServoToTarget(unsigned long timeToWait_ms = 0xFFFFFFFF);
Move all servo to set angle
- Parameter
timeToWait_ms
: Default time is infinite, until servo move to target angle.
- Return
true
Finish all movement.
- static void stopAllServo(void);
Stop all servo movements.