Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void encoderDrive(double inches, String direction, double timeoutS, double topPower)
- {
- stopAndReset();
- int TargetFL = 0;
- int TargetFR = 0;
- int TargetBL = 0;
- int TargetBR = 0;
- double errorFL = 0;
- double errorFR = 0;
- double errorBL = 0;
- double errorBR = 0;
- double powerFL = 0;
- double powerFR = 0;
- double powerBL = 0;
- double powerBR = 0;
- String heading = direction;
- // Ensure that the opmode is still active
- if (opModeIsActive()) {
- if(heading == "f")
- {
- TargetFL = robot.fLMotor.getCurrentPosition() + (int)( inches* COUNTS_PER_INCH);
- TargetFR = robot.fRMotor.getCurrentPosition() + (int)( inches* COUNTS_PER_INCH);
- TargetBL = robot.bLMotor.getCurrentPosition() + (int)( inches* COUNTS_PER_INCH);
- TargetBR = robot.bRMotor.getCurrentPosition() + (int)( inches* COUNTS_PER_INCH);
- }
- else if(heading == "b")
- {
- TargetFL = robot.fLMotor.getCurrentPosition() - (int)( inches* COUNTS_PER_INCH);
- TargetFR = robot.fRMotor.getCurrentPosition() - (int)( inches* COUNTS_PER_INCH);
- TargetBL = robot.bLMotor.getCurrentPosition() - (int)( inches* COUNTS_PER_INCH);
- TargetBR = robot.bRMotor.getCurrentPosition() - (int)( inches* COUNTS_PER_INCH);
- }
- else if(heading == "r")
- {
- TargetFL = robot.fLMotor.getCurrentPosition() + (int)( inches* COUNTS_PER_INCH);
- TargetFR = robot.fRMotor.getCurrentPosition() - (int)( inches* COUNTS_PER_INCH);
- TargetBL = robot.bLMotor.getCurrentPosition() - (int)( inches* COUNTS_PER_INCH);
- TargetBR = robot.bRMotor.getCurrentPosition() + (int)( inches* COUNTS_PER_INCH); //weird should be +
- }
- else if(heading == "l")
- {
- TargetFL = robot.fLMotor.getCurrentPosition() - (int)( inches* COUNTS_PER_INCH);
- TargetFR = robot.fRMotor.getCurrentPosition() + (int)( inches* COUNTS_PER_INCH);
- TargetBL = robot.bLMotor.getCurrentPosition() + (int)( inches* COUNTS_PER_INCH); // weird should be +
- TargetBR = robot.bRMotor.getCurrentPosition() - (int)( inches* COUNTS_PER_INCH);
- }
- else
- {
- telemetry.addData("not a valid direction", heading );
- }
- // Determine new target position, and pass to motor controller
- robot.fLMotor.setTargetPosition(TargetFL);
- robot.fRMotor.setTargetPosition(TargetFR);
- robot.bRMotor.setTargetPosition(TargetBR);
- robot.bLMotor.setTargetPosition(TargetBL);
- // Turn On RUN_TO_POSITION
- robot.fLMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.fRMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.bRMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- robot.bLMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- // reset the timeout time and start motion.
- runtime.reset();
- /*robot.fLMotor.setPower(Speed);
- robot.fRMotor.setPower(Speed);
- robot.bRMotor.setPower(Speed);
- robot.bLMotor.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) && ((robot.fLMotor.isBusy() && robot.fRMotor.isBusy()) && robot.bLMotor.isBusy() && robot.bRMotor.isBusy()))
- {
- errorFL = TargetFL - robot.fLMotor.getCurrentPosition();
- errorFR = TargetFR - robot.fRMotor.getCurrentPosition();
- errorBL = TargetBL - robot.bLMotor.getCurrentPosition();
- errorBR = TargetBR - robot.bRMotor.getCurrentPosition();
- powerFL = topPower * pidMultiplierDriving(errorFL);
- powerFR = topPower * pidMultiplierDriving(errorFR);
- powerBL = topPower * pidMultiplierDriving(errorBL);
- powerBR = topPower* pidMultiplierDriving(errorBR);
- robot.fLMotor.setPower(Math.abs(powerFL));
- robot.fRMotor.setPower(Math.abs(powerFR));
- robot.bRMotor.setPower(Math.abs(powerBL));
- robot.bLMotor.setPower(Math.abs(powerBR));
- telemetry.addData("Path1", "Running to %7d :%7d :%7d :%7d", TargetFL, TargetFR, TargetBL, TargetBR);
- telemetry.addData("Path2", "Running at %7d :%7d :%7d :%7d", robot.fLMotor.getCurrentPosition(), robot.fRMotor.getCurrentPosition(), robot.bLMotor.getCurrentPosition(), robot.bRMotor.getCurrentPosition());
- //telemetry.addData("speeds", "Running to %7f :%7f :%7f :%7f", speedfL, speedfR, speedfL, speedbR);
- telemetry.update();
- }
- // Stop all motion;
- robot.fLMotor.setPower(0);
- robot.bLMotor.setPower(0);
- robot.fRMotor.setPower(0);
- robot.bRMotor.setPower(0);
- // Turn off RUN_TO_POSITION
- robot.bRMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.bLMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.fRMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- robot.fLMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- // sleep(250); // optional pause after each move
- }
- stopAndReset();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement