forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeleopFieldCentric.java
382 lines (320 loc) · 15.1 KB
/
TeleopFieldCentric.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package org.firstinspires.ftc.teamcode;
import com.acmerobotics.dashboard.FtcDashboard;
import com.acmerobotics.dashboard.config.Config;
import com.acmerobotics.dashboard.telemetry.MultipleTelemetry;
import com.acmerobotics.roadrunner.control.PIDCoefficients;
import com.acmerobotics.roadrunner.control.PIDFController;
import com.acmerobotics.roadrunner.geometry.Pose2d;
import com.acmerobotics.roadrunner.geometry.Vector2d;
import com.outoftheboxrobotics.photoncore.PhotonCore;
import com.qualcomm.hardware.lynx.LynxModule;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.DigitalChannel;
import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
import org.firstinspires.ftc.robotcore.external.navigation.CurrentUnit;
import org.firstinspires.ftc.teamcode.roadrunnerconfig.drive.DriveConstants;
import org.firstinspires.ftc.teamcode.roadrunnerconfig.drive.SampleMecanumDrive;
import org.firstinspires.ftc.teamcode.vision.BlueConeDetectionPipeline;
import org.firstinspires.ftc.teamcode.vision.PoleDetectionPipeline;
import org.firstinspires.ftc.teamcode.vision.RedConeDetectionPipeline;
import org.openftc.easyopencv.OpenCvCamera;
import org.openftc.easyopencv.OpenCvCameraFactory;
import org.openftc.easyopencv.OpenCvCameraRotation;
/**
* This opmode demonstrates how one would implement field centric control using
* `SampleMecanumDrive.java`. This file is essentially just `TeleOpDrive.java` with the addition of
* field centric control. To achieve field centric control, the only modification one needs is to
* rotate the input vector by the current heading before passing it into the inverse kinematics.
* <p>
* See lines 42-57.
*/
@TeleOp(name = "Teleop Field Centric")
@Config
public class TeleopFieldCentric extends LinearOpMode {
private static final PIDFController armController = new PIDFController(new PIDCoefficients(0.1, 0, 0));
// Declare a PIDF Controller to regulate heading
// Use the same gains as SampleMecanumDrive's heading controller
private final PIDFController headingController = new PIDFController(SampleMecanumDrive.HEADING_PID);
DcMotorEx slide;
DcMotorEx arm;
//ColorSensor color;
DigitalChannel magnet;
double slideTargetPosition;
double slideError;
CRServo claw;
double speed;
double armTargetPosition;
double armError;
double slidePeakCurrentAmps;
String hubNames;
OpenCvCamera camera;
RedConeDetectionPipeline redConeDetectionPipeline;
BlueConeDetectionPipeline blueConeDetectionPipeline;
PoleDetectionPipeline poleDetectionPipeline;
Mode armMode;
@Override
public void runOpMode() throws InterruptedException {
armMode = Mode.DOWN;
PhotonCore.enable();
PhotonCore.CONTROL_HUB.setBulkCachingMode(LynxModule.BulkCachingMode.AUTO);
PhotonCore.EXPANSION_HUB.setBulkCachingMode(LynxModule.BulkCachingMode.AUTO);
//Initialization Period
// RoadRunner Init
// Initialize SampleMecanumDrive
SampleMecanumDrive drive = new SampleMecanumDrive(hardwareMap);
// We want to turn off velocity control for teleop
// Velocity control per wheel is not necessary outside of motion profiled auto
drive.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
// Retrieve our pose from the PoseStorage.currentPose static field
// See AutoTransferPose.java for further details
drive.setPoseEstimate(PoseStorage.currentPose);
//blue = PoseStorage.blue;
// Set input bounds for the heading controller
// Automatically handles overflow
headingController.setInputBounds(-Math.PI, Math.PI);
armController.setOutputBounds(-0.75, 0.75);
/* Motor Init */
// Initiate Slide
slide = hardwareMap.get(DcMotorEx.class, "slide");
slide.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
slide.setCurrentAlert(8, CurrentUnit.AMPS);
slide.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
// Initiate Arm
arm = hardwareMap.get(DcMotorEx.class, "arm");
arm.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
arm.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
arm.setCurrentAlert(4, CurrentUnit.AMPS);
magnet = hardwareMap.get(DigitalChannel.class, "magnet");
magnet.setMode(DigitalChannel.Mode.INPUT);
//color = hardwareMap.get(ColorSensor.class, "color");
// Initiate Claw
claw = hardwareMap.get(CRServo.class, "claw");
claw.setPower(0.5);
// Vision
int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
camera = OpenCvCameraFactory.getInstance().createWebcam(hardwareMap.get(WebcamName.class, "Webcam 1"), cameraMonitorViewId);
blueConeDetectionPipeline = new BlueConeDetectionPipeline();
redConeDetectionPipeline = new RedConeDetectionPipeline();
poleDetectionPipeline = new PoleDetectionPipeline();
camera.setPipeline(poleDetectionPipeline);
camera.openCameraDeviceAsync(new OpenCvCamera.AsyncCameraOpenListener() {
@Override
public void onOpened() {
camera.startStreaming(800, 448, OpenCvCameraRotation.UPRIGHT);
}
@Override
public void onError(int errorCode) {
}
});
telemetry.setMsTransmissionInterval(50);
telemetry = new MultipleTelemetry(telemetry, FtcDashboard.getInstance().getTelemetry());
FtcDashboard.getInstance().startCameraStream(camera, 15);
/* Variable Init */
slideTargetPosition = 0.0;
armTargetPosition = 0.0;
speed = .8;
slidePeakCurrentAmps = 0.0;
waitForStart();
if (isStopRequested()) return;
//Run Period
while (opModeIsActive() && !isStopRequested()) {
// Update the controller input bounds
if (gamepad1.left_bumper) {
speed = .35;
} else if (gamepad1.right_bumper) {
speed = 1;
} else {
speed = .35;
}
// Create a vector from the gamepad x/y inputs
// Then, rotate that vector by the inverse of that heading
Vector2d input = new Vector2d(
-gamepad1.left_stick_y * speed,
-gamepad1.left_stick_x * speed
);
Pose2d poseEstimate = drive.getPoseEstimate();
input = input.rotated(-poseEstimate.getHeading() + Math.toRadians(90.0));
// Pass in the rotated input + right stick value for rotation
// Rotation is not part of the rotated input thus must be passed in separately
Vector2d controllerHeading = new Vector2d(-gamepad1.right_stick_y, -gamepad1.right_stick_x);
if (controllerHeading.distTo(new Vector2d(0.0, 0.0)) < 0.7) {
drive.setWeightedDrivePower(
new Pose2d(
input.getX(),
input.getY(),
(gamepad1.left_trigger - gamepad1.right_trigger)
)
);
} else {
// Set the target heading for the heading controller to our desired angle
headingController.setTargetPosition(controllerHeading.angle() + Math.toRadians(90.0));
// Set desired angular velocity to the heading controller output + angular
// velocity feedforward
double headingInput = (headingController.update(poseEstimate.getHeading())
* DriveConstants.kV)
* DriveConstants.TRACK_WIDTH;
drive.setWeightedDrivePower(
new Pose2d(
input.getX(),
input.getY(),
headingInput
));
}
if (gamepad1.right_stick_button) {
headingController.setTargetPosition(drive.getExternalHeading() + poleDetectionPipeline.getMaxRect().x);
// Set desired angular velocity to the heading controller output + angular
// velocity feedforward
double headingInput = (headingController.update(poseEstimate.getHeading())
* DriveConstants.kV)
* DriveConstants.TRACK_WIDTH;
drive.setWeightedDrivePower(
new Pose2d(
input.getX(),
input.getY(),
headingInput
));
}
Double clawPowerAsBool = 0.0;
if (gamepad1.left_bumper) {
clawPowerAsBool = 1.0;
}
if (armMode != Mode.MOVING_DOWN) {
claw.setPower(0.5 + gamepad2.left_trigger - gamepad2.right_trigger + clawPowerAsBool);
} else {
claw.setPower(0.9 + gamepad2.left_trigger - gamepad2.right_trigger + clawPowerAsBool); //TODO UPDATE
}
//arm.setPower(gamepad2.right_stick_x * 0.5);
// Update everything. Odometry. Etc.
drive.update();
//FieldcENTRIC
if (gamepad1.dpad_down && gamepad1.dpad_left && gamepad1.dpad_up && gamepad1.dpad_right) {
drive.setPoseEstimate(new Pose2d(0, 0, Math.toRadians(90.0)));
}
// Slide
slideTargetPosition = slideTargetPosition + (-gamepad2.left_stick_y * 20);
slideTargetPosition = slideTargetPosition + (-gamepad2.left_stick_y * 20);
if (gamepad2.y || gamepad1.y) {
slideTargetPosition = 1200;
armMode = Mode.MOVING_UP;
}
if (gamepad2.b || gamepad1.b) {
slideTargetPosition = 400;
armMode = Mode.MOVING_UP;
}
if (gamepad2.a || gamepad1.a) {
slideTargetPosition = 20;
armMode = Mode.MOVING_DOWN;
}
if (gamepad2.x || gamepad1.x) {
slideTargetPosition = 1200;
armMode = Mode.MOVING_DOWN;
}
if (gamepad2.dpad_right && gamepad2.dpad_left) {
armMode = Mode.DOWN;
arm.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
arm.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
slide.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
slide.setMode(DcMotor.RunMode.RUN_TO_POSITION);
}
if (slideTargetPosition > 1100) {
slideTargetPosition = 1100;
} else if (slideTargetPosition < 0) {
slideTargetPosition = 0;
}
// overly complex slide code
// obtain the encoder position and calculate the error
slideError = slideTargetPosition - slide.getCurrentPosition();
slide.setTargetPosition((int) slideTargetPosition);
slide.setTargetPositionTolerance(10);
if (slideError > 0) {
slide.setPower(0.8);
} else {
slide.setPower(-0.8);
}
if (!slide.isOverCurrent() && !(gamepad2.left_stick_y > 0)) {
slide.setMode(DcMotor.RunMode.RUN_TO_POSITION);
} else {
slide.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
slide.setPower(gamepad1.left_stick_y);
slideTargetPosition = slide.getCurrentPosition();
}
// update peak current if larger then previous
if (slide.getCurrent(CurrentUnit.AMPS) > slidePeakCurrentAmps) {
slidePeakCurrentAmps = slide.getCurrent(CurrentUnit.AMPS);
}
// arm
/*
armController.setTargetPosition(armTargetPosition);
// make sure arm is not overcurrent
if (!arm.isOverCurrent()) {
arm.setPower(0);
//arm.setPower(armController.update(arm.getCurrentPosition()));
} else {
arm.setPower(0);
}
*/
switch (armMode) {
case UP:
arm.setPower(-gamepad2.right_stick_y * 0.5);
break;
case DOWN:
arm.setPower(-gamepad2.right_stick_y * 0.5);
break;
case MOVING_UP:
if (arm.getCurrentPosition() >= 350 || gamepad2.x || gamepad1.x) {
armMode = Mode.UP;
arm.setPower(0);
} else {
if (slide.getCurrentPosition() > 100 && !arm.isOverCurrent()) {
arm.setPower(0.75);
// compensate for arm gravity using the arm angle, weight, and length
//arm.setPower((0.75 * Math.cos(Math.toRadians(arm.getCurrentPosition() / 2.0))) + 0.75);
} else {
arm.setPower(0);
}
}
break;
case MOVING_DOWN:
if (arm.getCurrentPosition() <= 5) {
armMode = Mode.DOWN;
arm.setPower(0);
} else {
arm.setPower(-0.25);
}
break;
}
// if right bumper on gamepad2 pressed, reset the slide encoder
// color
// Vision
// Print pose to telemetry
telemetry.addData("x", poseEstimate.getX());
telemetry.addData("y", poseEstimate.getY());
telemetry.addData("heading", poseEstimate.getHeading());
telemetry.addData("armPosition", arm.getCurrentPosition());
telemetry.addData("armTargetPosition", armTargetPosition);
telemetry.addData("armCurrent", arm.getCurrent(CurrentUnit.AMPS));
telemetry.addData("slidePeakCurrent", slidePeakCurrentAmps);
telemetry.addData("slideTargetPosition", slideTargetPosition);
telemetry.addData("slidePosition", slide.getCurrentPosition());
telemetry.addData("controllerHeading", controllerHeading.angle());
telemetry.addData("Magnet", magnet.getState());
telemetry.addData("Armstate", armMode);
telemetry.addData("hubNames", hubNames);
//telemetry.addData("colorBlue", color.blue());
//telemetry.addData("colorRed", color.red());
telemetry.addData("servoPosition", claw.getPower());
telemetry.addData("poleWidth", poleDetectionPipeline.getMaxRect().width);
telemetry.update();
}
}
enum Mode {
UP,
MOVING_UP,
MOVING_DOWN,
DOWN
}
}