Shop OBEX P1 Docs P2 Docs Learn Events
servo programming — Parallax Forums

servo programming

jdnphotojdnphoto Posts: 5
edited 2010-09-29 13:05 in BASIC Stamp
Can anyone tell me if there is a way of programming a servo to change position by one increment each time through a loop. Example first time through loop pulsout 14, 750 next time through loop pulsout 14, 800 and so on until loop is completed by counting. Thanks for you help.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-29 06:59
    Sure, use a FOR loop like this:

    FOR i = 750 TO 2000 STEP 50
    PULSOUT 14,i
    PAUSE 20
    NEXT i
  • jdnphotojdnphoto Posts: 5
    edited 2010-09-29 07:10
    I tried something similar and all it did was rotate the servo from 750 to 1000.

    Here is part of my program so you can see the total picture. Please look at the "'TILT"

    PULSOUT HB25D, 500 + 129 ' FORWARD MOTION
    PAUSE 20

    PULSOUT HB25H, 500 + 50 ' UP MOTION
    PAUSE 20

    SEROUT 10, BAUD, ["[E159}"] ' ROTATE
    GOSUB CHECKBUSY2
    PAUSE 10

    CHECKBUSY2:
    PAUSE 10
    CB_LOOP2:
    IF IN8 = 1 THEN CB_LOOP2
    PAUSE 100

    FOR TiltPrevPosition = 750 TO 1000 STEP 4 'TILT
    PULSOUT 15, TiltPrevPosition
    NEXT
    PAUSE 3000

    LOOP UNTIL (COUNTER = 62)
  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-29 07:21
    You have to have the PAUSE statement in the loop. A servo expects a control pulse about 50 times a second and cannot respond to pulses more frequent than that. Make the STEP size large enough so the servo moves through its range as quickly as you want even through the pulses (and loop steps) are only occurring 50 times a second.
  • jdnphotojdnphoto Posts: 5
    edited 2010-09-29 07:24
    Thank you so much I will try that.
  • ercoerco Posts: 20,256
    edited 2010-09-29 09:43
    Also make sure you have a RETURN at the end of your Checkbusy2 routine. Can't tell from your edited program listing.
  • jdnphotojdnphoto Posts: 5
    edited 2010-09-29 12:29
    I tried the following code. The servo "tilt" goes through all steps before advancing the main loop. I need it to move (step) 4 then hold this position until the main loop returns to it the next time. Then move (step) 4 hold again and so on.


    Routine2:

    DEBUG "Routine 2 is running", CR


    '
    Initialization
    DO: LOOP UNTIL HB25D = 1 'Wait for HB25 Power up
    LOW HB25D 'Make I/Opin Output/Low
    PAUSE 5 'Wait for HB25 to Initialize
    PULSOUT HB25D, 750 'Stop Motor 1

    '
    Initialization HB25H
    DO: LOOP UNTIL HB25H = 1 'Wait for HB25 Power up
    LOW HB25H 'Make I/Opin Output/Low
    PAUSE 5 'Wait for HB25 to Initialize
    PULSOUT HB25H, 750 'Stop Motor 2

    PAUSE 1000

    '
    Program Code
    UpCounter = 0

    DO
    UpCounter = UpCounter + 1


    PULSOUT HB25D, 500 + 129 'Forward 1/4"
    PAUSE 5000

    PULSOUT HB25H, 500 + 50 'Up 1/2"
    PAUSE 5000

    SEROUT 10, baud, ["{E-150}"] 'Rotate
    GOSUB checkbusy2
    PAUSE 5000

    checkbusy2:
    PAUSE 10
    CB_loop2:
    IF IN8 = 1 THEN CB_loop2
    PAUSE 100


    FOR TiltPrevPosition = 750 TO 1000 STEP 4
    PULSOUT Tilt, TiltPrevPosition 'Tilt
    PAUSE 5000
    NEXT



    DEBUG BELL
    PAUSE 3000 'Hold for 3 seconds

    LOOP UNTIL (UpCounter = 62)

    GOSUB Routine3:
  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-29 12:51
    Please re-read erco's reply. Your program logic is in trouble and the program will eventually stop working since you have GOSUBs without RETURNs.

    Your existing FOR loop won't work particularly well. You have a PAUSE 5000 in the loop which means that the servo will get one control pulse every 5 seconds. That won't work. Servos need control pulses every 20ms or they'll shut themselves off. That causes mechanical jitter and slippage.

    I think you need to back up and talk about what your program is supposed to do. Go back to a flowchart or some similar sort of diagram, even a written list of actions and when they occur.
  • Daniel HarrisDaniel Harris Posts: 207
    edited 2010-09-29 13:05
    Try this:
    Routine2:
    
    DEBUG "Routine 2 is running", CR
    
    
    '------------Initialization----------------------------
    DO: LOOP UNTIL HB25D = 1 'Wait for HB25 Power up
    LOW HB25D 'Make I/Opin Output/Low
    PAUSE 5 'Wait for HB25 to Initialize
    PULSOUT HB25D, 750 'Stop Motor 1
    
    '------------Initialization HB25H----------------------------
    DO: LOOP UNTIL HB25H = 1 'Wait for HB25 Power up
    LOW HB25H 'Make I/Opin Output/Low
    PAUSE 5 'Wait for HB25 to Initialize
    PULSOUT HB25H, 750 'Stop Motor 2
    
    PAUSE 1000
    
    '------------Program Code-----------------------------
    UpCounter = 0
    
    '----------change here --------------
    TiltPrevPosition = 750
    '----------change here --------------
    DO
    UpCounter = UpCounter + 1
    
    
    PULSOUT HB25D, 500 + 129 'Forward 1/4"
    PAUSE 5000
    
    PULSOUT HB25H, 500 + 50 'Up 1/2"
    PAUSE 5000
    
    SEROUT 10, baud, ["{E-150}"] 'Rotate
    GOSUB checkbusy2
    PAUSE 5000
    
    checkbusy2:
    PAUSE 10
    CB_loop2:
    IF IN8 = 1 THEN CB_loop2
    PAUSE 100
    
    '----------change here --------------
    PULSOUT Tilt, TiltPrevPosition 'Tilt
    
    TiltPrevPosition = TiltPrevPosition + 4
    '----------change here --------------
    
    
    DEBUG BELL
    PAUSE 3000 'Hold for 3 seconds
    
    LOOP UNTIL (UpCounter = 62)
    
    GOSUB Routine3:
    
Sign In or Register to comment.