How to Write Robot Opmodes - Pattonville-Robotics/7856 GitHub Wiki
Autonomous
For this year's game, all autonomous are named with the form MainAutonomousEndpositionTeamcolor. So when we are on red team and would want to end our position on the center vortex we select MainAutonomousCenterRed. If we wanted to end on the corner vortex then we would select MainAutonomousCornerRed.
Also during autonomous we have an automethods class that stores all of our autonomous methods that we will be using during autonomous.
Steps to create an autonomous:
- Your class must extend LinearOpMode
- Declare your fields right below the class declaration
- Underneath your fields, type in
runOpMode()
and press enter - Inside the
runOpMode
method, callinitialize()
andwaitForStart()
. You may notice thatinitialize()
is not yet defined. We'll fix that in a minute. - Below those calls, write the processes you want the autonomous to do
- At the bottom of your class, create a method called
initialize()
that instantiates all the fields you declared at the top of the class. - Before the class definition, register your autonomous with
@Autonomous(name = "Sample Autonomous", group = OpModeGroups.TESTING)
Sample Autonomous:
@Autonomous(name = "Sample Autonomous", group = OpModeGroups.TESTING)
public class MainAutonomousCenterRed extends LinearOpMode {
private EncoderDrive drive;
@Override
public void runOpMode() throws InterruptedException {
initialize();
waitForStart();
autoMethods.runAutonomousProcess();
while(opModeIsActive()) {
telemetry.update();
idle();
}
public void initialize() {
drive = new EncoderDrive(hardwareMap, this, new RobotParameters.Builder().driveGearRatio(2).encodersEnabled(true).build());
telemetry.addData("init", "Initialized");
}
}
Tele-Op
It is only necessary to have one Tele-Op class. The controls should be designed to fit the desires of the designated driver, because ultimately he or she will be driving the robot in a match.
Steps to create a Tele-Op (Many of these steps are the same for autonomous):
- Your class must extend LinearOpMode
- Declare your fields right below the class declaration
- Underneath your fields, type in
runOpMode()
and press enter - Inside the
runOpMode
method, callinitialize()
andwaitForStart()
. You may notice thatinitialize()
is not yet defined. We'll fix that in a minute. - Then, right under
waitForStart()
, create awhile(opModeIsActive())
loop, and inside calldoLoop()
andidle()
. Again, thedoLoop()
method is not defined either. - At the bottom of your class, create a method called
initialize()
that instantiates all the fields you declared at the top of the class. - Below the
initialize()
method, create adoLoop()
method. This method is responsible for all processes carried out during Tele-Op, which is continuously called in thewhile
loop. It should contain all controls for the robot, such as driving and toggles.
Sample Tele-Op:
@TeleOp(name = "Sample TeleOp", group = OpModeGroups.TESTING) // Registers the OpMode
public class SampleTeleOp extends LinearOpMode {
// Fields
private EncoderDrive drive;
private ToggleableGamePad gamePad1;
@Override
public void runOpMode() throws InterruptedException {
initialize();
waitForStart();
while (opModeIsActive()) { // Runs continuously as long as the opMode is active
doLoop(); // Calls the method below that carries out all TeleOp controls
idle();
}
}
public void initialize() {
drive = new EncoderDrive(hardwareMap, this, new RobotParameters.Builder().driveGearRatio(2).encodersEnabled(true).build());
gamePad1 = new ToggleableGamePad(this.gamepad1);
}
public void doLoop() {
drive.moveFreely(gamepad1.left_stick_y, gamepad1.right_stick_y); // Joystick y-values control driving
// Add any toggle buttons or other controls here
}
}
General Information
Telemetry
In many situations it is useful to print to a console in order to get feedback while the code is running. With telemetry, we can log information to the phone app. The line telemetry.addData("Put your tag here", "Write your message here")
will print a message to the phone whenever it is called in an OpMode.
Gamepad
The gamepad is used to control the robot, and has a variety of buttons, joysticks, and triggers. Here is a list of all the controls on the gamepad and how they are represented in code.
Joysticks
Joysticks return a value between -1 and 1, and can be used with the x and y axes.
gamepad1.left_stick_y
, gamepad1.right_stick_y
, gamepad1.left_stick_x
, gamepad1.right_stick_x
Joysticks can also be pressed down like a button, returning a boolean value.
gamepad1.left_stick_button
, gamepad1.right_stick_button
D-Pad
There are four D-Pad parts, and each counts as a button, which return a boolean value.
gamepad1.dpad_up
, gamepad1.dpad_down
, gamepad1.dpad_left
, gamepad1.dpad_right
Triggers
The triggers are two controls on the top of the controller that each return a value between 0 and 1.
gamepad1.left_trigger
, gamepad1.right_trigger
Buttons
The buttons are the four colored buttons labeled A, B, X, and Y. They each return a boolean value.
gamepad1.a
, gamepad1.b
, gamepad1.x
, gamepad1.y
Bumpers
The bumpers are two buttons on top of the controller next to the triggers that each return a boolean value.
gamepad1.left_bumper
, gamepad1.right_bumper
Start and Back
The start and back buttons are located in the center of the gamepad, and return boolean values.
gamepad1.start
, gamepad1.back
Imports
In the Main-Repository, there will always be some code already imported into each class when it's created. When you use another class, you will need to import it. Our code follows the pattern org.pattonvillerobotics.commoncode
. You can press alt-enter to import it. Or, to automatically import classes, go to Android Studio > Preferences > Editor > General > Auto Import > Check all checkboxes. Once you do this, you won't have to write import statements.