Shop OBEX P1 Docs P2 Docs Learn Events
Controlling Servo Movement — Parallax Forums

Controlling Servo Movement

biped_androidbiped_android Posts: 7
edited 2011-02-11 13:07 in Learn with BlocklyProp
Hello, I am new here, and to the Basic stamp stuff. I do have background in basic, analog and digital electronics (although it has been a few years since I've really dealt with it, so I am rusty). What I don't have is background in programming, so I'm needing a help please.

I bought a Stamps in Class Homework board, for this project, several weeks ago, and the programming is confusing the heck out of me.

I've got the REV D board, I've already downloaded the stamp editor 2.5.1, and the newest version of the microcontroller book.

First question, with the stamp editor, what are the differences of, I'm not sure what you want to call it, its the BS1, BS2, BS2sx, BS2p, BS2px, directories?


Servo/programming questions (I'm going to do my best to describe what I need to do)

The "PULSOUT" is confusing me a bit. I know that for PULSOUT 14, 14 is the pin number. Whats confusing me is how to figure out the movement of degrees.

What I need to do is use a servo to activate a switch 200 times and then stop after 200 times, and have an LED activate once finished. I need a switch to be pressed, by a person, to reset it and to start the process over again, until the unit is turned off.

I'm still working on the wiring for the LED and the reset switch, I'm not sure where to place them at, yet, I'm still going to work on it before I ask for help on it.

To tell the truth, I have no clue where, or how to start the programming. I've ran through the book but I keep getting confused... I am not the best at working from a book, I'm a hands on, visual learner.

Any help would be extremely appreciated.

If you need any more information please don't hesitate to ask.

Thank You,
John

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2011-02-09 19:03
    For the servo programming,

    First, find out what PULSOUT values you need for the two positions the servo arm is going to be in. One for pressed and one for not pressed. The values to test range between 500 and 1000.

    Here is a test program;
    DO
    PULSOUT 14, 500
    PAUSE 20
    LOOP
    

    It will send the PULSOUT value of 500 (1.000 milliseconds) to pin 14, forever.

    Change the value until you find the one you need.

    Once you know the values make two subroutines, kind of like this;
    PRESS:
    FOR i = 1 to 20
    PULSOUT 14, 500
    PAUSE 20
    NEXT
    RETURN
    
    UN_PRESS:
    FOR i = 1 to 20
    PULSOUT 14, 1000
    PAUSE 20
    NEXT
    RETURN
    

    Then your final program will look something like this;
    i   VAR   BYTE
    x  VAR   BYTE
    
    MAIN:
    
    FOR x = 1 to 200
    GOSUB PRESS
    GOSUB UNPRESS
    PAUSE 5000   ' five seconds between each press
    NEXT
    
    HIGH 7 
    
    PRESS:
    FOR i = 1 to 20
    PULSOUT 14, 500
    PAUSE 20
    NEXT
    RETURN
    
    UN_PRESS:
    FOR i = 1 to 20
    PULSOUT 14, 1000
    PAUSE 20
    NEXT
    RETURN
    
    END
    

    Rich H
  • biped_androidbiped_android Posts: 7
    edited 2011-02-09 19:26
    W9GFO wrote: »
    For the servo programming,

    First, find out what PULSOUT values you need for the two positions the servo arm is going to be in. One for pressed and one for not pressed. The values to test range between 500 and 1000.

    Here is a test program;
    DO
    PULSOUT 14, 500
    PAUSE 20
    LOOP
    
    It will send the PULSOUT value of 500 (1.000 milliseconds) to pin 14, forever.

    Change the value until you find the one you need.

    Once you know the values make two subroutines, kind of like this;
    PRESS:
    FOR i = 1 to 20
    PULSOUT 14, 500
    PAUSE 20
    NEXT
    RETURN
    
    UN_PRESS:
    FOR i = 1 to 20
    PULSOUT 14, 1000
    PAUSE 20
    NEXT
    RETURN
    
    Then your final program will look something like this;
    i   VAR   BYTE
    x  VAR   BYTE
    
    MAIN:
    
    FOR x = 1 to 200
    GOSUB PRESS
    GOSUB UNPRESS
    PAUSE 5000   ' five seconds between each press
    NEXT
    
    HIGH 7 
    
    PRESS:
    FOR i = 1 to 20
    PULSOUT 14, 500
    PAUSE 20
    NEXT
    RETURN
    
    UN_PRESS:
    FOR i = 1 to 20
    PULSOUT 14, 1000
    PAUSE 20
    NEXT
    RETURN
    
    END
    
    Rich H

    Just to make sure that I didn't word the switch area oddly..., the switch will ONLY be activated once the 200 cycles have been completed... The switch is only to restart the cycles again.

    This servo is going to take over switch cycle testing for products at where I work at. So instead of me having to move a joystick on products 800 times (200 per direction) this will do my work.

    Sorry if I made it confusing on my first post.

    ----
    I noticed one thing about your code...

    The line "GOSUB UNPRESS" is missing the "_" while the UN_PRESS Subroutine has the underscore. is that a typo or is that correct?

    ----

    I'll toss the test program in, and get my values, and will get back later tonight or tomorrow.

    ---

    Thank you for the quick response and help. :thumb:
  • W9GFOW9GFO Posts: 4,010
    edited 2011-02-09 19:35
    Good catch on the typo. This code is not tested.

    As written it is supposed to cycle the servo 200 times (to activate the switch), then light the led on pin 7.

    To reset you just push the "Reset" button, it is not part of the code. When you push the reset button on the Homework board the code starts over form the beginning.

    Rich H
  • biped_androidbiped_android Posts: 7
    edited 2011-02-09 19:46
    W9GFO wrote: »
    Good catch on the typo. This code is not tested.

    As written it is supposed to cycle the servo 200 times (to activate the switch), then light the led on pin 7.

    To reset you just push the "Reset" button, it is not part of the code. When you push the reset button on the Homework board the code starts over form the beginning.

    Rich H

    Okay, understood.

    Okay, I thought the reset button, reset the stamp back to factory. LOL

    I'll work on the code tomorrow, and get it loaded to the stamp.

    Thank you again for the help.
  • biped_androidbiped_android Posts: 7
    edited 2011-02-10 07:01
    okay, after getting a night of sleep. I understand what you mean by the reset button.

    Since I do not need an extra switch to reset the program, what would be different about the code without the switch?

    The led still needs to remain as to give another indication that the program has finished running.

    sorry for making this difficult :(
  • W9GFOW9GFO Posts: 4,010
    edited 2011-02-10 15:57
    Hmm, I read your situation as needing to hit a switch 200 times and that you would use a servo to hit that switch. Then after the 200th time a led would light indicating that the 200 times was completed. Then, after seeing that led, you would hit the reset button to start the process over.
    what would be different about the code without the switch?

    Isn't the whole point of the code to hit a switch 200 times?

    Rich H
  • biped_androidbiped_android Posts: 7
    edited 2011-02-10 16:31
    W9GFO wrote: »
    Hmm, I read your situation as needing to hit a switch 200 times and that you would use a servo to hit that switch. Then after the 200th time a led would light indicating that the 200 times was completed. Then, after seeing that led, you would hit the reset button to start the process over.



    Isn't the whole point of the code to hit a switch 200 times?

    Rich H


    Yes it is I think, I'll re-explain it, since I had a misunderstanding about the board and a few other things...

    The servo will be connected to a joy-stick on a mobile TV (using a piece of stiff wire), that joy-stick has to be tested for 200 cycles.

    The switch that I thought was going to be the "reset" switch is not needed anymore.

    The only thing I need this to do, is move the servo from 45degrees to 90degrees, and back to 45degrees, 200 times and the LED to light up after the 200th time.

    I worked on the code for this today, trying to understand it more, and I think I have a code that could work. I would like to see if I can have it checked out for any errors. I have not been able to upload it to my board yet, I'm waiting for the Serial to USB adapter to arrive.
    Counter VAR BYTE
    
        FOR Counter = 1 to 200
            PULSOUT 14, 500 'Start at 45 degrees
            PAUSE 500 'half second pause to imitate human action
            PULSOUT 14, 750 'End at 90 degrees
            PAUSE 500 'half second pause to imitate human action
            
        NEXT
        
            HIGH 7 'Activate LED AFTER 200 counts of cycle
            PAUSE 20
            
    END
    
    
  • W9GFOW9GFO Posts: 4,010
    edited 2011-02-10 21:05
    Counter VAR BYTE
    
        FOR Counter = 1 to 200
            PULSOUT 14, 500 'Start at 45 degrees
            PAUSE 500 'half second pause to imitate human action
            PULSOUT 14, 750 'End at 90 degrees
            PAUSE 500 'half second pause to imitate human action
            
        NEXT
        
            HIGH 7 'Activate LED AFTER 200 counts of cycle
            PAUSE 20
            
    END
    

    That won't work. You are sending the servo one pulse every half second. Servos need to be continuously refreshed, they expect about 50 pulses per second. That's why in my example code I placed each servo movement in it's own subroutine like this;
    45_Degrees:
    FOR i = 1 to 25  ' 25 times is about 1/2 second
    PULSOUT 14, 500
    PAUSE 20
    NEXT
    RETURN
    

    Rich H
  • biped_androidbiped_android Posts: 7
    edited 2011-02-11 06:53
    W9GFO wrote: »
    Counter VAR BYTE
    
        FOR Counter = 1 to 200
            PULSOUT 14, 500 'Start at 45 degrees
            PAUSE 500 'half second pause to imitate human action
            PULSOUT 14, 750 'End at 90 degrees
            PAUSE 500 'half second pause to imitate human action
            
        NEXT
        
            HIGH 7 'Activate LED AFTER 200 counts of cycle
            PAUSE 20
            
    END
    
    That won't work. You are sending the servo one pulse every half second. Servos need to be continuously refreshed, they expect about 50 pulses per second. That's why in my example code I placed each servo movement in it's own subroutine like this;
    45_Degrees:
    FOR i = 1 to 25  ' 25 times is about 1/2 second
    PULSOUT 14, 500
    PAUSE 20
    NEXT
    RETURN
    
    Rich H

    I figured I had something wrong, usually do :blank:

    Can I get an explanation for a couple of things, please?

    the "i" on the FOR line, what does the "i" mean/do? Can the "i" be anything or does it have to be an "i"? Such as "FOR MyWhatEver = 1 to 25" (the i is replaced by MyWhatEver)

    Also, for the PAUSE 20, will it always be a 20 after pause?
  • W9GFOW9GFO Posts: 4,010
    edited 2011-02-11 11:56
    The "i" can be just about anything. It is a variable that you declare.

    The reason for the PAUSE 20 is to space the PULSOUT commands apart by 20 milliseconds so that the pulses are sent at about 50 times a second. Technically, PAUSE 18 or 19 would be more accurate because the PULSOUT command itself consumes over a millisecond and the other lines of code also use a tiny bit of time. PAUSE 20 is used because it is a nice round number and it works just fine.

    If your loop included more lines of code you would want to reduce the PAUSE time so as to maintain the 50Hz pulse rate. Depending on how much time the program is spending running the other lines of code you may have a PAUSE 15, PAUSE 5 or even eliminate the PAUSE altogether. PAUSE 20 is the absolute highest PAUSE that you ever want in a PULSOUT loop. So no, it will not always be a 20 after the PAUSE.

    Rich H
  • biped_androidbiped_android Posts: 7
    edited 2011-02-11 13:07
    W9GFO wrote: »
    The "i" can be just about anything. It is a variable that you declare.

    The reason for the PAUSE 20 is to space the PULSOUT commands apart by 20 milliseconds so that the pulses are sent at about 50 times a second. Technically, PAUSE 18 or 19 would be more accurate because the PULSOUT command itself consumes over a millisecond and the other lines of code also use a tiny bit of time. PAUSE 20 is used because it is a nice round number and it works just fine.

    If your loop included more lines of code you would want to reduce the PAUSE time so as to maintain the 50Hz pulse rate. Depending on how much time the program is spending running the other lines of code you may have a PAUSE 15, PAUSE 5 or even eliminate the PAUSE altogether. PAUSE 20 is the absolute highest PAUSE that you ever want in a PULSOUT loop. So no, it will not always be a 20 after the PAUSE.

    Rich H

    ah okay.

    So anytime that PULSOUT is used, one has to maintain a 50Hz signal (or as close to it as possible).

    I'll post more in a bit, I have to finish work at, well, work.

    Thank you for explaining that more indepth to me :thumb:
Sign In or Register to comment.