Java 3: WPILIB

From Deep Blue Robotics Wiki
Jump to: navigation, search

Now that you know the basics of Java, it is time to apply your knowledge to programming robots. In this section, you will learn the basics of robot programming and how to use the development tools provided by FIRST.

We have not finished this page, mostly because there is another perfectly good tutorial on all of this stuff at ScreenSteps Live. For now, just read the following excerpts from the Screensteps Live tutorials, then move on to the next section, Command Based Programming.

FRC Updates

Note that the links in this section will need to be updated every year (current year: 2015).

Similar to how you had to download the necessary software to turn your computer into a programming machine; you now have to perform the final step to transform into a fully-fledged FRC computer.

Here is the ScreenSteps Live page detailing how to install the FRC Update Suite. Once you finish the download you will have the latest version of the Driver Station and all the FRC Utilities, which most notably include the roboRIO Imager used to configure brand new rRIOs.

This section of the Eclipse installation page shows how to get the latest FRC plugins needed to deploy code.

If your roboRIO is brand new then it has to be given the latest image and Java 8 needs to be downloaded on it. The imaging tutorial is here and Java 8 is here.

Iterative Robot

Iterative Robot is a basic template for creating robot projects. It contains five methods: robotInit, teleopInit, teleopPeriodic, autonomousInit, and autonomousPeriodic. The robotInit method is called once when the robot is turned on, the teleopInit method is called once when the teleop (remote operated) period starts, and the autonomousInit method is called once when the autonomous period starts. The teleopPeriodic method is called repeatedly during the teleop period, and the autonomousPeriodic method is called repeatedly during the autonomous period. By putting your code in each of the relevant methods, it is very easy to program basic robot functionality using this template.

TODO: How to create an iterative robot project

TODO: Example code


"WPILib lets you focus on making your robot do whatever it’s supposed to do, and not on the implementation details of devices like camera and gyros." -Brad Miller A Framework for Simplified Competition Robot Programming

WPIlib is a library provided to us by FIRST that we use to interface with the various electronics on the robot and to simplify the writing of the code. It is written by a group of college students attending Worcester Polytechnic Institute led by Brad Miller. (2015 Online Documentation). When you create an FRC Java Project, the WPILib will automatically be set as a required resource. To use classes from the WPILib, just import them from the edu.wpi.first.wpilib package:

  import edu.wpi.first.wpilibj.ClassThatYouWant;

View WPIlib Offline

Many times during competition there will be no wi-fi provided to you which is why FIRST has kindly provided us an offline version of the WPIlib documentation. This file is saved by default at 'C:\Users\username\wpilib\java\current\javadoc' under the name of 'index.html'.

Import WPI Source

While the .html file is a good reference it is sometimes useful to have the actual source code to view in Eclipse*; here is how:

  1. Find the "WPIlib-sources.jar file (most likely in 'C:\Users\username\wpilib\java\current\lib' and unzip it into its own folder.
  2. In Eclipse open up a new java project (alt + shift + n) and select 'Java Project'. Name it something that you will remember as the wpi library and under project layout select 'Use project folder as root for sources and class files'. Click Finish.
  3. Right click on the project you just made, select 'import' and then select General > File System.
  4. Browse for & select the folder you extracted the source to in step 1, in the left window check the box for the folder selected.

Congrats! You now have you very own copy of the WPI source code that you can view without internet access or even having to leave Eclipse.

  • Note: the project will have multiple errors so it won't compile but you will still be able to view it for reference.


Actuators are the parts of the robot that can move. The main type of actuators are motors (although there are others, such as pneumatics), which are controlled by speed controllers. There are a few different types of speed controllers, including Victors, Talons, Jaguars, and VictorSPs. However, for most purposes they can be used interchangeably.

The constructor for a speed controller requires the port that it is plugged into as the parameter:

 Victor motor = new Victor(1); // Creates a Victor in port 1

To set a motor value, use the set method. The motor values range from -1.0 (full reverse), to 1.0 (full forward). To stop a motor, set the motor to zero.

 motor.set(1); // Sets the motor to full forward

Motors also have the option to be safety enabled. When a motor is safety enabled, it will stop moving if it doesn't receive a new value for a long time (i.e. 0.1 seconds). This is useful to prevent accidentally running into things (or people) when there is an error in the code. By default, motors are not safety enabled, so to safety enable a motor, use the following line of code:

 motor.setSafetyEnabled(true); // Sets the motor to be safety enabled


