Difference between revisions of "Debug Stream"

From ROBOTC API Guide
Jump to: navigation, search
(Created page with "{| style="font-family:Verdana, Genega, sans-sarif; font-size:80%;color:gray;" width="100%" cellpadding="0%" cellspacing="0" border="0" |- | ''Main >> [[NXT_Main...")
 
Line 7: Line 7:
  
  
'''''“Traditional” Debugging Techniques'''''
+
'''“Traditional” Debugging Techniques'''
  
 
Debugging a program – finding the errors and correcting them – can be a slow process in solutions without a run-time debugger. Without a debugger you may have to resort to different techniques like:
 
Debugging a program – finding the errors and correcting them – can be a slow process in solutions without a run-time debugger. Without a debugger you may have to resort to different techniques like:
Line 16: Line 16:
  
 
There is also a built-in Debug Stream that you can use to keep track of your program from behind the scenes.  For example, you could print a message to the Debug Stream when you enter and exit loops, functions, etc.  Then you can view the cached Debug Stream to help in the debugging process.
 
There is also a built-in Debug Stream that you can use to keep track of your program from behind the scenes.  For example, you could print a message to the Debug Stream when you enter and exit loops, functions, etc.  Then you can view the cached Debug Stream to help in the debugging process.
 +
 +
<IMAGE HERE>
 
----
 
----
  
  
== AddToDatalog ==
+
== writeDebugStream ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| style="font-family:Courier New; color:black; background-color:#E6E6E6; text-align:left; font-size:100%;"| '''void AddToDatalog(const long data)'''
+
| style="font-family:Courier New; color:black; background-color:#E6E6E6; text-align:left; font-size:100%;"| '''void writeDebugStream(const string sFormatString, ...)'''
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|Adds an entry to the datalog.
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|Writes a String to the Debug Stream.
  
  
Line 31: Line 33:
 
   ! Explanation
 
   ! Explanation
 
   |-
 
   |-
   | style="border-style: solid; border-width: 0px 0px 0px 0px"|''data''  
+
   | style="border-style: solid; border-width: 0px 0px 0px 0px"|''sFormatString''  
   | style="border-style: solid; border-width: 0px 0px 0px 0px"|An integer or long variable to print to datalog
+
   | style="border-style: solid; border-width: 0px 0px 0px 0px"|A string to write to the debug stream (can be formatted!)
 
   |-
 
   |-
 
   |}
 
   |}
Line 39: Line 41:
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">
+
   |<syntaxhighlight lang="ROBOTC">writeDebugStream("int x is: %d", x); // writes the current value of int 'x' to the debug stream</syntaxhighlight>
int i;              // create variable 'i'
+
for(i=0; i<10; i++)  // cycle through "for loop" ten times (0-9), incrementing 'i' with each iteration:
+
{
+
  AddToDatalog(i);     // add variable 'i' to the datalog with each iteration of the loop
+
}</syntaxhighlight>
+
 
   |-
 
   |-
 
   |}
 
   |}
Line 51: Line 48:
  
  
== bHideDataFiles ==
+
== writeDebugStreamLine ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| style="font-family:Courier New; color:black; background-color:#E6E6E6; text-align:left; font-size:100%;"| '''word bHideDataFiles'''
+
| style="font-family:Courier New; color:black; background-color:#E6E6E6; text-align:left; font-size:100%;"| '''void writeDebugStreamLine(const string sFormatString, ...)'''
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|Boolean flag to indicate whether data files should be hidden or shown in the NXT GUI.
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|Writes a String to the Debug Stream starting on a new line.
  
  
   {|
+
   {| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#F2F2F2; text-align:left; font-size:100%; border-collapse: separate; border-spacing: 0; border-width: 0px; border-style: solid; border-color: #000;" cellpadding="5%"
 +
  ! style="border-style: solid; border-width: 0px 0px 0px 0px"|Parameter
 +
  ! Explanation
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">bHideDataFiles = true;   // data files will be HIDDEN and NOT SHOW on NXT GUI 
+
   | style="border-style: solid; border-width: 0px 0px 0px 0px"|''sFormatString''
bHideDataFiles = false; // data files will be SHOWN on NXT GUI</syntaxhighlight>
+
   | style="border-style: solid; border-width: 0px 0px 0px 0px"|A string to write to the debug stream (can be formatted!)
 
   |-
 
   |-
 
   |}
 
   |}
|-
 
|}
 
 
 
== nDatalogSize ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
| style="font-family:Courier New; color:black; background-color:#E6E6E6; text-align:left; font-size:100%;"| '''word nDatalogSize'''
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|The current size allocated to the datalog.
 
 
 
  {|
 
  |-
 
  |<syntaxhighlight lang="ROBOTC">
 
const int size = 2000;  // a constant int variable 'size' of size 2000   
 
nDatalogSize = size;    // create a new Datalog of size, 'size'</syntaxhighlight>
 
  |-
 
  |}
 
