More about Standard Servo Control with the BASIC Stamp 2
Andy Lindsay (Parallax)
Posts: 1,919
More About Standard Servo Control with the BASIC Stamp 2
This is additional information about Standard Servo Control that expands on What's a Microcontroller, Chapter 4, Activity #1.· Read Chapter 4 through Activity #1 first, then continue here.· After you’re done with this, move on to Chapter 4, Activity #2.
What's a Microcontroller:····Text (PDF)··· Parts Kit··· Full Kit (Lite)··· Full Kit
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Andy Lindsay
Education Department
Parallax, Inc.
Post Edited (Andy Lindsay (Parallax)) : 1/10/2007 1:30:29 AM GMT
This is additional information about Standard Servo Control that expands on What's a Microcontroller, Chapter 4, Activity #1.· Read Chapter 4 through Activity #1 first, then continue here.· After you’re done with this, move on to Chapter 4, Activity #2.
What's a Microcontroller:····Text (PDF)··· Parts Kit··· Full Kit (Lite)··· Full Kit
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Andy Lindsay
Education Department
Parallax, Inc.
Post Edited (Andy Lindsay (Parallax)) : 1/10/2007 1:30:29 AM GMT
Comments
A standard servo is a device that controls position. That’s why it works so good for model boat rudders and airplane flaps, because it can turn to and hold a position based on an electronic signal it receives. Figure 1 shows an example of the electronic signal that instructs a servo to hold a particular position. It’s a train of pulses that has to be repeated every 20 ms or so to keep reminding the servo what position it is supposed to be holding. The part of the signal that tells the servo where to turn is tpulse, the pulse duration. The tlow time doesn’t matter as much. If it’s between 10 and 30 ms, the servo should keep doing its job. The important thing with servo control is that the pulse durations tell the servo what position to point to, and the fact that the pulses are repeated every 20 ms make the servo hold that position.
······ Figure 1 - Servo Pulse Train
······
PULSOUT·Controls the Position the Servo Holds
Figure 2 shows how the pulse duration instructs the servo where to turn. The inner group of numbers (3, 2, 1, 12, 11, 10, 9) are the hour values on a clock face. Assuming the servo horn has been removed and reattached so that it point’s a clock’s·hour hand at 12:00 when it’s in the center of its 180-degree range of motion, the next group of numbers (0.5, 0.83, 1.17, 1.5, 1.83, 2.17, 2.5) are the number of·milliseconds the pulses have to last to make the servo point the clock’s hour hand in that particular direction. Example PULSOUT commands are included for making the servo point the hour hand to each hour.
······ Figure 2 – Servo Position Control·of a Clock’s Hour Hand
······
Since the PULSOUT command’s Duration argument is in 2 µs increments, you have to multiply the number of ms by 500 to get the number of PULSOUT duration units to use. For 0.5 ms pulses to make the servo’s horn point the hour hand to 3:00, use a PULSOUT Duration argument of 250. For a servo connected to P14, that’s PULSOUT 14, 250. Want to make it point at 2:00 instead? You’ll need 0.83 ms pulses instead of 0.5 ms pulses. The PULSOUT command’s duration argument·should then be·500 × 0.83 = 415. So the command that can be used to make pulses that direct the clock hour hand to 2:00 is PULSOUT 14, 415.
The PULSOUT command can be used to deliver the pulses (tpulse) shown in Figure 1. So what about the low time (tlow in Figure 1) so that the pulses are only transmitted every 20 ms or so?· The answer is·that the low time can be established with a PAUSE command.· PAUSE 20 fits the bill.· Also how can the pulse followed by the low signal be delivered repeatedly to keep the servo holding a particular position? That’s the job of a loop, like a DO…LOOP for indefinite repetitions, or a FOR…NEXT loop to control the number of repetitions. Inside the loop, there has to be a PULSOUT command to deliver the control pulse (tpulse) to the servo and a PAUSE command (tlow).
Estimating How long a Servo Holds a Particular Position
Figure 3 shows an example of a FOR…NEXT loop that will make the servo hold a certain position for a certain amount of time. Each time the loop repeats, another pulse + low time gets transmitted to the I/O pin. The value next to PULSOUT pin, ___ controls the servo’s position,·and the value next to FOR counter = 1 to ___ determines how long the servo holds the position.
······ Figure 3 – Servo For Next Loop
······
You can make a rough guess of how long the servo will hold a particular position by assuming that the pulses get delivered every 20 ms. In other words, assume that tcycle in Figure 1 is 20 ms. There are fifty 20 ms cycles in 1 s. That’s 50 × (20/1000) s = 1000/1000 = 1 s. So, for every 50 reps of the FOR…NEXT loop, it takes about 1 s. For example, FOR counter = 1 to 150… will take about 3 s.
Keep in mind that 50 cycles per second is just for making rough estimates. It’s really better to figure in the pulse duration, the pause time, and the BASIC Stamp’s fetch and execute time for the PBASIC loop. The PointTwoDirections.bs2 program below has two loops. From inspection, both loops will last about 3 seconds (150 reps), pointing the clock hand from Figure 2 at 10:30 (PULSOUT 14, 1000). Then, it will point the clock hand at 1:30 for about 3 seconds (PULSOUT 14, 500).··After the three seconds pointing at 1:30 has elapsed, the signals stop, so the servo no longer holds its position.· While the program is running, the servo's horn will resist any attempts·on your part to move it away from·10:30 or 1:30.· After the program ends and stops sending the pulse train, you will be able to rotate the servo horn (gently, otherwise you could damage the servo).
A More Precise Estimate
Instead of a rough estimate, how about a more precise estimate of both loops’ hold times. This isn’t real difficult. Start by adding up all the parts of one repeat of the FOR...NEXT loop (tloop). Then, multiply tloop by the number of loop repetitions (loop reps). The result will be a more precise estimate of how long the servo will hold a given position (thold).
· ····
······
For example, let’s take a closer look at the first loop in PointTwoDirections.bs2.·The value·tloop commands is around 1.5 ms.
······ tloop = tPULSOUT + tPAUSE + t loop commands
·············· = 2 ms + 20 ms + 1.5 ms
·············· = 23.5 ms
Then, the amount of time·the servo holds a given position is:
······ thold = tloop × loop reps
·············· = 23.5 ms × 150
·············· = (23.5/1000 s) × 150
·············· = 3.525 s
NOTE: The hold time (thold) for the second loop will be shorter, since the PULSOUT command is sending 1 ms pulses instead of 2 ms pulses.
What's Next?
Try some variations on the FOR…NEXT loop and PULSOUT commands in PointTwoDirections.bs2. Then, try What’s a Microcontroller, Chapter 4, Activity #2. In that activity, you will enter the number of loop repetitions and PULSOUT durations into the Debug Terminal, and the BASIC Stamp will use those values to modify its FOR…NEXT loop and PULSOUT command. You can then compare your numbers and results to Figure 2 in this document.
Standard vs. Continuous Rotation Servos
With the advent of hobby robotics, there is a new kind of servo available that has been electrically and mechanically modified to function as a low speed motor.· It’s called a Continuous Rotation Servo.· Signals similar to the ones that dictate a standard servo's position can instead be used to dictate the speed and direction of a continuous rotation servo's output shaft.· A wheel is typically mounted on the continuous rotation servo's output shaft instead of a horn, making it a useful robot motor.··To find out more about continuous rotation servos and how they are used as robot motors, see Robotics with the Boe-Bot.
······ Robotics with the Boe-Bot:····Text (PDF)··· Full Kit
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Andy Lindsay
Education Department
Parallax, Inc.
Post Edited (Andy Lindsay (Parallax)) : 1/10/2007 8:39:49 PM GMT
Thank you,
Jim
Bob
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Scott
Would this work?:
' {$STAMP BS2}
' {$PBASIC 2.5}
counter VAR Word
FOR counter = 1 TO 150 ;150 represents the necessary time value
PULSOUT 14, 1000 ;make servo hold position
PAUSE 20 ;necessary servo pause is this necessary now?
PULSOUT 13, 1000 ;pulseout to HB-25 motorcontroller to make a motor run while servo is held
PAUSE 20 ;is this pause necessary?
NEXT
Also, how jittery would this be, as it is linear, as the servo is not running in the background?
The point is, the servo isn't being signalled with that "20" pause; it'll be (should·be)·signalled with a new Pulseout after an elapsed time of 20, whether that elapsed time is caused by a pause or by other work you make the controller do (executing other PBasic statements). The trick is to figure out what you can do in that period of 20 --how long will your other statements take to execute?; you would figure that out by trial and error, or maybe by using information developed by some other Stamp users, esp. by Tracy Allen (see his website).
PAR
PAR
This helped immensely, as I wont have to waste that 20ms anymore!
I would recommend doing all your PULSOUT commands within your main loop with a single PAUSE 20 in there. During events when a subroutine is called and/or a pulse value changes the servos and HB-25 will be updated almost immediately and all will always continue to receive the refresh. If you cannot do this in the main loop then you can put that section into a subroutine that is called, but again, all servos and HB-25 should receive their pulses followed by a PAUSE 20 before continuing. Does this help? Let me know. Take care.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
This helps too. I am a teacher, and as an advanced project, I am building a robotic platform out of an Emaxx dual motor 4wd rc truck chassis. It has been done before, but not that I know of with a basic stamp except for one at drexel university. I want it to be something a high schooler could do with pbasic, be totally autonomous, and look cool as well.
So far, thanks to your help, and help from others in this forum, things are moving along nicely! I now have the steering figured out, the speed control is next. After that I want to add ir sensors, and down the road I am considering using your bs2 cmu cam, GPS or Electronic Compass to navigate to find objects in an open field. Kind of like a robo magellan, or a small scale DARPA Grand Challenge Vehicle. Do you think a bs2sx can handle all this?
We'll see! Thanks for your help!
Thanks again!
I am not sure what is causing it to start the program over, but it surely makes it difficult to vary the horn position mid-program.
Any ideas?
Thanks!
- Adam
Solution -- replace the battery. Or, get a wall-wart (6 volts @ 500 mA is good) power supply.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
but it can work well
if control 1 servo, it work well,
this is the listing of program
' {$STAMP BS2sx}
' {$PBASIC 2.5}
temp VAR Word
DO
GOSUB jalan
LOOP
END
jalan:
'
munndur'
FOR temp = 1 TO 150
PULSOUT 0,1500 'servo1
PULSOUT 2,2100 'servo2
PAUSE 5
NEXT
'
naik'
FOR temp = 1 TO 500
PULSOUT 1,3000 'servo3
PULSOUT 3,3000 'servo4
PAUSE 5
NEXT
'
maju'
FOR temp = 1 TO 150
PULSOUT 0,2100 'servo1
PULSOUT 2,1500 'servo2
PAUSE 5
NEXT
'
turun'
FOR temp = 1 TO 150
PULSOUT 1,500 'servo3
PULSOUT 3,500 'servo4
PAUSE 5
NEXT
RETURN
what's wrong in my program?
Some things I noticed: You have "PAUSE 5" in the loops. Servos need control pulses only every 20ms or so. "PAUSE 20" would work better. You might need to change your loops since they would take more time to execute.
You didn't say what you want to do. I see that you're moving servos 0 and 2 first, then servos 1 and 3, then servos 0 and 2, then servos 1 and 3 again. If that is not what you want, you will have to change your program to do what you want.
if i control servo 0, it work well,
its the program:
FOR temp = 1 TO 150
PULSOUT 0,1500 'servo1
PAUSE 5
NEXT
but if i control servo 0 and servo 2 simultaneously, it can't work well
its the program:
FOR temp = 1 TO 150
PULSOUT 0,1500 'servo1
PULSOUT 2,2100 'servo2
PAUSE 5
NEXT
As a general rule, when you have problems with controlling more than one servo, it's usually due to an inadequate power supply.
I looked through this code (Easy Bluetooth Code For GUI (.bs2) http://www.parallax.com/Portals/0/Downloads/docs/prod/comm/30085_EasyBluetoothPBASIC.zip ) and noticed that there is no "PAUSE 20" in the code for servos' rotation.
And via the Easy Bluetooth GUI Boe-Bot navigates smoothly.
So I removed "PAUSE 20" from the code, described in this discussion "How to – Boe-Bot Robot with Easy Bluetooth Module" http://forums.parallax.com/forums/default.aspx?f=6&m=351119 , and after that my Boe-Bot moves smoothly too. But with "PAUSE 20" it moves in short continuous impulses.
However, I had to make the keyboard's "Repeat delay" longer and "Repeat rate" slower at the control panel on my laptop. Otherwise the bluetooth connection was sometimes "overwhelmed" with large amount of characters to be transmitted.
I guess that in case of navigation via Easy Bluetooth Module the role of "PAUSE 20" is played by the delay of bluetooth sending navigation characters.
I just checked that if I use the following code with "PAUSE 20" without navigation via Easy Blutooth Module but just running on Boe-Bot, Boe-Bot also moves smoothly:
So it seems that "PAUSE 20" is not that universal, at least when using the navigation via Easy Bluetooth Module.
(I have a recent USB "Rev D" Boe-Bot).
Post Edited (Alex-VII) : 6/9/2010 1:57:26 PM GMT
2. The Servo Control Signal is a 1 mSec to 2 mSec pulse, repeated every 20 milliseconds. That "20 millisecon pause" can be implemented by "other code", but repeating the signal faster than that does not help. Some control boards don't require the repetition, and some "digital servos" don't as well, but other wise it's pretty "universal".
OK. I will not. I did not looked at dates as this thread came out one of the first when I tried to search in Google.