Motor Control

 In order to control the motor variables that we setup in the Motor Setup portion of this tutorial, we need to start by initiating some useful variables. Copy the following lines of code into the BasicOpMode_Iterative class of your program, but before the init or loop functions.

@TeleOp(name="Basic: Iterative OpMode", group="Iterative Opmode")
public class BasicOpMode_Iterative extends OpMode {
    double leftPower;
    double rightPower;
    double drive;
    double turn;

 Next we'll be utilizing these variables to adjust the motor power based on controller inputs. We'll do this by mapping the controller inputs to the drive and turn variables, using those variables to calculate left and right motor power, and then setting the motor powers to the left and right motor power variables. Copy the following lines of code into the loop function of your program.

    @Override
    public void loop() {
        drive = -gamepad1.left_stick_y;
        turn = gamepad1.right_stick_x;
        leftPower = Range.clip(drive + turn, -1.0, 1.0);
        rightPower = Range.clip(drive - turn, -1.0, 1.0);
        frontLeftMotor.setPower(leftPower);
        frontRightMotor.setPower(rightPower);
        backLeftMotor.setPower(leftPower);
        backRightMotor.setPower(rightPower);

 One thing to note here are that the minus sign before the first gamepad1, is flipping the value that the controller outputs, because controller y values are flipped by default, so this unflips it. Also, all that the Range function is doing is making sure that the output of the drive and turn calculations isn't lower than -1 or higher than 1, as that's the only range of inputs that the motor powers can accept.

 Now we have a fully functional Iterative OpMode!