You are viewing an older revision! See the latest version
Servo
A library for controlling a Radio Control (R/C) model Servo.
Hello World!¶
main.cpp
// Sweep the servo through its full range #include "mbed.h" #include "Servo.h" Servo myservo(p21); int main() { while(1) { for(float p=0; p<1.0; p += 0.1) { myservo = p; wait(0.2); } } }
Library¶
- /users/simon/programs/Servo - R/C Servo Library
This library controls a standard R/C model servo using a PwmOut signal, and provides control of the servo between min and max by setting it to 0.0 - 1.0.
As all servos respond differently, you can also use the calibrate function the range, so 0.0 - 1.0 is the maximum range of the servo.
Details¶
The underlying PwmOut period is set to 20ms, and by varying the pulsewidth (up to a maximum of 0.5ms to 2.5ms) the position of the servo is changed, usually by around 180 degrees. The library lets you calibrate the exact range.
The servo must connect to a PwmOut, and be supplied with a separate power supply, usually in the range 4.5-6.0v.
Warning
A servo requires higher current than the USB port can safely provide, and so it is essential that you power the servo using an external supply, such as a 4xAA (6v) battery pack or an appropriate DC power adaptor.
Examples¶
Calibrate a Servo
#include "mbed.h" #include "Servo.h" Servo myservo(p21); Serial pc(USBTX, USBRX); int main() { printf("Servo Calibration Controls:\n"); printf("1,2,3 - Position Servo (full left, middle, full right)\n"); printf("4,5 - Decrease or Increase range\n"); float range = 0.0005; float position = 0.5; while(1) { switch(pc.getc()) { case '1': position = 0.0; break; case '2': position = 0.5; break; case '3': position = 1.0; break; case '4': range += 0.0001; break; case '5': range -= 0.0001; break; } printf("position = %.1f, range = +/-%0.4f\n", position, range); myservo.calibrate(range, 45.0); myservo = position; } }