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...")
 
 
(20 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{| style="font-family:Verdana, Genega, sans-sarif; font-size:80%;color:gray;" width="100%" cellpadding="0%" cellspacing="0" border="0"
+
<yambe:breadcrumb self="Debug Stream">General|General Programming</yambe:breadcrumb>
|-
+
<br />
|
+
'''Dexter Industries''' has created a great introductory blog post on the debug stream, which can be [http://dexterindustries.com/howto/using-debugger-stream-in-robotc-for-lego found here].
''[[Main_Page|Main]] >> [[NXT_Main|NXT]] >> [[NXT: Functions| Functions]] >> [[Debug_Stream|Debug Stream]] ''
+
|-
+
|}
+
  
  
'''''“Traditional” Debugging Techniques'''''
+
{{tl|1|1}}
 +
<br />
  
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:
+
== “Traditional” Debugging Techniques ==
 +
{| class="wikiText"
 +
|-
 +
| 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 such as:
  
 
*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.
 
*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.
 
*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!
+
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.
 
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.
----
 
  
 
+
[[File:Debug_stream_3.png]]
== AddToDatalog ==
+
{| 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: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:#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
+
  |-
+
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|''data''
+
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|An integer or long variable to print to datalog
+
  |-
+
  |}
+
 
+
 
+
  {|
+
  |-
+
  |<syntaxhighlight lang="ROBOTC">
+
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>
+
  |-
+
  |}
+
 
|-
 
|-
 
|}
 
|}
  
 
+
== writeDebugStream ==
== bHideDataFiles ==
+
 
{| 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'''
+
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">writeDebugStream</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">const string </span><span class="bigCodeBasic">sFormatString</span><span class="bigCodePunc">, </span><span class="bigCodeBasic">...</span><span class="bigCodePunc">)</span>
 
|-
 
|-
| 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%;"|([[Data_Types#dataType_void|void]]) Writes a String to the Debug Stream.
 
+
 
+
  {|
+
  |-
+
  |<syntaxhighlight lang="ROBOTC">bHideDataFiles = true;  // data files will be HIDDEN and NOT SHOW on NXT GUI 
+
bHideDataFiles = false;  // data files will be SHOWN on NXT GUI</syntaxhighlight>
+
  |-
+
  |}
+
 
|-
 
|-
|}
+
|
 
+
  {| class="parameterTable" cellpadding="5%" width="100%"
 
+
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
== nDatalogSize ==
+
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
+
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
|-
+
| 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">
+
   | style="border-style: solid; border-width: 1px 0px 0px 0px"|''sFormatString''  
const int size = 2000;  // a constant int variable 'size' of size 2000   
+
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|A string to write to the debug stream<br />(can be formatted!)
nDatalogSize = size;   // create a new Datalog of size, 'size'</syntaxhighlight>
+
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_string|string]]
 
   |-
 
   |-
 
   |}
 
   |}
 
|-
 
|-
|}
+
|
 
+
 
+
== 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 
+
   |<syntaxhighlight lang="ROBOTC">writeDebugStream("int x is: %d", x);  // writes the current value of int 'x' to the debug stream</syntaxhighlight>
nxtDisplayCenteredTextLine(3, "Max Datalog Files:");    // display on a centered textline on line 3
+
nxtDisplayCenteredBigTextLine(4, "%d", nMaxDataFiles);  // (will display, "15")</syntaxhighlight>
+
 
   |-
 
   |-
 
   |}
 
   |}
 
|-
 
|-
 
|}
 
|}
 +
<br />
  
 
+
== writeDebugStreamLine ==
== nMaxDataFileSize ==
+
 
{| 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 nMaxDataFileSize'''
+
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">writeDebugStreamLine</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">const string </span><span class="bigCodeBasic">sFormatString</span><span class="bigCodePunc">, </span><span class="bigCodeBasic">...</span><span class="bigCodePunc">)</span>
 
|-
 
|-
| 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.
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Writes a String to the Debug Stream starting on a new line.
 
+
 
+
  {|
+
  |-
+
  |<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>
+
  |-
+
  |}
+
 
|-
 
|-
|}
+
|
 
+
  {| class="parameterTable" cellpadding="5%" width="100%"
 
+
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
== nUsedDatalogBytes ==
+
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
+
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
|-
+
| 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 
+
   | style="border-style: solid; border-width: 1px 0px 0px 0px"|''sFormatString''
while(nMaxDataFileSize - nUsedDatalogBytes > 0)  // while there are still bytes left (0 < bytes < size):
+
   | style="border-style: solid; border-width: 1px 0px 0px 0px"|A string to write to the debug stream<br />(can be formatted!)
{
+
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_string|string]]
   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>
 
   |-
 
   |-
 
   |}
 
   |}
 
|-
 
|-
 
|}
 
|}
 +
<br />

Latest revision as of 16:53, 20 July 2012

General Programming → Debug Stream


Dexter Industries has created a great introductory blog post on the debug stream, which can be found here.


Color Key
Function:
Variable:


“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 such as:
  • 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.

Debug stream 3.png

writeDebugStream

void writeDebugStream(const string sFormatString, ...)
(void) Writes a String to the Debug Stream.
Parameter Explanation Data Type
sFormatString A string to write to the debug stream
(can be formatted!)
string
writeDebugStream("int x is: %d", x);  // writes the current value of int 'x' to the debug stream


writeDebugStreamLine

void writeDebugStreamLine(const string sFormatString, ...)
(void) Writes a String to the Debug Stream starting on a new line.
Parameter Explanation Data Type
sFormatString A string to write to the debug stream
(can be formatted!)
string
writeDebugStreamLine("int x is: %d", x);  // writes the current value of int 'x' to the debug stream on a new line.