Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.firstinspires.ftc.teamcode;
- import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
- import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
- import com.qualcomm.robotcore.hardware.DcMotor;
- import com.qualcomm.robotcore.util.ElapsedTime;
- import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
- import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
- import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
- import org.opencv.core.Mat;
- @Autonomous
- public class AutoOp_Andrew extends LinearOpMode {
- private HardwareMurderbot robot;
- static {System.loadLibrary("opencv_java3");}
- private ElapsedTime runtime = new ElapsedTime();
- private final double TICKS_PER_MOTOR_REV = 537.6; // AndyMark Motor Encoders
- private final double DRIVE_GEAR_REDUCTION = 1.0; // This is < 1.0 if geared UP -- might have to check this cus official site says gearbox is 20:1
- private final double WHEEL_DIAMETER_INCHES = 4.0; // For figuring circumference
- private final double COUNTS_PER_INCH = (TICKS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) / (WHEEL_DIAMETER_INCHES * Math.PI);
- private final double PINION_POWER = 1.0;
- private final double DRIVE_POWER = 0.5;
- private final double TURN_POWER = 0.1;
- private final double TURN_ERROR = 0.25;
- private double origin = 0;
- private final double ROBOT_WIDTH = 15.125; //inches between two driving wheel centers approx
- @Override
- public void runOpMode() {
- // Initialize
- initMode();
- waitForStart();
- //Autonomous
- // encoderDrive(DRIVE_POWER, 12, 12, 5);
- gyroTurn(TURN_POWER, 180, 5, 5);
- // displaceGoldMineralv2();
- // wallAlign();
- // dropMarker();
- // drive2Crater();
- }
- public void initMode(){
- robot = new HardwareMurderbot(hardwareMap);
- //robot.initCV();
- robot.enableEncoders();
- origin = robot.imu.getAngularOrientation().thirdAngle;
- }
- // Drop from hanging position
- // public void unhangBot(){
- //
- // double inchesDown = 12;
- // int newPos = robot.pinion.getCurrentPosition() + (int)(COUNTS_PER_INCH * inchesDown);
- //
- // robot.pinion.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- //
- // runtime.reset();
- // robot.setPinionPower(PINION_POWER);
- //
- // while (opModeIsActive() && robot.pinion.isBusy()) {
- //
- // // Display it for the driver.
- // telemetry.addData("Target Position", "Running to %7d", newPos);
- // telemetry.addData("Current Position", "Running at %7d", robot.pinion.getCurrentPosition());
- // telemetry.update();
- // }
- //
- // // Stop all motion;
- // robot.setPinionPower(0);
- //
- // // Turn off RUN_TO_POSITION
- // robot.pinion.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- // }
- public void wallAlign(){
- gyroTurn(TURN_POWER, 45, 5, 5);
- encoderDrive(DRIVE_POWER,50, 50, 3.0);
- encoderDrive(DRIVE_POWER, -3, -3, 3.0);
- encoderTurn(TURN_POWER, -90, 5.0);
- }
- public void dropMarker(){
- encoderDrive(DRIVE_POWER, -30, -30, 5.0);
- // robot.teamMarkerDropper.setPosition(1);
- sleep(750);
- }
- public void drive2Crater(){
- encoderDrive(DRIVE_POWER, 55, 55, 5.0);
- encoderTurn(TURN_POWER, -10, 5);
- encoderDrive(DRIVE_POWER, 25, 25, 5.0);
- }
- public void displaceGoldMineralv2(){
- double strafeSpeed = 0.2;
- double STRAFE_BOUND = 12 * COUNTS_PER_INCH;
- double initialPos = robot.leftFrontDrive.getCurrentPosition();
- int refreshTick = 0;
- int scanCount = 0;
- // turn and move away from lander
- encoderStrafe(0.2, 6, 5);
- gyroTurn(TURN_POWER, 90, 5, 5);
- encoderDrive(0.2, 6, 6, 5);
- robot.setDrivePower(-strafeSpeed, strafeSpeed, strafeSpeed, -strafeSpeed);
- while(opModeIsActive() && !robot.cv.getAligned()){
- if(refreshTick != 0) refreshTick--;
- if(robot.cv.isFound()) {
- strafeSpeed /= 2;
- robot.setDrivePower(-strafeSpeed, strafeSpeed, strafeSpeed, -strafeSpeed);
- }
- if(Math.abs(robot.leftFrontDrive.getCurrentPosition() - initialPos) > STRAFE_BOUND && refreshTick == 0){
- strafeSpeed = -strafeSpeed;
- robot.setDrivePower(-strafeSpeed, strafeSpeed, strafeSpeed, -strafeSpeed);
- scanCount++;
- refreshTick = 5000;
- if(scanCount > 2) break;
- }
- }
- robot.setDrivePower(0,0);
- robot.cv.disable();
- encoderDrive(0.3, -26, -26, 5);
- encoderDrive(0.3, 25, 25, 5);
- sleep(100);
- // encoderTurn(TURN_POWER, LANDER_OFFSET, 5);
- }
- public double encoderTurn(double power, double degrees, double timeout){
- double robotSector = Math.PI * (degrees / 180.0);
- double turnInches = 2 * 0.5 * ROBOT_WIDTH * robotSector;
- encoderDrive(power, turnInches, -turnInches, 3);
- return turnInches;
- }
- // Drives the specifed distance (inches) with a timeout at the specified number of seconds at the specified speed using motor encoders (negative distance = reverse movement)
- public void encoderDrive(double power, double leftInches, double rightInches, double timeout) {
- int newLeftFrontTarget;
- int newLeftRearTarget;
- int newRightFrontTarget;
- int newRightRearTarget;
- // Ensure that the opmode is still active
- if (opModeIsActive()) {
- // Determine new target position, and pass to motor controller
- newLeftFrontTarget = robot.leftFrontDrive.getCurrentPosition() + (int) (-leftInches * COUNTS_PER_INCH);
- newLeftRearTarget = robot.leftFrontDrive.getCurrentPosition() + (int) (-leftInches * COUNTS_PER_INCH);
- newRightFrontTarget = robot.rightFrontDrive.getCurrentPosition() + (int) (-rightInches * COUNTS_PER_INCH);
- newRightRearTarget = robot.rightFrontDrive.getCurrentPosition() + (int) (-rightInches * COUNTS_PER_INCH);
- robot.leftFrontDrive.setTargetPosition(newLeftFrontTarget);
- robot.leftRearDrive.setTargetPosition(newLeftRearTarget);
- robot.rightFrontDrive.setTargetPosition(newRightFrontTarget);
- robot.rightRearDrive.setTargetPosition(newRightRearTarget);
- // Turn On RUN_TO_POSITION
- robot.leftFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.leftRearDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.rightFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.rightRearDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- // reset the timeout time and start motion.
- runtime.reset();
- robot.setDrivePower(Math.abs(power), Math.abs(power));
- // keep looping while we are still active, and there is time left, and both motors are running.
- // Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
- // its target position, the motion will stop. This is "safer" in the event that the robot will
- // always end the motion as soon as possible.
- // However, if you require that BOTH motors have finished their moves before the robot continues
- // onto the next step, use (isBusy() || isBusy()) in the loop test.
- while (opModeIsActive() &&
- (runtime.seconds() < timeout) &&
- (robot.leftFrontDrive.isBusy() && robot.rightFrontDrive.isBusy() &&
- robot.leftRearDrive.isBusy() && robot.rightRearDrive.isBusy())) {
- // Display it for the driver.
- telemetry.addData("Path1", "Running to %7d :%7d",
- newLeftFrontTarget, newRightFrontTarget,
- newLeftRearTarget, newRightRearTarget);
- telemetry.addData("Path2", "Running at %7d :%7d",
- robot.leftFrontDrive.getCurrentPosition(),
- robot.leftRearDrive.getCurrentPosition(),
- robot.rightFrontDrive.getCurrentPosition(),
- robot.rightRearDrive.getCurrentPosition());
- telemetry.update();
- }
- // Stop all motion;
- robot.setDrivePower(0, 0);
- // Turn off RUN_TO_POSITION
- robot.leftFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.leftRearDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.rightFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.rightRearDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- }
- }
- // Drives the specifed distance (inches) with a timeout at the specified number of seconds at the specified speed using motor encoders (negative distance = reverse movement)
- public void encoderStrafe(double power, double strafeInches, double timeout) {
- int newLeftFrontTarget;
- int newLeftRearTarget;
- int newRightFrontTarget;
- int newRightRearTarget;
- // Ensure that the opmode is still active
- if (opModeIsActive()) {
- // Determine new target position, and pass to motor controller
- newLeftFrontTarget = robot.leftFrontDrive.getCurrentPosition() + (int) (-strafeInches * COUNTS_PER_INCH);
- newLeftRearTarget = robot.leftFrontDrive.getCurrentPosition() + (int) (strafeInches * COUNTS_PER_INCH);
- newRightFrontTarget = robot.rightFrontDrive.getCurrentPosition() + (int) (strafeInches * COUNTS_PER_INCH);
- newRightRearTarget = robot.rightFrontDrive.getCurrentPosition() + (int) (-strafeInches * COUNTS_PER_INCH);
- robot.leftFrontDrive.setTargetPosition(newLeftFrontTarget);
- robot.leftRearDrive.setTargetPosition(newLeftRearTarget);
- robot.rightFrontDrive.setTargetPosition(newRightFrontTarget);
- robot.rightRearDrive.setTargetPosition(newRightRearTarget);
- // Turn On RUN_TO_POSITION
- robot.leftFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.leftRearDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.rightFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.rightRearDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- // reset the timeout time and start motion.
- runtime.reset();
- robot.setDrivePower(Math.abs(power), Math.abs(power));
- // keep looping while we are still active, and there is time left, and both motors are running.
- // Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
- // its target position, the motion will stop. This is "safer" in the event that the robot will
- // always end the motion as soon as possible.
- // However, if you require that BOTH motors have finished their moves before the robot continues
- // onto the next step, use (isBusy() || isBusy()) in the loop test.
- while (opModeIsActive() &&
- (runtime.seconds() < timeout) &&
- (robot.leftFrontDrive.isBusy() && robot.rightFrontDrive.isBusy() &&
- robot.leftRearDrive.isBusy() && robot.rightRearDrive.isBusy())) {
- // Display it for the driver.
- telemetry.addData("Path1", "Running to %7d :%7d",
- newLeftFrontTarget, newRightFrontTarget,
- newLeftRearTarget, newRightRearTarget);
- telemetry.addData("Path2", "Running at %7d :%7d",
- robot.leftFrontDrive.getCurrentPosition(),
- robot.leftRearDrive.getCurrentPosition(),
- robot.rightFrontDrive.getCurrentPosition(),
- robot.rightRearDrive.getCurrentPosition());
- telemetry.update();
- }
- // Stop all motion;
- robot.setDrivePower(0, 0);
- // Turn off RUN_TO_POSITION
- robot.leftFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.leftRearDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.rightFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.rightRearDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- }
- }
- // Turn using gyro
- public void gyroTurn(double power, double degrees, double timeout, int sleep) {
- origin = robot.imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.XYZ, AngleUnit.DEGREES).thirdAngle;
- double target = origin + degrees;
- // make target between -180 exclusive and 180 inclusive
- target = (target - 180) % 360 + 180;
- double heading = origin;
- double leftPower = power;
- double rightPower = power;
- double slowFactor = 1; // power = power / slowFactor
- runtime.reset();
- // set power -- need to check direction
- if((degrees - 180) % 360 + 180 >= 0) leftPower = -leftPower;
- else rightPower = -rightPower;
- while(opModeIsActive() && runtime.seconds() < timeout && Math.abs(heading - target) < TURN_ERROR) {
- if (slowFactor < 2 && Math.abs(heading - target) < 20) {
- leftPower /= 2;
- rightPower /= 2;
- slowFactor = 2;
- }
- else if (slowFactor < 4 && Math.abs(heading - target) < 10) {
- leftPower /= 2;
- rightPower /= 2;
- slowFactor = 4;
- }
- robot.setDrivePower(leftPower, rightPower);
- heading = robot.imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.XYZ, AngleUnit.DEGREES).thirdAngle;
- telemetry.addData("Heading", heading);
- telemetry.addData("Target", target);
- telemetry.addData("Origin", origin);
- telemetry.addData("Left", leftPower);
- telemetry.addData("Right", rightPower);
- telemetry.update();
- }
- robot.setDrivePower(0, 0);
- origin = robot.imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.XYZ, AngleUnit.DEGREES).thirdAngle;
- if (sleep > 0) sleep((long) sleep);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement