Motor Setup

Initializing Motor Variables

 The first thing we need to do when adding motor functionality to our project, is initializing the variables for our motors. Do so by copying the following lines of code into your project. It should be inside of the BasicOpMode_Iterative class brackets, but before the init or loop functions.

@TeleOp(name="Basic: Iterative OpMode", group="Iterative Opmode")
public class BasicOpMode_Iterative extends OpMode {
    private DcMotor frontLeftMotor = null;
    private DcMotor frontRightMotor = null;
    private DcMotor backLeftMotor = null;
    private DcMotor backRightMotor = null;

    @Override
    public void init() {

 And, of course, the number of the variables that you create, as well as the names of your motors, will vary based on the design of your robot.

Connecting Motor Variables to the Physical Motors

 The second step in adding motor functionality to our robot is connecting our motor variables to the physical motors. This is done through the hardwareMap, which is what the FTC Robot Controller app uses as an interface with the robot. To do this, copy the following lines of code into the init function of your project.

    @Override
    public void init() {
        frontLeftMotor = hardwareMap.get(DcMotor.class, "front_left_motor");
        frontRightMotor = hardwareMap.get(DcMotor.class, "front_right_motor");
        backLeftMotor = hardwareMap.get(DcMotor.class, "back_left_motor");
        backRightMotor = hardwareMap.get(DcMotor.class, "back_right_motor");
    }

 One important thing to note here, is that the purple variable names to the left of the equal signs must match the names of the variables you initialized earlier, and the green strings must match the names of your motors in the robot configuration on the FTC Robot Controller app on your phone.

Additional Setup

 There are a few more things we need to do. First, we need to reverse the directions of the motors on the right side of the robot due to their orientation. Next, we set the RunMode to RUN_WITHOUT_ENCODER. Encoders track the rotations of your motors, and if you would like to use them, use the RunMode RUN_USING_ENCODER instead. Third, we need to set the ZeroPowerBehavior to BRAKE so that our robot can't be pushed around just because the motors aren't moving at the moment. Finally, we set the motor powers to zero for good measure. Copy the following lines of code into your program, after the connection of the motor variables to the physical motors, but before the closing bracket of the init function.

        frontLeftMotor.setDirection(DcMotor.Direction.FORWARD);
        frontRightMotor.setDirection(DcMotor.Direction.REVERSE);
        backLeftMotor.setDirection(DcMotor.Direction.FORWARD);
        backRightMotor.setDirection(DcMotor.Direction.REVERSE);

        frontLeftMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
        frontRightMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
        backLeftMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
        backRightMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);

        frontLeftMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        frontRightMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        backLeftMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        backRightMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);

        frontLeftMotor.setPower(0);
        frontRightMotor.setPower(0);
        backLeftMotor.setPower(0);
        backRightMotor.setPower(0);