Shop OBEX P1 Docs P2 Docs Learn Events
i need to write a prgram that rotates the servo — Parallax Forums

i need to write a prgram that rotates the servo

axial28rraxial28rr Posts: 16
edited 2011-04-10 17:03 in General Discussion
i need to write a prgram that roates a servo using the debug terminal. the user must enter a number roattion value. the program below is for a user to hit a push button so the servo can turn 45 degrees from zero and turn back from 45 to zero by pressing the other push button i dont know if it works becuase i dont have stamp to test it any help greatly appreciate

this is what i have so far

' What's a Microcontroller - ServoCenter.bs2
' rotate the servo using the debug widow.
' {$STAMP BS2}
' {$PBASIC 2.5}

DEBUG "Program Running!", CR
DEBUG "press button in3",CR

DO
LOOP UNTIL IN3 = 1 AND IN4 = 0
PULSOUT 14, 500
STOP

DEBUG "press in4 to return to zero 0",CR
DO
LOOP UNTIL IN3 = 0 AND IN4 = 1
PULSOUT 14, 250
STOP

Comments

  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-04-09 19:41
    attachment.php?attachmentid=78421&d=1297987572

    Also, you might want to post this question in the Basic Stamp forum so you get a better chance of getting an answer.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-09 20:40
    You may not have a Stamp to test your program, but you can download "What's a Microcontroller?" and "Robotics with the BoeBoe" from Parallax's Download webpage. The first will teach you how the various statements work like DO / LOOP and the second will teach you how servos work. Both have plenty of example code.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-04-09 20:55
  • ercoerco Posts: 20,261
    edited 2011-04-09 21:28
    You'll need some PAUSE 20 commands after your PULSOUTS, which normally range from 500 to 1000 for a standard servo and a BS2.
  • axial28rraxial28rr Posts: 16
    edited 2011-04-10 06:30
    if i put a pause would that dely ? i just need it to go to 45 degrees with one push button and back to o degress with the other push button
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-10 07:10
    The PAUSE is a requirement for all servos. It has to do with how servos work. Chapter 4 of "What's a Microcontroller?" discusses servos and how they're used with a Stamp. There's also a Wikipedia article with a section on R/C Servos here.
  • axial28rraxial28rr Posts: 16
    edited 2011-04-10 07:40
    would this work i put a PAUSE 500 would this go to 45 degress and back with 2 push buttons ? or is 500 to high sorry am still learning

    i posted it in code forum i dont want to repost in basic stamp because it will be a double post unless a mod can move it

    thanks
    ' What's a Microcontroller - ServoCenter.bs2
    ' 0 degree to 45  degree and back to 0 degrees .
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    DEBUG "Program Running!", CR
    DEBUG "press button in3",CR
    DO
    LOOP UNTIL IN3 = 1 AND IN4 = 0
    PULSOUT 14, 500
    PAUSE 500
    STOP
     
    DEBUG "press in4 to return to zero 0",CR
     
    DO
    LOOP UNTIL IN3 = 0 AND IN4 = 1
    PULSOUT 14, 250
    PAUSE 500
    STOP
    
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-10 07:47
    No, it won't work. Read the Wikipedia article and the WAM chapter.

    Although it would have been better to post this originally in the Stamp forum, it's a general enough discussion of servos to leave here.
  • axial28rraxial28rr Posts: 16
    edited 2011-04-10 09:55
    i know how servos work with my r/c background just confused on the PAUSE command and how to use the range's withe the Pause command
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-10 10:05
    In that case, you know that R/C servos require that their control pulse be repeated about 50 times a second. The PULSOUT produces the control pulse with the 2nd parameter providing the pulse width (in 2us units for the BS2). If you use some kind of loop to cause the control pulse to be repeated, you need a roughly 19ms delay between pulses. The PAUSE 20 provides that. Your program only produces a single control pulse in each case. That's enough to cause the servo to twitch, but won't move it significantly. It takes maybe a half second or so for the servo to move through maybe 45 degrees and you'll need to repeat the control pulse about every 20ms for that time. If you want the servo to hold its position under load, you'll need to continue to produce control pulses. The "Robotics with the BoeBot" tutorial has some examples of combining sensing, decision making, and servo control all in one (or several) master loops so the servo(s) are continuously under control.
  • axial28rraxial28rr Posts: 16
    edited 2011-04-10 11:05
    ok this is what i came up with
    ' What's a Microcontroller - ServoCenter.bs2
    ' rotate the servo 45 to 0.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    DEBUG "Program Running!", CR
    DEBUG "press button in3",CR
      
    DO
      PULSOUT 15, 500
      PAUSE 20
      STOP
    LOOP
     
     DO
      LOOP UNTIL IN3 = 1 AND IN4 = 0 'goes to 45 degrees wehen push BUTTON 3 is pressed
      PULSOUT 15, 250
      PAUSE 20
      STOP
     
     DO
      LOOP UNTIL IN3 = 0 AND IN4 = 1 'goes back to zero when push BUTTON 4 is pressed
      PULSOUT 15, 500
      PAUSE 20
     STOP
     
    END
    
  • ercoerco Posts: 20,261
    edited 2011-04-10 12:09
    Hope you don't damage your servo with that Pulsout 250.
  • axial28rraxial28rr Posts: 16
    edited 2011-04-10 12:16
    i have it at pulsout 250 because i need it to start at 0 degrees with no input of pushbuttons and go to 45 degrees when push button 3 is pressed and got back to 0 degress when push button 4 is pressed
    is there another way of doing this ?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-04-10 12:44
    Your questions make it clear that you are refusing to avail
    yourself of the suggested reading list and that's the best
    way to get nowhere fast.

    It's necessary knowledge, your "background in r/c" notwithstanding.
  • axial28rraxial28rr Posts: 16
    edited 2011-04-10 12:52
    ok i am not tying to start anything here i just need some help i looked over the wam files and something is not clicking am learing how to wrtite programs just started this semister
    if you dont belive that i have a background in r/c heres my youtube page http://www.youtube.com/user/wirelesscps?feature=mhum
  • Mike GMike G Posts: 2,702
    edited 2011-04-10 12:55
    It appears to me that the code just waits for the first condition to be true. If IN3 and IN4 are zero then one pulse is sent out pin 15 and execution stops until the STAMP is reset. Who knows... There is an external button circuit too. If either IN3 or IN4 default to 1 on start, then nothing happens...ever. Well, assuming that a button press is a logical 1.

    If I were a gambling man I would bet that axial28rr has not read chapter 4 in "What's a Microcontroller?".
  • axial28rraxial28rr Posts: 16
    edited 2011-04-10 13:11
    i read chapter 4 i edit the frist line it has no buttons to be excuted because there is noting to be pushed its starting point is zero when powerd up
  • Mike GMike G Posts: 2,702
    edited 2011-04-10 13:16
    Pretty much the same thing happens...
    ' What's a Microcontroller - ServoCenter.bs2
    ' rotate the servo 45 to 0.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    DEBUG "Program Running!", CR
    DEBUG "press button in3",CR
      
    DO
      PULSOUT 15, 500
      PAUSE 20
      STOP
    LOOP
     
     DO
      LOOP UNTIL IN3 = 1 AND IN4 = 0 'goes to 45 degrees wehen push BUTTON 3 is pressed
      PULSOUT 15, 250
      PAUSE 20
      STOP
     
     DO
      LOOP UNTIL IN3 = 0 AND IN4 = 1 'goes back to zero when push BUTTON 4 is pressed
      PULSOUT 15, 500
      PAUSE 20
     STOP
     
    END
    

    The first DO...LOOP is entered, a pulse is sent, wait 20ms,and execution stops. The servo might twitch a bit depending on it's current angular position.
  • axial28rraxial28rr Posts: 16
    edited 2011-04-10 13:23
    correct i want the starting point to be low when powered up.

    but i dont want it to power down thats why i used stop instead of end until the last command is excuted

    -in3 is pushed moves to 45
    -in4 is pushed moves back to 0
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-10 13:35
    You obviously don't understand the DO / LOOP statement and I'm not sure you really understand the STOP and END statements. You need to spend some time learning the basics of PBasic and program structure. Work through the servo control examples in "What's a Microcontroller?", then, if you still have questions, come back with them and we'll be pleased to help.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-04-10 13:37
    Isn't the range of servo control pulses 1msec - 2msec?

    PE -- Therefore BS2 PULSOUT range 500-1000?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-10 13:46
    The "official" range of servo control pulses is indeed 1ms to 2ms with center being 1.5ms. Many servos respond to a wider range and you can often find servos that will respond to anything from 0.5ms to 2.5ms before they run into a mechanical stop. You can often find continuous motion servos responding to the wider range and there's no mechanical stop in that case.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-04-10 13:51
    0 - 90 deg, 1-2msec; center = 45deg, 1.5msec?

    PE - Surely one with an extensive background in r/c would have been on top of this?
    I don't have a youtube channel of me playing with r/c cars, but I can make a BS2 position a servo to 0, 45, 90 deg.
  • axial28rraxial28rr Posts: 16
    edited 2011-04-10 14:27
    PJ your great help man thanks am using paralax servos if your wondering if i understand they can be tweaked to go counter clockwise and clock wise like HI-tech serovs if you know how

    i don't fully understand the DO / LOOP because am new to this if i did understand i wouldent be asking?
  • Mike GMike G Posts: 2,702
    edited 2011-04-10 15:19
    STOP prevents the BASIC Stamp from executing any further instructions until it is reset. The following actions will reset the BASIC Stamp:
    1. Pressing and releasing the RESET button on the development board.
    2. Driving the RES pin low then letting it float (high).
    3. Downloading a new program
    4. Disconnecting then reconnecting the power.

    Your code
    ' What's a Microcontroller - ServoCenter.bs2
    ' rotate the servo 45 to 0.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    DEBUG "Program Running!", CR
    DEBUG "press button in3",CR
      
    DO
      PULSOUT 15, 500
      PAUSE 20
      STOP
    LOOP
    

    Your code will execute DEBUG, DEBUG, DO, PULSOUT, PAUSE, then STOP... it will never loop back to the DO. It will not execute any of the following DO...LOOPS... ever.

    Please don't take this the wrong way but it's very hard to believe, in a forum setting, that you have read any of the suggested materials. One, because in chapter 4 of WAM there are detailed explanations with source code that is directly related to what you're trying to accomplish.
    am using paralax servos if your wondering if i understand they can be tweaked to go counter clockwise and clock wise like HI-tech serovs
    Since you brought it up, please regale the community in the tweaking of Hitec vs Parallax (not paralax) servos.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-10 15:28
    Standard servos use the control pulse to specify a position, usually over a 90 degree or 180 degree range. Some specialized servos have a range of several turns. There's something called a continuous motion servo that uses the control pulse to specify the speed and direction of rotation. Parallax sells both. So does Hi-Tech. There's no tweaking involved. That's how they work.

    If you want to learn about servos, read some Wikipedia articles on the subject. If you want to learn about DO / LOOP statements, go through the introductory tutorials like "What's a Microcontroller?". Parallax staff have spent a lot of time putting together these tutorials. They're good at what they do. If, after you've gone through the tutorials and worked through the examples, you still don't understand, ask. Ask if you don't understand specific bits of the tutorials and say what those are.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-04-10 17:03
    I'm not yanking your chain, but here's a youtube --

    http://www.youtube.com/watch?v=E856jJRtF2s
Sign In or Register to comment.