Getting Started
Alright, let's create our first TeleOp. First, create a new file. We will name it "MyFirstTeleOp""
- In Android Studio, right-click into the
org.firstinspires.ftc.teamcodefolder, clickNew > Java Classand name the file. - In OnBot, click the
+, name the file, and clickNot an OpMode.
Now, delete all code except the first line, which should be package org.firstinspires.ftc.teamcode;
Below this line, type this in:
@TeleOp(name="MyFirstTeleOp", group="Linear OpMode")
What does that do?
This line tells the Robot that this OpMode is a TeleOp. The name is set to "MyFirstTeleOp", and the group is "Linear OpMode".
You can change these, they don't break any of your code. This is how your OpMode is displayed in the Driver Station. The name is what is shown as the title there, and the group groups multiple OpModes visually.
Creating the LinearOpMode
We are going to create this TeleOp as a LinearOpMode, which is easier than the alternative Iterative. There are use cases for each type, but we are going to start off with Linear. We will describe the differences in an upcoming lesson.
Now, let's write:
public class MyFirstTeleOp extends LinearOpMode {
// this space is here because this is where you will define your motors
}
We have just created a Java class. "But didn't we already do this? Yes, we did. We created the file, but it's this line that finalizes the creation. The name of the class is MyFirstTeleOp, which MUST be the same as the file name.
We extend the LinearOpMode class, so we can use the variables and functions available to us in Linear OpModes.
Setting up motors
Now, let's create some variables. These variables will be used to identify our motors in the code. We will also create a runtime variable, based on the ElapsedTime object. And on top of that, a servo for a claw.
@TeleOp(name="MyFirstTeleOp", group="Linear Opmode")
public class MyFirstTeleOp extends LinearOpMode {
private ElapsedTime runtime = new ElapsedTime(); // this makes a timer variable
private DcMotor leftDrive;
private DcMotor rightDrive;
private DcMotor armMotor;
private Servo clawServo;
// note that all motors and the servo aren't equal to anything(null), which basically means they don't do anything quite yet
}
Imports
You should notice by now that as you write, these imports (or similar) have been created at the top of the file:
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
Each one of these lines recieves the code we need to write our OpMode. As you write, these packages have been added automatically. If they don't, you will get an alert to import them.
Initializing our robot
Initialization is critical to code, even though the robot does not do anything yet. What initialization does is tell what motors should do based on the port and the wiring that is on the robot. In order to set up the code to do this, type the following:
@TeleOp(name="MyFirstTeleOp", group="Linear Opmode")
public class MyFirstTeleOp extends LinearOpMode {
private ElapsedTime runtime = new ElapsedTime();
private DcMotor leftDrive;
private DcMotor rightDrive;
private DcMotor armMotor;
private Servo clawServo;
@Override
public void runOpMode() {
// initialization code goes here, executed when INIT is pressed
waitForStart(); // don't run any of the code after, until initialization is over and play/start is pressed
runtime.reset(); // this sets that timer variable we made earlier to 0
}
Programming what the robot will do
Finally, we are now able to start giving our robot instructions on what to do during the match when it receives an input of some sort.
To start out the section of this part of the code, type this:
@TeleOp(name="MyFirstTeleOp", group="Linear Opmode")
public class MyFirstTeleOp extends LinearOpMode {
private ElapsedTime runtime = new ElapsedTime();
private DcMotor leftDrive;
private DcMotor rightDrive;
private DcMotor armMotor;
private Servo clawServo;
@Override
public void runOpMode() {
// initialization code goes here
waitForStart();
runtime.reset();
while (opModeIsActive()) {
// code goes here
}
}
}
You should see this loop that runs while opModeIsActive() is true. This variable becomes true when you hit the play/start button on the driver station.
The code that happens here will be running throughout the TeleOp. Without this, for example, if we hit A on the GamePad once, we wouldn't be able to hit it again.
Notice how there are 3
}at the end of the code. This is very important that you do not have more or less than what is required for the code to function properly. Most errors in code are due to a misplaced brace. Just keep in mind that every time there is an opening brace{, a closing one must match with it.