Shop OBEX P1 Docs P2 Docs Learn Events
Servo programing — Parallax Forums

Servo programing

ThirteenevenThirteeneven Posts: 11
edited 2011-05-07 18:51 in BASIC Stamp
hi
I just bought a parallax standard servo and board of education with a basic stamp 2 in it. I need to rotate clockwise 30 degrees and then rotate back counterclockwise 30 degrees what would be a good code for the Basic Stamp Editor
Thank you
Josh

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-08-20 17:37
    Look in the "Robotics with the BoeBot" tutorial downloadable from Parallax (go to main webpage & click on Resources / Downloads / Stamps in Class Downloads. It discusses servos and how they work. It mostly talks about continuous motion servos, but the same ideas and examples are applicable, just that the control pulses affect the servo position rather than the speed and direction. Servos are not exact things, so you'll have to experiment to find what control pulse widths correspond to your two positions.
  • ManetherenManetheren Posts: 117
    edited 2009-08-20 18:21
    I used the following code to get a gate i created to move 90 degrees.· I found that the standard servo started at 200 and ended at around 1200.· My first one (reset) was to set the gate to the closed position and whereever it was it would close with the 200.· I used the temp variable to store the code on current location because if you just let it run through the loop and the gate was not in the correct location it would just crash back to it and then start running the loop.· Let me know if you do not understand the loops below.



    'Initialization

    Temp··· VAR···· Word

    Reset:

    FOR temp = 200 TO 200························ 'Reset the cate to closed'
    PULSOUT Servo_pin,temp
    PAUSE 20
    NEXT





    Main:

    IF (CloseGate = 1) THEN
    FOR temp = Temp TO 200········ 'Use currently stored word for TEMP as starting point.
    PULSOUT Servo_pin,temp
    PAUSE 20
    IF (interlock = 0)THEN············ 'Break the loop when the gate reaches the latch.'
    Gate = 2
    GOTO main
    ENDIF
    NEXT
    ELSEIF (OpenGate = 1) THEN
    FOR temp = Temp TO 700
    PULSOUT Servo_pin,temp
    PAUSE 20
    IF (interlock = 0) AND (closegate = 1) THEN············ 'Break the loop if the BOT reaks INTERLOCK while gate is closing.'
    gate = 2
    GOTO main
    ENDIF
    NEXT
    ENDIF

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tia'Shar Manetheren
  • dylantsdylants Posts: 13
    edited 2011-05-04 06:00
    this is what I used

    DO

    FOR counter = 500 TO 1000 STEP 20
    PULSOUT 14, counter
    PAUSE 20
    DEBUG DEC5 counter, CR, CRSRUP
    NEXT



    FOR counter = 1000 TO 500 STEP 20
    PULSOUT 14, counter
    PAUSE 20
    DEBUG DEC5 counter, CR, CRSRUP
    NEXT
    LOOP
  • Mike GreenMike Green Posts: 23,101
    edited 2011-05-04 06:59
    You have to adjust the PAUSE time to account for the time used by the DEBUG. For most Stamps, the DEBUG works at 9600 Baud which takes roughly 1ms per character. Your DEBUG statements will take about 7-8 milliseconds to execute and you have to change the PAUSE to maybe 12 instead of 20.

    With the step size you've got, there are 25 steps. That comes out to a total time of 1/2 second. That's not much for the servo to move through its mostly full range. You might try changing your step size to 10 rather than 20. That would allow a whole second for the movement.
  • kidsasukekidsasuke Posts: 8
    edited 2011-05-07 18:51
    Basically, the servo sweeps back and forth between 250 and 1250. It's suggested (at least in the course I was taking) that you don't go to the edges because the servo basically freaks out for whatever reason (tres technical, non?). This represents 0 degrees (far right) at 250 and 180 degrees (far left) at 1250. If you take the difference of 1000 between the pulsout values and divide it by 180, you can figure out what increment of pulsout values will equal x number of degrees.

    From some quick math, that makes 166.666666 repeating for 30 degrees of rotation. Since the BS2 chip doesn't support values after the decimal point, you'll round off to either 166 or 167 (your choice, not much of a difference). This is the difference between your "FOR counter" values (e.g. 500 to 667), just look at Manetheren or dylants examples. Then, it's simply a matter of figuring out the pause values to make it move at the right rate between the two positions. 20 will lock the servo in position, while obviously smaller pauses tend to equal faster motion. Make sure to constantly test the servo motion as you add code, as the increase in processing seems to mess with the servo's motion.

    I hope this can help to clarify what you're looking at. I'm not the most well-versed in this field, but I had to do a LOT of work with the servo for my final project.
Sign In or Register to comment.