Shop OBEX P1 Docs P2 Docs Learn Events
code help — Parallax Forums

code help

totcohandtotcohand Posts: 28
edited 2009-03-07 15:50 in Robotics
I am trying to get my bot to take left and right measurements when an object is detected and choose if the left (time1) or the right (time2)
is the way to go. I dont know if I am writing this correctly or not.· I made 2 subs. Time_1 and Time_2, but the bot does not seem to take these measurements into account.

'{$stamp BS2}
'{$Pbasic 2.5}
'Lynxmotion Bot
'Written by Jason Smith 25Feb09
'
'
'
Variables
Right·· PIN 14········································ 'Variable for right motor controller
Left··· PIN 15········································ 'Variable for left motor controller
Hturret PIN 01········································ 'Variable for Horizontle Turret movement
Vturret PIN 12········································ 'Variable for verticle turret movement
Ping··· PIN 00········································ 'Variable for Ping sensor
counter VAR Byte······································ 'Variable for counter
Time··· VAR Word······································ 'Variable for time
Time1·· VAR Byte······································ 'Variable for time1
Time2·· VAR Byte······································ 'Variable for time2
'
Initialize
PAUSE 5000············································ 'Wait 5 sec before starting
'
Main Program
Main:
GOSUB radar··········································· 'check distance
IF time > 1000 THEN GOSUB Forward····················· 'if distance is clear go Foward
IF time <= 1000 THEN GOSUB turret_scan················ 'if object is detected then scan for clear path
IF time1 > time2 THEN GOSUB turn_left················· 'if time1 is greater than time2 then turn left
IF time2 > time1 THEN GOSUB turn_right················ 'if time2 is greater than time1 then turn right
GOTO Main

'
Sub-Routines
Forward:
DO WHILE Time > 1000··································· 'keep moving until Ping senses something in the way
GOSUB radar············································ 'run radar subroutine
IF Time <= 1000 THEN GOTO Main························· 'keep checking distance while moving foward
PULSOUT right, 400
PULSOUT left, 400······································ 'Go forward
LOOP
Back_Up:
FOR counter = 1 TO 20
GOSUB radar
IF time > 700 THEN GOTO Main·························· 'return to main if time is right
PULSOUT right, 800
PULSOUT left, 500····································· 'Back up
PAUSE 18
NEXT
RETURN
Turn_Right:
DO UNTIL time > 1000································· 'keep moving for 130 pulses
GOSUB radar
IF Time > 1000 THEN GOTO Main·························· 'keep checking distance while turning
PULSOUT right, 740
PULSOUT left, 800······································ 'Turn right
PAUSE 18
LOOP
RETURN
Turn_Left:
FOR counter = 1 TO 130································· 'keep moving for 130 pulses
GOSUB radar
IF Time > 1000 THEN GOTO Main·························· 'keep checking distance while turning
PULSOUT right, 800
PULSOUT left, 740······································ 'Turn left
PAUSE 18
NEXT
RETURN
Turret_Scan:
PULSOUT right, 750
PULSOUT left, 750
FOR counter = 1 TO 120
PULSOUT hturret, 800
NEXT
GOSUB time_1
PAUSE 1500
FOR counter = 1 TO 230
PULSOUT hturret, 450
NEXT
GOSUB time_2
PAUSE 1500
FOR counter = 1 TO 51
PULSOUT hturret, 825
NEXT
RETURN
Radar:
PULSOUT ping, 5
PULSIN ping, 1, time
RETURN
Time_1:
PULSOUT ping, 5
PULSIN ping, 1, time1
RETURN
Time_2:
PULSOUT ping, 5
PULSIN ping, 1, time2
RETURN

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-03-04 17:16
    IF time <= 1000 THEN GOSUB turret_scan

    would better be served by using an else statement since you could modify the time in the previous if statement, soon validating this.

    Next, you should nest the last two if statements in your main under the GOSUB turret_scan. You can also make the last an else statement.

    You can use debug statements to figure out where you program flow is going to, and to see values in your program.

    Finally, when posting lots of code attach it, or at least use the CODE formatters.
  • totcohandtotcohand Posts: 28
    edited 2009-03-07 15:50
    Thanks for the help SRLM, and ill do that next time.
Sign In or Register to comment.