-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.java
More file actions
518 lines (469 loc) · 14.4 KB
/
Copy pathRobot.java
File metadata and controls
518 lines (469 loc) · 14.4 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
import edu.wpi.first.wpilibj.SpeedControllerGroup;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.drive.MecanumDrive;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.cameraserver.CameraServer;
import edu.wpi.first.wpilibj.SerialPort;
import edu.wpi.first.wpilibj.SerialPort.Port;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import com.analog.adis16448.frc.ADIS16448_IMU;
public class Robot extends TimedRobot {
// WPI_TalonSRX _frontTLeftMotor = null;
// WPI_TalonSRX _frontTRightMotor = null;
// WPI_TalonSRX _rearTRightMotor = null;
// WPI_TalonSRX _rearTLeftMotor = null;
WPI_VictorSPX _frontVLeftMotor = null;
WPI_VictorSPX _frontVRightMotor = null;
WPI_VictorSPX _rearVRightMotor = null;
WPI_VictorSPX _rearVLeftMotor = null;
SpeedControllerGroup leftMotors = null;
SpeedControllerGroup rightMotors = null;
DifferentialDrive _dDrive = null;
MecanumDrive _mDrive = null;
Joystick _joy1 = null;
Joystick _joy2 = null;
private UsbCamera camera = null;
// Network Tables
NetworkTableEntry xEntry;
NetworkTableEntry yEntry;
NetworkTable table;
// Serial Port Information
SerialPort theThePort = null;
// Line Tracker
DigitalInput lineTracker0 = null;
DigitalInput lineTracker1 = null;
DigitalInput lineTracker2 = null;
DigitalInput lineTracker3 = null;
DigitalInput lineTracker4 = null;
// 10 Degrees of Freedom
ADIS16448_IMU imu;
double zDegree = 0;
double pastZDegree = zDegree;
int zDegreeIterations = 0;
double targetDegree = 0;
double origLine2Degree = 0;
double rotationCounter = 1;
double rotation = 0;
double forwardMotion = 0;
boolean lTrack0 = false;
boolean lTrack1 = false;
boolean lTrack2 = false;
boolean lTrack3 = false;
boolean lTrack4 = false;
boolean autoTrackingEnabled = true;
// Sections of code to include or exclude
boolean nTables = false; // Network Tables in Use
boolean mDrive = true; // Mecanum Drive
boolean dDrive = false; // Differential Drive
boolean cServer = true; // Camera Server
boolean jCam = false; // Jevois Camera
boolean lTrack = true; // Line Tracker
boolean tenDegrees = true; // 10 degrees of freedom
boolean pneumatics = false; // Pneumatics System
// yeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeees
String jCamString = " ";
// Timer
Timer robotTimer = new Timer();
// Guard It Safe, this's the IMU calibration one
boolean didItAlready = false;
boolean imuIsWorkingCorrectly = true; // IMU is Working or Not
// Pneumatics
DoubleSolenoid pneuAction;
/**
* This function is run when the robot is first started up and should be used
* for any initialization code.
*/
@Override
public void robotInit() {
robotTimer.start(); // Start the timer for IMU Calibration Guard It Safes.
// Setup the joystick
try {
_joy1 = new Joystick(0);
} catch (Exception e0) {
}
try {
_joy2 = new Joystick(1);
} catch (Exception e1) {
}
// 30 - Intake - Upper
// 31 - Intake - Lower
// 20 - Rear Lifter
// 21 - Intake Lifter
// 22 - Fangs Lifter 2
// 23 - Front Lifter 1
// Setup the Drive System
//if (mDrive) {
_frontVLeftMotor = new WPI_VictorSPX(13);
_frontVRightMotor = new WPI_VictorSPX(12);
_rearVRightMotor = new WPI_VictorSPX(11);
_rearVLeftMotor = new WPI_VictorSPX(10);
_mDrive = new MecanumDrive(_frontVLeftMotor, _rearVLeftMotor, _frontVRightMotor, _rearVRightMotor);
// } else if (dDrive) {
// _frontTLeftMotor = new WPI_TalonSRX(13);
// _frontTRightMotor = new WPI_TalonSRX(12);
// _rearTRightMotor = new WPI_TalonSRX(11);
// _rearTLeftMotor = new WPI_TalonSRX(10);
// leftMotors = new SpeedControllerGroup(_frontTLeftMotor, _rearTLeftMotor);
// rightMotors = new SpeedControllerGroup(_frontTRightMotor, _rearTRightMotor);
// _dDrive = new DifferentialDrive(leftMotors, rightMotors);
// }
if (lTrack) {
try {
lineTracker0 = new DigitalInput(0);
lineTracker1 = new DigitalInput(1);
lineTracker2 = new DigitalInput(2);
lineTracker3 = new DigitalInput(3);
lineTracker4 = new DigitalInput(4);
} catch (Exception ex) {
}
}
try {
lineTracker0 = new DigitalInput(0);
} catch (Exception ex) {
}
try {
lineTracker1 = new DigitalInput(1);
} catch (Exception ex) {
}
try {
lineTracker2 = new DigitalInput(2);
} catch (Exception ex) {
}
try {
lineTracker3 = new DigitalInput(3);
} catch (Exception ex) {
}
try {
lineTracker4 = new DigitalInput(4);
} catch (Exception ex) {
}
if (tenDegrees) {
// 10 Degrees of Freedom
try {
imu = new ADIS16448_IMU();
} catch (Exception e) {
imu = new ADIS16448_IMU();
}
try {
imu.reset();
imu.calibrate();
} catch (Exception e) {
}
}
if (cServer) {
try {
camera = CameraServer.getInstance().startAutomaticCapture(0);
} catch (Exception e) {
}
if (camera == null) { // Don't ask why!
camera = null; // Seriously, don't!
}
}
if (nTables) {
// Network Tables
NetworkTableInstance inst = NetworkTableInstance.getDefault();
table = inst.getTable("deepSpace");
xEntry = table.getEntry("X");
yEntry = table.getEntry("Y");
}
// Serial Port Logic
if (jCam) {
try {
theThePort = new SerialPort(115200, Port.kUSB);
} catch (Exception e) {
jCamString = e.toString();
}
int retval = 0;
if (theThePort != null) {
retval = theThePort.writeString("ping\n");
}
if (retval > 0) {
SmartDashboard.putString("Error", "The the error: " + retval);
SmartDashboard.putString("Error2", theThePort.readString());
}
}
if (pneumatics) {
DoubleSolenoid pneuAction = new DoubleSolenoid(8, 9);
}
SmartDashboard.putBoolean("IMU Working", imuIsWorkingCorrectly);
}
@Override
public void robotPeriodic() {
// To calibrate imu manually, press start on both controllers. Only usable
// before auton.
// NOTE: The 3667 Programmers are not liable for any mistakes that are made if
// you accidentally
// press these buttons. Oh yes, and if you think it was our fault we will find
// you and you will
// regret ever thinking so.
if (_joy1.getRawButton(8) && _joy2.getRawButton(8)) {
manualImuCalibration();
}
if (!didItAlready) {
imuCalibration();
}
// Network Table Test Work
if (nTables) {
if (_joy1.getRawButton(2)) {
double xDouble = Math.random();
double yDouble = Math.random();
xEntry.setDouble(xDouble);
yEntry.setDouble(yDouble);
}
}
if (_joy1.getRawButton(3) && tenDegrees) {
imu.reset();
imuIsWorkingCorrectly = true;
SmartDashboard.putBoolean("IMU Working", imuIsWorkingCorrectly);
}
if (mDrive) {
if (tenDegrees) {
SmartDashboard.putNumber("zDegree", zDegree);
SmartDashboard.putNumber("targetDegree", targetDegree);
SmartDashboard.putBoolean("Line Tracker 0", lTrack0);
SmartDashboard.putBoolean("Line Tracker 1", lTrack1);
SmartDashboard.putBoolean("Line Tracker 2", lTrack2);
SmartDashboard.putBoolean("Line Tracker 3", lTrack3);
SmartDashboard.putBoolean("Line Tracker 4", lTrack4);
SmartDashboard.putBoolean("IMU Working", imuIsWorkingCorrectly);
SmartDashboard.putBoolean("Auto Tracking", autoTrackingEnabled);
}
}
if (_joy2.getRawButton(4) || _joy1.getRawButton(4))
if (autoTrackingEnabled) {
autoTrackingEnabled = false;
} else {
autoTrackingEnabled = true;
}
}
// This, before match has begun, should go periodically until we did it.
private void imuCalibration() {
if (Timer.getMatchTime() > 0) {
robotTimer.stop();
didItAlready = true;
} else if (robotTimer.get() > 300.0) // if 5+ mins have passed since power on
{
didItAlready = true;
manualImuCalibration();
robotTimer.stop();
}
}
// Manually calibrate the IMU whenever, disable or change for testing
private void manualImuCalibration() {
imuIsWorkingCorrectly = false;
SmartDashboard.putBoolean("IMU Working", imuIsWorkingCorrectly);
try {
imu.reset();
imu.calibrate();
} catch (Exception e) {
}
imuIsWorkingCorrectly = true;
SmartDashboard.putBoolean("IMU Working", imuIsWorkingCorrectly);
zDegreeIterations = 0;
}
public double find45Degree(double zDegree) {
double retDoub = -1;
int povVal1 = _joy1.getPOV();
int povVal2 = _joy2.getPOV();
if (povVal2 >= 0) {
retDoub = povVal2;
} else if (povVal1 >= 0) {
retDoub = povVal1;
} else {
int plusOne = (int) zDegree;
int minusOne = (int) zDegree;
while (retDoub < 0) {
switch (plusOne) {
case 0:
retDoub = 0;
break;
case 45:
retDoub = 45;
break;
case 90:
retDoub = 90;
break;
case 135:
retDoub = 135;
break;
case 180:
retDoub = 180;
break;
case 225:
retDoub = 225;
break;
case 270:
retDoub = 270;
break;
case 315:
retDoub = 315;
break;
case 360:
retDoub = 0;
break;
default:
break;
}
switch (minusOne) {
case 0:
retDoub = 0;
break;
case 45:
retDoub = 45;
break;
case 90:
retDoub = 90;
break;
case 135:
retDoub = 135;
break;
case 180:
retDoub = 180;
break;
case 225:
retDoub = 225;
break;
case 270:
retDoub = 270;
break;
case 315:
retDoub = 315;
break;
case 360:
retDoub = 0;
break;
default:
break;
}
if (retDoub < 0) {
plusOne++;
minusOne--;
}
}
}
return retDoub;
}
@Override
public void autonomousInit() {
}
@Override
public void autonomousPeriodic() {
teleopPeriodic();
}
@Override
public void teleopPeriodic() {
// Setup Stafe values
double strafe = 0;
if (_joy1.getRawAxis(0) > 0.1 || _joy1.getRawAxis(0) < -0.1) {
strafe = _joy1.getRawAxis(0);
} else if (_joy1.getRawAxis(2) > 0.1) {
strafe = _joy1.getRawAxis(2) * -1.0;
} else if (_joy1.getRawAxis(3) > 0.1) {
strafe = _joy1.getRawAxis(3);
}
if (mDrive) {
if (tenDegrees) {
zDegree = Math.round(imu.getAngleZ()) % 360;
if (zDegree < 0) {
zDegree += 360;
}
targetDegree = find45Degree(zDegree);
lTrack0 = lineTracker0.get();
lTrack1 = lineTracker1.get();
lTrack2 = lineTracker2.get();
lTrack3 = lineTracker3.get();
lTrack4 = lineTracker4.get();
double forwardMotion = _joy1.getRawAxis(1) * -1;
if (autoTrackingEnabled) { // Line Tracker Enabled
rotation = _joy1.getRawAxis(4);
if (lTrack0) {
rotation = rotation + turnSpeed(0.3);
strafe = strafe + 0.6;
_mDrive.driveCartesian(strafe, forwardMotion, rotation, 0);
} else if (lTrack4) {
rotation = rotation + turnSpeed(0.3);
strafe = strafe - 0.6;
_mDrive.driveCartesian(strafe, forwardMotion, rotation, 0);
} else if (lTrack1) {
rotation = rotation + turnSpeed(0.2);
strafe = strafe + 0.4;
_mDrive.driveCartesian(strafe, forwardMotion, rotation, 0);
} else if (lTrack3) {
rotation = rotation + turnSpeed(0.2);
strafe = strafe - 0.4;
_mDrive.driveCartesian(strafe, forwardMotion, rotation, 0);
} else if (lTrack2) {
rotation = rotation + turnSpeed(0.1);
_mDrive.driveCartesian(0, forwardMotion, rotation, 0);
} else {
_mDrive.driveCartesian(strafe, forwardMotion, _joy1.getRawAxis(4), 0);
}
} else {
_mDrive.driveCartesian(strafe, forwardMotion, _joy1.getRawAxis(4), 0);
}
} else {
// The the mecanum drive is listed below
if (mDrive) {
_mDrive.driveCartesian(strafe, _joy1.getRawAxis(1) * -1, _joy1.getRawAxis(4), 0);
}
}
if (_joy1.getRawAxis(4) < -0.2 || _joy1.getRawAxis(4) > 0.2) {
if (pastZDegree == zDegree) {
zDegreeIterations++;
if (zDegreeIterations > 15) {
imuIsWorkingCorrectly = false; // We have a real Problem
}
} else {
pastZDegree = zDegree;
zDegreeIterations = 0;
}
if (pneumatics) {
if (_joy1.getRawButton(5) || _joy2.getRawButton(5)) {
pneuAction.set(DoubleSolenoid.Value.kReverse);
} else {
pneuAction.set(DoubleSolenoid.Value.kForward);
}
}
}
}
// The the Network Table cool Code is within the if Statement
if (dDrive) {
if (_joy1.getRawButton(1)) {
double xDouble = 0;
xDouble = xEntry.getDouble(xDouble);
double yDouble = 0;
yDouble = yEntry.getDouble(yDouble);
_dDrive.arcadeDrive(xDouble * -1.0, yDouble);
} else {
_dDrive.arcadeDrive(_joy1.getRawAxis(1) * -1.0, _joy1.getRawAxis(4));
}
}
}
public double turnSpeed(double fullspeed) {
double returnSpeed = fullspeed;
if (targetDegree == 0 && zDegree > 270) {
returnSpeed = returnSpeed * -1.0; // we are overlapping zero
}
if (zDegree > targetDegree) {
returnSpeed = returnSpeed * -1.0;
}
return returnSpeed;
}
/**
* This function is called periodically during test mode.
*/
@Override
public void testPeriodic() {
}
}