View Full Version : Controlling Servos With Serout Command

06-11-2005, 02:35 AM
hi guys,

i just received my new bs2 px24 and i was wandering if you could explain to me how to control the servos with the serout command in detail, since ive been searching in the forum and i dont understand all the parameters you guys put in the serout command i would appretiate a parameter by parameter explanaition =P thanks A LOT!!!ˇin advance =D

06-11-2005, 02:59 AM
Very simple.

You write
lserv pin 0
rserv pin 1
baud con 16636
comˇ var byte
xˇˇˇˇˇ var word


serout 16, baud, [Enter command", cr]
debugin comˇˇˇˇˇˇˇˇˇˇˇˇˇˇ 'you press r or l
if com = "R" or com = "r" then
for x = 1 to 500
pulsout 1, 800ˇˇˇˇˇˇˇˇˇˇˇˇ 'servo rotates right
pause 20

if com = "L" or com = "l" then
for x = 1 to 500
pulsout 0, 600ˇˇˇˇˇˇˇˇˇˇˇˇ 'servo rotates left
pause 20

goto start

That's all there is to it.ˇ Do you understand?

Sid Weaver
Need a bezel for your LCD?

Newzed@aol.com (mailto:Newzed@aol.com)

Jon Williams
06-11-2005, 03:04 AM

You should download and read our "What's A Microcontroller?" text -- it will get you going on servos and lots of other things.

Jon Williams
Applications Engineer, Parallax

06-11-2005, 05:39 AM
im sorry, i think i couldnt explain myself, what i wanted to do was to control a servo with the serout command not the pulsout one and i wanted to know how since the bs2px24 has different timings than the others etc....

Jon Williams
06-11-2005, 07:11 AM
You can find the standard timing values for the BS2px in the help file, as well as the formula for calculating the parameter for any baud rate.

I make my life easy by having this Conditional Compilation segment in my programming template (attached).ˇ SERIN/SEROUT paramaters always get people in trouble; that's why I built them in.

ˇˇˇ T1200ˇˇˇˇˇˇ CONˇˇˇˇ 813
ˇˇˇ T2400ˇˇˇˇˇˇ CONˇˇˇˇ 396
ˇˇˇ T4800ˇˇˇˇˇˇ CONˇˇˇˇ 188
ˇˇˇ T9600ˇˇˇˇˇˇ CONˇˇˇˇ 84
ˇˇˇ T19K2ˇˇˇˇˇˇ CONˇˇˇˇ 32
ˇˇˇ TMidiˇˇˇˇˇˇ CONˇˇˇˇ 12
ˇˇˇ T38K4ˇˇˇˇˇˇ CONˇˇˇˇ 6
ˇˇˇ T1200ˇˇˇˇˇˇ CONˇˇˇˇ 2063
ˇˇˇ T2400ˇˇˇˇˇˇ CONˇˇˇˇ 1021
ˇˇˇ T4800ˇˇˇˇˇˇ CONˇˇˇˇ 500
ˇˇˇ T9600ˇˇˇˇˇˇ CONˇˇˇˇ 240
ˇˇˇ T19K2ˇˇˇˇˇˇ CONˇˇˇˇ 110
ˇˇˇ TMidiˇˇˇˇˇˇ CONˇˇˇˇ 60
ˇˇˇ T38K4ˇˇˇˇˇˇ CONˇˇˇˇ 45
ˇˇˇ T1200ˇˇˇˇˇˇ CONˇˇˇˇ 3313
ˇˇˇ T2400ˇˇˇˇˇˇ CONˇˇˇˇ 1646
ˇˇˇ T4800ˇˇˇˇˇˇ CONˇˇˇˇ 813
ˇˇˇ T9600ˇˇˇˇˇˇ CONˇˇˇˇ 396
ˇˇˇ T19K2ˇˇˇˇˇˇ CONˇˇˇˇ 188
ˇˇˇ TMidiˇˇˇˇˇˇ CONˇˇˇˇ 108
ˇˇˇ T38K4ˇˇˇˇˇˇ CONˇˇˇˇ 84

SevenBitˇˇˇˇˇˇˇ CONˇˇˇˇ $2000
Invertedˇˇˇˇˇˇˇ CONˇˇˇˇ $4000
Openˇˇˇˇˇˇˇˇˇˇˇ CONˇˇˇˇ $8000

Baudˇˇˇˇˇˇˇˇˇˇˇ CONˇˇˇˇ T9600

Jon Williams
Applications Engineer, Parallax

06-11-2005, 07:25 PM
im too ignorant for the manual and i couldnt understand it :P so for example if i want to put the servo in center position i need a 1.5ms signal, what should i do? with serout?

Jon Williams
06-11-2005, 09:59 PM
That depends, of course, on which servo controller you're using. Parallax PSC? SEETRON SSC? Other? They all have different protocols. If you're under the impression you can send a serial message directly to a servo, well, you can't.

Jon Williams
Applications Engineer, Parallax

06-11-2005, 10:31 PM
Yes, if you hook the center wire from the Servo (I'm pretty sure that's the signal wire) to an I/O pin on the BS2, then you will HAVE to use the pulsout command to control the servo. That's the signal a Servo wants.

LOW ServoPin ' Make Servopin an Output, and output a zero 'idle' state
PULSOUT ServoPin, 750 ' This will send it to center position
PAUSE 20 ' A servo signal should be 'refreshed' every 20 to 50 mSec to hold a position
GOTO ServoRefresh

If you have a Servo Driver board of some kind (like the PSC) then you talk to THAT board with SEROUT, and THAT board will send pulsout-equivalent signals to the Servo.

A servo, by itself, does not know the SEROUT RS-232 protocol. You have to 'talk' what your device knows how to listen to. And the BS2 is well designed to 'talk' what a servo knows how to recieve -- the PULSOUT command does that.

P.S.ˇ Oops, the '750' value will give you a 1.5 mSec pulse (center) on a BS2.ˇ On a BS2px, you'll have to look in the manual to find the equivalent value.

06-12-2005, 12:11 AM
ive seen that in the other bs2 they do not use pulsout but instead serout and i would like to learn how to do that

Ryan Clarke
06-12-2005, 12:45 AM
First of all don't think that you aren't advanced enough for the "What's a Microcontroller"- it was written to be very accessable to people just learning (hence the title!) and it's a great place to start as Jon pointed out. And you can download it for free. You really should take a look at it...

If you've seen other code listings using serout and serin for controlling a servo then they are talking to some type of controlling device, such as the PSC controller. They are not sending serout/in commands directly to the servo; a servo only understands pulses, and varying those tell the servo were to turn to. (In the case of the continuous rotation servos they are sent a pulse to move/not move- this is because they are trying to reach a position that they will from their perspective never get to)- So all the fancy control of a servo is simply a function of changing the pulses you are sending. The PSC can alter those pulses in such a way as to make the resulting motion faster, slower, etc. - the SEROUT commands from the stamp are taken by the PSC and turned into the necessary pulses.

So try reading What's a Microcontroller, and then the documentation for the PSC.

You can find them here:

http://www.parallax.com/dl/docs/books/edu/wamv2_2.pdf (What's a Microcontroller)
http://www.parallax.com/dl/docs/prod/motors/ServoController.pdf (PSC)


Bruce Bates
06-12-2005, 01:00 AM
Scarecrow -

I think you may be missing what is being said. Here is a summary.

All standard R/C servos which are controlled directly from the Stamp, are controlled using the PULSOUT command, regardless of which Stamp platform you are using.

A 1.5 mSec pulse will center any standard R/C servo. Different values are used in the PULSOUT command to develop this 1.5 mSec pulse, since the speed of execution in many of the Stamps differs. The Stamp MANUAL or the Stamp Editor Help File, is your guide to the specific values required. Learning how to CENTER an R/C servo is the best first step you can make, in understanding how they operate using the Stamp as a servo driver.

Usually SEROUT is used ONLY when a separate Servo Controller Board (regardless of whose board it is) is used to drive the R/C servos, rather than having them driectly driven and controlled by the Stamp, regarless of the Stamp model. I say "usually" since I'm not familiar with the newer digital R/C servos, and I'm not sure how they are controlled. All analog servos will NOT respond properly to ANY SEROUT commnad, on ANY Stamp.

Servo Controller Boards are used when it's necessary to control multiple (more than two or three) servos simultaneously, and/or to permit the Stamp to do other things while the R/C servo motion is being controlled by the board. Otherwise the Stamp will be spending most of its time updating and refreshing the R/C servos as is necessary for their proper operation. One single command will not move an R/C servo to a particular position and keep it there indefinitely.

If you have a reference or link to the contrary, we'd be happy to take a look at it for you.

My apologies if this message sounds a bit stern, it wasn't meant to be. I just wanted to try to straighten out, in simple terms, what's been said in numerous different posts prior to this.


Bruce Bates

06-12-2005, 04:22 AM
thanks a lot for your post. It kind of cleared everything for me, I guess I wasn't paying enough attention to the other posts and also i kind of flash answered to them and i apologize......=(