forked from dmboogie/CopperBot2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot.java
665 lines (591 loc) · 17.9 KB
/
Robot.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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package org.usfirst.frc.team2586.robot;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.PowerDistributionPanel;
import edu.wpi.first.wpilibj.Preferences;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Servo;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.networktables.NetworkTable;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.Gyro;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class Robot extends IterativeRobot {
Joystick joy = new Joystick(0);
Joystick xbox = new Joystick(1);
Talon frontRightDrive = new Talon(0);
Talon rearRightDrive = new Talon(1);
Talon frontLeftDrive = new Talon(2);
Talon rearLeftDrive = new Talon(3);
Gyro gyro = new Gyro(0);
Encoder fRDcoder = new Encoder(0, 1);
Encoder rRDcoder = new Encoder(2, 3);
Encoder fLDcoder = new Encoder(4, 5);
Encoder rLDcoder = new Encoder(6, 7);
Timer timer = new Timer();
Solenoid liftUp = new Solenoid(0);
Solenoid liftDown = new Solenoid(1);
PowerDistributionPanel pdp = new PowerDistributionPanel();
CameraServer server;
Relay lux = new Relay(0);
Relay openClaw = new Relay (1);
Relay closeClaw = new Relay (2);
Servo cameraServoX = new Servo(9);
Servo cameraServoY = new Servo(8);
Servo eyeServo = new Servo(7);
boolean precisionMode = true;
boolean gyroMode = true;
boolean crabMode = false;
double gyroHeading = 0;
double autoSelect = 7;
double dangerZone = 0.5;
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
@Override
public void robotInit() {
//Starts the gyro and camera, along with providing default values for our Autonomous Picker and Gyro kP
gyro.initGyro();
SmartDashboard.putNumber("Auton", 7);
SmartDashboard.putNumber("Gyro_kP", 0.05);
server = CameraServer.getInstance();
server.setQuality(50);
// the camera name (ex "cam0") can be found through the roborio web
// interface
server.startAutomaticCapture("cam0");
// table = NetworkTable.getTable("database");
}
@Override
public void disabledPeriodic() {
//Lets us see if Precision/Gyro modes are on or off
double precision = joy.getRawAxis(3);
SmartDashboard.putBoolean("Precision Mode", precisionMode);
SmartDashboard.putBoolean("Gyro Mode", gyroMode);
SmartDashboard.putNumber("precision", precision);
if (joy.getRawButton(4)) {
gyroMode = false;
}
if (joy.getRawButton(6)) {
gyroMode = true;
}
gyro.reset();
gyroHeading = gyro.getAngle();
server = CameraServer.getInstance();
server.setQuality(50);
server.startAutomaticCapture("cam0");
}
@Override
public void autonomousInit() {
//Gets the timer ready for auton
gyroMode = true;
timer.reset();
timer.start();
gyro.reset();
gyroHeading = gyro.getAngle();
fRDcoder.reset();
rRDcoder.reset();
fLDcoder.reset();
rLDcoder.reset();
}
/**
* This function is called periodically during autonomous
*/
@Override
public void autonomousPeriodic() {
//the below is a large amount of witchcraft that has not been tested yet
//((as of now most of it has been tested but it is still largely witchcraft))
double time = timer.get();
double fRD = 0, rRD = 0, fLD = 0, rLD = 0;
double rotation = GyroMod(0);
//lets our supervisors know we aren't messing around on company time, also passes variables to the smart dashboard
//but mostly the supervision
//you won't get away with your slacking ways now, keh from accounting
SmartDashboard.putNumber("Time", time);
SmartDashboard.putBoolean("LiftUp", liftUp.get());
SmartDashboard.putBoolean("LiftDown", liftDown.get());
autoSelect = SmartDashboard.getNumber("Auton");
//this is our lovely "default model", available for five easy installments of all your money and everything you love
//we pick up the bin and hightail outta town like the sherrif's after our loot
//riding off into the sunset, the bin and the bot go into the auto zone together
//still a better love story than blues brothers
//oh there's also a SICKNASTY 90 degree turn after that with the help of our lovely gyro
//it kinda works without the gyro but that's not how coolkids do it
if (autoSelect == 1) {
SmartDashboard.putString("AutoMode", "BinStackSim2015");
if (time < 1) {
liftUp.set(true);
} else if (timer.get() > 2 && timer.get() < 4) {
fRD = -0.5;
rRD = -0.5;
fLD = 0.5;
rLD = 0.5;
}
else if (timer.get() > 4 && timer.get() < 10){
if (gyroMode){
gyroHeading = 90;
}
else{
fRD = -0.325;
rRD = -0.325;
fLD = -0.325;
rLD = -0.325;
}
}
else if (timer.get() > 10){
fRD = 0;
rRD = 0;
fLD = 0;
rLD = 0;
}
}
//we swooce forward right into the autozone. right into there. swoocing right in. the autozone. right there.
if (autoSelect == 2) {
SmartDashboard.putString("AutoMode", "with great passion");
if (time < 2) {
fRD = 0.25;
rRD = 0.25;
fLD = -0.25;
rLD = -0.25;
} else if (time > 2) {
fRD = 0;
rRD = 0;
fLD = 0;
rLD = 0;
}
}
//it's like the one above but we go backwards this time like a buncha nerds
if (autoSelect == 3) {
SmartDashboard.putString("AutoMode", "aaaaa run away");
if (time < 2) {
fRD = -0.25;
rRD = -0.25;
fLD = 0.25;
rLD = 0.25;
} else if (time > 2) {
fRD = 0;
rRD = 0;
fLD = 0;
rLD = 0;
}
}
//so this has never actually been tested or used or anything. look at that big block of code. never used!
//ah what a lost hope of our generation it is.
//anyway
//what it's supposed to do is lift up the bin, strafe to the crate, set the bin on the crate, pick up the crate, go back
//into the zone, profit
//it'd be rad as heck but it is also finnicky as heck so it's probs not happening yo
if (autoSelect == 4) {
SmartDashboard.putString("AutoMode", "fancypantsbot");
if (time < 3) {
liftUp.set(true);
}
else if (time > 5 && time < 6) {
fRD = -0.45;
rRD = 0.45;
fLD = -0.45;
rLD = 0.45;
}
else if (time > 6 && time < 9){
liftDown.set(true);
}
else if (time > 9 && time < 10){
fRD = -0.25;
rRD = -0.25;
fLD = 0.25;
rLD = 0.25;
}
else if (time > 10 && time < 11){
fRD = 0.25;
rRD = 0.25;
fLD = -0.25;
rLD = -0.25;
}
else if (time > 11 && time < 13) {
liftUp.set(true);
}
else if (time > 13 && time < 15) {
fRD = -0.5;
rRD = -0.5;
fLD = 0.5;
rLD = 0.5;
}
else if (time > 15){
fRD = 0;
rRD = 0;
fLD = 0;
rLD = 0;
}
}
//it's like a barrel roll, without the roll. or the barrel. we strafe for a couple seconds.
//i can't remember why this exists.
if (autoSelect == 5) {
SmartDashboard.putString("AutoMode", "DO A SIDE ROLL THING FOX");
if (gyroMode){
gyroHeading = 0;
}
if (time < 6 && fRDcoder.getDistance() > (-750*30)){
fRD = -0.45;
rRD = 0.45;
fLD = -0.45;
rLD = 0.45;
}
else {
fRD = 0;
rRD = 0;
fLD = 0;
rLD = 0;
}
}
//up! down! liftup! liftdown! it doesn't actually lift down. don't ask me why. ask the strongman.
if (autoSelect == 6){
SmartDashboard.putString("AutoMode", "the strongman?");
if (time < 5) {
liftUp.set(true);
}
else if (time > 5) {
liftDown.set(true);
}
}
//this is the single most difficult of all code to code. I sat here in front of the computer for many lightyears trying to
//figure out just how the heck. it is my masterpiece. it does literally nothing. you stand still.
if (autoSelect == 7) {
SmartDashboard.putString("AutoMode", "STANDING HERE, I REALIIIIZE");
fRD = 0;
rRD = 0;
fLD = 0;
rLD = 0;
}
//we go "nyoom" forward using our encoders this time, not just the timer. hopefully more accurate than
//that one evil clock jerk i hate
if (autoSelect == 8) {
double error = 50 - fRDcoder.getDistance();
double kP = 0.5;
double output = error*kP;
SmartDashboard.putString("AutoMode", "encode THIS, nerd");
if (time < 1) {
liftUp.set(true);
} else if (time > 1 && time < 8) {
if (fRDcoder.getDistance() < (50-dangerZone)){
fRD = output;
rRD = output;
fLD = -output;
rLD = -output;
}
else if (fRDcoder.getDistance() > (50+dangerZone)){
fRD = -output;
rRD = -output;
fLD = output;
rLD = output;
}
}
else if (time > 8 && time < 10){
if (gyroMode){
gyroHeading = 90;
}
else{
fRD = -0.325;
rRD = -0.325;
fLD = -0.325;
rLD = -0.325;
}
}
else if (timer.get() > 10){
fRD = 0;
rRD = 0;
fLD = 0;
rLD = 0;
}
}
if (autoSelect == 9){
SmartDashboard.putString("AutoMode", "crab bot 2k15");
if (timer.get() < 3){
if (gyroMode){
gyroHeading = -90;
}
else{
fRD = -0.325;
rRD = -0.325;
fLD = -0.325;
rLD = -0.325;
}
}
else if (timer.get() < 6){
fRD = -0.25;
rRD = -0.25;
fLD = 0.25;
rLD = 0.25;
}
}
if (autoSelect == 10){
SmartDashboard.putString("AutoMode", "wise mandible hug");
if (time < 1){
closeClaw.set(Relay.Value.kForward);
}
else if (time > 1 && time < 2){
liftUp.set(true);
}
else if (time > 2 && time < 8 && fRDcoder.getDistance() > (-3000)){
liftUp.set(false);
fRD = -0.25;
rRD = -0.25;
fLD = 0.25;
rLD = 0.25;
}
else if (time > 8 && time < 11){
gyroHeading = -90;
}
else if (time > 11 && time < 14) {
liftDown.set(true);
fRD = 0;
rRD = 0;
fLD = 0;
rLD = 0;
}
else if (time > 14 && time < 15) {
openClaw.set(Relay.Value.kForward);
}
}
if (autoSelect == 11){
SmartDashboard.putString("AutoMode", "counter mandible hug");
if (time < 1){
closeClaw.set(Relay.Value.kForward);
}
else if (time > 1 && time < 2){
liftUp.set(true);
}
else if (time > 2 && time < 8 && fRDcoder.getDistance() > (-3000)){
liftUp.set(false);
fRD = -0.25;
rRD = -0.25;
fLD = 0.25;
rLD = 0.25;
}
else if (time > 8 && time < 11){
gyroHeading = -90;
}
else if (time > 11 && time < 14) {
liftDown.set(true);
fRD = 0;
rRD = 0;
fLD = 0;
rLD = 0;
}
else if (time > 14 && time < 15) {
openClaw.set(Relay.Value.kForward);
}
}
//this right here makes sure our robot doesn't do crazy coolkid sicknasty 360 degree noscope turns every time we want
//to turn it a little bit to the right
if (rotation > 0.4){
rotation = 0.4;
}
if (rotation < -0.4){
rotation = -0.4;
}
//makes our robot atually do the thing
//the thing being moving
frontRightDrive.set(fRD + rotation);
rearRightDrive.set(rRD + rotation);
frontLeftDrive.set(fLD + rotation);
rearLeftDrive.set(rLD + rotation);
SmartDashboard.putNumber("fRDcoder", fRDcoder.getDistance());
SmartDashboard.putNumber("rRDcoder", rRDcoder.getDistance());
SmartDashboard.putNumber("fLDcoder", fLDcoder.getDistance());
SmartDashboard.putNumber("rLDcoder", rLDcoder.getDistance());
}
@Override
public void teleopInit() {
//recalibrates the gyro!
gyro.reset();
gyroHeading = gyro.getAngle();
gyroMode = false;
lux.set(Relay.Value.kForward);
fRDcoder.reset();
rRDcoder.reset();
fLDcoder.reset();
rLDcoder.reset();
}
/**
* This function is called periodically during operator control and david in just teh berst
*/
@Override
public void teleopPeriodic() {
//Gets the three axis of our joystick, accounting for the dead zone
double x = -valueWithDeadzone(joy.getRawAxis(0), .1);
double y = -valueWithDeadzone(joy.getRawAxis(1), .1);
double rotation = -valueWithDeadzone(joy.getRawAxis(2), .4);
double precision = joy.getRawAxis(3);
double xboxRX = xbox.getRawAxis(4);
double xboxRY = xbox.getRawAxis(5);
double lTrigger = xbox.getRawAxis(2);
double rTrigger = xbox.getRawAxis(3);
rotation = GyroMod(rotation);
SmartDashboard.putNumber("Gyro angle", gyro.getAngle());
SmartDashboard.putNumber("fRDcoder", fRDcoder.getDistance());
SmartDashboard.putNumber("rRDcoder", rRDcoder.getDistance());
SmartDashboard.putNumber("fLDcoder", fLDcoder.getDistance());
SmartDashboard.putNumber("rLDcoder", rLDcoder.getDistance());
//Triggers precision mode on or off, which slows the robot to 1/3 motor output
/*if (joy.getRawButton(7)) {
precisionMode = false;
}
if (joy.getRawButton(8)) {
precisionMode = true;
}*/
//Triggers gyro mode on or off (also resetting it to avoid dangerous funky time)
if (joy.getRawButton(11)) {
gyroMode = false;
gyro.reset();
gyroHeading = gyro.getAngle();
}
if (joy.getRawButton(12)) {
gyroMode = true;
gyro.reset();
gyroHeading = gyro.getAngle();
}
//LIFTS
liftUp.set(joy.getRawButton(8) || joy.getRawButton(1));
//SETS DOWN
liftDown.set(joy.getRawButton(2) || joy.getRawButton(7));
//sets the values for our four drives
double fRD = (x + y + rotation);
double rRD = -(x - y - rotation);
double fLD = -(-x + y - rotation);
double rLD = (-x - y + rotation);
//echoes of precision mode still linger on
if (precisionMode) {
fRD = fRD * ((precision+1) *0.5);
rRD = rRD * ((precision+1) *0.5);
fLD = fLD * ((precision+1) *0.5);
rLD = rLD * ((precision+1) *0.5);
}
SmartDashboard.putNumber("precision", precision);
//lets us know what our drives are up to, we're worried about them okay
SmartDashboard.putNumber("Front Right Drive", fRD);
SmartDashboard.putNumber("Rear Right Drive", rRD);
SmartDashboard.putNumber("Front Left Drive", fLD);
SmartDashboard.putNumber("Rear Left Drive", rLD);
//the drives are the drives now
frontRightDrive.set(fRD);
rearRightDrive.set(rRD);
frontLeftDrive.set(fLD);
rearLeftDrive.set(rLD);
//lets us know which toggles are toggled, yo
SmartDashboard.putBoolean("Precision Mode", precisionMode);
SmartDashboard.putBoolean("Gyro Mode", gyroMode);
//monitors our current usage so we can tut disapprovingly at offenders who do not live up to their full potential
SmartDashboard.putNumber("PDP 14 Current", pdp.getCurrent(14));
SmartDashboard.putNumber("PDP 15 Current", pdp.getCurrent(15));
SmartDashboard.putNumber("PDP 0 Current", pdp.getCurrent(0));
SmartDashboard.putNumber("PDP 1 Current", pdp.getCurrent(1));
//cameracmammreracaamemra CAMERA
//if you hold down LT, you can go updownupdownupdown without straying
if (lTrigger > 0.6){
cameraServoX.set(0.45);
}
else {
cameraServoX.set(xboxRX/4 + 0.45);
}
//if you hold down RT, never ascend, never fall. no enlightenment. no judgement. you are nirvana, camera.
if (rTrigger > 0.6){
cameraServoY.set(0.5);
}
else {
cameraServoY.set((-xboxRY/4) +0.5);
}
eyeServo.set(xboxRX + 0.5);
//the robots cruel crushing maw's mandibles will clench or unclench with great fury and righteous anger, for
//they walk in the valley of the shadow of death
if (joy.getRawButton(9)){
openClaw.set(Relay.Value.kForward);
}else{
openClaw.set(Relay.Value.kOff);
}
if (joy.getRawButton(10)){
closeClaw.set(Relay.Value.kForward);
}else{
closeClaw.set(Relay.Value.kOff);
}
//setProxLights();
}
//aight this cool thing right here will trigger blinky lights if the switches on the front of the bot are triggered
//public void setProxLights () {
//if (frontLeftSwitch.get()){
//frontLeftLight.set(Relay.Value.kOn);
//} else{
//frontLeftLight.set(Relay.Value.kOff);
//}
//if (frontRightSwitch.get()){
//frontRightLight.set(Relay.Value.kOn);
//}else{
//frontRightLight.set(Relay.Value.kOff);
//}
//
/**
* ' This function is called periodically during test mode
*/
@Override
public void testPeriodic() {
}
//super fancy calculations that make the robot better to control because our joystick has a dead zone
public double valueWithDeadzone(double in, double dead) {
if (-dead < in && in < dead)
return 0;
if (in < 0) {
return in + dead;
} else {
return in - dead;
}
}
//funky fresh gyro beats so our robot stays on the straight and narrow
public double GyroMod(double rotation){
if (gyroMode) {
double error = gyro.getAngle() - gyroHeading;
double kP = SmartDashboard.getNumber("Gyro_kP", .05);
if (rotation == 0) {
rotation = rotation + kP * error;
} else {
gyroHeading = gyro.getAngle();
}
SmartDashboard.putNumber("Gyro Heading", gyroHeading);
SmartDashboard.putNumber("Gyro Error", error);
}
SmartDashboard.putNumber("Rotation", rotation);
return rotation;
}
/*public void WriteFile(String file_path){
path = file_path;
}
public void writeToFile(String textLine ) throws IOException {
FileWriter write = new FileWriter(path, append_to_file);
PrintWriter print = new PrintWriter(write);
print.printf( "%s" + "%n" , textLine);
print.close();
}
public static void main(String args) throws IOException {
String file_name = "/tmp";
//try {
//ReadFile file = new ReadFile(file_name);
// }
}*/
}