OpMode and LinearOpMode
When coding for FTC, there are two different "versions" of organizing and running your code. Both have their advantages — for convenience this page uses OpMode, but you can use whichever one you'd like.
OpMode
OpMode is a much more organized way of running through your code. It requires two methods to run:
public void init() {}— runs once when the driver presses "INIT" on the Driver Stationpublic void loop() {}— runs repeatedly 50 times a second when the driver presses "PLAY"
There are 3 other optional methods:
init_loop()— runs 50x/sec after "INIT" but before "PLAY". Helpful for calibrating motors and servos.start()— runs once after "PLAY"stop()— runs once after "STOP". Helpful for ending processes that need to be terminated.
To create a file using OpMode:
@TeleOp()
public class fileName extends OpMode {
public void init() {
// Your code goes here
}
public void loop() {
// Your code goes here
}
}
You may notice the @TeleOp(). This is crucial — it lets the Driver Station figure out whether your program is a TeleOp or Autonomous program. In other words, a program where a human can use a gamepad and control the robot, or a program where the robot moves on its own to score.
LinearOpMode
LinearOpMode doesn't use multiple methods to differentiate between INIT, START, and STOP. Instead, there is one method and several commands:
public void runOpMode() {}— the only method, requiredwaitForStart()— waits for the driver to press "START" before continuing executionopModeIsActive()— returns true/false if the code is in its "loop" period
@TeleOp()
public class fileName extends LinearOpMode {
// Variables and anything else goes up here
public void runOpMode() {
// Code that would normally go in init() goes here
waitForStart();
while (opModeIsActive()) {
// Code that would normally go in loop() goes here
}
}
}
System.out.print()) and for ensuring that loops aren't infinite.
Imports
When programming for FTC, you will have to import classes and libraries often. If you write a piece of code like:
if (gamepad1.left_stick_y > 1.0) {
motor.setPower(1.0);
}
Android Studio will highlight gamepad1.left_stick_y and throw an error. This is because while you can use gamepad1.left_stick_y, you need to import the proper class. If you hover over it, it will ask if you want to import a class — click import! If you are using OnBotJava, it should automatically import classes and libraries for you.
Telemetry
Telemetry is FTC's System.out.print() equivalent. It is used to send human-readable data to the Driver Station.
The most commonly used methods:
telemetry.addData()— takes two arguments: a caption (like"x: ") and data (any variable or String)telemetry.update()— takes no arguments. Refreshes and resends data to the Driver Station. Required for any telemetry data to show up!
int x = 400;
telemetry.addData("", "hi!");
telemetry.addData("x: ", x);
telemetry.update();
Gamepad Input
A key part of the robot is being able to take in human input! Every team does this via a gamepad. You use the gamepad class to check the values of every button on the gamepad.
WIP — More details coming soon.
Motors
WIP — Coming soon.
Servos
WIP — Coming soon.