-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChassisAuton.java
executable file
·185 lines (147 loc) · 8.52 KB
/
ChassisAuton.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package org.firstinspires.ftc.teamcode;
import org.firstinspires.ftc.robotcore.external.Telemetry;
import com.qualcomm.robotcore.hardware.DcMotor;
public class ChassisAuton {
//Rev Hex HD Motor 2240 counts per rotation
static final double COUNTS_PER_MOTOR_REV = 2240; // 20 - 537.6, 40 - 1120, 60 - 1680 (cpr) Gear Neverest Motor Encoder
static final double DRIVE_GEAR_REDUCTION = 1; // 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);
HardwarePlatter theHardwarePlatter;
public ChassisAuton(HardwarePlatter hwPlatter) {
theHardwarePlatter = hwPlatter;
theHardwarePlatter.leftFrontDrive.setDirection(DcMotor.Direction.FORWARD);
theHardwarePlatter.rightFrontDrive.setDirection(DcMotor.Direction.REVERSE);
theHardwarePlatter.leftBackDrive.setDirection(DcMotor.Direction.FORWARD);
theHardwarePlatter.rightBackDrive.setDirection(DcMotor.Direction.REVERSE);
}
void driveAuton(double distanceInches, double speed) {
//double speed = 1;
double leftInches = distanceInches;
double rightInches = distanceInches;
double timeoutS;
int newLeftFTarget;
int newRightFTarget;
int newLeftBTarget;
int newRightBTarget;
theHardwarePlatter.leftFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
theHardwarePlatter.rightFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
theHardwarePlatter.leftBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
theHardwarePlatter.rightBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
theHardwarePlatter.leftFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
theHardwarePlatter.rightFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
theHardwarePlatter.leftBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
theHardwarePlatter.rightBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// Determine new target position, and pass to motor controller
newLeftFTarget = theHardwarePlatter.leftFrontDrive.getCurrentPosition() + (int) (leftInches * COUNTS_PER_INCH);
newRightFTarget = theHardwarePlatter.rightFrontDrive.getCurrentPosition() + (int) (rightInches * COUNTS_PER_INCH);
newLeftBTarget = theHardwarePlatter.leftBackDrive.getCurrentPosition() + (int) (leftInches * COUNTS_PER_INCH);
newRightBTarget = theHardwarePlatter.rightBackDrive.getCurrentPosition() + (int) (rightInches * COUNTS_PER_INCH);
theHardwarePlatter.leftFrontDrive.setTargetPosition(newLeftFTarget);
theHardwarePlatter.rightFrontDrive.setTargetPosition(newRightFTarget);
theHardwarePlatter.leftBackDrive.setTargetPosition(newLeftBTarget);
theHardwarePlatter.rightBackDrive.setTargetPosition(newRightBTarget);
// Turn On RUN_TO_POSITION
theHardwarePlatter.leftFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
theHardwarePlatter.rightFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
theHardwarePlatter.leftBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
theHardwarePlatter.rightBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
// reset the timeout time and start motion.
//runtime.reset();
theHardwarePlatter.leftFrontDrive.setPower(Math.abs(speed));
theHardwarePlatter.rightFrontDrive.setPower(Math.abs(speed));
theHardwarePlatter.leftBackDrive.setPower(Math.abs(speed));
theHardwarePlatter.rightBackDrive.setPower(Math.abs(speed));
while (theHardwarePlatter.leftBackDrive.isBusy() && theHardwarePlatter.rightBackDrive.isBusy()) {
//telemetry.addLine("Encoders_run");
//telemetry.update();
}
}
void rotateAuton(double distanceInches, double speed) {
double leftInches = distanceInches;
theHardwarePlatter.leftFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
theHardwarePlatter.rightFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
theHardwarePlatter.rightBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
theHardwarePlatter.leftBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// Determine new target position, and pass to motor controller
theHardwarePlatter.leftFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
theHardwarePlatter.rightFrontDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
theHardwarePlatter.leftBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
theHardwarePlatter.rightBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
int newLeftBTarget = theHardwarePlatter.leftBackDrive.getCurrentPosition() + (int) (leftInches * COUNTS_PER_INCH);
theHardwarePlatter.leftBackDrive.setTargetPosition(newLeftBTarget);
theHardwarePlatter.rightBackDrive.setTargetPosition(-newLeftBTarget);
theHardwarePlatter.leftFrontDrive.setTargetPosition(newLeftBTarget);
theHardwarePlatter.rightFrontDrive.setTargetPosition(-newLeftBTarget);
theHardwarePlatter.leftFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
theHardwarePlatter.rightFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
theHardwarePlatter.leftBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
theHardwarePlatter.rightBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
theHardwarePlatter.leftBackDrive.setPower(Math.abs(speed));
theHardwarePlatter.rightBackDrive.setPower(Math.abs(speed));
theHardwarePlatter.leftFrontDrive.setPower(Math.abs(speed));
theHardwarePlatter.rightFrontDrive.setPower(Math.abs(speed));
while (theHardwarePlatter.leftBackDrive.isBusy()) {
}
theHardwarePlatter.leftBackDrive.setPower(0);
theHardwarePlatter.rightBackDrive.setPower(0);
theHardwarePlatter.leftFrontDrive.setPower(0);
theHardwarePlatter.rightFrontDrive.setPower(0);
}
public boolean isDriveBusy() {
if (!theHardwarePlatter.leftBackDrive.isBusy() || !theHardwarePlatter.rightBackDrive.isBusy()) {
// Stop all motion;
theHardwarePlatter.leftFrontDrive.setPower(0);
theHardwarePlatter.rightFrontDrive.setPower(0);
// Turn off RUN_TO_POSITION
theHardwarePlatter.leftFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
theHardwarePlatter.rightFrontDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
return (false);
} else {
return (true);
}
}
void rotate(double power) {
theHardwarePlatter.leftFrontDrive.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
theHardwarePlatter.rightFrontDrive.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
theHardwarePlatter.leftBackDrive.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
theHardwarePlatter.rightBackDrive.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
theHardwarePlatter.leftFrontDrive.setPower(power);
theHardwarePlatter.rightFrontDrive.setPower(-power);
theHardwarePlatter.leftBackDrive.setPower(power);
theHardwarePlatter.rightBackDrive.setPower(-power);
}
void display(Telemetry telemetry) {
}
void stop() {
rotate(0);
}
boolean driveToWall(double distanceCM, double power) {
if (getDistanceFromWall() > distanceCM) {
//keep driving to wall
drive2(power);
return (false);
} else {
// stop driving
stop();
return (true);
}
}
void drive2(double power) {
theHardwarePlatter.leftFrontDrive.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
theHardwarePlatter.rightFrontDrive.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
theHardwarePlatter.leftBackDrive.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
theHardwarePlatter.rightBackDrive.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
theHardwarePlatter.leftFrontDrive.setPower(power);
theHardwarePlatter.rightFrontDrive.setPower(power);
theHardwarePlatter.leftBackDrive.setPower(power);
theHardwarePlatter.rightBackDrive.setPower(power);
}
double getDistanceFromWall() {
if (theHardwarePlatter.rangeSensor.rawUltrasonic() == Double.NaN)
return (theHardwarePlatter.rangeSensor.rawOptical());
else
return (theHardwarePlatter.rangeSensor.rawUltrasonic());
}
}