Lesson: Exercise: Using Logging

Overview: 
Explore adding logging of debugging information to a file on the controller phone and then pulling that file back to the PC for examination.
Objectives: 

Add the provided logging utility source code to your project and then understand how to use logging in your programs.

Content: 

We are now going to take a look at logging (also called tracing) as a tool to debug our robot programs. Logging is recording useful information from the robot program to a disk file on the EV3 controller. You can then download that file to your PC and examine it. It can be very useful to record information while your robot is running during a match so you can look at it afterwards and see what took place. If you have not read the general lesson on Logging, you should do that now.

To get started, we need to get the provided logging code into your robot controller project. In the teamcode package, create a new class called Logging. Click here to open the logging class code. Copy the code and paste it into the new Logging class you just created. This adds the logging class to your project and makes it available for use in your OpModes.

Now we are going to copy the DriveCircleTouch example and add logging to it. Create a new class in the teamcode package called DriveCircleLogging. Copy the code below into that class.

Now lets discuss the changes made to implement logging. We added a constructor method to this class and in that method call Logging.Setup(). This initializes the logging system. We then write a message to the log file with the Logging.log() method. We then added other messages recording the progress of the OpMmode.

The logging class will write the messages to a file called Logging.txt in the top directory of the controller phone. You can use ADB (Android Debug Bridge) to download that file from the phone. Open the terminal window at the bottom of Android Studio. Copy and paste  this command into the terminal window and press enter:

adb pull //storage/sdcard0/Logging.txt c:\temp\robot_logging.txt

This will pull the file from the phone into the PC directory specified. You can then view the file with Notepad.

Run the program 3 times, once letting it run the 5 seconds and stop on timeout. Then  run again and press the touch button before the 5 seconds passes. Last, run it and click the Stop button on the Driver Station before time runs out. Then pull the Logging.txt file back to your PC and take a look. It should look like this:

<0>02:43:46:360 DriveCircleLogging.<init>(DriveCircleLogging.java:25): Starting Drive Circle Logging
<1>02:43:46:377 DriveCircleLogging.runOpMode(DriveCircleLogging.java:41): waiting for start
<1>02:43:47:286 DriveCircleLogging.runOpMode(DriveCircleLogging.java:49): running
<1>02:43:53:299 DriveCircleLogging.runOpMode(DriveCircleLogging.java:68): timeout
<1>02:43:53:301 DriveCircleLogging.runOpMode(DriveCircleLogging.java:81): out of while loop
<1>02:43:53:312 DriveCircleLogging.runOpMode(DriveCircleLogging.java:91): stopFlag=true, i=3, d=3.750000
<1>02:43:53:314 DriveCircleLogging.runOpMode(DriveCircleLogging.java:93): done
<0>02:43:54:647 ========================================================================
<0>02:43:54:650 DriveCircleLogging.<init>(DriveCircleLogging.java:25): Starting Drive Circle Logging
<2>02:43:54:662 DriveCircleLogging.runOpMode(DriveCircleLogging.java:41): waiting for start
<2>02:43:55:305 DriveCircleLogging.runOpMode(DriveCircleLogging.java:49): running
<2>02:43:57:456 DriveCircleLogging.runOpMode(DriveCircleLogging.java:74): button touched
<2>02:43:57:464 DriveCircleLogging.runOpMode(DriveCircleLogging.java:81): out of while loop
<2>02:43:57:480 DriveCircleLogging.runOpMode(DriveCircleLogging.java:91): stopFlag=true, i=3, d=3.750000
<2>02:43:57:483 DriveCircleLogging.runOpMode(DriveCircleLogging.java:93): done
<0>02:43:59:727 ========================================================================
<0>02:43:59:733 DriveCircleLogging.<init>(DriveCircleLogging.java:25): Starting Drive Circle Logging
<3>02:43:59:755 DriveCircleLogging.runOpMode(DriveCircleLogging.java:41): waiting for start
<3>02:44:00:937 DriveCircleLogging.runOpMode(DriveCircleLogging.java:49): running

Note the log message that uses format specifiers to merge variable data into the log message. You can read more about formatting here.

One final thing to note. Looking at the trace, we can see that when we pressed the Stop button on the DS, the trace ends immediately and the code after the while block is not executed. Yet it is when we end on timeout or button touch. The reason is that when the Stop button is pressed, the built in controller app code that runs your code throws an exception. That exception stops OpMode execution. This is done to make sure your OpMode does not run after the Stop button is pressed. However, this prevents your code from doing anything after the Stop button is pressed. There might be some clean up or perhaps logging that you would like to do before your OpMode ends. If so, you can run code after the while block by wrapping the while block in a try/catch block:

Catching the exception allows you to execute code after the OpMode has been signaled to stop. Note that if you do this, you are responsible for making sure your OpMode performs no action that could be construed as gaining an advantage in the game and you should make sure your code does end in a timely manner.

 

Lesson navigation: