-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrive.java
77 lines (63 loc) · 3.59 KB
/
Drive.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.util.ElapsedTime;
public class Drive {
static final double COUNTS_PER_MOTOR_REV = 537.6 ;
static final double DRIVE_GEAR_REDUCTION = 1.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);
/*
* 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 static void encoder(double speed, double distance, double timeoutS, LinearOpMode opmode, RRBotHardware robot) {
ElapsedTime runtime = new ElapsedTime();
runtime.reset();
// Ensure that the opmode is still active
if (opmode.opModeIsActive()) {
// Determine new target position, and pass to motor controller
int newRightTarget = robot.frontRightDrive.getCurrentPosition() + (int) (distance * COUNTS_PER_INCH);
int newLeftTarget = robot.frontLeftDrive.getCurrentPosition() + (int) (distance * COUNTS_PER_INCH);
if (distance > 0) {
robot.frontRightDrive.setPower(speed);
robot.frontLeftDrive.setPower(speed);
robot.rearRightDrive.setPower(speed);
robot.rearLeftDrive.setPower(speed);
while (opmode.opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(robot.frontRightDrive.getCurrentPosition() < newRightTarget && robot.frontLeftDrive.getCurrentPosition() < newLeftTarget)) {
// Display it for the driver.
opmode.telemetry.addData("Encoder Target", "%7d :%7d", newRightTarget, newLeftTarget);
opmode.telemetry.addData("Current Encoder", "%7d :%7d",
robot.frontRightDrive.getCurrentPosition(),
robot.frontLeftDrive.getCurrentPosition());
opmode.telemetry.update();
}
} else {
robot.frontRightDrive.setPower(-speed);
robot.frontLeftDrive.setPower(-speed);
robot.rearRightDrive.setPower(-speed);
robot.rearLeftDrive.setPower(-speed);
while (opmode.opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(robot.frontRightDrive.getCurrentPosition() > newRightTarget && robot.frontLeftDrive.getCurrentPosition() > newLeftTarget)) {
// Display it for the driver.
opmode.telemetry.addData("Encoder Target", "%7d :%7d", newRightTarget, newLeftTarget);
opmode.telemetry.addData("Current Encoder", "%7d :%7d",
robot.frontRightDrive.getCurrentPosition(),
robot.frontLeftDrive.getCurrentPosition());
opmode.telemetry.update();
}
}
robot.frontRightDrive.setPower(0.0);
robot.frontLeftDrive.setPower(0.0);
robot.rearRightDrive.setPower(0.0);
robot.rearLeftDrive.setPower(0.0);
// sleep(250); // optional pause after each move
}
}
}