Shop OBEX P1 Docs P2 Docs Learn Events
How to pause a servo motor in the middle location. — Parallax Forums

How to pause a servo motor in the middle location.

Maintenance manMaintenance man Posts: 17
edited 2011-11-29 00:04 in BASIC Stamp
I was wondering how I can make a servo motor stop midway,pause for a second or so then continue all the way to one side then come back,pause for a second or so midway, then continue all the way in other direction.I would like to continue this action where the servo turns one way then the other way in which it pauses for a second or so every time it is in the middle.Thank you all for your help.

Comments

  • graffixgraffix Posts: 389
    edited 2011-11-26 11:10
    Try chapter 4 controlling motion, in Whats a microcontroller book version 2.2 .Its a free download at parallax.com page 115 is some code
    this should be close its a newer version http://www.parallax.com/Portals/0/Downloads/docs/prod/edu/28123-WAM-v3.0.pdf
  • Maintenance manMaintenance man Posts: 17
    edited 2011-11-26 11:31
    750 is the middle when I pulse out ? Do I use the 750 for both directions? Thank you for your help.
  • hover1hover1 Posts: 1,929
    edited 2011-11-26 11:42
    750 is the middle when I pulse out ? Do I use the 750 for both directions? Thank you for your help.

    Use 1000 for Counter Clockwise, than 750 for Center, then 500 for clockwise, then 750 for center. (750 will always return to center)

    Rinse repeat.
  • Maintenance manMaintenance man Posts: 17
    edited 2011-11-26 12:04
    Thank you very much for your help.
  • ercoerco Posts: 20,256
    edited 2011-11-26 12:20
    Using FOR/NEXT loops, you can move the servo any direction, any speed you want. Such as:

    for b0=500 to 1000 step 1
    pulsout 1,b0
    pause 20
    next

    then try step 2 or anything larger. And obviously you could stop mistroke using

    for b0=500 to 750

    etc....
  • Maintenance manMaintenance man Posts: 17
    edited 2011-11-26 23:20
    This is the program I have used to pause the servo at the 90 deg mark in which it stops for about a second in this position then continues to the far left then right.I adjusted the numbers to get a full stroke out of the servo motor at 0 degrees and 180 degrees. Can I improve on this program? Please let me know. I will have to mechanically adjust the servo so that it stops more in the middle. I adjusted the numbers as a learning process with the servo as is. Thank you all for your help.




    ' {$STAMP BS2}
    ' {$PBASIC 2.0}


    x VAR Word
    OUTPUT 14
    here:
    FOR x = 1 TO 100
    PULSOUT 14, 250
    PAUSE 10
    NEXT
    PAUSE 500
    FOR x = 1 TO 100
    PULSOUT 14, 750
    PAUSE 10
    NEXT
    PAUSE 1000
    FOR x = 1 TO 100
    PULSOUT 14, 1090
    PAUSE 10
    NEXT
    PAUSE 500
    FOR x = 1 TO 100
    PULSOUT 14, 750
    PAUSE 10
    NEXT
    PAUSE 1000
    GOTO here
  • W9GFOW9GFO Posts: 4,010
    edited 2011-11-26 23:45
    Your PAUSEs are a little short. The standard pause length for servo control is PAUSE 20. A regular hobby servo expects to get a pulse 50 times every second. That would be 50 20ms pauses for every second. However, the pulse itself and the other bits of code take some time to execute so I tend to use PAUSE 19 to come closer to 50Hz. Also, if you use PAUSE 500 or 1000, the servo will be unpowered during that time, if it is under any load it may lose it's position.

    The following code should perform exactly as before except the servo will hold position better.
    ' {$STAMP BS2}
    ' {$PBASIC 2.0}
    
    
    x VAR Word
    
    DO
    
    FOR x = 1 TO 75
    PULSOUT 14, 250
    PAUSE 19
    NEXT
    
    FOR x = 1 TO 100
    PULSOUT 14, 750
    PAUSE 19
    NEXT
    
    FOR x = 1 TO 75
    PULSOUT 14, 1090
    PAUSE 19
    NEXT
    
    FOR x = 1 TO 200
    PULSOUT 14, 750
    PAUSE 19
    NEXT
    
    LOOP
    
  • Maintenance manMaintenance man Posts: 17
    edited 2011-11-27 00:25
    Thank you very much for your help, I appreciate it. To make sure I understand this; my FOR NEXT loop is how many times the program will repeat this section holding the servo at that location ? Example would be Pin 14 holding the servo at 250 while the loop repeated 1 to 75 pausing 19 ms between each loop then going on to the next. So actualy the longer I want to hold position the longer I would put the for x = 1 to a higher number than 75 depending on how long I would want it to hold. I hope I am on the right track. Thank you very much for your time and help.
  • W9GFOW9GFO Posts: 4,010
    edited 2011-11-27 01:51
    Yes, that is correct.
  • graffixgraffix Posts: 389
    edited 2011-11-27 07:22
    attachment.php?attachmentid=78421&d=1297987572
  • Maintenance manMaintenance man Posts: 17
    edited 2011-11-27 10:22
    This is great; thank you so very much graffix for your help in properly posting code; I greatly appreciate it a lot.




























  • Maintenance manMaintenance man Posts: 17
    edited 2011-11-27 16:31
    On these 2 programs for servo control, I would like some feed back on how I wrote the programs and they way I did the remarks. Thank you all for your help.
    ' {$STAMP BS2}
    ' {$PBASIC 2.0}
    
    
    
    x VAR Word                'a variable called x one word long
    
    here:                     'a label
    
    FOR x = 1 TO 75           'beginning of a for...next loop for how long the servo is held at a location (270)
    PULSOUT 14, 270           'the pin (P14) that sends out the postion(270) for the servo to stop at the far right
    PAUSE 19                  'pause 19 ms
    NEXT                      'if x has reached 75 at this stage,the program will continue
    
    
    FOR x = 1 TO 100          'beginning of a for...next loop for how long the servo is held at a location(750)
    PULSOUT 14, 750           'the pin (P14) that sends out the postion(750)for the servo to stop in the middle
    PAUSE 19                  'pause 19 ms
    NEXT                      'if x has reached 100 at this stage,the program will continue
    
    
    FOR x = 1 TO 75           'beginning of a for...next loop for how long the servo is held at a location(1160)
    PULSOUT 14, 1160          'the pin (P14) that sends out the postion(1160)for the servo to stop at the far left
    PAUSE 19                  'pause 19 ms
    NEXT                      'if x has reached 75 at this stage,the program will continue
    
    
    FOR x = 1 TO 100          'beginning of a for...next loop for how long the servo is held at a location(750)
    PULSOUT 14, 750           'the pin (P14) that sends out the postion(750)FOR the servo TO STOP in the middle
    PAUSE 19                  'pause 19 ms
    NEXT                      'if x has reached 100 at this stage,the program will continue
    
    
    GOTO here                 'go back to the here label to do it all again
    
    


    
    ' {$STAMP BS2}
    ' {$PBASIC 2.0}
    
    
    x VAR Word                   'a variable called x one word long
    
    
    here:                        'a label
    
    
    FOR x = 1 TO 100             'beginning of a for...next loop for how long the servo is held at a location (270)
    PULSOUT 14, 270              'the pin (P14) that sends out the postion(270) for the servo to stop at the far right
    PAUSE 19                     'pause 19 ms
    NEXT                         'if x has reached 100 at this stage,the program will continue
    
    
    FOR x = 1 TO 100             'beginning of a for...next loop for how long the servo is held at a location(750)
    PULSOUT 14, 750              'the pin (P14) that sends out the postion(750)for the servo to stop in the middle
    PAUSE 19                     'pause 19 ms
    NEXT                         'if x has reached 100 at this stage,the program will continue
    
    
    FOR x = 1 TO 100             'beginning of a for...next loop for how long the servo is held at a location(270)
    PULSOUT 14, 270              'the pin (P14 that sends out the postion (270)for the servo to stop at the far right.
    PAUSE 19                     'pause 19 ms
    NEXT                         'if x has reached 100 at this stage,the program will continue
    
    
    FOR x = 1 TO 100             'beginning of a for...next loop for how long the servo is held at a location (1150)
    PULSOUT 14, 1150             'the pin (P14) that sends out the postion (1150)for the servo to stop at the far right
    PAUSE 19                     'apuse 19 ms
    NEXT                         'if x has reached 100 at this stage,the program will continue
    
    
    GOTO here                    'go back to the here label to do it all again
    
    
  • W9GFOW9GFO Posts: 4,010
    edited 2011-11-27 17:43
    I think you are on the right track - a little wordy for me but it is more important that it work for you.

    Try to avoid using GOTO. DO..LOOP will accomplish what you need.

    You might want to try subroutines next.

    Partial example;
    ' {$STAMP BS2}
    ' {$PBASIC 2.0}
    
    
    
    x VAR Word                'a variable called x one word long
    
    DO
    
    GOSUB LEFT
    GOSUB CENTER
    GOSUB RIGHT
    GOSUB CENTER
    
    LOOP
    
    LEFT:
    FOR x = 1 TO 75           'beginning of a for...next loop for how long the servo is held at a location (270)
    PULSOUT 14, 270           'the pin (P14) that sends out the postion(270) for the servo to stop at the far right
    PAUSE 19                  'pause 19 ms
    NEXT                      'if x has reached 75 at this stage,the program will continue
    RETURN
    
    RIGHT:
    enter code here
    RETURN
    
    CENTER:
    enter code here
    RETURN
    
  • Maintenance manMaintenance man Posts: 17
    edited 2011-11-27 17:55
    Thank you very much for you help W9GFO.
  • graffixgraffix Posts: 389
    edited 2011-11-27 17:59
    Hows the speed of the servo motion,did you understand what erco was talking about?
  • Maintenance manMaintenance man Posts: 17
    edited 2011-11-28 17:59
    I think I understand. The position of the servo is 500 for the far right, 750 is for the middle, and 1000 is for the far lift ; in which the servo can be controlled to stop any ware between 500 and 1000, depending on numbers used . As far as the speed of the servo, this depends on the for next loop such as x = 1 to 100 or faster movement x=1 to 75 where the servo is held in a certain position for so many loops before it moves to the next step. If I am off a little, please put me back on track if you could be so kind. Thank you very much for the great help. Nothing is worse than going on with someting that you think you understand, to find out later you were wrong. I just want to learn things right. Thanks to all for the help and your time.
  • ercoerco Posts: 20,256
    edited 2011-11-28 19:41
    Sounds like you got it, Maintenance Man! A steady stream of pulses is required to constantly drive the servo. Pulsewidth determines location, 500-1000 on a BS2. Servo speed is controlled by your program gradually changing the pulsewidth, usually by an incrementing loop. And you can go fast one way and slow in the other, as my latest multiservo demo video shows: http://www.youtube.com/watch?v=HSLktpOkrGs
  • AImanAIman Posts: 531
    edited 2011-11-28 22:10
    You have learned the first rule of robotics - The only thing you need to remember about robotics is don't stop trying.

    This is more advanced than what you are talking about but it works like a charm. You can run a servo and in the pauses of the servo run sonar like PING. The pauses may need to be increased by a couple ms but it will not be visually noticeable and will not affect the speed of your code. So instead of saying Pause 19 you might say Pause 20 or Pause 21.

    My sonar was not a PING and ran a bit slower than PING and the servo loop could easily handle the timing.
  • W9GFOW9GFO Posts: 4,010
    edited 2011-11-29 00:04
    If you add something inside the servo loop you will need to decrease the PAUSE length not increase it. The goal is to send the servo a pulse every 20 milliseconds, Whatever time the new code takes to execute should be subtracted from the PAUSE length.
Sign In or Register to comment.