Shop OBEX P1 Docs P2 Docs Learn Events
Wrote my own navigation code, but need help fixing code. — Parallax Forums

Wrote my own navigation code, but need help fixing code.

noobmunchernoobmuncher Posts: 124
edited 2008-08-22 22:02 in Robotics
Hello, since my robot is some what similar in make up to the Boe Bot I am modifying the Roaming with the PING))) - Advanced (.bs2) program to work for it. www.parallax.com/Portals/0/Downloads/src/prod/RoamingWithPING-V1.0.zip

Although i am running into two problems i am seeming to find difficult to fix even though normally it is a minor adjustment. i am trying to have the robot rely purely on the ping measurement and not need the IR signals to operate. Also my servo is facing forward, (i.e the side with the shaft is facing the front of the robot.). I tried using the simplified roaming with ping, but it was... not what i was looking for.

Any help with this code would be greatly appreciated.

Post Edited (noobmuncher) : 8/22/2008 9:06:39 AM GMT

Comments

  • DgswanerDgswaner Posts: 795
    edited 2008-08-21 14:04
    you might be better off starting from scratch. when you break down that code it's actually pretty simple. Id start by getting your bots basic move commands working, left right forward backwards...... then get the servo holding the Ping working, get it to go to the 5 different locations, then make or borrow the code that compares the ranges of each position, and then have it turn accordingly. all of the pieces of the Roaming with ping are hard coaded, there isn't a complex formula to make it work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster

    DGSwaner
  • noobmunchernoobmuncher Posts: 124
    edited 2008-08-22 06:20
    Okay thanks, Yeah i was planning on starting from scratch (and am actually doing a brand new program as i type this). Thanks for confirming that it was pretty hard coded though, I thought i might have just been really tired and not thinking straight. freaked.gif
  • noobmunchernoobmuncher Posts: 124
    edited 2008-08-22 08:45
    Okay so i wrote my own code, seems to look good to me, but a few things are preventsing me from running the program, please take a look and let me know where i went wrong. I assume by the " Expected "=" " while highlighting my greater than symbol something is wrong with that, but i don't have any idea what.

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

    '
    ' Basic Navigation program for my yet to be named cylindrical robot. The robot will
    ' initially use just the ((PING senor for it's object detection.
    '

    '
    ' Variables
    '
    counter VAR Word
    CmConstant CON 2260
    cmDistance_left VAR Word
    cmDistance_center VAR Word
    cmDistance_right VAR Word
    time_left VAR Word
    time_center VAR Word
    time_right VAR Word
    direction VAR Word
    movement VAR Word


    ' Initialization
    FOR counter = 1 TO 150
    PULSOUT 14, 750
    PAUSE 20
    NEXT

    '
    ' Main procedure
    '
    DO

    GOSUB Scan

    IF cmDistance_left > cmDistance_center OR cmDistance_right THEN
    GOSUB go_left
    ELSE
    cmDistance_center > cmDistance_left OR cmDistance_right THEN
    GOSUB go_forward
    ELSE
    cmDistance_right > cmDistance_left OR cmDistance_center THEN
    GOSUB go_right
    ENDIF
    LOOP

    '
    'Subroutines
    '

    Scan: 'Scan sub-route

    FOR counter = 1 TO 150 'Turn Left 90 Degrees
    PULSOUT 14, 350
    PAUSE 20
    NEXT
    PULSOUT 15, 5 'Take measurement
    PULSIN 15, 1, time_left
    cmDistance_left = cmConstant ** time_left
    DEBUG HOME, DEC3 cmDistance_left, " cm"
    PAUSE 100
    FOR counter = 1 TO 150 'Center Head
    PULSOUT 14, 750
    PAUSE 20
    NEXT
    PULSOUT 15, 5 'Take measurement
    PULSIN 15, 1, time_center
    cmDistance_center = cmConstant ** time_center
    DEBUG HOME, DEC3 cmDistance_center, " cm"
    PAUSE 100
    FOR counter = 1 TO 150 'Turn Right 90 Degrees
    PULSOUT 14, 1150
    PAUSE 20
    NEXT
    PULSOUT 15, 5 'Take measurement
    PULSIN 15, 1, time_right
    cmDistance_right = cmConstant ** time_right
    DEBUG HOME, DEC3 cmDistance_right, " cm"
    PAUSE 100
    RETURN

    go_left:
    FOR counter = 1 TO 150
    PULSOUT 14, 750
    PAUSE 20
    NEXT

    FOR counter = 1 TO 20
    PULSOUT 13, 650
    PULSOUT 12, 650
    PAUSE 20
    NEXT
    cmDistance_left - 20 / 15.24 * 40.65 = movement
    FOR counter = 1 TO movement
    PULSOUT 13, 850
    PULSOUT 12, 650
    PAUSE 20
    NEXT
    RETURN

    go_forward:
    FOR counter = 1 TO 150
    PULSOUT 14, 750
    PAUSE 20
    NEXT
    cmDistance_center - 20 / 15.24 * 40.65 = movement
    FOR counter = 1 TO movement
    PULSOUT 13, 850
    PULSOUT 12, 650
    PAUSE 20
    NEXT
    RETURN

    go_right:
    FOR counter = 1 TO 150
    PULSOUT 14, 750
    PAUSE 20
    NEXT
    cmDistance_right - 20 / 15.24 * 40.65 = movement
    FOR counter = 1 TO movement
    PULSOUT 13, 850
    PULSOUT 12, 650
    PAUSE 20
    NEXT
    RETURN
  • noobmunchernoobmuncher Posts: 124
    edited 2008-08-22 08:59
    No never mind it has to do with the
    (cmDistance_left - 20) / (15.24 * 40.65) = movement
    and also the "Loop" command at the end of the main section. Please help, i don't know how to fix this. freaked.gif
  • ScurgeScurge Posts: 7
    edited 2008-08-22 16:22
    you got it backwards. in an assignment statement, the varible that is getting assigned must be 1st, then the "=" then the argument. eg. :

    variable = (figure_1 * figure_2) / figure_3

    EDIT # 2

    ALSO, you cannot use decimal like that, you have to use whole numbers to represent decimals.
    There is a way to do it using the */ command, i think, but i dont fully understand it yet. (if anyone can clearify the */ concept for me that would be awesome!)
    the way i do it is pull out my trusty TI-83 and use the FRACT command to convert a decimal to a fraction and add it to the whole number.
    ex:
    3.5 would be (3 + (1/2))

    so, in your case, it would be:

    movement = (cmDistance_left - 20) / ((15 + (6/25)) * (40 + (13/20)))

    to make this concept a little more clear,

    in the case of:
    variable_1 = variable_2
    the value held in variable_2 will always be assigned to variable_1 (this is true in all programing languages)

    just remember the variable you are assigning will always be on the left of the "=" and the value or string your are assigning to it will be on the right.

    and a TIP:

    I personally like to enclose all my arguements for an IF statement in parenthesis. Although this is not necessary in PBASIC, (i got in the habit with C++) it also makes the code easier to read, and helps eliminate errors in the code.
    Example:

    IF (argument_1 < argument_2) THEN
    PULSOUT 13, 850
    PULSOUT 12, 650
    PAUSE 20
    ELSE
    GOSUB my_sub
    ENDIF

    * this may seem frivilous, but as your IF's get more complex, using AND OR operators, this can make a big difference in your debugging.

    EDIT

    i just noticed,

    IF cmDistance_left > cmDistance_center OR cmDistance_right THEN
    GOSUB go_left
    ELSE
    cmDistance_center > cmDistance_left OR cmDistance_right THEN
    GOSUB go_forward
    ELSE
    cmDistance_right > cmDistance_left OR cmDistance_center THEN
    GOSUB go_right
    ENDIF
    LOOP

    i believe your 1st ELSE should be an ELSEIF and your last ELSE doesn't need a statement.

    example:

    IF (cmDistance_left > cmDistance_center OR cmDistance_right) THEN
    GOSUB go_left
    ELSEIF (cmDistance_right > cmDistance_left OR cmDistance_center) THEN 'The argument has to be on the same line as the ELSEIF as if it were just an IF
    GOSUB go_right
    ELSE
    GOSUB go_forward
    ENDIF


    that should work, either that or im making a fool of myself.. lol

    Post Edited (Scurge) : 8/22/2008 5:11:51 PM GMT
  • noobmunchernoobmuncher Posts: 124
    edited 2008-08-22 22:02
    Thanks alot, it works now, but it seems to only go one distance each time and always resorts to go left.
Sign In or Register to comment.