Lesson: Programming the RoboRio

Explore the details of using Java to program the RoboRio control system.

Understand the different programming models used with the RoboRio control system. Understand the model we will be using in the following lessons. Understand how to create a new Java project for the RoboRio and how to compile and deploy it to the RoboRio.


RoboRio based robots can be programmed with one of three models or styles of program design. These are:

  • Iterative
  • Command
  • Sample

You can read a discussion of the three models here, but for our lessons we are going to use the Sample (also called Simple) model. It is our belief that this is the easiest model to start with.

All of the models use the idea of extending a base class. This is similar to the FTC/Tetrix model. Your program has no main method, the main method is part of the WPILib program that runs on the RoboRio and hosts your program. Your program will consist of at least one class that extends the base class of the programming model you are using. Your class will override specific methods in the base class that will be called by the WPILib hosting program to start, stop and select the mode (autonomous/teleop) of your code.

The FRC competition match has two timed periods (or modes), autonomous and operator Control or teleop. In autonomous, your robot must accomplish tasks without any human input. In teleop, your robot is under human control via joysticks or other input devices.

Lets go ahead and create your first RoboRio Java program to see what this looks like. Start Eclipse and open the Window pull down menu. Select Preferences. On the Preferences dialog select WpiLib Preferences. Enter your team number (1111 for our sample) and save. Next open the File pull down menu. Select New, Project, Robot Java Project (under WPILib Robot Java Development). This will start a new project dialog. In the next screen give the project the name "Sample Robot" and select the Sample Robot model. Click next to create the project.

Once the project is created, navigate the project tree to the src directory to the package directory and finally to the Robot.java source file. The package is automatically generated by the plugin. Double click to open the file. This is the robot source code created by the WPILib plugin. However, we are not going to use this file as it is. It is useful to have the project created as described above because the project files have a number of customizations for deploying the code to the RoboRio. However, the sample code that is created for you is not what we want to use for our lesson. Here is a basic robot control program:

Select all of the code and copy it. Then go to Eclipse and select all of the code in the Robot.java file below the package statement (keep your package). Paste the code you copied from the .txt file into the Robot.java file in Eclipse replacing all of the original code below the package statement. Now we have the source code for the Robot class (for this lesson) we are using to extend the SampleRobot class from the WPILib.

We can now explore the Sample Robot project source code. As you can see, we create a class called Robot that extends SampleRobot. Our code will override the methods of SampleRobot to implement robot control. We will provide custom implementations of these methods:

  • robotInit()
  • autonomous()
  • operatorControl()
  • disabled()
  • test()

The robotInit() method is called once after the WPILib host has loaded your class and all initializations needed by WPILib are completed. It is here that you can perform any needed setup or initialization of variables or classes. In our simple example, there is nothing that is needed. Note we initialized the hardware items (motors and joystick) in the Robot class constructor. We could have done this in robotInit() as well.

After robotInit(), disabled() is called, because that is the start-up state of our robot. disabled() is called whenever the robot is disabled by the driver station or the field control system. When switching from autonomous mode to teleop() mode, disabled() will be called in between because your robot will be in the disabled state for some period of time between the periods.

When the autonomous period starts or autonomous mode is enabled from the DS, the autonomous() method is called. Your code that executes your autonomous functions will be placed in this method. Your code should remain in this method until your autonomous activities are completed or until the autonomous period ends.

When the operator control or teleop period starts or teleop mode is enabled from the DS, the operatorControl() method is called. Your  code that executes your driver controlled functions will be placed in this method. Your code will remain in this method until the teleop period ends.

The test() method can be initiated from the DS and is used to test your code. We will discuss the test() method later.

Once you have a program ready to compile, click the green arrow button on the toolbar. This will start the compile or build process. If there are no errors, the WPILib plugin will attempt to connect to the RoboRio and download (deploy) your program. A console log will open showing the results of the compile and download. Here is more information on the build/download process.

Note that downloading a program assumes you have connected your PCs wireless network to the robot network created by the robot wireless router. If your program downloads successfully, you can test it with the driver station.

With FLL or FTC robots it is pretty easy to throw together a test robot you can use to test your programming skills. This is not the case with FRC level robots. While your team may have a test robot for use as a testing platform, it may be that you will not be able to test your code until the robot for the current competition season is far enough along to be at least partially operational. There are some simulation tools available and discussed in the on-line documentation but we will not be covering simulation here.

In our next lesson we will discuss the Sample Robot project in more detail.


Lesson navigation: