BOEBOT W/PING

Trying to make boebot drive forward until ping gets within 10 inches of an object. Not sure how to get both running together. They work fine when separated.




' {$STAMP BS2}
' {$PBASIC 2.5}


InConstant CON 890 'sets InConstant as a constant (890)

inDistance VAR Word 'sets InDistance as a variable (word)
time VAR Word 'sets label_time as a variable (word)
'counter VAR word
'FOR counter = 1 TO 122

DO

HIGH 0 'sets pin 3 high
PULSOUT 0, 500 'output pulse pin 3
LOW 1 'sets pin 4 low
PULSOUT 1, 500 'output pulse pin 4
GOSUB My_Subroutine
PAUSE 100

LOOP

My_Subroutine:


DO
LOW 15 'sets pin 15 low
PULSOUT 15, 5 'outputs a pulse on pin 15
PULSIN 15, 1, time 'recieves a pulse on pin 15

inDistance = inConstant ** time

DEBUG CR, DEC3 inDistance, " inch"

PAUSE 100

LOOP UNTIL (inDistance <= 10)
«1

Comments

  • 37 Comments sorted by Date Added Votes
  • First time coding in Pbasic...
  • tomcrawfordtomcrawford Posts: 853
    edited December 2017 Vote Up0Vote Down
    IN MySubroutine, you loop until the distance is less than 10. Clearly, you are not issuing pulses during that time. I would make MySubroutine simply measure one time and return the distance. Then the main program can decide based on that datum.

    If you put the code between code flags,
    it.  Preserves.    The.   Formatting
    
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
    Cool, CA, USA 95614
  • EGGHEAD wrote: »
    First time coding in Pbasic...

    Thank you for posting.

    I think this could be accomplished a couple of ways, but I favor a sub routine approach. Break up each operation into subroutines; first a routine to check the Ping distance against the constant 10, then a routine for the servo pulse if distance is < 10. You can use another routine to stop if you want, or just use the END command.
    Joshua Donelson
  • To keep the servo's rotating forward I had to use a loop, is that not correct
    ?
  • I said first time coding in pbasic but what I should have said was my first time coding.
  • EGGHEAD wrote: »
    If not for Failure Success would have NEVER been possible..

  • EGGHEAD wrote: »
    To keep the servo's rotating forward I had to use a loop, is that not correct
    ?

    Totally. You have to send a continuous string of pulses to the servo(s) to keep them turning. And when your BS-2 enters My_Subroutine, the servo pulses stop because it never exits the subroutine. Here is a pointer to an example that combines a Ping with a BoeBot.
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
    Cool, CA, USA 95614
  • Thanks for the example code that's exactly what I'm looking for Tom. I noticed they have OBEX four propeller in spin code but do they have such a sight for bs2 or basic stamp code
  • EGGHEADEGGHEAD Posts: 26
    edited December 2017 Vote Up0Vote Down
    Tom Crawford just for s**** and giggles I decided to run that program that you put online for me and I'm still having the same issues with my boebot where it's very jerky in its movement's and the server's don't seem to be running correctly, what else could be causing this?
  • tomcrawfordtomcrawford Posts: 853
    edited December 2017 Vote Up0Vote Down
    Egghead, I can't take credit for the program. I just posted the pointer. The program is by Parallax and is part of the BoeBot documentation. Go to the STORE and look under the BoeBot USB or Serial, whichever you have. Look under Downloads and Documentation as well as Additional Resourses. As to why it is jerky, don't know. I never actually did much servo stuff with BS-2, but everybody says "check your batteries first".
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
    Cool, CA, USA 95614
  • I've been playing with this thing all day today and I'm about ready to pull my hair out and then I remember I don't have much hair left so I should be careful..
  • In Pbasic can you have Objects like Spin?
  • EGGHEAD wrote: »
    In Pbasic can you have Objects like Spin?

    Not really. And the BS-2 can only do one thing at a time. No parallel processing like the Propellor.

    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
    Cool, CA, USA 95614
  • Tom Crawford I'm running slow for some reason, I (just) got what you were talking about in your first post. In my subroutine I need to write code to measure at least once and return the distance. I'm still having issues with the jerky motion with the servos though.
  • EGGHEAD wrote: »
    Tom Crawford I'm running slow for some reason, I (just) got what you were talking about in your first post. In my subroutine I need to write code to measure at least once and return the distance. I'm still having issues with the jerky motion with the servos though.

    The battery voltage could be a potential issue; do you have a multi-meter to check the battery pack? Looking at the code above, I didn't see PAUSE 20(ms), which is customary for smooth servo control. This might be a dumb question too, but have you centered the servos? Sometimes they will move around instead of stopping if they have not been adjusted.
    Joshua Donelson
  • JD, currently I'm using the code that Tom posted so that wouldn't be the problem. And to be honest any question you posed at me would not be dumb by any stretch of the imagination because I did not center the servos, I have no idea had to do that. I also have no encoders is that part of the problem possibly?

  • PublisonPublison Posts: 9,942
    edited December 2017 Vote Up0Vote Down
    You should run through the BoeBot setup book. It shows how to center the servos and test the servos.

    The BoeBot does not have encoders.

    https://www.parallax.com/sites/default/files/downloads/28125-Robotics-With-The-Boe-Bot-v3.0.pdf
    Infernal Machine
  • Thanks Publison.
    I will do that.
  • Publison wrote: »
    You should run through the BoeBot setup book.

    +1E6

    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
    Cool, CA, USA 95614
  • JDJD Posts: 503
    edited December 2017 Vote Up0Vote Down
    EGGHEAD,

    I Agree, Robotics WithThe Boe-Bot is a great introduction to using BASICS language and robotics in general. A+ material :)

    Activity #4 : Centering the Servos starting on page 49 will help make sure that the servo that your using are centered. There is code on Page 51 that is nice to save; if you have any continuous servos in a project, you can center them before getting started. I have had a robot slowly roll away because of incorrectly centered servos. haha
    ' Robotics with the Boe-Bot - CenterServoP12.bs2
    ' This program sends 1.5 ms pulses to the servo connected to
    ' P12 for manual centering.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    DEBUG "Program Running!"
    DO
     PULSOUT 12, 750
     PAUSE 20
    LOOP
    
    Joshua Donelson

  • I will center the servo's and try again JD. By the way my program has now completely changed from what I posted originally, so don't worry about that being my issue now. What I do not understand is if I write the code:

    DO

    LOW 5
    PULSOUT 5, 500
    HIGH 6
    PULSOUT 6, 500

    LOOP

    compile and run this code the servo's work great, so what eludes me is why when I add the PING code the servo's behave completely different.

    I just wanted to give special thanks to JD, tomcrawford , and Publison. I'm new at this and frankly started a bit late in life but I'm having a lot of fun.
    Everyone on this forum has went above and beyond what they need to and it is much appreciated, your contributions are definitely noticed.


  • Here is a pointer to continuous rotation servo doumentation. The section on communication protocol talks about the pulse stream you have to send the servo.

    FWIW, it is never too late to start. Welcome to the forum. As long as you keep trying, we'll keep helping.
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
    Cool, CA, USA 95614
  • EGGHEAD wrote: »
    I will center the servo's and try again JD. By the way my program has now completely changed from what I posted originally, so don't worry about that being my issue now. What I do not understand is if I write the code:

    DO

    LOW 5
    PULSOUT 5, 500
    HIGH 6
    PULSOUT 6, 500

    LOOP

    compile and run this code the servo's work great, so what eludes me is why when I add the PING code the servo's behave completely different.

    I just wanted to give special thanks to JD, tomcrawford , and Publison. I'm new at this and frankly started a bit late in life but I'm having a lot of fun.
    Everyone on this forum has went above and beyond what they need to and it is much appreciated, your contributions are definitely noticed.


    You're welcome we are all happy to help. Tom said it perfectly, it's never to late and always feel free to ask.

    Controlling a servo smoothly is all about timing, a 1-2ms pulse depending on direction and speed, with a 20ms pause between each pulse. The 20ms is needed so a microcontroller doesn't flood the servo with pulses create a jerky motion. I have added where you would want to add the 20ms pulses in the code below. A good method is anytime a servo is sent a pulse, wait 20ms before sending it another pulse.
    DO
      LOW 5
      PULSOUT 5, 500
      PAUSE 20               '<- be sure to follow each pulse with a 20ms pause
      HIGH 6
      PULSOUT 5, 500
      PAUSE 20               '<- be sure to follow each pulse with a 20ms pause
    LOOP
    
    Joshua Donelson
  • tomcrawfordtomcrawford Posts: 853
    edited December 2017 Vote Up0Vote Down
    EGGHEAD, There are a couple of problems with your program:
    DO
    LOW 5
    PULSOUT 5, 500
    HIGH 6
    PULSOUT 6, 500
    LOOP
    

    1. Since you set I/O6 HIGH, PULSOUT will generate a negative-going pulse on a normally high signal. Parallax servos all like normally low signals with active high pulses.
    2. You aren't leaving enough time between pulses. Each pulse should happen every 20 msec (that is, 50 Hz). Your program runs at about 300 Hz as written.
    3. A pulse width value of 500 will produce a pulse of 1000 usec (that is, 1 msec.). The servos like 1300 to 1700 usec.

    All in all, I am surprised your servos are doing anything.

    Here is something I tried. I moved the servos to 14 and 15 since I have a Board of Education. Also, since I am using high speed servos (#900-00025), I made sure the board was jumped for Vin at the servo pins.
    width  var   word
    timer  var   byte
    
    FOR width = 1400 TO 1600 STEP 5     '1.4 to 1.6 millisec
      DEBUG CR, "PulseWidth= ", DEC width
       FOR Timer = 1 to 50             'keep same pulse width for 1 second
          PULSOUT 15, width/2        'pulsout for BS-2 is steps of 2 usec/code]
          PULSOUT 14, 750              'place keeper uses up 1.5
          PAUSE 17                          'total of 20,000 microsec
          NEXT
       NEXT
    

    This keeps the pulses at just about 50 Hz, which is ideal for these servos. You don't have to set the pins high or low; they will come up LOW which is what you want. The program will ramp from medium fast one way through zero through medium fast the other way.

    Edit: JD, I didn't see your comment before I wrote mine. Sorry, didn't mean to step on you.
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
    Cool, CA, USA 95614

  • Edit: JD, I didn't see your comment before I wrote mine. Sorry, didn't mean to step on you.

    No worries, we both are helping. There is more than one way to get a program to run, but I think pause between servo pulses will help.
    Joshua Donelson
  • tomcrawford :

    No worry's. For now I have been running the program https://www.parallax.com/sites/default/files/downloads/28015-Roaming-With-PING-Simple-BASIC-Stamp-Code-v1.0.zip
    I'm going to rewrite my code completely because of yours, JD's,and Publison's help I have a much better foundation to start from, but I digress. While running the above code I was still seeing that jerky motion out of the servos and when I went through the process to center the servos after connecting everything the boebot was just turning in circles. I used a small screwdriver while the program was running and turned the Pot's on the servos and it started going in the right direction. This entire time the servos were still jerky, But
    If I run
    DO
    LOW 5
    PULSOUT 5, 500
    HIGH 6
    PULSOUT 6, 500
    LOOP
    then the servos run as smooth as rain. Go figure.

  • Go figure. Well, I'd continue to work through Parallax exercises. Let us know if you get stuck.
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
    Cool, CA, USA 95614
  • EGGHEAD,

    Normally the BOE-Bot uses P12 and P13 for the wheel servos but in your first program you are using P0 and P1 and then you use P5 and P6 in a later program.

    Is your BOE-Bot wired like on page 81 of Robotics with the BOE-Bot?
    Also did you add the Piezospeaker on page 89?


    I assume that you have your Ping mounted on a Ping Bracket so what pin is that servo using P15?

    You might also want to check the voltage of your batteries because servos can suck them dry faster than you might think.
  • Genetix,

    Robotics with the BOE-Bot, never read it. Not sure how the BOE-Bot is wired on page 81.

    I looked up data sheets on the components and board and proceeded from there. My BOE-Bot has three servos, 2 continuous and one standard. My Ping which is in a Ping bracket is mounted to the standard servo which is placed in the front of the BOE-Bot.

    I'm not using the code that I posted originally, I'm using code that Tomcrawford turned me onto and I'm re-writing my code, but I still am having issues with both the continuous servos and my Ping sensor not working even with correct code. I've tested both the servos and the Ping independently and they work fine, but not together.
  • ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    Ping PIN 15
    PingServo PIN 14
    LeftServo PIN 13
    RightServo PIN 12

    rawDist VAR Word
    sweepcount VAR Word
    x VAR Byte
    pulseCount VAR Byte


    Main:
    FOR sweepcount = 350 TO 1150 STEP 200
    FOR x = 0 TO 5
    PULSOUT 14, sweepcount
    PAUSE 20
    NEXT
    GOSUB GetSonar
    NEXT

    FOR sweepcount = 950 TO 550 STEP 200
    FOR x = 0 TO 5
    PULSOUT 14, sweepcount
    PAUSE 20
    NEXT
    GOSUB GetSonar
    NEXT
    GOTO Main

    GetSonar:
    LOW Ping
    PULSOUT Ping, 5
    PULSIN Ping, 1, rawDist

    IF (rawDist < 600) THEN
    IF (sweepcount < 750) THEN
    GOSUB Turn_Left
    ELSEIF (sweepcount > 750) THEN
    GOSUB Turn_Right
    ELSE
    GOSUB Back_Up
    ENDIF
    ELSE
    GOSUB Forward_Pulse
    ENDIF

    RETURN

    Forward_Pulse:
    FOR pulsecount = 0 TO 5
    PULSOUT LeftServo, 850
    PULSOUT RightServo, 650
    PAUSE 20
    NEXT
    RETURN

    Turn_Left:
    FOR pulseCount = 0 TO 10
    PULSOUT LeftServo, 650
    PULSOUT RightServo, 650
    PAUSE 20
    NEXT
    RETURN

    Turn_Right:
    FOR pulseCount = 0 TO 10
    PULSOUT LeftServo, 850
    PULSOUT RightServo, 850
    PAUSE 20
    NEXT
    RETURN

    Back_Up:
    FOR pulseCount = 0 TO 3
    PULSOUT LeftServo, 750
    PULSOUT RightServo, 750
    PAUSE 20
    NEXT

    FOR pulseCount = 0 TO 7
    PULSOUT LeftServo, 650
    PULSOUT RightServo, 850
    PAUSE 20
    NEXT
    RETURN
Sign In or Register to comment.