Skip to content

Instantly share code, notes, and snippets.

@battis
Created April 2, 2020 18:58
Show Gist options
  • Save battis/06ba5f30ddd8187b4cafe4303b5f71d6 to your computer and use it in GitHub Desktop.
Save battis/06ba5f30ddd8187b4cafe4303b5f71d6 to your computer and use it in GitHub Desktop.
Enable encoder-supplemented consistency in Tank Drive
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 EncodedTankDrive 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");
/*
* Turn on encoder sensing to provide more consistent speeds across all four motors. The
* control system will now try to match the measured motor speed (using the encoders) to the
* requested motor speed as closely as possible.
*/
leftFront.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
leftRear.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightFront.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightRear.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
/*
* 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