Shop OBEX P1 Docs P2 Docs Learn Events
Help with my program — Parallax Forums

Help with my program

C.J. PowersC.J. Powers Posts: 5
edited 2009-03-09 17:33 in Robotics
Hi I'm really new at programming and the boebot. I could use some help combining two programs I have working independently already.

one is avoiding the edge of a table with IR sensors.

which looks like this:

' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
irDetectLeft VAR Bit ' Variable declarations.
irDetectRight VAR Bit
pulseLeft VAR Word
pulseRight VAR Word
loopCount VAR Byte
pulseCount VAR Byte
FREQOUT 4, 2000, 3000 ' Signal program start/reset.
DO ' Main Routine.
FREQOUT 10, 1, 38500 ' Check IR detectors.
irDetectLeft = IN11
FREQOUT 2, 1, 38500
irDetectRight = IN0
' Decide navigation.
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
pulseCount = 1 ' Both detected,
pulseLeft = 850 ' one pulse forward.
pulseRight = 650
ELSEIF (irDetectRight = 1) THEN ' Right not detected,
pulseCount = 10 ' 10 pulses left.
pulseLeft = 650
pulseRight = 650
ELSEIF (irDetectLeft = 1) THEN ' Left not detected,
pulseCount = 10 ' 10 pulses right.
pulseLeft = 850
pulseRight = 850
ELSE ' Neither detected,
pulseCount = 15 ' back up and try again.
pulseLeft = 650
pulseRight = 850
ENDIF
FOR loopCount = 1 TO pulseCount ' Send pulseCount pulses
PULSOUT 13,pulseLeft
PULSOUT 12,pulseRight
PAUSE 20
NEXT
LOOP


the other is using the CMUcam1 without the appmod, to play a sound when it sees the color it detected.

that program looks like this:

'{$STAMP BS2}
'{$PBASIC 2.5}
RcvData VAR Byte(10)
n VAR Byte
Confid VAR Byte
' Pause 1 second for CMUcam startup
PAUSE 1000
' Send "reset" to sync CMUcam and Stamp
SEROUT 15, 84, [noparse][[/noparse]"RS",CR]
SERIN 14, 84, [noparse][[/noparse]WAIT (":")]
PAUSE 1000
' Green LED on
SEROUT 15, 84, [noparse][[/noparse]"L1 1",CR]
SERIN 14, 84, [noparse][[/noparse]WAIT (":")]
PAUSE 100
' Turn on auto adjust for 5 seconds
SEROUT 15, 84, [noparse][[/noparse]"CR 18 44",CR]
SERIN 14, 84, [noparse][[/noparse]WAIT (":")]
PAUSE 100
' Pause 5 seconds for CMUcam to auto adjust to lighting conditions
PAUSE 5000
' Turn off auto adjust
SEROUT 15, 84, [noparse][[/noparse]"CR 18 44 19 32",CR]
SERIN 14, 84, [noparse][[/noparse]WAIT (":")]
PAUSE 100
' Green LED auto mode
SEROUT 15, 84, [noparse][[/noparse]"L1 2",CR]
SERIN 14, 84, [noparse][[/noparse]WAIT (":")]
PAUSE 100
' Give user time to place color target close in front of camera
PAUSE 5000
' Send command - Set poll mode - only sends one return packet -
' of data after each command - reduces data flow
SEROUT 15, 84, [noparse][[/noparse]"PM 1",CR]
SERIN 14, 84, [noparse][[/noparse]WAIT (":")]
PAUSE 100
' Send command - Set raw data mode - also suppress Ack:/Nak: to -
' further reduce serial data
SEROUT 15, 84, [noparse][[/noparse]"RM 3",CR]
PAUSE 100
' Track Window command looks at the center of CMUcam image -
' grabs the color information and sends to the Track Color function
' Send command - Track window
SEROUT 15, 84, [noparse][[/noparse]"TW",CR]
' Ignore the S packet and M packet from TW
PAUSE 2000
Main:
' Send command - Track color (with no arguments) -
' will track last color grabbed by TW command
SEROUT 15, 84, [noparse][[/noparse]"TC",CR]
SERIN 14, 84, [noparse][[/noparse]STR RcvData\10]
Confid = RcvData(9)
' 45 is aprox H center
IF RCVData(2) > 65 AND Confid > 20 THEN Left
IF RCVData(2) < 25 AND Confid > 20 THEN Left
IF RCVData(8) < 150 AND Confid > 25 THEN Left
' Trim the pulsout numbers for your servos
GOTO Main
Left:
FOR n = 1 TO 10
FREQOUT 4, 2000, 3000
GOTO main

