Some Terminology and Structure First
Before we begin, let's go over some basic terminology. I highly suggest you also look at the Code Library as well.
The robot uses two pieces of hardware to interpret and run the code:
- The Driver Station — allows the driver to run and get values from your code
- The Control Hub — actually outputs the code and runs the motors
When writing your code, there are a few methods that are required for the robot to run:
init()— runs once when you press "Init" on the Driver Stationloop()— runs 50x a second
There are 3 other optional methods:
init_loop()— runs 50x/sec after you press "Init" and before you press "Start"start()— runs once after you press "Start"stop()— runs once after you press "Stop"
Our code will only use the init() and loop() methods for now.
Start of Your Program
First, create a new Java Class in the teamcode folder. Name it "MyFirstTeleOpProgram". Everything should start properly set up and your file should look something like this:
package org.firstinspires.ftc.teamcode.IntoTheDeep.TeleOp;
public class MyFirstTeleOpProgram {
}
This program will be moving one motor. To start, create a new variable with the type DcMotorEx and name it motor. You should get an error and DcMotorEx should be highlighted red. It might suggest that you import a class — this is normal! If it suggests it, import it. A new line should appear at the top:
import com.qualcomm.robotcore.hardware.DcMotorEx;
If you didn't get the suggestion, just copy and paste the import line above into your code.
Using init()
Next, we're going to add one of our required methods. Inside of our class, create a new method with the header public void init() {. Inside, we'll allow the Driver Station to locate a specific motor from the control hub!
Add the line:
motor = hardwareMap.get(DcMotorEx.class, "testMotor");
This sets the motor variable to a specific motor name and type. The part we really care about is "testMotor" at the end. In the Driver Station, you'll assign a port on the Control Hub to a specific name. This name should match what you put here, so the Control Hub and Driver Station know which motor to move and which port it's plugged into.
This line should give you another error on hardwareMap.get — import the class if asked! At this point your code should look like this:
package org.firstinspires.ftc.teamcode.IntoTheDeep.TeleOp;
import static org.firstinspires.ftc.robotcore.external
.BlocksOpModeCompanion.hardwareMap;
import com.qualcomm.robotcore.hardware.DcMotorEx;
public class MyFirstTeleOpProgram {
DcMotorEx motor;
public void init() {
motor = hardwareMap.get(DcMotorEx.class, "testMotor");
}
}
Movement!
Finally, let's get the motor to move! Create a new method with the header public void loop() {.
Inside the loop() method, we're going to create an if/else statement that reads the gamepad joysticks to move the motor. First write:
if (gamepad1.left_stick_y > 0) {
This checks if the y value of the left joystick on the gamepad is greater than 0 — in other words, are you moving the joystick up? You should get an error on gamepad1, so import the class!
Now add the line to actually move the motor:
motor.setPower(1.0);
.setPower() attribute requires a double to work, so provide a double and not an integer. Motors have a -1.0 to 1.0 range of power — anything higher or lower just gets clamped to -1.0 or 1.0.
Now we have an issue — if you ran this code and moved the joystick, the motor would never stop! This could be dangerous. Add an else statement that sets the motor power to 0 when the joystick isn't pushed forward:
else {
motor.setPower(0.0);
}
Notice how even zeros need a ".0" for doubles!
Here's the final code:
package org.firstinspires.ftc.teamcode.IntoTheDeep.TeleOp;
import static org.firstinspires.ftc.robotcore.external
.BlocksOpModeCompanion.gamepad1;
import static org.firstinspires.ftc.robotcore.external
.BlocksOpModeCompanion.hardwareMap;
import com.qualcomm.robotcore.hardware.DcMotorEx;
public class MyFirstTeleOpProgram {
DcMotorEx motor;
public void init() {
motor = hardwareMap.get(DcMotorEx.class, "motor");
}
public void loop() {
if (gamepad1.left_stick_y > 0) {
motor.setPower(1.0);
} else {
motor.setPower(0.0);
}
}
}
Uploading and Running Your Code
WIP — This section is coming soon.
Challenges
- Make the motor spin backwards
- Make the motor speed based on how far forward the joystick is pushed. (Hint: You can put variables into
.setPower()) - Add a second motor!