|-
 
|}
 
 
 
== nMaxDataFiles ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
| style="font-family:Courier New; color:black; background-color:#E6E6E6; text-align:left; font-size:100%;"| '''word nMaxDataFiles'''
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|Maximum number of saved datalog files (DATAnnnn.RDT) allowed on a NXT.
 
 
 
  {|
 
  |-
 
  |<syntaxhighlight lang="ROBOTC">nMaxDataFiles = 15;                                    // allow up to 15 Datalog files on one NXT 
 
nxtDisplayCenteredTextLine(3, "Max Datalog Files:");    // display on a centered textline on line 3
 
nxtDisplayCenteredBigTextLine(4, "%d", nMaxDataFiles);  // (will display, "15")</syntaxhighlight>
 
  |-
 
  |}
 
|-
 
|}
 
 
 
== nMaxDataFileSize ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
| style="font-family:Courier New; color:black; background-color:#E6E6E6; text-align:left; font-size:100%;"| '''word nMaxDataFileSize'''
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|Maximum size (in 100 byte units) of all datalog files saved on the NXT.
 
 
 
  {|
 
  |-
 
  |<syntaxhighlight lang="ROBOTC">nMaxDataFileSize = 10;                                    // 10*100 = 1000 = roughly 1 kilobyte 
 
nxtDisplayCenteredTextLine(3, "Max Datalog Files:");      // display on a centered textline on line 3
 
nxtDisplayCenteredBigTextLine(4, "%d", nMaxDataFileSize);  // (will display, "1000")</syntaxhighlight>
 
  |-
 
  |}
 
|-
 
|}
 
 
 
== nUsedDatalogBytes ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
| style="font-family:Courier New; color:black; background-color:#E6E6E6; text-align:left; font-size:100%;"| '''const word nUsedDatalogBytes'''
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|The number of bytes currently containing data in the datalog.
 
 
 
  {|
 
  |-
 
  |<syntaxhighlight lang="ROBOTC">nMaxDataFileSize = 10;                          // 10*100 = 1000 = roughly 1 kilobyte 
 
while(nMaxDataFileSize - nUsedDatalogBytes > 0)  // while there are still bytes left (0 < bytes < size):
 
{
 
  AddToDatalog(data);                              // write to datalog (fill it up!)
 
}</syntaxhighlight>
 
  |-
 
  |}
 
|-
 
|}
 
 
== SaveNxtDatalog ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
| style="font-family:Courier New; color:black; background-color:#E6E6E6; text-align:left; font-size:100%;"| '''void SaveNxtDatalog()'''
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|Saves the existing datalog from RAM memory into a NXT file named "DATAnnnn.rdt" where 'nnnn' is numeric and increases by one on every save.
 
  
  
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">SaveNxtDatalog();  // saves the Datalog</syntaxhighlight>
+
   |<syntaxhighlight lang="ROBOTC">writeDebugStreamLine("int x is: %d", x);  // writes the current value of int 'x' to the debug stream on a new line.</syntaxhighlight>
 
   |-
 
   |-
 
   |}
 
   |}
 
|-
 
|-
 
|}
 
|}

Revision as of 23:29, 20 December 2011

Main >> NXT >> Functions >> Debug Stream


“Traditional” Debugging Techniques

Debugging a program – finding the errors and correcting them – can be a slow process in solutions without a run-time debugger. Without a debugger you may have to resort to different techniques like:

  • There’s no way to determine if your program is executing the intended logic. So you add code to play different tones/sounds to your program as it executes different “blocks” of code. You determine from the sound what is being executed within your program.
  • If your robot platform supports a display device (which could be a serial link to your PC or an LCD display on the robot) then you would have to add “print” statements to your program code at various points in your program. By examining the display, you can (hopefully) determine what’s happened in your program's execution by the display.

Both of the above techniques are available in ROBOTC. However, a real-time debugger eliminates the need to resort to them. There’s no need to add code for debugging to your program. A built-in debugger provides better functionality without ever having to modify your source code!

There is also a built-in Debug Stream that you can use to keep track of your program from behind the scenes. For example, you could print a message to the Debug Stream when you enter and exit loops, functions, etc. Then you can view the cached Debug Stream to help in the debugging process.

<IMAGE HERE>



writeDebugStream

void writeDebugStream(const string sFormatString, ...)
Writes a String to the Debug Stream.


Parameter Explanation
sFormatString A string to write to the debug stream (can be formatted!)


writeDebugStream("int x is: %d", x);  // writes the current value of int 'x' to the debug stream


writeDebugStreamLine

void writeDebugStreamLine(const string sFormatString, ...)
Writes a String to the Debug Stream starting on a new line.


Parameter Explanation
sFormatString A string to write to the debug stream (can be formatted!)


writeDebugStreamLine("int x is: %d", x);  // writes the current value of int 'x' to the debug stream on a new line.