RoboRIO

Overview: 
Learn about Interfaces which are definitions of the fields and methods implementing classes must expose.
Objectives: 

Understand Interfaces and be able to use them when appropriate.

Content: 

Just as a class is a description of an object, an Interface is a description of a class. An Interface defines the fields (variables) and methods that a class must provide (public access) to users of the class. The Interface does not define how a class derives the value of a field or the result of a method, only that a class that implements an Interface must expose that Interface's fields and methods. As such, the Interface defines a public facing API that the class provides. Interfaces are also called Contracts, in that the Interface defines a contact or agreement (in terms of the fields and methods exposed) between the class and users of the class.

One power of Interfaces is that any class that implements an Interface can be used where ever that Interface is defined as a required data type. This concept is best explained by an example.

A simplifed view of the PIDController class in the FRC library shows a constructor that takes two classes as input, a PIDSource object and a PIDOutput object. PIDSource and PIDOutput are both Interfaces. Digressing for a moment, a simplified PIDController needs two things to perform its function. An input or process control value that will be used by the PIDController to compute an output value, which must be sent to some other class for action. Now in most cases the input value will come from an encoder and the output value will go to a motor. You could define the PIDController class constructor as Public PIDController(Encoder enc, Motor motor). There are two problems with this. First, the PIDController class and the Encoder and Motor classes have not agreed on how data will pass between the Encoder and PIDController class and how data will pass between the PIDController and Motor class. The second issue is that the PIDController as defined will only work with Encoders and Motors. What if we wanted to use some other classes as the input and output objects? Interfaces solve both of these problems.

The simplified PIDController class constructor actually looks like this: Public PIDController(PIDSource source, PIDOutput output). PIDSource and PIDOutput are Interfaces and they define what fields and methods are required for any class that wants to act as a PID source or PID output object. These Interfaces define the contract between the PIDController class and any class wanting to act as a PID source data provider or a PID output data consumer. The example Interfaces look like this:

A class implementing the PIDSource Interface must provide a method defined as double pidGet(). That method must return the current input value to be used by the PIDController. Since any class passed into the PIDController constructor for input must implement the PIDSource Interface, the PIDController now knows what method to call on the source object reference variable to get the input value. For instance, the Encoder class implements PIDSource and as such must provide the method pidGet() along with its other methods. When pidGet() is called, the Encoder returns the current tick count. So an Encoder object can be passed to the PIDController constructor. But so can any other class that implements PIDSource. A class implementing an Interface would look like this:

Any class that implements PIDOuput must provide the pidWrite(double value) method. That method when called, will take the passed value and perform some action with it. The PIDController knows that any class implementing PIDOutput will have  the pidWrite() method and so it knows how to send the output value it has calculated from the input, by calling the pidWrite method on the output object reference variable. The Motor class implements PIDOutput and provides the pidWrite(double value) method and sets it's motor power from the value. So a Motor object can be passed to the PIDController constructor as an output object. But so can any other class that implements PIDOutput.

Classes can implement more than one Interface along with any other fields or methods they wish. Interfaces are very powerful and allow a class expecting an Interface (like PIDController) to work with any number of other classes that implement that Interface.

Note that Interfaces only define the expected fields and methods to be exposed by the implementing class. The actual implementation (code) is contained in the class implementing the Interface.

Like many things in Java (and other languages), this is a basic introduction to Interfaces. There is a lot more to Interfaces which you can read about here. This lesson should be enough to understand Interfaces and use them in your robot code.

 

Navigation:

Interfaces

Just as a class is a description of an object, an Interface is a description of a class. An Interface defines the fields (variables) and methods that a class must provide (public access) to users of the class. The Interface does not define how a class derives the value of a field or the result of a method, only that a class that implements an Interface must expose that Interface's fields and methods. As such, the Interface defines a public facing API that the class provides.

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 RoboRio 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.

Overview: 
Explore the use of logging to debug robot programs.
Objectives: 

Understand logging and how to implement it in RoboRio based robot control 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 RoboRio 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.

Note that the FRC control system supports using System.out.println to write information to the RioLog and to the session log on the Driver Station. However these two ways to log information have drawbacks. The RioLog does not persist and is not available during match play. The DS log also contains extensive logging from the control system and it can be hard to separate out your logged information. Niether form of logging provides automatic identification of where in your program the logged information comes from. The logging code provided here automatically marks all logged information with the date, time, name of method and location in your source file where the information was logged. Only your logged information is recorded. The information is recorded in a new disk file for each startup of the RoboRio and can be easily downloaded after sessions or matches for review. Note that the logged information is also sent to the RioLog and the DS log.

To use the Logging class below with the Sample robot program, create a new class called Logging in the same place as Robot.java. Copy and paste the code into that class.

Now you can modify the Sample program as follows to use logging instead of System.out.println():

You can view the log file on the RoboRio disk with a browser. Navigate to ftp://roborio-nnnn-frc.local/home/lvuser and select the desired log file. You can download the log file with Windows Explorer. Navigate to the url <to be inserted> and copy the desired log file to a local directory.

Note: Log files are numbered 0-99. After 99 no new log files will be created and an error will be logged to the DS console window. You must use Windows Explorer to delete old log files.

 

Navigation:

Version 2: What is Programming?

Version 2: Intro to Pneumatics

Version 2: Intro to Sensors

Version 2: Sample Robot in more Detail

Pages