Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void drive (char direction, double power, int inches, int angle) {
- // Start encoder position at 0
- robot.resetDriveEncoders();
- robot.runWithEncoders();
- setBrake();
- // For calculating loop times
- ElapsedTime looptime = new ElapsedTime();
- double startLoop;
- double endLoop;
- // Drive power will not be less than MIN_DRIVE or more than MAX_DRIVE
- final double MAX_DRIVE = power;
- final double MIN_DRIVE = 0.1;
- int totalDistance = robot.inchesToTicks(inches);
- double accPhase = totalDistance / 3;
- int offset; // error calculation
- double MAX_ACC = (0.5 - 0.1) / (50/4); // still testing good acceleration
- double topSpeed = (totalDistance/3)*MAX_ACC + MIN_DRIVE;
- double decSlope = (-topSpeed / (2*totalDistance/3));
- int currentPosition; // current position of motor used for trip looping
- int currentDrive; // currentposition minus the starting position
- // will slow down near the end for long distance, high speed travels
- boolean longDistance = false;
- if (inches >= 40)
- {
- longDistance = true;
- }
- double startAngle = angle; // angle to drive
- double currentAngle = robot.getCurrentAngle();
- telemetry.addData("Info", "Angle to turn " + angle + " current angle " + currentAngle);
- telemetry.update();
- int angleTurn = (int) (angle - currentAngle);
- if (angleTurn > 10)
- {
- if (angleTurn > 0)
- {
- turn(1, 0.25, angleTurn);
- telemetry.addData("Turn", "turning left " + angleTurn);
- telemetry.update();
- }
- else if (angleTurn < 0)
- {
- turn(2, 0.25, angleTurn);
- telemetry.addData("Turn", "turning right " + angleTurn);
- telemetry.update();
- }
- }
- robot.currentAngle = startAngle; // setting angle back to target angle
- telemetry.addData("Angle: ", "Start Angle: " + startAngle);
- telemetry.update();
- RobotCore.frontLeftBeforeTravel = RobotCore.frontLeftDrive.getCurrentPosition();
- RobotCore.frontRightBeforeTravel = RobotCore.frontRightDrive.getCurrentPosition();
- RobotCore.backLeftBeforeTravel = RobotCore.backLeftDrive.getCurrentPosition();
- RobotCore.backRightBeforeTravel = RobotCore.backRightDrive.getCurrentPosition();
- RobotCore.frontLeftCurrentPosition = 0;
- RobotCore.frontRightCurrentPosition = 0;
- RobotCore.backLeftCurrentPosition = 0;
- RobotCore.backRightCurrentPosition = 0;
- currentPosition = RobotCore.frontLeftBeforeTravel;
- currentDrive = 0;
- switch (direction) {
- case 'f':
- case 'F':
- RobotCore.frontLeftTargetPosition = RobotCore.frontLeftBeforeTravel + totalDistance;
- RobotCore.frontRightTargetPosition = RobotCore.frontRightBeforeTravel + totalDistance;
- RobotCore.backLeftTargetPosition = RobotCore.backLeftBeforeTravel + totalDistance;
- RobotCore.backRightTargetPosition = RobotCore.backRightBeforeTravel + totalDistance;
- double target = (RobotCore.frontLeftTargetPosition + RobotCore.frontRightTargetPosition + RobotCore.backLeftTargetPosition + RobotCore.backRightTargetPosition ) / 4;
- looptime.reset();
- while ( (currentPosition < target) && (Math.abs(currentPosition - target) > 50) && opModeIsActive()) {
- startLoop = looptime.milliseconds();
- robot.bulkData = robot.expansionHub.getBulkInputData();
- currentPosition = robot.bulkData.getMotorCurrentPosition(robot.frontLeftDrive);
- currentDrive = Math.abs(currentPosition - robot.frontLeftBeforeTravel);
- if (longDistance)
- {
- if (currentPosition > (3*totalDistance/4))
- {
- robot.frontLeftPower = 0.15;
- robot.frontRightPower = 0.15;
- robot.backLeftPower = 0.15;
- robot.backRightPower = 0.15;
- robot.velocityDrive();
- endLoop = looptime.milliseconds() - startLoop;
- NUM_END_LOOP++;
- AVG_END_LOOP = AVG_END_LOOP + endLoop;
- continue;
- }
- }
- offset = (int) (robot.getCurrentAngle());
- robot.frontLeftCurrentPosition = Math.abs(currentPosition - robot.frontLeftBeforeTravel);
- robot.frontRightCurrentPosition = Math.abs(robot.bulkData.getMotorCurrentPosition(robot.frontRightDrive) - robot.frontRightBeforeTravel);
- robot.backLeftCurrentPosition = Math.abs(robot.bulkData.getMotorCurrentPosition(robot.backLeftDrive) - robot.backLeftBeforeTravel);
- robot.backRightCurrentPosition = Math.abs(robot.bulkData.getMotorCurrentPosition(robot.backRightDrive) - robot.backRightBeforeTravel);
- if (currentDrive < accPhase)
- {
- robot.frontLeftPower = (robot.frontLeftCurrentPosition*MAX_ACC) + MIN_DRIVE;
- robot.frontRightPower = (robot.frontRightCurrentPosition*MAX_ACC) + MIN_DRIVE;
- robot.backLeftPower = (robot.backLeftCurrentPosition*MAX_ACC) + MIN_DRIVE;
- robot.backRightPower = (robot.backRightCurrentPosition*MAX_ACC) + MIN_DRIVE;
- }
- else if (currentDrive > accPhase)
- {
- robot.frontLeftPower = (decSlope*(robot.frontLeftCurrentPosition - totalDistance) + MIN_DRIVE);
- robot.frontRightPower = (decSlope*(robot.frontRightCurrentPosition - totalDistance) + MIN_DRIVE);
- robot.backLeftPower = (decSlope*(robot.backLeftCurrentPosition - totalDistance) + MIN_DRIVE);
- robot.backRightPower = (decSlope*(robot.backRightCurrentPosition - totalDistance) + MIN_DRIVE);
- telemetry.addData("Phase", "Decelerate");
- }
- else
- {
- robot.frontLeftPower = MAX_DRIVE;
- robot.frontRightPower = MAX_DRIVE;
- robot.backLeftPower = MAX_DRIVE;
- robot.backRightPower = MAX_DRIVE;
- telemetry.addData("Phase", "Max");
- }
- robot.frontLeftPower = Range.clip(robot.frontLeftPower, MIN_DRIVE, MAX_DRIVE);
- robot.frontRightPower = Range.clip(robot.frontRightPower, MIN_DRIVE, MAX_DRIVE);
- robot.backLeftPower = Range.clip(robot.backLeftPower, MIN_DRIVE, MAX_DRIVE);
- robot.backRightPower = Range.clip(robot.backRightPower, MIN_DRIVE, MAX_DRIVE);
- robot.frontLeftPower = robot.frontLeftPower + (offset - startAngle) / 100;
- robot.frontRightPower = robot.frontRightPower - (offset - startAngle) / 100;
- robot.backLeftPower = robot.backLeftPower + (offset - startAngle) / 100;
- robot.backRightPower = robot.backRightPower - (offset - startAngle) / 100;
- robot.velocityDrive();
- endLoop = looptime.milliseconds() - startLoop;
- TOTAL_LOOP_TIME = TOTAL_LOOP_TIME + endLoop;
- NUMBER_OF_LOOPS++;
- if (endLoop > 70)
- {
- NUMBER_OF_SLOW_LOOPS++;
- }
- if (endLoop > MAX_LOOP_TIME)
- {
- MAX_LOOP_TIME = endLoop;
- }
- telemetry.addData("Loop time: ", endLoop );
- telemetry.addData("Moving forward", robot.frontLeftPower);
- }
- break;
- case 'b':
- case 'B':
- RobotCore.frontLeftTargetPosition = RobotCore.frontLeftBeforeTravel - totalDistance;
- RobotCore.frontRightTargetPosition = RobotCore.frontRightBeforeTravel - totalDistance;
- RobotCore.backLeftTargetPosition = RobotCore.backLeftBeforeTravel - totalDistance;
- RobotCore.backRightTargetPosition = RobotCore.backRightBeforeTravel - totalDistance;
- target = (RobotCore.frontLeftTargetPosition + RobotCore.frontRightTargetPosition + RobotCore.backLeftTargetPosition + RobotCore.backRightTargetPosition ) / 4;
- looptime.reset();
- while ((currentPosition > target) && (Math.abs(currentPosition - target) > 50) && opModeIsActive()) {
- startLoop = looptime.milliseconds();
- robot.bulkData = robot.expansionHub.getBulkInputData();
- currentPosition = robot.bulkData.getMotorCurrentPosition(robot.frontLeftDrive);
- currentDrive = Math.abs(currentPosition - robot.frontLeftBeforeTravel);
- if (longDistance)
- {
- if (currentDrive > (3*totalDistance/4))
- {
- robot.frontLeftPower = -0.15;
- robot.frontRightPower = -0.15;
- robot.backLeftPower = -0.15;
- robot.backRightPower = -0.15;
- robot.frontLeftDrive.setVelocity(robot.frontLeftPower*2600);
- robot.frontRightDrive.setVelocity(robot.frontRightPower*2600);
- robot.backLeftDrive.setVelocity(robot.backLeftPower*2600);
- robot.backRightDrive.setVelocity(robot.backRightPower*2600);
- endLoop = looptime.milliseconds() - startLoop;
- NUM_END_LOOP++;
- AVG_END_LOOP = AVG_END_LOOP + endLoop;
- continue;
- }
- }
- offset = (int) (robot.getCurrentAngle());
- robot.frontLeftCurrentPosition = Math.abs(currentPosition - robot.frontLeftBeforeTravel);
- robot.frontRightCurrentPosition = Math.abs(robot.bulkData.getMotorCurrentPosition(robot.frontRightDrive) - robot.frontRightBeforeTravel);
- robot.backLeftCurrentPosition = Math.abs(robot.bulkData.getMotorCurrentPosition(robot.backLeftDrive) - robot.backLeftBeforeTravel);
- robot.backRightCurrentPosition = Math.abs(robot.bulkData.getMotorCurrentPosition(robot.backRightDrive) - robot.backRightBeforeTravel);
- if (currentDrive < accPhase)
- {
- robot.frontLeftPower = (robot.frontLeftCurrentPosition*MAX_ACC) + MIN_DRIVE;
- robot.frontRightPower = (robot.frontRightCurrentPosition*MAX_ACC) + MIN_DRIVE;
- robot.backLeftPower = (robot.backLeftCurrentPosition*MAX_ACC) + MIN_DRIVE;
- robot.backRightPower = (robot.backRightCurrentPosition*MAX_ACC) + MIN_DRIVE;
- }
- else if (currentDrive > accPhase)
- {
- robot.frontLeftPower = (decSlope*(robot.frontLeftCurrentPosition - totalDistance) + MIN_DRIVE);
- robot.frontRightPower = (decSlope*(robot.frontRightCurrentPosition - totalDistance) + MIN_DRIVE);
- robot.backLeftPower = (decSlope*(robot.backLeftCurrentPosition - totalDistance) + MIN_DRIVE);
- robot.backRightPower = (decSlope*(robot.backRightCurrentPosition - totalDistance) + MIN_DRIVE);
- telemetry.addData("Phase", "Decelerate");
- }
- else
- {
- robot.frontLeftPower = MAX_DRIVE;
- robot.frontRightPower = MAX_DRIVE;
- robot.backLeftPower = MAX_DRIVE;
- robot.backRightPower = MAX_DRIVE;
- telemetry.addData("Phase", "Max");
- }
- robot.frontLeftPower = Range.clip(robot.frontLeftPower, MIN_DRIVE, MAX_DRIVE);
- robot.frontRightPower = Range.clip(robot.frontRightPower, MIN_DRIVE, MAX_DRIVE);
- robot.backLeftPower = Range.clip(robot.backLeftPower, MIN_DRIVE, MAX_DRIVE);
- robot.backRightPower = Range.clip(robot.backRightPower, MIN_DRIVE, MAX_DRIVE);
- robot.frontLeftPower = -(robot.frontLeftPower - (offset - startAngle) / 100);
- robot.frontRightPower = -(robot.frontRightPower + (offset - startAngle) / 100);
- robot.backLeftPower = -(robot.backLeftPower - (offset - startAngle) / 100);
- robot.backRightPower = -(robot.backRightPower + (offset - startAngle) / 100);
- robot.velocityDrive();
- endLoop = looptime.milliseconds() - startLoop;
- TOTAL_LOOP_TIME = TOTAL_LOOP_TIME + endLoop;
- NUMBER_OF_LOOPS++;
- telemetry.addData("Loop time: ", endLoop );
- telemetry.addData("Moving backward", robot.frontLeftPower);
- telemetry.update();
- }
- break;
- default:
- break;
- }
- robot.drive(0);
- robot.runWithEncoders();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement