OpModes
Bind the controller's buttons to your commands in a teleop OpMode.
- The three commands on each mechanism from Writing Commands, building clean.
- The
robot.armflywheelfield from Mechanisms.
Each way the robot can run is its own class, marked with an annotation.
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 MyTeleop file
Open src/main/java/first/robot/opmode/MyTeleop.java. You created this teleop file in Project Setup. 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 class holds one empty constructor, and this lesson fills it. Edit this file rather than making a new one: a second @Teleop class puts two teleops on the driver station.
An autonomous routine gets its own class, marked @Autonomous. We will work on those 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.
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.
Check your work
Run WPILib: Build Robot Code. Nothing moves until Hardware Simulation, so check the file as well as the build.
You should see
BUILD SUCCESSFULas the last line of the terminal.- One hit when you search the
opmodefolder for@Teleop. - A
whileFalseon 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?