Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.firstinspires.ftc.teamcode.Autonomous;
- import com.disnodeteam.dogecv.CameraViewDisplay;
- import com.disnodeteam.dogecv.DogeCV;
- import com.disnodeteam.dogecv.detectors.roverrukus.GoldAlignDetector;
- 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;
- /*
- * Created by A.J on 1/13/19
- */
- @Autonomous(name="Autodrive Red", group="Pushbot")
- //@Disabled
- public class AutoRed extends LinearOpMode {
- Hardware robot = new Hardware();
- private ElapsedTime runtime = new ElapsedTime();
- private GoldAlignDetector detector;
- private boolean found = false;
- private int location = 3;
- static final double COUNTS_PER_MOTOR_REV = 1440 ; // eg: Motor Encoder
- 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 DRIVE_SPEED = 0.4; //full speed is 1.0
- static final double TURN_SPEED = 0.125;
- @Override
- public void runOpMode() {
- robot.init(hardwareMap);
- // Send telemetry message to signify robot waiting;
- telemetry.addData("Status", "Resetting Encoders");
- telemetry.update();
- // Resets encoders and initialize them zero values out
- robot.LeftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- robot.RightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- robot.LeftMotorback.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- robot.RightMotorback.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- robot.Pulley.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- robot.LeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.RightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.LeftMotorback.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.RightMotorback.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.Pulley.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- // Send telemetry message to indicate successful Encoder reset
- telemetry.addData("Path0", "Starting at %7d :%7d and %7d :%7d",
- robot.LeftMotor.getCurrentPosition(),
- robot.LeftMotorback.getCurrentPosition(),
- robot.RightMotor.getCurrentPosition(),
- robot.RightMotorback.getCurrentPosition());
- telemetry.update();
- waitForStart();
- /* AUTONOMOUS MODE SEQUENCE */
- /* Unhooks itself */
- pulleyControl(1,1,5.625); // moves down to touch the ground 5.6
- encoderDrive(1,-2,-2,2); // move left
- middleWheelControl(1,6,2 ); // move forwards
- encoderDrive(1,-5,-5,5); // move more left
- /* initalizes detector */
- detector = new GoldAlignDetector(); // Create detector
- detector.init(hardwareMap.appContext, CameraViewDisplay.getInstance()); // Initialize it with the app context and camera
- detector.useDefaults(); // Set detector to use default settings
- // Optional tuning
- detector.alignSize = 100; // How wide (in pixels) is the range in which the gold object will be aligned. (Represented by green bars in the preview)
- detector.alignPosOffset = 0; // How far from center frame to offset this alignment zone.
- detector.downscale = 0.4; // How much to downscale the input frames
- detector.areaScoringMethod = DogeCV.AreaScoringMethod.MAX_AREA; // Can also be PERFECT_AREA
- //detector.perfectAreaScorer.perfectArea = 10000; // if using PERFECT_AREA scoring
- detector.maxAreaScorer.weight = 0.005;
- detector.ratioScorer.weight = 5;
- detector.ratioScorer.perfectRatio = 1.0; // Ratio adjustment
- detector.enable(); // Start the detector!
- /* Scans to find cube */
- ElapsedTime holdTimer = new ElapsedTime();
- holdTimer.reset();
- int i = 0;
- while(opModeIsActive() && (holdTimer.time() < 10 || !found || i<=2)) {
- sleep(1000);
- if (detector.isFound()) {
- middleWheelControl(1,15,10);
- found = true;
- location = i;
- telemetry.addData("boolean: ", found);
- telemetry.addData("location ", i);
- telemetry.update();
- detector.disable();
- break;
- }else{
- if(i==2){
- detector.disable();
- break;
- }else{
- if(i==1){
- encoderDrive(1, 9, 9, 7);
- }else{
- encoderDrive(1, 7, 7, 6);
- }
- i++;
- }
- }
- }
- switch (location){
- /* Moves based on where the cube was located */
- case 0: // Left
- encoderDrive(1,5,-5,5); // turn back toward marker square
- encoderDrive(1,6,6,6); // move back towards back
- teamMarkerControl(1,0); // drops teammarker
- teamMarkerControl(1,1); // picks up servo
- encoderDrive(1,-8.5,8.5,6); // turn back toward crater
- middleWheelControl(1,10,8); // moves robot toward wall
- encoderDrive(1,50,50,20); // toward crater
- break;
- case 1: // Middle
- middleWheelControl(1,3,3); // moves forward
- encoderDrive(1,10,-10,10); // turns
- middleWheelControl(1,-7,5);
- teamMarkerControl(1,0);
- teamMarkerControl(1,1);
- encoderDrive(1,2,-2,2);
- encoderDrive(1,-50,-50,15);
- break;
- case 2: // Right
- encoderDrive(1,11,-11,10); // turns back toward marker square
- encoderDrive(1,5,5,5); // move toward marker square
- teamMarkerControl(1,0);
- teamMarkerControl(1,1);
- middleWheelControl(1,-8,5);
- encoderDrive(1,-45,-45,15); // move more left
- break;
- case 3: // Right for 33.3%
- encoderDrive(1,11,-11,10); // turns back toward marker square
- encoderDrive(1,5,5,5); // move toward marker square
- teamMarkerControl(1,0);
- teamMarkerControl(1,1);
- middleWheelControl(1,-6,5);
- encoderDrive(1,-45,-45,15); // move more left
- break;
- }
- }
- public void encoderDrive(double speed,
- double leftInches, double rightInches,
- double timeoutS) {
- /* Used to move/control the drive train (inches) */
- int newLeftTarget;
- int newRightTarget;
- int newLeftbackTarget;
- int newRightbackTarget;
- if (opModeIsActive()) {
- newLeftTarget = robot.LeftMotor.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
- newLeftbackTarget = robot.LeftMotorback.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
- newRightTarget = robot.RightMotor.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
- newRightbackTarget = robot.RightMotorback.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
- robot.LeftMotor.setTargetPosition(newLeftTarget);
- robot.LeftMotorback.setTargetPosition(newLeftbackTarget);
- robot.RightMotor.setTargetPosition(newRightTarget);
- robot.RightMotorback.setTargetPosition(newRightbackTarget);
- robot.LeftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.LeftMotorback.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.RightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.RightMotorback.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- runtime.reset();
- robot.LeftMotor.setPower(Math.abs(speed));
- robot.LeftMotorback.setPower(Math.abs(speed));
- robot.RightMotor.setPower(Math.abs(speed));
- robot.RightMotorback.setPower(Math.abs(speed));
- while (opModeIsActive() &&
- (runtime.seconds() < timeoutS) &&
- ((robot.LeftMotor.isBusy() && robot.LeftMotorback.isBusy()) && (robot.RightMotor.isBusy() && robot.RightMotorback.isBusy()))) {
- // Display it for the driver.
- telemetry.addData("Path1", "Running to %7d :%7d", newLeftTarget, newRightTarget);
- telemetry.addData("Path2", "Running at %7d :%7d",
- robot.LeftMotorback.getCurrentPosition(),
- robot.RightMotor.getCurrentPosition());
- telemetry.update();
- }
- robot.LeftMotor.setPower(0);
- robot.LeftMotorback.setPower(0);
- robot.RightMotor.setPower(0);
- robot.RightMotorback.setPower(0);
- robot.LeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.LeftMotorback.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.RightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.RightMotorback.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- sleep(150); // optional pause after each move
- }
- }
- public void teamMarkerControl(double holdTime, double position){
- /* Method to move/control the marker servo */
- ElapsedTime holdTimerServo = new ElapsedTime();
- holdTimerServo.reset();
- while(opModeIsActive() && holdTimerServo.time() < holdTime){
- robot.teamMarker.setPosition(position);
- }
- }
- public void pulleyControl(double speed,
- double downinches, double timeoutS) {
- /*
- Used to move the pulley motor(inches)
- */
- int Target;
- if (opModeIsActive()) {
- Target = robot.Pulley.getCurrentPosition() + (int)(downinches * COUNTS_PER_INCH);
- robot.Pulley.setTargetPosition(Target);
- robot.Pulley.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- runtime.reset();
- robot.Pulley.setPower(Math.abs(speed));
- while (opModeIsActive() &&
- (runtime.seconds() < timeoutS) &&
- (robot.Pulley.isBusy())) {
- //Display it for the driver.
- telemetry.addData("Path1", "Running to %7d", Target);
- telemetry.addData("Path2", "Running at %7d",
- robot.Pulley.getCurrentPosition());
- telemetry.update();
- }
- robot.Pulley.setPower(0);
- robot.Pulley.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- }
- }
- public void middleWheelControl(double speed,
- double inches, double timeoutS) {
- /*
- Used to move the pulley motor(inches)
- */
- int Target;
- if (opModeIsActive()) {
- Target = robot.MiddleWheel.getCurrentPosition() + (int)(inches * COUNTS_PER_INCH);
- robot.MiddleWheel.setTargetPosition(Target);
- robot.MiddleWheel.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- runtime.reset();
- robot.MiddleWheel.setPower(Math.abs(speed));
- while (opModeIsActive() &&
- (runtime.seconds() < timeoutS) &&
- (robot.MiddleWheel.isBusy())) {
- //Display it for the driver.
- telemetry.addData("Path1", "Running to %7d", Target);
- telemetry.addData("Path2", "Running at %7d",
- robot.MiddleWheel.getCurrentPosition());
- telemetry.update();
- }
- robot.MiddleWheel.setPower(0);
- robot.MiddleWheel.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- sleep(100);
- }
- }
- public void findCube(double timeS){
- /*
- * Scans 135 degrees +- to find a yellow cube while opmode is on.
- * Checks from left to right then right to left if still not found.
- */
- ElapsedTime holdTimer = new ElapsedTime();
- holdTimer.reset();
- while(opModeIsActive() && (holdTimer.time() < timeS || found)) {
- for (int i = 0; i <= 2 || found; i++) {
- if (detector.isFound() && detector.getAligned()) {
- encoderDrive(.5, 1, 1, 1);
- found = true;
- location = i;
- } else {
- encoderDrive(.5, 1, -1, 1);
- sleep(250); // Just to make sure it turns fully before checking again
- }
- }
- if (!found) {
- for (int i = 2; i >= 0 || found; i--) {
- if (detector.isFound() && detector.getAligned()) {
- encoderDrive(.5, 1, 1, 1);
- found = true;
- location = i;
- } else {
- encoderDrive(.7, -1, 1, 1);
- sleep(250);
- }
- }
- }
- }
- telemetry.addData("Location found: ", location);
- telemetry.update();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement