0% found this document useful (0 votes)
169 views6 pages

Servo - Cookbook - Mbed

Uploaded by

Cảnh Manu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views6 pages

Servo - Cookbook - Mbed

Uploaded by

Cảnh Manu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

(/)

Handbook(/handbook/)

Cookbook(/cookbook/)

Code(/code/)

Questions(/questions/)

Forum(/forum/) Compiler(/compiler/)

Dashboard(/dashboard/)

mbed(/)
Cookbook(/cookbook/) Servo(/cookbook/Servo)

Search mbed.org...

Go

Login or signup(https://mbed.org/acco
Cookie notice(/handbook/Cookies/)

Servo(/cookbook/Servo)
A library for controlling a Radio Control (R/C) model Servo.

Recent changes(/cookbook/Special:RecentChan Table of Contents


1. Hello World! 2. Library 3. Details 4. Examples

Wi Go (/cookbook/WiGo)
avnet(/search/? q=avnet&type=), internet of things(/search/?q=internet of things&type=), IOT(/search/? q=IOT&type=), WiGo(/search/?q=Wi-Go&type=)

Homepage (/cookbook/Homepage) Text LCD Enhanced (/cookbook/Text-LCDEnhanced)

Hello World!

Import this program(https://mbed.org/compiler/#import:https://mbed.org/users/simon/code/Servo_HelloWorld/) Events (http://mbed.org/users/simon/programs/Servo_HelloWorld/latest/docs/main_8cpp_source.html)(http://mbed.org/users/simon/programs/Servo_HelloWorld (/cookbook/Events)


- main.cpp(/users/simon/code/Servo_HelloWorld/docs/40d2fd0b99e6/main_8cpp_source.html)
/ /H e l l oW o r l dt os w e e pas e r v ot h r o u g hi t sf u l lr a n g e # i n c l u d e" m b e d . h " # i n c l u d e" S e r v o . h " S e r v om y s e r v o ( p 2 1 ) ; i n tm a i n ( ){ f o r ( f l o a tp = 0 ;p < 1 . 0 ;p+ =0 . 1 ){ m y s e r v o=p ; w a i t ( 0 . 2 ) ; } }

HD44780(/search/? q=HD44780&type=), LCD(/search/?q=LCD&type=), text(/search/?q=text&type=)

Prototype To Hardware KL25Z (/cookbook/Prototype-ToHardware-KL25Z)


KL25z(/search/? q=KL25z&type=)

Compiler Error 84 (/cookbook/CompilerError-84) Compiler Error 194 (/cookbook/CompilerError--194) m3pi preparation

Library

Import this library into a (/cookbook/m3piprogram(https://mbed.org/compiler/#import:https://mbed.org/users/simon/code/Servo/;mode:lib) preparation) (http://mbed.org/users/simon/programs/Servo/latest/docs/classServo.html)(http://mbed.org/users/simon/programs/Servo/latest/docs/classServo.html)


- Servo Class Reference(/users/simon/code/Servo/docs/36b69a7ced07//classServo.html)

Public Member Functions

Interfacing with Python (/cookbook/Interfacingwith-Python)

interface(/search/? q=interface&type=), python(/search/? Servo q=python&type=) (/users/simon/code/Servo/docs/36b69a7ced07/classServo.html#a550914ccdb13d817f6460dd80 (PinName pin) Compiler Error 101 Create a servo object connected to the specified PwmOut pin. (/cookbook/CompilerError-101) (/users/simon/code/Servo/docs/36b69a7ced07/#a550914ccdb13d817f6460dd80aec23c3)

void

write DMU02 (/users/simon/code/Servo/docs/36b69a7ced07/classServo.html#adf6f102a108f8093b48c08b1f7 (/cookbook/DMU02) (float percent) Set the servo position, normalised to it's full range. (/users/simon/code/Servo/docs/36b69a7ced07/#adf6f102a108f8093b48c08b1f72d5413)

float

read (/users/simon/code/Servo/docs/36b69a7ced07/classServo.html#a868d9d1513fbaf34c4d5d15a7 () Read the servo motors current position. (/users/simon/code/Servo/docs/36b69a7ced07/#a868d9d1513fbaf34c4d5d15a7fa4baa8)

void

position (/users/simon/code/Servo/docs/36b69a7ced07/classServo.html#a9c41903bf587e02b4f57eb957 (float degrees) Set the servo position. (/users/simon/code/Servo/docs/36b69a7ced07/#a9c41903bf587e02b4f57eb957ae82341)

void

calibrate (/users/simon/code/Servo/docs/36b69a7ced07/classServo.html#ab9876359dc71baa008846bef0

(float range=0.0005, float degrees=45.0) Allows calibration of the range and angles for a particular servo. (/users/simon/code/Servo/docs/36b69a7ced07/#ab9876359dc71baa008846bef055cbaaf)

Servo (/users/simon/code/Servo/docs/36b69a7ced07/classServo.html) & operator= (/users/simon/code/Servo/docs/36b69a7ced07/classServo.html#a99dd298eb7dad01cf06fa65b7 (float percent) Shorthand for the write and read functions. (/users/simon/code/Servo/docs/36b69a7ced07/#a99dd298eb7dad01cf06fa65b7761b149)

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

Import this program(https://mbed.org/compiler/#import:https://mbed.org/users/simon/code/ServoProgram/) (http://mbed.org/users/simon/programs/ServoProgram/5zt36/docs/main_8cpp_source.html)(http://mbed.org/users/simon/programs/ServoProgram/5zt36/do


- main.cpp(/users/simon/code/ServoProgram/docs/7b3eabfa1a0f/main_8cpp_source.html)

# i n c l u d e" m b e d . h " # i n c l u d e" S e r v o . h " S e r v om y s e r v o ( p 2 1 ) ; S e r i a lp c ( U S B T X ,U S B R X ) ; i n tm a i n ( ){ p r i n t f ( " S e r v oC a l i b r a t i o nC o n t r o l s : \ n " ) ; p r i n t f ( " 1 , 2 , 3-P o s i t i o nS e r v o( f u l ll e f t ,m i d d l e ,f u l lr i g h t ) \ n " ) ; p r i n t f ( " 4 , 5-D e c r e a s eo rI n c r e a s er a n g e \ n " ) ; f l o a tr a n g e=0 . 0 0 0 5 ; f l o a tp o s i t i o n=0 . 5 ; w h i l e ( 1 ){ s w i t c h ( p c . g e t c ( ) ){ c a s e' 1 ' :p o s i t i o n=0 . 0 ;b r e a k ; c a s e' 2 ' :p o s i t i o n=0 . 5 ;b r e a k ; c a s e' 3 ' :p o s i t i o n=1 . 0 ;b r e a k ; c a s e' 4 ' :r a n g e+ =0 . 0 0 0 1 ;b r e a k ; c a s e' 5 ' :r a n g e=0 . 0 0 0 1 ;b r e a k ; } p r i n t f ( " p o s i t i o n=% . 1 f ,r a n g e=+ / % 0 . 4 f \ n " ,p o s i t i o n ,r a n g e ) ; m y s e r v o . c a l i b r a t e ( r a n g e ,4 5 . 0 ) ; m y s e r v o=p o s i t i o n ; }

Thch

Post a com m ent

Ask a question(/questions/create/?url=/cookbook/Servo)

Last modified 14 Mar 2012(14 Mar 2012), by (/users/rcd/) abc cba(/users/rcd/). 14 comments (/cookbook/Servo#commentform) library(/search/?q=library&type=Wiki), Servo(/search/?q=Servo&type=Wiki)

14 comments:
(/users/4180_1/) jim hamblen(/users/4180_1/) #(/comments/cr/93/29/#c75) 27 Aug 2010(27 Aug 2010) All of the RC servos have the three wire connectors, but the different brands move the three pins around and use different wire color codes. Each company does this to try to lock users into their RC equipment. The info on which pin is which can be hard to find. There is even a company that makes conversion cables! Here is a handy pinout table I found with some pictures:

http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html#ServoConnectors(http://www.horrorseek.com/home/halloween/wolfstone/Motors/svoint_RCServos.html

(/users/4180_1/) jim hamblen(/users/4180_1/) #(/comments/cr/93/29/#c77) 31 Aug 2010(31 Aug 2010) Servos have a drive wheel that is controlled by a PWM coded signal. The servo shown below is a Futaba S3003 which is identical, internally, to the Tower TS53J servo. Radio control servomotors are mass-produced for the hobby market and are therefore relatively inexpensive and consistently available. They are ideally suited for robotics applications. Internally, the servo contains a DC drive motor seen on the left in the image below, built-in control circuitry, and a gear reduction system. They are small, produce a relatively large amount of torque for their size, and run at the appropriate speed for a small robotics drive motor.

The control circuitry of the servo uses a potentiometer (variable resistor) that is used to sense the angular position of the output shaft. The potentiometer is the tall component on the right in the image. The output shaft of a servo normally travels 180-210 degrees. A single control bit is used to specify the angular position of the shaft. The timing of this bit specifies the angular position for the shaft. The potentiometer senses the angle, and if the shaft is not at the correct angle, the internal control circuit turns the motor in the correct direction until the desired angle is sensed. The control signal bit specifies the desired angle. The desired angle is encoded using pulse width modulation (PWM). The width of the active high pulse varies from 1-2 ms. A 1ms pulse is 0 degrees, 1.5ms is 90 degrees and a 2 ms pulse is approximately 180 degrees. New timing pulses are sent to the servo every 20 ms. Normally, a servo has a mechanical stop that prevents it from traveling move than half a revolution. If this stop is removed along with other modifications to the potentiometer, a servo can be converted to a continuously rotating drive motor for robots. You can find web sites showing how to convert a servo for continuous rotation.

(/users/4180_1/) jim hamblen(/users/4180_1/) #(/comments/cr/93/29/#c117) 23 Sep 2010(23 Sep 2010) If you have the .1" breakaway header pins with long pins on both sides or even wire wrap pins, you can break off a strip of three pins and plug them into the servo cable connector. The servo cable can then plug directly into the breadboard. This works a bit nicer than putting jumper wires directly into the servo cable connector.

http://www.pololu.com/catalog/product/1065(http://www.pololu.com/catalog/product/1065) is seen above. Also available from Sparkfun, http://www.sparkfun.com/products/10158(http://www.sparkfun.com/products/10158). Many of the right angle header strips will also work.

(/users/DavyDick/) David Dick(/users/DavyDick/) #(/comments/cr/93/29/#c226) 24 Oct 2010(24 Oct 2010)

I am intrigued by the read() function. It would seem to allow us to check that the servo did rotate to the required position, without any hindrance or obstruction. However, I thought that feedback is only available in the very expensive servos. Does this function work for all servos?

(/users/mindw0rk/) Karolis Staaitis(/users/mindw0rk/) #(/comments/cr/93/29/#c228) 24 Oct 2010(24 Oct 2010) @David, Yes, this does work on all analog servos that have a potentiometer in them.

(/users/hyla/) Christoph Klein(/users/hyla/) #(/comments/cr/93/29/#c280) 04 Nov 2010(04 Nov 2010) _How_ does that work?? The servo certainly does not _send_ anything back? Christoph

(/users/simon/) Simon Ford(/users/simon/) #(/comments/cr/93/29/#c281) 04 Nov 2010(04 Nov 2010)

(/users/simon/)

When you call read(), it is simply returning where you set it with a write() i.e. the target position. This is mainly so you can easily do things like relative position changes. It doesn't actually know where the servo is, so can't tell you if the motor has been stalled etc. The closed loop aspect of this type of R/C servo is within the servo itself, so you don't get any feedback. If you do want lots of control/feedback, something like the digital Dynamixel AX12 Servo(Dynamixel-AX12-Servo) might be interesting.

(/users/ms767210/) mike seiler(/users/ms767210/) #(/comments/cr/93/29/#c345) 14 Nov 2010(14 Nov 2010) Hi, just wondering what ppl are using to work the code above. Win7 doesn't have a terminal program anymore. So what can I use to interact with supplied code? Should the keys still work even though I can't see output?

(/users/ms767210/) mike seiler(/users/ms767210/) #(/comments/cr/93/29/#c353) 15 Nov 2010(15 Nov 2010) Ok so I did a search of the site for terminal and found a link to a program called terraterm. Installed it along with latest mbed serial driver and got the code working. Very nice, thanks to the author. For some reason mbed is now on com4? whereas before the new driver was installed it was com3. There are no negative consequences from this that I can see.

(/users/PakoMBED/) Fran Alvarez(/users/PakoMBED/) #(/comments/cr/93/29/#c647) 13 Jan 2011(13 Jan 2011) ...mmm just testing the newly adquired MPC with the "hello world" example and the compiler says it cannot find "Servo.h" ("Cannot open source input file "Servo.h": No such file or directory (E5)" in file "/main.cpp"). Tried to find it at the class doc page, but ain't luck... Someone to point a newbie what to do? I've tried importing from the project window, no way... I'll go on reading, just in case I missed something! Thanks so much!

(/users/PakoMBED/) Fran Alvarez(/users/PakoMBED/) #(/comments/cr/93/29/#c648) 13 Jan 2011(13 Jan 2011) OOOps! Found! just for the record: http://mbed.org/users/simon/libraries/Servo/le3jpb/docs/Servo_8h_source.html(http://mbed.org/users/simon/libraries/Servo/le3jpb/docs/Servo_8h_source.html) Sorry to bore you all, just put it in case sb finds same difficulty... I guess I have now to include this file as I did with mines (I thought Servo.h was a system-integrated library). Thanks and sorry for so silly question!

(/users/ThreeRulesSafe/) Kerwin Lumpkins(/users/ThreeRulesSafe/) #(/comments/cr/93/29/#c1704) 01 Sep 2011(01 Sep 2011) Hello. Is there a way using the Servo library to disable the servo line in the event that the user wants the line to just have flat 0V? A disconnect method or similar. I've tried various values with the available functions but do not see a way to do this. Thanks, Kerwin

(/users/otzinyung/) Tzin Yung Ooi(/users/otzinyung/) #(/comments/cr/93/29/#c1915) 17 Oct 2011(17 Oct 2011) Hi, I have a problem while testing the program provided above which is the one setting servo to move full left, middle and full right. From the results that I've obtained, the range between full left and full right was only 90 degree. Was it suppose to be 180degree? Hope that someone could help me out of this. Thanks

(/users/Romik/) Roman Kuzmin(/users/Romik/) #(/comments/cr/93/29/#c4130) 12 Oct 2012(12 Oct 2012) This library it doesn't work with LPC11U24

1# i n c l u d e" m b e d . h " 2# i n c l u d e" S e r v o . h " 3 4S e r v om y s e r v o ( p 2 6 ) ; 5 6i n tm a i n ( ){ 7 f o r ( f l o a tp = 0 ;p < 1 . 0 ;p+ =0 . 1 ){ 8 m y s e r v o=p ; 9 w a i t ( 0 . 2 ) ; 1 0 } 1 1} 1 2

mbed

blog(/blog)

get an mbed(/order)

(http://www.mbed.org) about mbed(/handbook/About) we're hiring!(/handbook/Jobs)

support(/handbook/Help)

service status(/handbook/Service-

status)

privacy policy(/privacy)

terms and conditions(/terms)

Language: en(/setlang?lang=en) ja(/setlang?lang=ja) es(/setlang?lang=es) de(/setlang?lang=de)

mbed, the fastest way to prototype with ARM based microcontrollers. ^ back to top

You might also like