Lesson 2

Welcome to Lesson 2!

In this lesson, you will learn about programming motors and how to control them using input.

Motors

Making A New Motor In Teleop

One of the motor classes that we use (the best one!!!) is the DcMotorEx class We declare them at the beginning of the code, inside of the Teleop class, and initialize them in the init() method. A lot of the other code will be explained but make sure to pay attention to how we declare and initialize the motor (“private DcMotorEx” and “hardwareMap”).

@TeleOp(name="Teleop")
public class Teleop extends OpMode { //declaring all of the motors
    private DcMotorEx frontLeft;
    private DcMotorEx frontRight;
    private DcMotorEx backLeft;
    private DcMotorEx backRight;

    public void init() { //initializing all of the motors
        frontLeft = hardwareMap.get(DcMotorEx.class, "frontLeft");
        frontRight = hardwareMap.get(DcMotorEx.class, "frontRight");
        backLeft = hardwareMap.get(DcMotorEx.class, "backLeft");
        backRight = hardwareMap.get(DcMotorEx.class, "backRight");
    }
}

Setting The Direction Of The Motor

Motors have one way they spin, and it’s easy to have motors spinning the wrong way when powered. In order to fix this, we can use a handy method called setDirection(). As seen in the code below, we can set each motor to either “REVERSE” or “FORWARD”. Setting the direction of the motor is usually done in the init() method.

frontLeft.setDirection(DcMotorEx.Direction.REVERSE);
frontRight.setDirection(DcMotorEx.Direction.FORWARD);
backLeft.setDirection(DcMotorEx.Direction.REVERSE);
backRight.setDirection(DcMotorEx.Direction.FORWARD);

You can test the direction your motors by setting the power of each motor to one and seeing which way they spin.

Setting The Power Of The Motor

In order to set a power to the motor, we use the setPower() method. This method takes in a double (decimal) from 0 to 1. A power value of 1 means to spin the motor at max power.

frontLeft.setPower(1);
frontRight.setPower(1);
backLeft.setPower(1);
backRight.setPower(1);

One drawback of using setPower() is that the speed at which the motors rotate is based on the voltage of the battery, so if the battery is low, the motors will spin slowly.

Running Code

In order for the robot to get the code, we need to connect the laptop to the control hub (or you can do something fancy I forgo how to do lol). Once it’s connected, you should be able to run the code from android studio by clicking the green triangle in the top. After it’s done, get the driver hub paired with the robot, select the code you want to run from the driver hub, and press run!

Take Input From Controller

The parent class (or superclass) of your teleop is the OpMode Class (note the “extends OpMode” in code above). The OpMode class has two gamepad variables declared - gamepad1 and gamepad2. The controller you will most likely be using is the one shown below:

Here are the names of the main controls for the gamepad:

ControlCode
Left Stick X Valueleft_stick_x
Left Stick Y Valueleft_stick_y
Right Stick X Valueright_stick_x
Right Stick Y Valueright_stick_y
Left Bumper Buttonleft_bumper
Right Bumper Buttonright_bumper
Left Trigger Buttonleft_trigger
Right Trigger Buttonright_trigger
Dpad Updpad_up
Dpad Downdpad_down
Dpad Leftdpad_left
Dpad Rightdpad_Right
Button Aa
Button Bb
Button Xx
Button Yy

Telemetry

Telemetry is a very useful way to check the robot’s status from the driver hub. Most of the time, it will be used for things like initialization or runtime, but you can also use it to debug code and test code. The two main methods that are used for updating data in Telemetry are addData() and addLine().

telemetry.addData("Status", "Initialized"); //prints on the screen "Status : Initialized"
telemetry.addLine("Running!!!!!") //prints on the screen "Running!!!!!"
telemetry.addLine("Hello World!"); //brings your robot to life and makes it start questioning its identity