Shop OBEX P1 Docs P2 Docs Learn Events
More about Standard Servo Control with the BASIC Stamp 2 — Parallax Forums

More about Standard Servo Control with the BASIC Stamp 2

edited 2010-06-09 19:44 in Learn with BlocklyProp
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

Comments

  • edited 2007-01-10 00:48
    The Servo Control Signal

    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
    ······ attachment.php?attachmentid=44946

    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
    ······ attachment.php?attachmentid=44947

    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
    ······ attachment.php?attachmentid=44948

    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).

    ' PointTwoDirections.bs2
    ' Point at 10:30, then point at 1:30
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    counter VAR Word
    
    FOR counter = 1 TO 150
    
    PULSOUT 14, 1000
    PAUSE 20
    
    NEXT
    
    FOR counter = 1 TO 150
    
    PULSOUT 14, 500
    PAUSE 20
    
    NEXT
    
    END
    
    


    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).

    · ···· attachment.php?attachmentid=44949

    ······ attachment.php?attachmentid=44950

    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
  • JimFolsJimFols Posts: 11
    edited 2007-01-12 05:18
    This is great. I spent a lot of time getting the 'servo position' part in my head. This makes it much easier to understand.

    Thank you,

    Jim
  • BobalouBobalou Posts: 7
    edited 2007-01-13 07:15
    Wow! Activity #1 left me wondering just how the pulse duration determined position, and how it knew to go CCW or CW. So I decided to ask here. This post answered all my questions. Thanks.

    Bob
  • hacktorioushacktorious Posts: 72
    edited 2007-02-14 01:42
    This post really cleared things up and helped me grasp the concept of the standard servo. Thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Scott
  • churdchurd Posts: 9
    edited 2007-03-06 02:28
    If I could ask to take this one step farther, how would one hold the position of the servo, while making something else happen, like make a motor run on a different pin? Would you bury this in the for next loop as well?

    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?
  • PARPAR Posts: 285
    edited 2007-03-06 07:43
    churd said...
    If I could ask to take this one step farther, how would one hold the position of the servo, while making something else happen, like make a motor run on a different pin? Would you bury this in the for next loop as well?

    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?
    You can "time share" the pauses. I.e., instead of "Pause"ing for 20, you can do something else for 20, or you can do several things during that 20, or you can do something else for 10 and then pause for 10, yielding a total "pause" of 20.

    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
  • churdchurd Posts: 9
    edited 2007-03-06 12:27
    Thanks Par

    This helped immensely, as I wont have to waste that 20ms anymore!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-06 21:13
    Churd,

    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
  • churdchurd Posts: 9
    edited 2007-03-06 21:39
    Chris:

    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!
  • legiondttmlegiondttm Posts: 4
    edited 2007-03-28 18:41
    When using the PULSOUT command, I noticed that the program will restart back up at the very beginning. (Every time the pulsout is executed, the program starts over). I am using a BS2px stamp and a HS-311 standard servo.

    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
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-03-28 19:33
    Yes, this is a classic "You don't have enough power for the servo and the BS2, so every time the servo starts to move, it brown-out resets the BS2" symptom.

    Solution -- replace the battery. Or, get a wall-wart (6 volts @ 500 mA is good) power supply.
  • legiondttmlegiondttm Posts: 4
    edited 2007-03-28 20:10
    Would using separate power supplies work? Or would that cause potential problems for the components?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-28 20:13
    In many cases separate supplies are recommended. All you need to do is be sure the ground lines are common (tied together). Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • churdchurd Posts: 9
    edited 2007-03-29 01:07
    I solved my problem by using the standard 9V through a regulator to power the stamp, and my servos and other sensors are all powered through a 1500mAH 7.2 V pack. This is also regulated to 5 V. With 3 Sharp IR, an IRPD, and a Quad encoder,I figure I should be able to get at the very least 1 hr out of the 7.2 V pack, and forever from the 9V.
  • leoboyleoboy Posts: 2
    edited 2008-02-24 19:48
    i have control two standard servo simultaneously,
    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?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-24 20:30
    You didn't say what happens when you try to control more than one servo.

    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.
  • leoboyleoboy Posts: 2
    edited 2008-02-25 06:02
    i mean that servo 0 and 2 simultaneously, and then servo 1 and 3 simultaneously,

    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
  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-25 06:55
    Again, if you want specific suggestions, you will have to describe more thoroughly what you've done and what you're observing.
    As a general rule, when you have problems with controlling more than one servo, it's usually due to an inadequate power supply.
  • tahlorntahlorn Posts: 18
    edited 2008-03-29 17:39
    So I entered the code given at the top so as to let the servo go to one position, pause, then go to the other. But It goes to the first position, and then twitches rather than going to the second. If i just use the first or second part of the code, it goes to where I need. But if I use both part, then it twitches and doesn't go anywhere. I know the servo works fine on its own. Any suggestions?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-29 17:52
    Usually this is a power supply problem. For some reason, the supply voltage drops after the servo has run for a little while and the Stamp resets starting the program over again. If you leave out the first part of the program, the servo moves for a short time, then the program finishes. If you're running off batteries, get fresh ones. If you're using some kind of AC supply, make sure it can furnish several hundred milliAmps with peaks to around 1 Ampere since that's what a normal size servo can draw. If you're running the servo off a 5V regulator, make sure the regulator can supply that kind of current without overheating since that will shut down the regulator.
  • tahlorntahlorn Posts: 18
    edited 2008-03-30 00:34
    I actually have it running off of the USb/serial port for power. Voltage that the servo is powered from drops from 4.85V to 4.75V. And actually now the servo isn't even doing anything, even with jsut the first part. So it probably is just the power, will have to get an additional power supply.
  • Alex-VIIAlex-VII Posts: 8
    edited 2010-06-09 13:31
    Mike Green said...


    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.


    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:

    
    FOR counter = 1 TO 122
    
      PULSOUT 13, 850
      PULSOUT 12, 650
      PAUSE 20
    
    NEXT
    
    
    



    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
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-06-09 14:24
    1. PLEASE don't "resurrect" really old threads -- this one was last posted to March of 2008. Start a new thread if you want to.

    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".
  • Alex-VIIAlex-VII Posts: 8
    edited 2010-06-09 19:44
    allanlane5 said...
    1. PLEASE don't "resurrect" really old threads -- this one was last posted to March of 2008. Start a new thread if you want to.


    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.
Sign In or Register to comment.