Instructional Material: Exercise: Using Logging
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 controller device. 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 device. You can use ADB (Android Debug Bridge) to download that file from the device. Open the terminal window at the bottom of Android Studio. Copy and paste this command into the terminal window and press enter:
ZTE: adb pull //storage/sdcard0/Logging.txt c:\temp\robot_logging.txt
MOTO G: adb pull sdcard/Logging.txt c:\temp\robot_logging.txt
Control Hub: adb pull sdcard/Logging.txt c:\temp\robot_logging.txt
This will pull the file from the device 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
Note the log message that uses format specifiers to merge variable data into the log message. You can read more about formatting here.