Shop OBEX P1 Docs P2 Docs Learn Events
Ping and servo question — Parallax Forums

Ping and servo question

totcohandtotcohand Posts: 28
edited 2008-12-20 18:54 in Robotics
I just picked up a BS2 homework board kit·about a month ago, went through the book and then decided to just try and·"wing" it and·throw together a robot so to speak. I think that I have done pretty well so far, I bought the boe-bot chassis,·tank tread kit, ping sensor, and fabricated my own stationary bracket for the ping.·Right now I have the robot travelling forward, when the time from ping is less than 1100 it will turn right and continue going straight. I have some other sub·routines written in my program, but are not using them yet. My problem is that my servos seem to pulse with the "radar" portion of my program. When the light on the ping sensor comes on, my servos seem to lose some power. It is not too noticeable, but you can hear it pulse and it adds a slight jerking movement. Is it because you can only run one command at a·time and thats the slight pause between running "radar" and·moving?·Sorry for·such the long post, but I appreciate the help. Also if anybody has any suggestions for how I am writing my program that would be great as this is the first one I have done.
Thanks!·

Comments

  • totcohandtotcohand Posts: 28
    edited 2008-12-20 05:36
    'What's a microcontroller .bs2
    '{$stamp BS2}
    '{$Pbasic 2.5}
    '
    Variables
    right PIN 1
    left PIN 0
    ping PIN 14
    counter VAR Word
    Time VAR Word

    '
    Initialize
    PAUSE 3500


    '
    Main Routine

    DO
    GOSUB radar
    IF (time > 1700) THEN
    GOSUB forward 'Go Forward
    GOSUB radar
    ELSEIF (time < 1700) THEN
    GOSUB turn_right
    GOSUB radar
    ENDIF ' and check again
    GOSUB radar
    LOOP

    '
    Subroutines

    Back_Up:
    FOR counter = 1 TO 20
    PULSOUT right, 765
    PULSOUT left, 500
    PAUSE 20
    NEXT
    RETURN

    Forward:
    GOSUB radar
    FOR counter = 1 TO 50
    PULSOUT right, 500
    PULSOUT left, 800
    PAUSE 20
    NEXT
    RETURN

    Radar:
    PULSOUT ping, 5
    PULSIN ping, 1, time
    RETURN

    Turn_right:
    FOR counter = 1 TO 130
    PULSOUT right, 740
    PULSOUT left, 800
    PAUSE 20
    NEXT
    RETURN
  • MikerocontrollerMikerocontroller Posts: 310
    edited 2008-12-20 06:08
    · You can eliminate all the extra calls to the Radar subroutine.· You really only need to call it once in the main program and you can eliminate the GOSUB Radar call in the Forward subroutine and the last call to it in your main program.· You can see that the Radar subroutine gets called twice if the time variable value is >1700.· In some instances it can be called as many as three times consecutively.

    DO
    · GOSUB Radar
    · IF (time <????) THEN
    ··· GOSUB Back-Up
    · ELSEIF (time< 1700) THEN
    ··· GOSUB Turn_right
    · ELSE
    ··· GOSUB Forward
    · ENDIF
    LOOP·

    · P.S.· I think that your code formatting is just fine and is easy to follow.· Good work so far.

    Post Edited (Mikerocontroller) : 12/20/2008 6:29:26 AM GMT
  • GWJaxGWJax Posts: 267
    edited 2008-12-20 15:00
    Nice code totco but mike is correct about the return commands and had done a good job in doing your main code subroutine. I have another option for you if you would like to try it as well. I have made your code a bit different and also added the backup routine as well. The forward routine will keep moving until it detects an object and after that if will move right, also while moving right if it detects an object it will the backup. Below is the code I changed so you can just copy and past it into the stamp. Keep up the great coding!!!

    Jax


    'What's a microcontroller .bs2
    '{$stamp BS2}
    '{$Pbasic 2.5}
    '
    Variables
    right PIN 1
    left PIN 0
    ping PIN 14
    counter VAR Byte
    Time VAR Word

    '
    Initialize
    PAUSE 3500


    '
    Main Routine
    Main:
    GOSUB radar ' check distance
    IF time > 1700 THEN GOSUB Forward ' if distance is clear go Foward
    IF time <= 1700 AND time > 700 THEN GOSUB Turn_Right ' if object is detected then turn right
    IF time <= 700 THEN GOSUB Back_UP ' backup if object is in front of Bot
    GOTO Main


    '
    Subroutines

    Back_Up:
    FOR counter = 1 TO 20
    GOSUB radar
    IF time > 900 THEN GOTO Main ' return to main and make a right turn if conditions are right
    PULSOUT right, 765
    PULSOUT left, 500
    PAUSE 18
    NEXT
    RETURN

    Forward:

    DO WHILE Time > 1700 ' keep moving until Ping senses something in the way
    GOSUB radar
    IF Time <= 1700 THEN GOTO Main ' keep checking distance while moving foward
    PULSOUT right, 500
    PULSOUT left, 800
    PAUSE 18
    LOOP


    Radar:
    PULSOUT ping, 5
    PULSIN ping, 1, time
    RETURN

    Turn_right:
    FOR counter = 1 TO 130 ' keep moving for 130 pulses
    GOSUB radar
    IF Time > 1700 THEN GOTO Main ' keep checking distance while turning
    PULSOUT right, 740
    PULSOUT left, 800
    PAUSE 18
    NEXT
    RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • totcohandtotcohand Posts: 28
    edited 2008-12-20 17:59
    Thanks alot Mikerocontroller and GWJax! I have found this forum very valuable in the robotics learning process! I am planning on using this robot as a smaller model and scale it up quite a bit in the future. I will post pics when I am completely finished with this one. Thanks again this is a big help.
  • GWJaxGWJax Posts: 267
    edited 2008-12-20 18:54
    No problem Totcohand, If you have any more question just ask. Hope to see your pictures of your bot soon.

    Jax

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
Sign In or Register to comment.