R/C Servo

Radio Control style servo motor

Hello World

Import programServo_HelloWorld

Example to sweep a servo through its range

Library

Import libraryServo

A class to control a model R/C servo, using a PwmOut

Notes

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.

/media/uploads/simon/servoschematic.png /media/uploads/simon/servophoto.jpg

Examples

Import program

00001 #include "mbed.h"
00002 #include "Servo.h"
00003 
00004 Servo myservo(p21);
00005 Serial pc(USBTX, USBRX);
00006 
00007 int main() {
00008     printf("Servo Calibration Controls:\n");
00009     printf("1,2,3 - Position Servo (full left, middle, full right)\n");
00010     printf("4,5 - Decrease or Increase range\n");
00011 
00012     float range = 0.0005;
00013     float position = 0.5;
00014     
00015     while(1) {                   
00016         switch(pc.getc()) {
00017             case '1': position = 0.0; break;
00018             case '2': position = 0.5; break;
00019             case '3': position = 1.0; break;
00020             case '4': range += 0.0001; break; 
00021             case '5': range -= 0.0001; break; 
00022         }
00023         printf("position = %.1f, range = +/-%0.4f\n", position, range);
00024         myservo.calibrate(range, 45.0); 
00025         myservo = position;
00026     }
00027 }

Import program

00001 // Hello World to sweep a servo through its full range
00002 
00003 #include "mbed.h"
00004 #include "Servo.h"
00005 
00006 Servo myservo(D14);
00007 
00008 int main() {    
00009     for(float p=0; p<1.0; p += 0.1) {
00010         myservo = p;
00011         wait(0.2);
00012     }
00013 }



An Introduction to RC Servos has more background information on RC Servos.


Please log in to start a discussion or ask a question.