Skip to content

Instantly share code, notes, and snippets.

@battis
Created March 27, 2020 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save battis/718d9bd2ebd430f3b94699574d12a3a5 to your computer and use it in GitHub Desktop.
Save battis/718d9bd2ebd430f3b94699574d12a3a5 to your computer and use it in GitHub Desktop.
A basic tank drive OpMode
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
@TeleOp
public class MyFirstOpMode extends OpMode {
/*
* Declare the four motor objects that we will be controlling throughout this OpMode. They are
* instance variables (a.k.a. data fields) so that _all_ methods can access them and they will
* retain their values between method calls.
*/
private DcMotor
leftFront,
leftRear,
rightFront,
rightRear;
/*
* Initialize the motor objects to the values already stored in the Robot Controller app
* configuration on the phone, so that our DcMotor objects refer to the actual motors connected
* physically to the robot controller phone.
*/
@Override
public void init() {
leftFront = hardwareMap.get(DcMotor.class, "Left Front");
leftRear = hardwareMap.get(DcMotor.class, "Left Rear");
rightFront = hardwareMap.get(DcMotor.class, "Right Front");
rightRear = hardwareMap.get(DcMotor.class, "Right Rear");
/*
* Remember that forward and reverse refer to the motor's rotational direction (generally
* a motor turning forwards will rotate clockwise as you stare end-on at the output shaft,
* but it's _always_ a good idea to test that the wiring polarity didn't get reversed!)
*
* This means that for the motors to move the _robot_ forward, the motors on the left side
* will need to rotate in _their_ reverse direction to be pushing in the same direction as
* the motors on the right side of the robot.
*/
leftFront.setDirection(DcMotorSimple.Direction.REVERSE);
leftRear.setDirection(DcMotorSimple.Direction.REVERSE);
}
@Override
public void loop() {
/*
* We read the current position of the left and right joysticks' Y-axis as a value -1 to 1.
* We think of 1 as being "100% pushed forward" and -1 as "100% pushed backwards" -- 0 is
* when the joystick is centered.
*/
double
leftPower = gamepad1.left_stick_y,
rightPower = gamepad1.right_stick_y;
/*
* We translate the joystick positions directly into motor power, since the motor is
* expecting a power value between -1 and 1, where 1 is "100% power forward" and -1 is
* "100% power in reverse".
*
* Note that, because we reversed the motor directions for the left-side motors in our
* init() method above, we don't have to worry about that here: the left-side motors are
* treating all of our instructions as "opposite day" already.
*/
leftFront.setPower(leftPower);
leftRear.setPower(leftPower);
rightFront.setPower(rightPower);
rightRear.setPower(rightPower);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment