ms-robotics-resources.github.io

Making Your First TeleOp Program

Page in Progress

This page will walk you through making your first TeleOp program in Andriod Studio. Make sure you have Road-Runner and Andriod studio installed, if you don’t you can go here to install them.

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:

When writing your code, there are a few methods that are required for the robot to run. The first required method is init(), which runs once when you pres “Init” on the Driver Station. The second required method is loop(), which runs 50x a second. There are 3 other methods that are optional, init_loop(),start() and stop(). init_loop() runs 50x a second after you press “Init” and before you press start, which gets added as another button on the Driver Station. start() runs once after you press start, and stop() runs once after you press stop.
Our code will only use the init() and loop() methods for now.

Start of your Program

To start your program, first create a new file Java Class in the teamcode folder. Name it “MyFirstTeleOpProgram”. Everything should start properly setup 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, name it motor. You should get an error and DcMotorEx should be highlighted red. It might also suggest that you import a class. This is normal. If it suggests to import a class, import it!. A new line of code should apear at the top of the file that says import com.qualcomm.robotcore.hardware.DcMotorEx;, thats importing a new type of variable called DcMotorEx, which is good! If you didn’t get the suggestion to import a new class, just copy and paste import com.qualcomm.robotcore.hardware.DcMotorEx; into the code, you can also hover over the red highlighted text to see what the problem is.

Using init()

Next, we are going to add one of our required methods, init()! Go down a line (by pressing “enter”) and type public void init() {, then press enter. Andriod studio should auto complete the { and add another bracket, thats fine. Inside these brackets, we’re going to allow the Driver Station to locate a specific motor from the control hub!
Add the line: motor = hardwareMap.get(DcMotorEx.class,"motor");. This line sets the motor variable we established earlier to a specific motor name and type. Although, the only part we really care about is the "motor" section at the end. In the Driver Station, you configure a motor port to have a name, the name should be what in these quotations! It allows the Driver Station to know which motor to tell to move when the code asks.

This line should give you another error, this time on the hardwareMap.get. It will also ask you to import something which you should do! If it doesn’t ask, hover over with your mouse and import it that way.
At this point your code should look something 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, "motor");
    }

}

Movement!

Finally, let’s get the motor to move! Now we will use the loop() method! Click outside of the } bracket that belonds to the init() method (it’s the curly bracket right below the last line we added). Here add public void loop() { and press enter.
Inside the loop() method, we’re going to create an If/Else statement that will read the gamepad joysticks to move the motor! First write if (gamepad1.left_stick_y > 0) { and press enter. This line is checking to see if the y value of the left joystick on the gamepad is greater than 0, in other words, are you moving the joystick up? Again, you should get an error on the gamepad1, import the class it asks you too :)
Now we will add code to actually move the motor! Inside the if statement, add the line motor.setPower(1.0);. This line is setting the power of the motor variable to 1. But motor variable has been set to equal an actual real motor! Keep in mind, the .setPower() attribute requires a double to work, so make sure you provide a double and not an integer

Now we have an issue though, if you ran this code and moved the joystick, the motor would never stop! This could be dangerous, especially if its on a big robot with way more than just 1 motor! On the same line as the ending } bracket of the if statement, add else { and press enter. This section will run if the above if statement isn’t met (as in, if the gamepad joy stick is less than or equal to 0 since that wouldn’t run the code inside the if statement). Inside the else section, add motor.setPower(0.0); (Notice how even zero’s have to have a “.0” in doubles!). This line sets the motor power to 0 if the joystick isn’t pushed forward.
This should be your 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 tutorial {
    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


Now that your code works, try a few challenges!

Challenges: