/* Copyright (c) 2017 FIRST. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted (subject to the limitations in the disclaimer below) provided that * the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or * other materials provided with the distribution. * * Neither the name of FIRST nor the names of its contributors may be used to endorse or * promote products derived from this software without specific prior written permission. * * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS * LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 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.hardware.HardwareMap; import com.qualcomm.robotcore.util.ElapsedTime; /** * This file illustrates the concept of driving a path based on encoder counts. * It uses the common Pushbot hardware class to define the drive on the robot. * The code is structured as a LinearOpMode * * The code REQUIRES that you DO have encoders on the wheels, * otherwise you would use: PushbotAutoDriveByTime; * * This code ALSO requires that the drive Motors have been configured such that a positive * power command moves them forwards, and causes the encoders to count UP. * * The desired path in this example is: * - Drive forward for 48 inches * - Spin right for 12 Inches * - Drive Backwards for 24 inches * - Stop and close the claw. * * The code is written using a method called: encoderDrive(speed, leftInches, rightInches, timeoutS) * that performs the actual movement. * This methods assumes that each movement is relative to the last stopping place. * There are other ways to perform encoder based moves, but this method is probably the simplest. * This code uses the RUN_TO_POSITION mode to enable the Motor controllers to generate the run profile * * Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name. * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list */ @Autonomous(name="14657 Auto Drive - Crater", group="OpMode") //Disabled public class AutoDrive14657_Crater extends LinearOpMode { /* Declare OpMode members. */ //HardwarePushbot robot = new HardwarePushbot(); // Use a Pushbot's hardware /* Public OpModleftDrive = null; public DcMotor rightDrivee members. */ private ElapsedTime runtime = new ElapsedTime(); private DcMotor bLeftMotor = null; private DcMotor bRightMotor = null; private DcMotor fLeftMotor = null; private DcMotor fRightMotor = null; private DcMotor mArm = null; private DcMotor hookArm = null; // private CRServo mServo = null; // private CRServo extendServo = null; // private CRServo collectorFlip = null; private DcMotor mArm2 = null; HardwareMap hwMap = null; public static final double MID_SERVO = 0.5 ; public static final double ARM_UP_POWER = 0.45 ; public static final double ARM_DOWN_POWER = -0.45 ; static final double COUNTS_PER_MOTOR_REV = 200 ; //static final double COUNTS_PER_MOTOR_REV = 1440 ; // eg: TETRIX Motor Encoder // change this value static final double COUNTS_PER_HOOKARM_REV = 400 ; static final double DRIVE_GEAR_REDUCTION = 2.0 ; // This is < 1.0 if geared UP static final double WHEEL_DIAMETER_INCHES = 4.0 ; // For figuring circumference static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) / (WHEEL_DIAMETER_INCHES * 3.1415); static final double COUNTS_PER_INCH_HOOKARM = (COUNTS_PER_HOOKARM_REV * DRIVE_GEAR_REDUCTION) / (WHEEL_DIAMETER_INCHES * 3.1415); static final double DRIVE_SPEED = 0.04; static final double TURN_SPEED = 0.05; /* Initialize standard Hardware interfaces */ public void init(HardwareMap ahwMap) { // Save reference to Hardware map hwMap = ahwMap; // Define and Initialize Motors bLeftMotor = hardwareMap.get(DcMotor.class, "BLeftMotor"); bRightMotor = hardwareMap.get(DcMotor.class, "BRightMotors"); fLeftMotor = hardwareMap.get(DcMotor.class, "FLeftMotor"); fRightMotor = hardwareMap.get(DcMotor.class, "FRightMotor"); mArm = hardwareMap.get(DcMotor.class,"MineralArm"); hookArm = hardwareMap.get(DcMotor.class, "HookArm"); // mServo = hardwareMap.get(CRServo.class, "MCollect"); // extendServo = hardwareMap.get(CRServo.class, "MExtend"); // collectorFlip = hardwareMap.get(CRServo.class, "RMineral"); mArm2 = hardwareMap.get(DcMotor.class, "MineralArm2"); //bLeftMotor.setDirection(DcMotor.Direction.FORWARD); // Set to REVERSE if using AndyMark motors //bRightMotor.setDirection(DcMotor.Direction.REVERSE);// Set to FORWARD if using AndyMark motors //fLeftMotorLeftMotor.setDirection(DcMotor.Direction.FORWARD); //bRightMotorightMotorRightMotor.setDirection(DcMotor.Direction.REVERSE); // Set all motors to zero power fLeftMotor.setPower(0); fRightMotor.setPower(0); bRightMotor.setPower(0); bLeftMotor.setPower(0); // lift arm when init // ArmDrive(1,10,5); // Set all motors to run without encoders. // May want to use RUN_USING_ENCODERS if encoders are installed. bLeftMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER); bRightMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER); fRightMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER); fLeftMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER); // hookArm.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE); mArm.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE); mArm2.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE); // Define and initialize ALL installed servos. } @Override public void runOpMode() { /* * Initialize the drive system variables. * The init() method of the hardware class does all the work here */ init(hardwareMap); // Send telemetry message to signify robot waiting; telemetry.addData("Status", "Resetting Encoders"); // telemetry.update(); fLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); fRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); bRightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); bLeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); hookArm.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); fRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER); fLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER); bLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER); bRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER); hookArm.setMode(DcMotor.RunMode.RUN_USING_ENCODER); // Send telemetry message to indicate successful Encoder reset telemetry.addData("Path0", "Starting at %7d :%7d", fRightMotor.getCurrentPosition(), bRightMotor.getCurrentPosition(), fLeftMotor.getCurrentPosition(), bLeftMotor.getCurrentPosition()); telemetry.update(); // Wait for the game to start (driver presses PLAY) waitForStart(); ArmDrive(1,1.5,0.5); sleep(2000); // Move forward 1 inch encoderDrive(DRIVE_SPEED,-1.9, 1.9, -1.9, 1.9, 0.73); // Turn 12 inches to the left encoderDrive(TURN_SPEED,29, 29, 29,29, 1.6); // Move forward 2 to 3 feet encoderDrive(DRIVE_SPEED,-33, 33, -33, 33, 2.2); telemetry.addData("Path", "Complete"); telemetry.update(); } /* * Method to perfmorm a relative move, based on encoder counts. * Encoders are not reset as the move is based on the current position. * Move will stop if any of three conditions occur: * 1) Move gets to the desired position * 2) Move runs out of time * 3) Driver stops the opmode running. */ public void encoderDrive(double speed, double fLeftInches, double fRightInches, double bLeftInches, double bRightInches, double timeoutS) { int newfLeftTarget; int newbLeftTarget; int newfRightTarget; int newbRightTarget; // Ensure that the opmode is still active if (opModeIsActive()) { // Determine new target position, and pass to motor controller newfLeftTarget = fLeftMotor.getCurrentPosition() + (int)(fLeftInches * COUNTS_PER_INCH); newfRightTarget = fRightMotor.getCurrentPosition() + (int)(fRightInches * COUNTS_PER_INCH); newbLeftTarget = bLeftMotor.getCurrentPosition() + (int)(bLeftInches * COUNTS_PER_INCH); newbRightTarget = bRightMotor.getCurrentPosition() + (int)(bRightInches * COUNTS_PER_INCH); fLeftMotor.setTargetPosition(newfLeftTarget); fRightMotor.setTargetPosition(newfRightTarget); bRightMotor.setTargetPosition(newbRightTarget); bLeftMotor.setTargetPosition(newbLeftTarget); // Turn On RUN_TO_POSITION fLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION); fRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION); bLeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION); bRightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION); // reset the timeout time and start motion. runtime.reset(); bRightMotor.setPower(Math.abs(speed)); bLeftMotor.setPower(Math.abs(speed)); fRightMotor.setPower(Math.abs(speed)); fLeftMotor.setPower(Math.abs(speed)); // 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() < timeoutS) && (fLeftMotor.isBusy() && fRightMotor.isBusy() && bLeftMotor.isBusy() && bRightMotor.isBusy())) { // Display it for the driver. telemetry.addData("Path1", "Running to %7d :%7d", newfLeftTarget, newfRightTarget, newbLeftTarget, newbRightTarget); telemetry.addData("Path2", "Running at %7d :%7d", fLeftMotor.getCurrentPosition(), fRightMotor.getCurrentPosition(), bLeftMotor.getCurrentPosition(), bRightMotor.getCurrentPosition()); telemetry.update(); } // Stop all motion; fLeftMotor.setPower(0); fRightMotor.setPower(0); bLeftMotor.setPower(0); bRightMotor.setPower(0); // Turn off RUN_TO_POSITION fLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER); fRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER); bLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER); bRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER); // sleep(250); // optional pause after each move } } public void ArmDrive(double speed, double armInches, double timeoutS) { int newArmInches; telemetry.addData("opModeIsActive: ", opModeIsActive()); telemetry.update(); // sleep(5000); // Ensure that the opmode is still active if (opModeIsActive()) { // Determine new target position, and pass to motor controller newArmInches = hookArm.getCurrentPosition() + (int)(armInches * COUNTS_PER_INCH_HOOKARM); telemetry.addData("newArmInches: ", newArmInches); telemetry.update(); telemetry.addData("getCurrentPosition: ", hookArm.getCurrentPosition()); telemetry.update(); // sleep(5000); hookArm.setTargetPosition(newArmInches); // Turn On RUN_TO_POSITION hookArm.setMode(DcMotor.RunMode.RUN_TO_POSITION); // reset the timeout time and start motion. runtime.reset(); // hookArm.setPower(Math.abs(speed)); hookArm.setPower(speed); // 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() < timeoutS) && (hookArm.isBusy())) { // while ((runtime.seconds() < timeoutS) && // (hookArm.isBusy())) { // Display it for the driver. telemetry.addData("Path1", newArmInches); telemetry.addData("Path2", hookArm.getCurrentPosition() ); telemetry.update(); } // Stop all motion; hookArm.setPower(0); // Turn off RUN_TO_POSITION hookArm.setMode(DcMotor.RunMode.RUN_USING_ENCODER); telemetry.addData("End of ArmDrive", newArmInches); telemetry.update(); // sleep(5000); } } }