-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackOfFieldBlueAuto.java
More file actions
307 lines (265 loc) · 11.6 KB
/
Copy pathBackOfFieldBlueAuto.java
File metadata and controls
307 lines (265 loc) · 11.6 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Copyright 2025 FIRST Tech Challenge Team 31834
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.hardware.dfrobot.HuskyLens;
@Autonomous
public class BackOfFieldBlueAuto extends LinearOpMode {
private static final double TICKS_PER_REV = 1425.1;
private DcMotor motorFL, motorFR, motorBL, motorBR;
private DcMotor intake, rotaryspin;
private DcMotorEx launcher;
private PIDController launcherPID;
private double targetLauncherVelocity = 3100;
private double currentVelocity = 0;
private double pidPower = 0;
private Servo pushup, rotarykick;
int x = -15;
double turn = 0;
int RotateStage = 1;
private HuskyLens husky;
// HuskyLens detection variables
private volatile boolean tagVisible = false;
private volatile double tagX = 0;
int ObId = 0;
@Override
public void runOpMode() {
motorFL = hardwareMap.get(DcMotor.class, "motor2");
motorBR = hardwareMap.get(DcMotor.class, "motor1");
motorFR = hardwareMap.get(DcMotor.class, "motor3");
motorBL = hardwareMap.get(DcMotor.class, "motor4");
launcher = hardwareMap.get(DcMotorEx.class, "launcher");
rotarykick = hardwareMap.get(Servo.class, "rotarykick");
pushup = hardwareMap.get(Servo.class, "pushup");
rotaryspin = hardwareMap.get(DcMotor.class, "rotaryspin");
husky = hardwareMap.get(HuskyLens.class, "husky");
motorFL.setDirection(DcMotor.Direction.REVERSE);
motorBR.setDirection(DcMotor.Direction.FORWARD);
motorFR.setDirection(DcMotor.Direction.REVERSE);
motorBL.setDirection(DcMotor.Direction.REVERSE);
launcher.setDirection(DcMotor.Direction.FORWARD);
launcher.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
telemetry.addData("Status", "Initialized");
telemetry.update();
waitForStart();
boolean brakeMode = true;
rotarykick.setPosition(0);
// ================== HUSKYLENS DETECTION THREAD WITH TELEMETRY ==================
new Thread(() -> {
while (opModeIsActive()) {
HuskyLens.Block[] blocks = husky.blocks();
sleep(20);
if (blocks == null) {
telemetry.addLine("blocks() returned null");
telemetry.update();
sleep(50);
continue;
}
if (blocks.length == 0) {
telemetry.addLine("blocks array is empty");
telemetry.update();
sleep(50);
continue;
}
HuskyLens.Block found = null;
for (HuskyLens.Block b : blocks) {
if (b == null) continue;
telemetry.addData("Detected block", "ID=" + b.id + ", X=" + b.x + ", Y=" + b.y +
", Width=" + b.width + ", Height=" + b.height);
if (b.id == 1) { // change ID here if needed
found = b;
break;
} else {
if (b.id > 2) {
ObId = b.id;
telemetry.addData("ob tag is" , ObId);
telemetry.update();
}
}
}
if (found != null) {
tagVisible = true;
tagX = found.x;
telemetry.addData("Found tag", "ID=" + found.id + ", X=" + found.x);
} else {
tagVisible = false;
telemetry.addLine("No tag with ID 2 found");
}
telemetry.update();
sleep(50);
telemetry.addData("ob tag is" , ObId);
telemetry.update();
}
}).start();
// ================== MAIN AUTO LOOP ==================
while (opModeIsActive()) {
// Move rotary spin
rotaryspin.setTargetPosition(x);
rotaryspin.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rotaryspin.setPower(1.0);
while (opModeIsActive() && rotaryspin.isBusy()) {
telemetry.addData("rotaryspin Pos", rotaryspin.getCurrentPosition());
telemetry.update();
}
rotaryspin.setPower(0);
rotaryspin.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// Initial move
motorFL.setPower(0.8);
motorFR.setPower(0);
motorBL.setPower(0);
motorBR.setPower(0.8);
sleep(1360);
motorFL.setPower(0);
motorFR.setPower(0);
motorBL.setPower(0);
motorBR.setPower(0);
sleep(400);
// Launcher PID setup and small backward nudge
launcherPID = new PIDController(0.0250, 0.0007, 0.0003);
motorFL.setPower(-0.7);
motorFR.setPower(-0.7);
motorBL.setPower(-0.7);
motorBR.setPower(-0.7);
sleep(200);
motorFL.setPower(0);
motorFR.setPower(0);
motorBL.setPower(0);
motorBR.setPower(0);
sleep(200);
// ================== WAIT FOR TAG ==================
while (opModeIsActive() && !tagVisible) {
telemetry.addLine("Waiting for AprilTag ID 2...");
telemetry.update();
sleep(50);
}
// ================== ALIGNMENT ==================
if (tagVisible) {
double cx = 180.0;
double acceptableError = 15.0;
double errorX = tagX - cx;
while (opModeIsActive() &&
(errorX < -acceptableError || errorX > acceptableError)) {
turn = -(errorX / cx) * 0.9;
motorFL.setPower(-turn);
motorFR.setPower(-turn);
motorBL.setPower(-turn);
motorBR.setPower(turn);
telemetry.addData("Turning to align", "errorX=" + errorX + ", turn=" + turn);
telemetry.update();
// Update errorX from HuskyLens
HuskyLens.Block[] updated = husky.blocks();
if (updated != null && updated.length > 0 && updated[0] != null) {
errorX = updated[0].x - cx;
} else {
break; // stop if no blocks detected
}
}
motorFL.setPower(0);
motorFR.setPower(0);
motorBL.setPower(0);
motorBR.setPower(0);
}
// ================== REST OF AUTO (unchanged) ==================
DcMotor.ZeroPowerBehavior behavior =
brakeMode ? DcMotor.ZeroPowerBehavior.BRAKE : DcMotor.ZeroPowerBehavior.FLOAT;
motorFL.setZeroPowerBehavior(behavior);
motorFR.setZeroPowerBehavior(behavior);
motorBL.setZeroPowerBehavior(behavior);
motorBR.setZeroPowerBehavior(behavior);
for (int f = 0; f < 4; f++) {
if (ObId == 5) {
if (RotateStage == 1) {
x = 460; //was -15
} else {
if (RotateStage == 2) {
x = -15; //was 460
} else {
x = 935;
}
}
} else {
if (ObId == 3) {
if (RotateStage == 1) {
x = -15;
} else {
if (RotateStage == 2) {
x = 460;
} else {
x = 935;
}
}
} else {
if (RotateStage == 1) {
x = -15;
} else {
if (RotateStage == 2) {
x = 935;
} else {
x = 460;
}
}
}
}
RotateStage += 1;
//x += 475;
rotaryspin.setTargetPosition(x);
rotaryspin.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rotaryspin.setPower(1.0);
while (opModeIsActive() && rotaryspin.isBusy()) {
telemetry.addData("rotaryspin Pos", rotaryspin.getCurrentPosition());
telemetry.update();
}
rotaryspin.setPower(0);
rotaryspin.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
new Thread(() -> {
sleep(1000);
rotarykick.setPosition(0.2);
sleep(500);
rotarykick.setPosition(0);
}).start();
for (int i = 0; i < 100; i++) {
currentVelocity = launcher.getVelocity();
pidPower = launcherPID.update(targetLauncherVelocity, currentVelocity);
pidPower = Math.max(Math.min(pidPower, 0.3), 0.0);
launcher.setPower(pidPower);
sleep(20);
}
new Thread(() -> {
sleep(2000);
pushup.setPosition(5.0 / 180.0);
sleep(600);
pushup.setPosition(160.0 / 180.0);
}).start();
for (int i = 0; i < 100; i++) {
currentVelocity = launcher.getVelocity();
pidPower = launcherPID.update(targetLauncherVelocity, currentVelocity);
pidPower = Math.max(Math.min(pidPower, 0.9), 0.0);
launcher.setPower(pidPower);
sleep(20);
}
pidPower = 0.0;
launcher.setPower(pidPower);
rotarykick.setPosition(0);
sleep(100);
}
sleep(10000);
}
}
}