Sensors are components that can collect data (inputs) for the robot. One of the most important types is the Encoder, which measures rotation of a single motor and translates the information into something more useful, like change in distance.

TODO: Like with speed controllers, you have to set up the ports...

To query the distance accumulated under operation, do:


To set this and other values to 0, use:


For more, look at classes that inherit from SensorBase, and have a look at the list of sensors.


To get input values from the driver, create Joystick objects.

The constructor for a joystick requires a port number. You will learn how to set a joystick's port later in the FRC DriverStation section.

 Joystick joystick = new Joystick(1); // Creates a Joystick in port 1

To get a joystick's value, use the getX and getY methods. The joystick's x-axis is left-right, and the y-axis is forward-back. The get methods return a value from -1.0 (forward/left) to 1.0 (back/right).

 motor.set(joystick.getY()); // Sets a motor to the value of the joystick


RobotDrive is a premade class for controlling a robot's drivetrain.

The constructor for a RobotDrive requires all of the drivetrain motors. A RobotDrive can have either 2 or 4 motors.

RobotDrive drive2 = new RobotDrive(leftMotor, rightMotor);
RobotDrive drive4 = new RobotDrive(frontLeftMotor, rearLeftMotor, frontRightMotor, rearRightMotor);

If motors are on the robot facing opposite the direction that the RobotDrive expects, then you must tell the RobotDrive that they are inverted. Note that one side of the drivetrain is automatically considered to be inverted, because that is the most common drivetrain configuration. The motor to invert is a constant in the RobotDrive class, either kFrontLeft, kFrontRight, kRearLeft, or kRearRight. If the robot only has 2 drive motors, use the rear constants.


RobotDrives also contain a setSafetyEnabled method. However, unlike single motors, RobotDrives are safety enabled by default.

In tank drive mode, each joystick controls one side of the drivetrain. In arcade drive mode, one joystick drives the robot forward and backward, while the other joystick turns it left and right. The RobotDrive class has tankDrive and arcadeDrive methods to automatically use these drive modes. Tank drive requires the values for the left and right sides of the drivetrain, while arcade drive requires the forward-back value and the left-right value. The robot drive automatically corrects for the fact that joystick Y-values are negative (ie. -1.0 is forward).

drive.tankDrive(leftJoystick.getY(), rightJoystick.getY());
drive.arcadeDrive(leftJoystick.getY(), rightJoystick.getX());

FRC DriverStation

A program for operating the robot


To begin import the SmartDashboard class into your project (edu.wpi.first.wpilibj.smartdashboard.SmartDashboard). To put a value on the dashboard type in "SmartDashboard.put" followed by 'Number', 'String', or 'Boolean' depending on what you want to display. Notice that the method requires a string called a 'key' in addition to the value; the key acts as the value's name and identifier and will be displayed alongside it. When writing competition code the strings used as keys should follow our coding conventions, meaning they should:

  • be prefixed by the name of the subsystem associated with the value (e.g. "DriveTrain")
  • not contain spaces, tabs, carriage returns, underscores, dashes, colons, or other punctuation or whitespace
  • begin with a capital letter and use CamelCase
    // These methods put values onto SmartDashboard
    SmartDashboard.putNumber("Key", value);
    SmartDashboard.putString("Key", value);
    SmartDashboard.putBoolean("Key", value);
    // These methods read values from SmartDashboard
    double x = SmartDashboard.getNumber("Key");
    double y = SmartDashboard.getNumber("Key", defaultValue);

To change display options of individual key-values, you can change the 'widget' used to display it by checking the Editable item from the View menu. When the SmartDashboard is editable you can right-click and select 'Change-to" to change which widget is being used as well as move and resize the widget. Experiment for yourself to see the different widgets available for each type of value. There are also widgets that can be added without any existing values, like the Camera Viewer widget, by going to the view menu and selecting "Add..." Some widgets also have settings that can be adjusted by right clicking the widget and selecting "Properties..."


The Webdash is the online interface for the RoboRIO. It allows us to access the files system, set passwords, and update firmware on the electronics. To access the Webdash, type "roborio-199-FRC.local" into the address bar of your web browser while connected to the robot.

Next Lesson: Command-Based Programming