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.
- The three commands on each mechanism from Writing Commands, building clean.
- The
robot.armflywheelfield 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.
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?