Introduction
Now that you've mastered several small programming tasks, you need to step back and reflect on the experience. Everyone makes mistakes when programming, whether you are a novice or an expert. Of course, the expert can draw upon past experiences to help solve problems with a program (debugging the program). As a new programmer, you need to be aware of tools that will help you exterminate the bugs in your programs.
Your challenge will be to demonstrate proper use of all these techniques in Lab 4: Line Following (TEAMS Grand Prix)
So here we go...
History of the computer term "bug"
Click here to see a full size photo of first computer bug
List of Debugging Tools
1. Plan Ahead
- (Groan) - yes, this means you should put together a flow chart, algorithm, or pseudocode before you begin.
- Start with a pencil version, and tape a couple pieces of paper together to give yourself some room to work.
- If your flow chart does not work (logic incorrect) then your program won't work either. Have someone else give you feedback on your flowchart.
- As you revise your code, you can mark up your flow chart as well.
- Remember: every lab you turn in must include a flowchart that matches your code.
- Comment your code!!
- Use tabs and returns to properly format your code!!
2. Write code in small, functional chunks and test as you go
- Too often programmers try to write the entire program (expecting that it will work perfectly), then end up with several errors throughout their code that are difficault to find.
- Instead, you should write small pieces of code to perform specific tasks, test that code, then move on to the next piece.
- While you are building up your code in this fashion, you can "comment out" the portions of code that you are not working on and want to "keep out of the way" while testing (put a // before a single line, or use /* and */ for a block of code)
- This is Prof. Martin's #1 rule of thumb--Ms. Thomas, Jim and I agree wholeheartedly!
3. Step through the program just as the computer would (become the machine!)
- Many compilers offer a "stepping" tool to debug a program. A stepping tool actually allows you to watch the execution of the the program line by line (e.g., you press the enter key, and the next line is executed, and you can observe all variable values). Unfortunately, this step debugger tool is not available for the Create CM!!
- However, [_you_] can use your knowledge of how code works to [_carefully_] step through the process on your own. This is perhaps the most important debugging tool!
4. Use lights and sounds
- One problem we have working with robots is that we cannot easily "see" what the robot "sees." The Create does not even have a display for us to use.
- Well, you do have several LEDs and the use of songs to give you feedback as you are executing your program
- Use the LEDs to your advantage
- You have 5 LED under you control. Plan their use!! Use different LED pattern for each key part of your program.
- You also have multiple colors to use. How can you let the user know what state the robot is in?? What does the colr RED vs. GREEN indicate??
- You can use flashing patterns as well (but be careful about using cr8_delay(msec)! )
- Use Sound to communicate
- Use the various pre-defined songs (beep functions) to let the user know what is going on
- You can also define new songs if you like. Can someone post sample code below? (Pre-tested, of course!)
5. Display values on the Cricket 4 Digit LED Display
- Use the cricket display--it's easy to use!
- Be sure to keep Cricket display plugget into the "cargo" ePort
6. Send text and data back to RealTerm
- Greg developed three tools to use to send data and text back to RealTerm. See page 6 of C Programming Guide.
- cr8_printf( text_&_variables_to_print_to_RealTerm_screen , list_of_variables); -- Here's an example of printf used in the sample program below:
- cr8_text_to_usb(msg); -- You can use this function to make Realterm print a message. Replace msg with the message you want to send, with a double-quote on each end.
- cr8_data_to_usb(data16, use_sign); -- This function sends a data value to the computer in ASCII format. Replace use_sign with true if the data is signed, false if not.
- Remember, you need to set up RealTerm and use the black and red button on the command module. Click Here to see a summary of the instructions for using RealTerm while running your robot.
Try out this sample code!
- Update your template files
- Create a new project directory called "Debug Demo" and copy the file below into that directory:
- Before you download and run the program...
- Analyze the code and predict what it will do!
- Note how LEDs, sound, the Cricket display, and RealTerm have been used to help the user "see" what is happening.
- How useful are these tools?
- By the way, do you notice anything unusual happening when you are running this program??