FANDOM


All FRC Java programs are based on the WPILib Java library, which allows students to interact with the hardware on their robot via Java programs. This training will focus on the commonly used classes and methods from WPILib as a means to teach the basics of robot programming in Java. However, WPILib is a very large library with many tools that are only useful in special cases (and many we haven't even discovered), so keep in mind that this training will only scratch the surface of what WPILib can be used for.

FIRST releases a new version of WPILib every year. This training is written for the 2016 edition, but since core functionality tends to remain the same, this training may not become obsolete for a few years.

The Structure of an FRC Program

The Saline Singularity has historically used the IterativeRobot type of robot code, so this training will teach how IterativeRobot works. In other words, if you want to use the Command-Based Robot (which has a number of benefits but we haven't tried it yet), you're on your own.

Keep in mind that there is no main() method when programming in FRC. Instead, the following types of methods are called by the robot, so they are the starting points for all code.

There are three modes that the robot can be run in, each of which have separate code:

  • Autonomous Mode: The robot runs in this mode at the beginning of each game (15 seconds in most years). The robot will not receive any input from controllers during this period.
  • Teleoperated Mode: This is the main mode that makes up the vast majority of an FRC match. The robot will be operated by the control of the drivers via the controllers. It occurs after autonomous mode.
  • Test Mode: This mode is never called during the game. It is useful for testing functionality, calibrating controls, preparing the robot for the game, and other for other operations that will be used outside of the game.
  • Disabled Mode: Robot actuators will not function in this mode; however, code can still be executed.

There are two main types of methods that will be called in Robot.java - Init() methods and Periodic() methods.

  • Init() methods are called once when a mode (teleop, autonomous, etc.) is entered. This is where you should put any setup code for a mode, such as setting initial variable values. Init methods are called before Periodic methods.
  • Periodic() methods are the ones we use the most. They are called during their respective mode over and over again -- each time the method completes, it is called again. All code that controls the robot goes here.

There is also a special method called robotInit() that is called whenever the robot is turned on. It is also called when the RoboRIO is restarted via DriverStation. All initial setup code that must be called before the robot does anything goes here.

Example Methods

disabledPeriodic() {} //Called repeatedly when the robot is disabled
autonomousInit() {} //Called once when the robot enters autonomous

Using Variables in Robot.java

When working with variables in Robot.java, most variables and objects must be visible to all methods in the class. In programming terms, we are saying that these variables and objects must have the correct scope. To do this, the variables must be declared as class fields and then defined in robotInit() or another init method.

Here's an example of code that wouldn't work:

package org.usfirst.frc.team5066.robot;
 
public class Robot extends IterativeRobot {
    public void robotInit()
        String message = "Welcome to the Saline Singularity!" // The variable is declared and defined in one line
    }
 
    public void teleopPeriodic() {
        //Sends a message to the DriverStation Console
        DriverStation.reportWarning(message, false);
    }
}

This code will not work because, since message is declared in robotInit(), it does not exist outside of robotInit(). This is what programmers refer to as the "scope" of the variable.

To fix this problem, we must simply move the variable declaration to the beginning of the java class. The variable will then be considered a "field" because it is visible to the entire class. Here is the code that will work:

package org.usfirst.frc.team5066.robot;
 
public class Robot extends IterativeRobot {
    String message; // The variable is declared
 
    public void robotInit()
        message = "Welcome to the Saline Singularity!" //The variable is defined
    }
 
    public void teleopPeriodic() {
        //Sends a message to the DriverStation Console
        DriverStation.reportWarning(message, false);
    }
}

When you look at the source code for any of our robot programs, you will scroll through many, many lines of variable declarations before you get to any code that adds functionality because of the nature of scope.

Using the Console

The console is very useful for outputting information during the execution of the program. However, the normal Java console does not work in FRC. Instead, we must use one of a few FRC tools for console functionality, SmartDashboard. The DriverStation console, however, is quite good for warnings and debugging.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.