Shop OBEX P1 Docs P2 Docs Learn Events
How do I combine two codes together? — Parallax Forums

How do I combine two codes together?

bisctboybisctboy Posts: 6
edited 2008-08-24 15:23 in Robotics
I want to combine "RoamingWithWhiskers.bs2" and "FastIrRoaming.bs2". I am completely new to this and when I try to copy and paste what I think needs to be combined, it gives me a label is missing ':' error.

The code for "RoamingWithWhiskers.bs2" is:

'
[noparse][[/noparse] Title ]
' Robotics with the Boe-Bot - RoamingWithWhiskers.bs2
' Boe-Bot uses whiskers to detect objects, and navigates around them.
' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.
DEBUG "Program Running!"
'
[noparse][[/noparse] Variables ]
pulseCount VAR Byte ' FOR...NEXT loop counter.
'
[noparse][[/noparse] Initialization ]
FREQOUT 4, 2000, 3000 ' Signal program start/reset.
'
[noparse][[/noparse] Main Routine ]
DO
IF (IN5 = 0) AND (IN7 = 0) THEN ' Both whiskers detect obstacle
GOSUB Back_Up ' Back up & U-turn (left twice)
GOSUB Turn_Left
GOSUB Turn_Left
ELSEIF (IN5 = 0) THEN ' Left whisker contacts
GOSUB Back_Up ' Back up & turn right
GOSUB Turn_Right
ELSEIF (IN7 = 0) THEN ' Right whisker contacts
GOSUB Back_Up ' Back up & turn left
GOSUB Turn_Left
ELSE ' Both whiskers 1, no contacts
GOSUB Forward_Pulse ' Apply a forward pulse
ENDIF ' and check again
LOOP
'
[noparse][[/noparse] Subroutines ]
Forward_Pulse: ' Send a single forward pulse.
PULSOUT 13,850
PULSOUT 12,688
PAUSE 20
RETURN
Turn_Left: ' Left turn, about 90-degrees.
FOR pulseCount = 0 TO 20
PULSOUT 13, 650
PULSOUT 12, 688
PAUSE 20
NEXT
RETURN
Turn_Right:
FOR pulseCount = 0 TO 20 ' Right turn, about 90-degrees.
PULSOUT 13, 850
PULSOUT 12, 850
PAUSE 20
NEXT
RETURN
Back_Up: ' Back up.
FOR pulseCount = 0 TO 40
PULSOUT 13, 650
PULSOUT 12, 817
PAUSE 20
NEXT
RETURN


And this code for "FastIrRoaming.bs2" is:

' Robotics with the Boe-Bot - FastIrRoaming.bs2
' Higher performance IR object detection assisted navigation
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
irDetectLeft VAR Bit ' Variable Declarations
irDetectRight VAR Bit
pulseLeft VAR Word
pulseRight VAR Word
FREQOUT 4, 2000, 3000 ' Signal program start/reset.
DO ' Main Routine
FREQOUT 8, 1, 38500 ' Check IR Detectors
irDetectLeft = IN9
FREQOUT 2, 1, 38500
irDetectRight = IN0
' Decide how to navigate.
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
pulseLeft = 650
pulseRight = 812
ELSEIF (irDetectLeft = 0) THEN
pulseLeft = 850
pulseRight = 812
ELSEIF (irDetectRight = 0) THEN
pulseLeft = 650
pulseRight = 688
ELSE
pulseLeft = 850
pulseRight = 688
ENDIF
PULSOUT 13,pulseLeft ' Apply the pulse.
PULSOUT 12,pulseRight
PAUSE 15
LOOP ' Repeat main routine


I have tested both of these codes seperately to make sure my Boe-Bot is responding correctly to the IR LED's and the Whiskers. Any help would be greatly appreciated!

Comments

  • FranklinFranklin Posts: 4,747
    edited 2008-08-23 18:40
    Your best bet is to read the code and try to under stand what is happening. break the code down into smaller groups then, when you understand it start writing your own program incorporating the ideas you learned from the other code. The code you are trying to combine do the same thing in two different ways and would be hard to 'cut and paste' into one program.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • bisctboybisctboy Posts: 6
    edited 2008-08-23 21:30
    Got it....You're right. I just sat down and read what each line says and then does. Here is the code that I combined. It does exactly what I wanted it to do. Thanks!

    '
    [noparse][[/noparse] Title ]

    ' Robotics with the Boe-Bot - Roaming With Whiskers and Fast IR.bs2
    ' Boe-Bot uses whiskers to detect objects, and navigates around them.

    ' {$STAMP BS2} ' Stamp directive.
    ' {$PBASIC 2.5} ' PBASIC directive.

    DEBUG "Roaming With Whiskers and Fast IR"

    '
    [noparse][[/noparse] Variables ]

    pulseCount VAR Byte ' FOR...NEXT loop counter.
    irDetectLeft VAR Bit
    irDetectRight VAR Bit
    pulseLeft VAR Word
    pulseRight VAR Word

    '
    [noparse][[/noparse] Initialization ]

    FREQOUT 4, 2000, 3000 ' Signal program start/reset.

    '
    [noparse][[/noparse] Main Routine ]

    '
    IR Routine

    DO
    · FREQOUT 8, 1, 38500 ' Check IR Detectors
    · irDetectLeft = IN9

    · FREQOUT 2, 1, 38500
    · irDetectRight = IN0

    · IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN ' Decide how to navigate.
    ··· pulseLeft = 650
    ··· pulseRight = 812
    · ELSEIF (irDetectLeft = 0) THEN
    ··· pulseLeft = 850
    ··· pulseRight = 812
    · ELSEIF (irDetectRight = 0) THEN
    ··· pulseLeft = 650
    ··· pulseRight = 688
    · ELSE
    ··· pulseLeft = 850
    ··· pulseRight = 688
    · ENDIF

    '
    Whisker Routine

    · PULSOUT 13,pulseLeft ' Apply the pulse.
    · PULSOUT 12,pulseRight
    · PAUSE 15

    · IF (IN5 = 0) AND (IN7 = 0) THEN ' Both whiskers detect obstacle
    ··· GOSUB Back_Up ' Back up & U-turn (left twice)
    ··· GOSUB Turn_Left
    ··· GOSUB Turn_Left
    · ELSEIF (IN5 = 0) THEN ' Left whisker contacts
    ··· GOSUB Back_Up ' Back up & turn right
    ··· GOSUB Turn_Right
    · ELSEIF (IN7 = 0) THEN ' Right whisker contacts
    ··· GOSUB Back_Up ' Back up & turn left
    ··· GOSUB Turn_Left
    · ELSE ' Both whiskers 1, no contacts
    ··· GOSUB Forward_Pulse ' Apply a forward pulse
    · ENDIF ' and check again
    LOOP

    '
    [noparse][[/noparse] Subroutines ]

    Forward_Pulse: ' Send a single forward pulse.
    · PULSOUT 13,850
    · PULSOUT 12,688
    · PAUSE 20
    · RETURN

    Turn_Left: ' Left turn, about 90-degrees.
    · FOR pulseCount = 0 TO 20
    · PULSOUT 13, 650
    · PULSOUT 12, 688
    ··· PAUSE 20
    · NEXT
    · RETURN

    Turn_Right:
    · FOR pulseCount = 0 TO 20 ' Right turn, about 90-degrees.
    ··· PULSOUT 13, 850
    ··· PULSOUT 12, 850
    ··· PAUSE 20
    · NEXT
    · RETURN

    Back_Up: ' Back up.
    · FOR pulseCount = 0 TO 40
    ··· PULSOUT 13, 650
    ··· PULSOUT 12, 817
    ··· PAUSE 20
    · NEXT
    · RETURN
  • FranklinFranklin Posts: 4,747
    edited 2008-08-24 05:28
    Good, now look at the ir code where the if statements do pulseleft and right then look at your whisker code that uses subroutines to do the moving. There is some more integration you could do here to use only the subroutines.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • bisctboybisctboy Posts: 6
    edited 2008-08-24 14:03
    Actually, I was hoping to get rid of the sub routines in the Whisker part of the code because they make my boe bot slower. The IR code (which doesn't use sub routines) makes the bot maneuver alot quicker and smoother around what it encounters versus the evasive maneuvers in the Whisker code.
  • FranklinFranklin Posts: 4,747
    edited 2008-08-24 15:23
    That's because the IR code is testing the IR then making ONE PULSOUT each time throught the loop. The whisker code does more by backing up 40 times wiht a pause of 20 ms for a total of 800 ms (.8 sec) this allows the bot to move away from the problem but if it hits nothing this should not even be a factor. A good way to see what is happening is to draw a flow chart where each decision is a box and the flow branches depending on the conditions at the box.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
Sign In or Register to comment.