forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeAutoFar.java
More file actions
265 lines (211 loc) · 10.2 KB
/
Copy pathDecodeAutoFar.java
File metadata and controls
265 lines (211 loc) · 10.2 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
/* Copyright (c) 2021 FIRST. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted (subject to the limitations in the disclaimer below) provided that
* the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of FIRST nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.util.ElapsedTime;
@Autonomous(name="DecodeAutoFar", group="Decode Challenge", preselectTeleOp="DecodeTeleOp")
public class DecodeAutoFar extends LinearOpMode {
//Number of balls to launch
private static final int BALL_COUNT = 3;
//Target launcher RPM for far launch
private static final double TARGET_RPM = 3000.0;
//RPM drop threshold to detect a launch
private static final double LAUNCH_DETECT_DROP = 0.15;
//Time to wait for RPM to recover after launch (ms)
private static final int RPM_RECOVERY_TIME_MS = 500;
/** Maximum time to wait for a single ball launch (ms) */
private static final int MAX_LAUNCH_WAIT_MS = 5000;
/** Distance to drive forward after launching (inches) */
private static final double DRIVE_FORWARD_INCHES = 18.0;
//Drive speed during forward movement (0-1)
private static final double DRIVE_SPEED = 0.4;
//Encoder ticks per motor revolution (GoBilda 6000 RPM)
private static final double LAUNCHER_TICKS_PER_REV = 28.0;
//Encoder ticks per inch of travel
private static final double DRIVE_TICKS_PER_INCH = 41.8;
// Hardware
private DcMotor leftFrontDrive = null;
private DcMotor leftBackDrive = null;
private DcMotor rightFrontDrive = null;
private DcMotor rightBackDrive = null;
private DcMotorEx launcherRight = null;
private DcMotorEx launcherLeft = null;
private CRServo topLeft = null;
private CRServo topRight = null;
private ElapsedTime runtime = new ElapsedTime();
@Override
public void runOpMode() {
// Initialize hardware
initHardware();
// Display configuration during init
telemetry.addData("Status", "Initialized");
telemetry.addData("Ball Count", BALL_COUNT);
telemetry.addData("Target RPM", TARGET_RPM);
telemetry.addData("Drive Distance", "%.1f inches", DRIVE_FORWARD_INCHES);
telemetry.update();
waitForStart();
runtime.reset();
if (opModeIsActive()) {
double targetVelocity = (TARGET_RPM / 60.0) * LAUNCHER_TICKS_PER_REV;
launcherRight.setVelocity(targetVelocity);
launcherLeft.setVelocity(targetVelocity);
waitForLauncherReady(TARGET_RPM, 3000);
for (int ball = 1; ball <= BALL_COUNT && opModeIsActive(); ball++) {
launchBall();
if (ball < BALL_COUNT) {
// Wait for RPM to recover before next ball
sleep(RPM_RECOVERY_TIME_MS);
waitForLauncherReady(TARGET_RPM, 2000);
}
}
stopFeeder();
launcherRight.setVelocity(0);
launcherLeft.setVelocity(0);
driveForward(DRIVE_FORWARD_INCHES, DRIVE_SPEED);
telemetry.addData("Runtime", "%.1f seconds", runtime.seconds());
telemetry.update();
}
}
/**
* Initialize all hardware.
*/
private void initHardware() {
// Drive motors
leftFrontDrive = hardwareMap.get(DcMotor.class, "left_front_drive");
leftBackDrive = hardwareMap.get(DcMotor.class, "left_back_drive");
rightFrontDrive = hardwareMap.get(DcMotor.class, "right_front_drive");
rightBackDrive = hardwareMap.get(DcMotor.class, "right_back_drive");
leftFrontDrive.setDirection(DcMotor.Direction.REVERSE);
leftBackDrive.setDirection(DcMotor.Direction.REVERSE);
rightFrontDrive.setDirection(DcMotor.Direction.FORWARD);
rightBackDrive.setDirection(DcMotor.Direction.FORWARD);
// Reset drive encoders
leftBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
leftBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// Launcher motors
launcherRight = hardwareMap.get(DcMotorEx.class, "launcher_right");
launcherLeft = hardwareMap.get(DcMotorEx.class, "launcher_left");
launcherRight.setDirection(DcMotor.Direction.FORWARD);
launcherLeft.setDirection(DcMotor.Direction.REVERSE);
launcherRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
launcherLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// Feeder servos
topLeft = hardwareMap.get(CRServo.class, "top_left");
topRight = hardwareMap.get(CRServo.class, "top_right");
}
/**
* Wait for launcher to reach target RPM.
*/
private void waitForLauncherReady(double targetRPM, int timeoutMs) {
ElapsedTime timer = new ElapsedTime();
double targetVelocity = (targetRPM / 60.0) * LAUNCHER_TICKS_PER_REV;
double threshold = targetVelocity * 0.95; // 95% of target
while (opModeIsActive() && timer.milliseconds() < timeoutMs) {
double currentVelocity = (launcherRight.getVelocity() + launcherLeft.getVelocity()) / 2.0;
double currentRPM = (currentVelocity / LAUNCHER_TICKS_PER_REV) * 60.0;
telemetry.addData("Launcher", "%.0f / %.0f RPM", currentRPM, targetRPM);
telemetry.update();
if (currentVelocity >= threshold) {
return; // Ready!
}
sleep(50);
}
}
/**
* Launch a single ball using RPM drop detection.
* Runs feeder until RPM drops (indicating ball passed through) then recovers.
*/
private void launchBall() {
ElapsedTime launchTimer = new ElapsedTime();
double targetVelocity = (TARGET_RPM / 60.0) * LAUNCHER_TICKS_PER_REV;
double dropThreshold = targetVelocity * (1.0 - LAUNCH_DETECT_DROP);
boolean ballLaunched = false;
topLeft.setPower(-1.0);
topRight.setPower(1.0);
while (opModeIsActive() && launchTimer.milliseconds() < MAX_LAUNCH_WAIT_MS && !ballLaunched) {
double currentVelocity = (launcherRight.getVelocity() + launcherLeft.getVelocity()) / 2.0;
double currentRPM = (currentVelocity / LAUNCHER_TICKS_PER_REV) * 60.0;
telemetry.addData("Launching", "RPM: %.0f (drop at %.0f)", currentRPM,
(dropThreshold / LAUNCHER_TICKS_PER_REV) * 60.0);
telemetry.update();
if (currentVelocity < dropThreshold) {
// RPM dropped
ballLaunched = true;
telemetry.addData("Launch", "Detected!");
telemetry.update();
}
sleep(20);
}
stopFeeder();
}
/**
* Stop the feeder servos.
*/
private void stopFeeder() {
topLeft.setPower(0);
topRight.setPower(0);
}
/**
* Drive forward a specified distance using rear wheel encoders.
*/
private void driveForward(double inches, double speed) {
int targetTicks = (int)(inches * DRIVE_TICKS_PER_INCH);
leftBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightBackDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
leftBackDrive.setTargetPosition(targetTicks);
rightBackDrive.setTargetPosition(targetTicks);
leftBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftFrontDrive.setPower(speed);
rightFrontDrive.setPower(speed);
leftBackDrive.setPower(speed);
rightBackDrive.setPower(speed);
while (opModeIsActive() && (leftBackDrive.isBusy() || rightBackDrive.isBusy())) {
telemetry.addData("Driving", "L:%d R:%d / %d",
leftBackDrive.getCurrentPosition(),
rightBackDrive.getCurrentPosition(),
targetTicks);
telemetry.update();
sleep(50);
}
leftFrontDrive.setPower(0);
rightFrontDrive.setPower(0);
leftBackDrive.setPower(0);
rightBackDrive.setPower(0);
leftBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightBackDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
}