the idea behind it is to have the camera run its camera color program show the camera the color, remove the color and place it somewhere on the table top, then drive on the table top until the color is found, then play the sound, and stop.

I'm having a lot of trouble with the program, and help would be greatly appreciated.

Thanks.

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-03-08 23:08
    What you'll need to do is to figure out which parts of each program sets their respective elements and prepares to enter the main loop. In your new, combined program you'll have this information at the top of your program. Then you can have a main loop that checks on every iteration for the color, if found it breaks. Otherwise, it runs your movement algorithm (including table edge detection) for some amount of time (1/2 second maybe). Once it breaks from your main loop, you can have the song play and an END.
  • C.J. PowersC.J. Powers Posts: 5
    edited 2009-03-09 04:27
    I know what you mean, but putting it in to practice is a different story, I have tried coding it several times with no luck, I'm very new at programming, both of those programs are just edited versions of the original program to match my pins.

    I know which part of the programs do what, but changing them to mesh isn't working for me. I need to right the programs basicly likes this but with the proper algorithm:


    Calibrations
    main
    Table top program
    if color is found
    then beep
    goto main

    but I need help writing it... if anyone has the time.
  • SRLMSRLM Posts: 5,045
    edited 2009-03-09 04:55
    Best way to learn is to do it yourself. To that end, take a look at the various Stamps in class books that parallax has for free download, in particular What's a Microcontroller. The next step is to figure out what each line in your program does, and then you can begin to copy. I know it's not the answer you want (as a CS student I get it all the time...) but it works. You've got the general program outline done, so now you just need to figure out how to put it into code.

    As a side note, when posting code use either the attach option (for large blocks/full programs) or the code tags (for smaller snippets).
  • Tony StarkTony Stark Posts: 3
    edited 2009-03-09 06:22
    Hi. Tony Stark here. I have experience coding some stuff using Adobe Flash Pro 8 (Actionscript). It has nothing to do with microchips, but I have valuable information about approaching a problem with your program not working and feelings of confusion. SRLM is steering you in the right direction. You can easily overwhelm yourself if you try to get large amounts of code to "magically work". It hardly works this way in the coding world. You have to break down the task you have into smaller pieces. Get rid of some lines of code and leave it to a level where you feel you understand what is going on. Step-by-step, begin to add one more function or one more logical step. Compile and verify everything is still okay after having added just a few lines more of code. When it works, go ahead and add another small section of code. The key here is to always HAVE CONTROL over your work. Don't let your work take control over you. Remember: one step at a time...not all at once. Troubleshooting your own code takes patience and determination...and an occasional glance at your help files and documentation. Don't be afraid to admit that you don't understand what a particular line of code means. Use the help files. Review and learn. Then continue. ONE STEP AT A TIME. Stick to your plan. Good luck!
    -Tony Stark, Stark Industries
  • C.J. PowersC.J. Powers Posts: 5
    edited 2009-03-09 07:25
    Thanks guys
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-03-09 17:33
    C.J. in future posts please attach full programs rather than pasting them into the message. This makes it easier to follow and load into the BASIC Stamp Editor. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
Sign In or Register to comment.