Gray Matter
WorkshopOpModes
WPILib 2027 is still in alpha: these pages change as the APIs settle.
LESSON 13

OpModes

Each way the robot can run is its own class, marked with an annotation. Project Setup generated one marked @Teleop. You edit it so the controller's buttons run the commands you just wrote.

Branchmech-2-Commands7 minutes
You’ll need
  • The three commands on each mechanism from Writing Commands, building clean.
  • The robot.armflywheel field from Mechanisms.

What mechanism are you working on?

The lesson below is written for the one you pick. Switch back any time to read it for the other.

The generated MyTeleop

Open src/main/java/first/robot/opmode/MyTeleop.java. The New Project Creator created this teleop file for you. The @Teleop on the class is what puts it on the driver station. If you leave it off, it no longer shows up as an OpMode you can choose.

The generated file holds five empty methods with comments in them: disabledPeriodic, start, periodic, end and close. We do not need them in this project, so you replace the whole class below. They are useful to know for future projects, such as autonomous routines. Edit this file rather than making a new one: a second @Teleop class puts two teleops on the driver station.

MyAuto.java beside it is marked @Autonomous and stays empty for now. We will work on autonomous routines later.

Add the controller

The controller lives in Robot.java, beside the mechanisms, so every OpMode reaches it the same way: robot.driver. Add the import and the field.

src/main/java/first/robot/Robot.java
package first.robot;
 
import first.robot.mechanisms.Arm;
import first.robot.mechanisms.Flywheel;
import org.wpilib.command3.Scheduler;
import org.wpilib.command3.button.CommandXboxController;
import org.wpilib.framework.OpModeRobot;
 
public class Robot extends OpModeRobot {
// The robot's mechanisms. Public so OpModes can use them.
public final Arm arm = new Arm();
public final Flywheel flywheel = new Flywheel();
 
// The driver's controller on port 0. OpModes bind its buttons through robot.driver.
public final CommandXboxController driver = new CommandXboxController(0);
 
// ... the constructor and robotPeriodic, unchanged
}

new CommandXboxController(0) creates an Xbox controller. The 0 is its port on the driver station.

Bind the buttons

Replace everything below the copyright header with this.

src/main/java/first/robot/opmode/MyTeleop.java
package first.robot.opmode;
 
import first.robot.Robot;
import org.wpilib.opmode.PeriodicOpMode;
import org.wpilib.opmode.Teleop;
 
@Teleop(name = "Teleop")
public class MyTeleop extends PeriodicOpMode {
public MyTeleop(Robot robot) {
// Left trigger: push the arm up while held, stop when released.
robot.driver.leftTrigger().whileTrue(robot.arm.runFast()).whileFalse(robot.arm.stop());
}
}
src/main/java/first/robot/opmode/MyTeleop.java
package first.robot.opmode;
 
import first.robot.Robot;
import org.wpilib.opmode.PeriodicOpMode;
import org.wpilib.opmode.Teleop;
 
@Teleop(name = "Teleop")
public class MyTeleop extends PeriodicOpMode {
public MyTeleop(Robot robot) {
// Right trigger: spin fast while held, drop back to the slow voltage when released.
robot
.driver
.rightTrigger()
.whileTrue(robot.flywheel.runFast())
.whileFalse(robot.flywheel.runSlow());
 
// A: spin fast while held, stop when released.
robot.driver.a().whileTrue(robot.flywheel.runFast()).whileFalse(robot.flywheel.stop());
}
}
  • @Teleop(name = "Teleop") is the name in the driver station's list.
  • MyTeleop(Robot robot) hands this OpMode the robot, so it can reach your mechanism and the controller.
  • robot.driver.leftTrigger() is a button on the controller you added to Robot.
  • whileTrue runs a command while the button is held, and cancels it on release.
  • whileFalse runs a command once the button is released.

Check your work

Run WPILib: Build Robot Code. Nothing moves until Hardware Simulation, so check the file as well as the build.

Check

You should see

  • BUILD SUCCESSFUL as the last line of the terminal.
  • One hit when you search the opmode folder for @Teleop.
  • A whileFalse on every binding line.

Check yourself

A binding reads robot.driver.rightTrigger().whileTrue(robot.flywheel.runFast()).whileFalse(robot.flywheel.runSlow()). You release the trigger. What is running a moment later?

Instead of editing MyTeleop, a teammate makes a new TeleopOpMode.java with @Teleop on it and the same bindings. What goes wrong?

Pick an answer for each.