forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSVPUtiliserCeModeTeleop.java
More file actions
467 lines (347 loc) · 20.6 KB
/
SVPUtiliserCeModeTeleop.java
File metadata and controls
467 lines (347 loc) · 20.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* Copyright (c) 2025 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 android.content.Context;
import android.media.SoundPool;
import com.qualcomm.ftccommon.SoundPlayer;
import com.qualcomm.hardware.rev.Rev9AxisImu;
import com.qualcomm.hardware.rev.Rev9AxisImuOrientationOnRobot;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.IMU;
import com.qualcomm.robotcore.hardware.PIDFCoefficients;
import com.qualcomm.robotcore.hardware.VoltageSensor;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
import org.firstinspires.ftc.robotcore.external.navigation.CurrentUnit;
import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
import java.io.IOException;
import java.util.Locale;
/*
* This OpMode illustrates how to program your robot to drive field relative. This means
* that the robot drives the direction you push the joystick regardless of the current orientation
* of the robot.
*
* This OpMode assumes that you have four mecanum wheels each on its own motor named:
* front_left_motor, front_right_motor, back_left_motor, back_right_motor
*
* and that the left motors are flipped such that when they turn clockwise the wheel moves backwards
*
* Use Android Studio to Copy this Class, and Paste it into your team's code folder with a new name.
* Remove or comment out the @Disabled line to add this OpMode to the Driver Station OpMode list
*
*/
@TeleOp(name = "Robot: Field Relative Mecanum Drive MAIN YAY", group = "Robot")
public class SVPUtiliserCeModeTeleop extends LinearOpMode {
// This declares the four motors needed
DcMotor frontLeftDrive;
DcMotor frontRightDrive;
DcMotor backLeftDrive;
DcMotor backRightDrive;
DcMotor shooter;
DcMotor intake;
DcMotor feeder;
DcMotor feeder2;
Rev9AxisImu imu;
Orientation angles;
WebTelemetryStreamer webTelemetryStreamer;
WebInterface webInterface;
boolean shooterToggle = false;
boolean lastGamepadX = false;
Toggler aToggler = new Toggler();
DriveController driveController;
double yawZero;
// This declares the IMU needed to get the current direction the robot is facing
// IMU imu;
@Override
public void runOpMode() {
Rev9AxisImu.Parameters parameters = new Rev9AxisImu.Parameters(new Rev9AxisImuOrientationOnRobot(Rev9AxisImuOrientationOnRobot.LogoFacingDirection.UP, Rev9AxisImuOrientationOnRobot.I2cPortFacingDirection.BACKWARD));
imu = hardwareMap.get(Rev9AxisImu.class, "external_imu");
imu.initialize(parameters);
System.out.println("MAIN ACTUAL OPCODE YAY A");
frontLeftDrive = hardwareMap.get(DcMotor.class, "front_left_drive");
frontRightDrive = hardwareMap.get(DcMotor.class, "front_right_drive");
backLeftDrive = hardwareMap.get(DcMotor.class, "back_left_drive");
backRightDrive = hardwareMap.get(DcMotor.class, "back_right_drive");
shooter = hardwareMap.get(DcMotor.class, "shooter");
intake = hardwareMap.get(DcMotor.class, "intake");
feeder = hardwareMap.get(DcMotor.class, "feeder");
feeder2 = hardwareMap.get(DcMotor.class, "feeder2");
webTelemetryStreamer = new WebTelemetryStreamer(8886);
Thread webTelemetryStreamerThread = new Thread(webTelemetryStreamer);
webTelemetryStreamerThread.start(); // start server
webInterface = new WebInterface(8885);
webInterface.addParameter("shooter_power", 0.6);
webInterface.addParameter("feeder2_power", 0.6); //0.4
webInterface.addParameter("Kp_drive", 0.01);
webInterface.addParameter("Ki_drive", 0.00);
webInterface.addParameter("Kd_drive", 0.00);
PIDFCoefficients defaults_shooter =((DcMotorEx) shooter).getPIDFCoefficients(DcMotor.RunMode.RUN_USING_ENCODER);
defaults_shooter.p = 90; // set default
webInterface.addParameter("Kp_shooter", defaults_shooter.p);
webInterface.addParameter("Ki_shooter", defaults_shooter.i);
webInterface.addParameter("Kd_shooter", defaults_shooter.d);
webInterface.addParameter("Kf_shooter", defaults_shooter.f);
// webInterface.addParameter("dbsizec", 0.3);
// webInterface.addParameter("dbdepthc", 0.3);
Thread webInterfaceThread = new Thread(webInterface);
webInterfaceThread.start(); // start server
// SoundPlayer soundPlayer = new SoundPlayer(1, 4096);
// soundPlayer.play()
// SoundPlayer.getInstance().startPlaying();
intake.setDirection(DcMotor.Direction.FORWARD);
intake.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
feeder.setDirection(DcMotor.Direction.FORWARD);
feeder.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
feeder2.setDirection(DcMotor.Direction.FORWARD);
feeder2.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
shooter.setDirection(DcMotor.Direction.FORWARD);
shooter.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// We set the left motors in reverse which is needed for drive trains where the left
// motors are opposite to the right ones.
backLeftDrive.setDirection(DcMotor.Direction.REVERSE);
frontLeftDrive.setDirection(DcMotor.Direction.REVERSE);
// This uses RUN_USING_ENCODER to be more accurate. If you don't have the encoder
// wires, you should remove these
frontLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
frontRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
frontLeftDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
frontRightDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
backLeftDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
backRightDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
// imu = hardwareMap.get(IMU.class, "imu");
// This needs to be changed to match the orientation on your robot
RevHubOrientationOnRobot.LogoFacingDirection logoDirection =
RevHubOrientationOnRobot.LogoFacingDirection.RIGHT;
RevHubOrientationOnRobot.UsbFacingDirection usbDirection =
RevHubOrientationOnRobot.UsbFacingDirection.FORWARD;
// RevHubOrientationOnRobot orientationOnRobot = new
// RevHubOrientationOnRobot(logoDirection, usbDirection);
// imu.initialize(new IMU.Parameters(orientationOnRobot));
waitForStart();
driveController = new DriveController(imu, frontRightDrive, frontLeftDrive, backLeftDrive, backRightDrive, new PIDController(0.02, 0.001, 0.001));//new PIDController(0.0, 0.0, 0.0));
driveController.init();
angles = imu.getRobotOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
driveController.yawZero = 0.0 - angles.firstAngle;
// yawZero = 0.0 - angles.firstAngle;
int loopcounter = 0;
while (opModeIsActive()) {
loop2(loopcounter++);
}
// try {
webInterface.stop();
// } catch (IOException e) {}
try {
webTelemetryStreamer.stop();
} catch (IOException e) {}
}
private double avg(double a, double b) {
return (a + b)/2;
}
private double boolToDoubleBecauseItWontCast(boolean input) {
return input ? 1.0 : 0.0;
}
public void loop2(int loopcounter) {
telemetry.addLine("Press A to reset Yaw");
telemetry.addLine("Hold left bumper to drive in robot relative");
telemetry.addLine("The left joystick sets the robot direction");
telemetry.addLine("Moving the right joystick left and right turns the robot");
driveController.drivingPID.setCoefs(
webInterface.getParameter("Kp_drive"),
webInterface.getParameter("Ki_drive"),
webInterface.getParameter("Kd_drive")
);
// driveController.drivingPID.deadbandSizeCoef = webInterface.getParameter("dbsizec");
// driveController.drivingPID.deadbandDepthCoef = webInterface.getParameter("dbdepthc");
// driveController.drivingPID.setDeadbandStuff(webInterface.getParameter("dbsizec"), webInterface.getParameter("dbdepthc"));
((DcMotorEx) shooter).setPIDFCoefficients(DcMotor.RunMode.RUN_USING_ENCODER,
new PIDFCoefficients(
webInterface.getParameter("Kp_shooter"),
webInterface.getParameter("Ki_shooter"),
webInterface.getParameter("Kd_shooter"),
webInterface.getParameter("Kf_shooter")
)
);
// System.out.print("hashfuaehwoigfheraoiwghioe: ");
// System.out.println(webInterface.getParameter("Kp_drive"));
// System.out.println(driveController.drivingPID.Kp);
// System.out.println(driveController.drivingPID.Ki);
// System.out.println(driveController.drivingPID.Kd);
angles = imu.getRobotOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
telemetry.addData("connection", imu.getConnectionInfo());
// wrong labels
telemetry.addData("heading", formatAngle(angles.angleUnit, angles.firstAngle));
telemetry.addData("roll", formatAngle(angles.angleUnit, angles.secondAngle));
telemetry.addData("pitch", formatAngle(angles.angleUnit, angles.thirdAngle));
// send slower to stop network getting backed up (maybe it would help if I don't flush data inside of sendData - oh wait I'm already doing that)
if (loopcounter % 3 == 0) {
webTelemetryStreamer.sendData("heading", angles.firstAngle);
webTelemetryStreamer.sendData("roll", angles.secondAngle);
webTelemetryStreamer.sendData("pitch", angles.thirdAngle);
webTelemetryStreamer.sendData("PIDTarget", (180/3.14159) * driveController.drivingPID.target);
webTelemetryStreamer.sendData("PIDOutput", driveController.drivingPID.output);
webTelemetryStreamer.sendData("x", gamepad1.left_stick_x * 100.0);
webTelemetryStreamer.sendData("y", gamepad1.left_stick_y * 100.0);
webTelemetryStreamer.sendData("theta", gamepad1.right_stick_x * 100.0);
webTelemetryStreamer.sendData("current_frontLeftDrive", ((DcMotorEx) frontLeftDrive).getCurrent(CurrentUnit.MILLIAMPS));
webTelemetryStreamer.sendData("current_frontRightDrive", ((DcMotorEx) frontRightDrive).getCurrent(CurrentUnit.MILLIAMPS));
webTelemetryStreamer.sendData("current_backLeftDrive", ((DcMotorEx) backLeftDrive).getCurrent(CurrentUnit.MILLIAMPS));
webTelemetryStreamer.sendData("current_backRightDrive", ((DcMotorEx) backRightDrive).getCurrent(CurrentUnit.MILLIAMPS));
webTelemetryStreamer.sendData("speed_shooter", -((DcMotorEx) shooter).getVelocity(AngleUnit.DEGREES));
webTelemetryStreamer.sendData("speed_intake", -((DcMotorEx) intake).getVelocity(AngleUnit.DEGREES));
webTelemetryStreamer.sendData("speed_feeder", -((DcMotorEx) feeder).getVelocity(AngleUnit.DEGREES));
webTelemetryStreamer.sendData("speed_feeder2", ((DcMotorEx) feeder2).getVelocity(AngleUnit.DEGREES));
webTelemetryStreamer.sendData("current_shooter", ((DcMotorEx) shooter).getCurrent(CurrentUnit.MILLIAMPS));
webTelemetryStreamer.sendData("current_intake", ((DcMotorEx) intake).getCurrent(CurrentUnit.MILLIAMPS));
webTelemetryStreamer.sendData("current_feeder", ((DcMotorEx) feeder).getCurrent(CurrentUnit.MILLIAMPS));
webTelemetryStreamer.sendData("current_feeder2", ((DcMotorEx) feeder2).getCurrent(CurrentUnit.MILLIAMPS));
// get voltage sensor (there are two one is on the expansion hub and this is not ideal as it does not specify which one to use)
for (VoltageSensor sensor : hardwareMap.voltageSensor) {
// webTelemetryStreamer.sendData("battery_voltage", sensor.getVoltage() * 100); // multiply by 100 so it is in a similar range to other values
break;
}
}
// TODO: make opmode just to test every motors' current draw individually under no load
double invert_all_emergency = ((Math.min((gamepad1.left_bumper ? 1 : 0) + (gamepad2.left_bumper ? 1 : 0), 1.0)) * 2 - 1);
// If you press the A button, then you reset the Yaw to be zero from the way
// the robot is currently pointing
// if (gamepad1.a) {
// imu.resetYaw();
// }
// If you press the left bumper, you get a drive from the point of view of the robot
// (much like driving an RC vehicle)
// drive(-gamepad1.left_stick_y, gamepad1.left_stick_x, gamepad1.right_stick_x);
// 90° rotation
// drive(-gamepad1.left_stick_x, -gamepad1.left_stick_y, gamepad1.right_stick_x);
if (!gamepad1.right_bumper) {
// working:
driveController.driveFieldRelative(jsrc(gamepad1.left_stick_y), jsrc(gamepad1.left_stick_x), jsrc(gamepad1.right_stick_x));
// should attempt to correct for unintentional rotation
// driveController.driveFieldRelativeAuto(jsrc(gamepad1.left_stick_y), jsrc(gamepad1.left_stick_x), jsrc(gamepad1.right_stick_x));
} else {
telemetry.addLine("RIGHT BUMPER");
drive(gamepad1.left_stick_y, gamepad1.left_stick_x, gamepad1.right_stick_x);
}
// if (gamepad1.left_bumper) {
// drive(-gamepad1.left_stick_y, gamepad1.left_stick_x, gamepad1.right_stick_x);
// } else {
// driveFieldRelative(-gamepad1.left_stick_y, gamepad1.left_stick_x, gamepad1.right_stick_x);
// }
if (gamepad1.x && !lastGamepadX) {
shooterToggle = !shooterToggle;
}
if (gamepad1.dpad_up) {
driveController.yawZero = 0.0 - angles.firstAngle;
}
lastGamepadX = gamepad1.x;
aToggler.update(gamepad1.a);
double shooterPowerCoef = webInterface.getParameter("shooter_power"); // 0.80;
double upperWheelPower = webInterface.getParameter("feeder2_power"); // 0.3
double restPowerLevel = 1.9; // 1.9
shooter.setPower(shooterToggle ? -shooterPowerCoef : (-invert_all_emergency * (((gamepad1.left_trigger * shooterPowerCoef - gamepad1.right_trigger * shooterPowerCoef) + (gamepad2.left_trigger * shooterPowerCoef - gamepad2.right_trigger * shooterPowerCoef))/2.0)));
intake.setPower(avg(gamepad2.left_stick_y * restPowerLevel, invert_all_emergency * boolToDoubleBecauseItWontCast(aToggler.currentState || gamepad1.b || gamepad1.y) * restPowerLevel));
feeder.setPower(avg(gamepad2.right_stick_y * restPowerLevel, invert_all_emergency * boolToDoubleBecauseItWontCast(gamepad1.b || gamepad1.y) * restPowerLevel));
feeder2.setPower(avg(gamepad2.right_stick_x * upperWheelPower, invert_all_emergency * -boolToDoubleBecauseItWontCast(gamepad1.y) * upperWheelPower));
if (gamepad2.aWasPressed()) {
webInterface.setParameter("shooter_power", 0.55);
}
if (gamepad2.bWasPressed()) {
webInterface.setParameter("shooter_power", webInterface.getParameter("shooter_power") + 0.05);
}
if (gamepad2.xWasPressed()) {
webInterface.setParameter("shooter_power", webInterface.getParameter("shooter_power") - 0.05);
}
telemetry.addData("shooter_power", webInterface.getParameter("shooter_power"));
// gamepad2.resetEdgeDetection();
telemetry.update();
}
String formatAngle(AngleUnit angleUnit, double angle) {
return formatDegrees(AngleUnit.DEGREES.fromUnit(angleUnit, angle));
}
String formatDegrees(double degrees){
return String.format(Locale.getDefault(), "%.1f", AngleUnit.DEGREES.normalize(degrees));
}
private double jsrc(double power) {
// double a = 3.7;
// double b = 0.43;
double a = 3.33;
double b = 0.35;
return (Math.signum(power)*Math.pow(Math.abs(power), a) + (power * b)) / (1 + b);
}
// This routine drives the robot field relative
private void driveFieldRelative(double forward, double right, double rotate) {
// First, convert direction being asked to drive to polar coordinates
double theta = Math.atan2(forward, right);
double r = Math.hypot(right, forward);
// Second, rotate angle by the angle the robot is pointing
// reverse angle because the robot magically got mixed up and the angle was inverted
double yaw_corrected = ((((0.0 - angles.firstAngle) - this.yawZero) + 180.0)) % 360.0 - 180.0;
theta = AngleUnit.normalizeRadians(theta + (yaw_corrected) * (3.141592653589/180));//AngleUnit.normalizeRadians(theta -
//imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.RADIANS));
// Third, convert back to cartesian
double newForward = r * Math.sin(theta);
double newRight = r * Math.cos(theta);
// Finally, call the drive method with robot relative forward and right amounts
drive(newForward, newRight, rotate);
}
// Thanks to FTC16072 for sharing this code!!
public void drive(double forward, double right, double rotate) {
// This calculates the power needed for each wheel based on the amount of forward,
// strafe right, and rotate
double frontLeftPower = forward + right + rotate;
double frontRightPower = forward - right - rotate;
double backRightPower = forward + right - rotate;
double backLeftPower = forward - right + rotate;
double maxPower = 1.0;
double maxSpeed = 1.0; // make this slower for outreaches
// This is needed to make sure we don't pass > 1.0 to any wheel
// It allows us to keep all of the motors in proportion to what they should
// be and not get clipped
maxPower = Math.max(maxPower, Math.abs(frontLeftPower));
maxPower = Math.max(maxPower, Math.abs(frontRightPower));
maxPower = Math.max(maxPower, Math.abs(backRightPower));
maxPower = Math.max(maxPower, Math.abs(backLeftPower));
// We multiply by maxSpeed so that it can be set lower for outreaches
// When a young child is driving the robot, we may not want to allow full
// speed.
frontLeftDrive.setPower(maxSpeed * (frontLeftPower / maxPower));
frontRightDrive.setPower(maxSpeed * (frontRightPower / maxPower));
backLeftDrive.setPower(maxSpeed * (backLeftPower / maxPower));
backRightDrive.setPower(maxSpeed * (backRightPower / maxPower));
}
}