Lesson: Programming the RoboRio

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

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.

Content: 

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 that is part of the WPILib. 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 VSCode and look in the upper right hand corner for the WPILib W icon and click on it. This will open the list of WPILib commands available. Find the command WPILib: Create New Project and select. Then select from a Template, then Java, then the Sample template. Pick a folder where you want to store your new project or select to have a new folder created for you. Give the project the name Sample Robot and enter your team number. Click Generate to create the new project.

Once the project is created, navigate the project tree to the src\main\java\frc\robot directory so you can see the Robot.java source file. Double click to open the file. This is the sample 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 above and copy it. Then go to VSCode and select all of the code in the Robot.java file. Paste the code you copied from the .txt file into the Robot.java file in VSCode replacing all of the original code. 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 or your  code are done here.

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 WPILib icon (W) upper right. Select the WPILib: Build Robot Code command to start the compile or build process. You can also select the WPILib: Deploy Robot Code command to build the program and 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. You can also click on the 3 dots right of the WPILib icon to see a drop down menu that has the build and deploy commands as menu choices. 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.

 

Navigation: