So you have ROBOTC installed on your computer and are ready to program your LEGO MINDSTORMS NXT, but you need a debugging technique to find possible run time errors in your program?
The 3 most common ways to do this in ROBOTC are:
- Add tones and sounds to your program code, and from the sound determine which part of the code is being executed.
- If you are using an LCD display (like in the MINDSTORMS NXT) then you can print out statements to the display from your code and by examining them find out the part of code that is being currently executed.
- Use the in-built Debug Stream functionality that helps by printing messages when you enter or exit loops and functions, or by printing the current value of a variable that you may have defined in the program.
The Debug Stream is the most useful option out of the three and is not available to the Basic user, but is available to the Expert and Super users. This page shows how to use the Debug Stream in ROBOTC.
Changing to Super User.
To change the Basic user level in your ROBOTC– Click on the Window menu and under the Menu Level option, select the Super User functionality.
You will now find that the number of options under the Robot menu and the functionalities available in Debugger Windows have increased. You will now be able to see the Debug Stream functionality under Debugger Windows. These functionalities will be grayed-out initially, and are available once you run a program.
Using Debug Stream in program.
Now use the predefined ROBOTC functions- writeDebugStream() and writeDebugStreamLine() in your program to print out a message or the current value of a variable, in the debug stream window. The writeDebugStream function writes a string, passed as an argument to the function, to the debug stream while the writeDebugStreamLine function writes the string to the debug stream and then goes to a new line. An example code showing how to write variables and messages to Debug Stream is shown here.
Note:- A message must be at most 19 characters long in ROBOTC. If a message or String variable exceeds 19 characters in length then a warning is displayed on compilation of the program- “String “…” exceeds 19 chars” ; and on running the program only the first 19 characters will be displayed. It is therefore a good practice to not let your messages exceed 19 characters in length, and if they do, use multiple writeDebugStream functions to print them.
Running the Debug Stream Example Program
After downloading the program, the Program Debug window will pop up with options: Start, Suspend, Step Into, Step Over, Step Out, Clear All and Refresh.
Now Go to “Robot –> Debugger Windows…” and click on “Debug Stream”. This will open up a blank Controller Debug Stream window.
On pressing the Start button of the Program Debug window, the program execution starts and the following output of the example code is observed in the Controller Debug Stream.
The execution stops automatically after the whole program has been executed. If you need better control over the program and want to observe how each command is executed then use the Step Into, Step Over and Step Out buttons of the Program Debug window.
Using Step Into button
By continuously pressing the Step Into button you can execute each command of your program and observe the outputs of the writeDebugStream and writeDebugStreamLine commands in the Controller Debug Stream. The yellow highlight shows the line to be executed next in the program. The output observed in the Controller Debug Stream at various stages of running the example code using Step Into are shown below.
By using the Step Over button you can execute a complete function in your code and by the use of Step Out button the execution will step out of the function. The Suspend button is used to pause the program execution and the Clear All button is used to reset all the variable values and clear the Controller Debug Stream window.
Questions? Please contact us or post a question in the forum below.
[sourcecode language=”cpp”] int i= 10;char ch= ‘d’;
float x= 3.1416;
ubyte byted= 66; // ASCII value of a character
char charray[]={‘D’,’E’,’X’,’T’,’E’,’R’}; // character array
string str= "Dexter Industries"; // String variable cannot exceed 19 chars in length
task main(){
writeDebugStreamLine("Int i is : %d", i); // write Integer value (base 10) to Debug Stream
writeDebugStreamLine("Int in oct : %o", i); // print Octal equivalent of integer (base 8)
writeDebugStreamLine("Int in hex : %X", i); // print Hexadecimal equivalent of integer (base 16)
// in capital letters in Debug Stream window
i=i+1; // increment i
writeDebugStreamLine("Int i now is: %d", i);
writeDebugStreamLine("Char ch is : %c", ch); // print Character value in Debug Stream window
// Print D E X T E R one by one in the same line (separated by single space) by reading each character from character array
for (int j=0;j ch=charray[j];
writeDebugStream("%c ", ch); // space added after character
}
writeDebugStreamLine(""); // go to next line after printing D E X T E R
writeDebugStreamLine("Float x is : %f", x); // print Float value to Debug Stream =3.14
writeDebugStreamLine("Float 1.0 is: %1.0f", x); // print Float with 1 value before decimal and 0 values after decimal =3
writeDebugStreamLine("Float 1.2 is: %1.2f", x); // print Float with 1 value before decimal and 2 values after decimal =3.14
writeDebugStreamLine("Float 1.5 is: %1.5f", x); // print Float with 1 value before decimal and 5 values after decimal =3.14160
writeDebugStreamLine("Float E : %E", x); // print Float in Mantissa and Exponent format using E character.
writeDebugStreamLine("Ubyte int : %i", byted); // print integer value of ubyte
writeDebugStreamLine("Ubyte char : %c", byted); // print character equivalent of ubyte value
byted=byted+1;
writeDebugStreamLine("Ubyte char : %c", byted);
writeDebugStream("String is : %s", str); // print the String variable "Dexter Industries" to Debug Stream and then
writeDebugStreamLine(" loves using RobotC"); // print the message string " loves using RobotC" in the same line.
}
[/sourcecode]