-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.java
More file actions
executable file
·503 lines (453 loc) · 17.9 KB
/
Robot.java
File metadata and controls
executable file
·503 lines (453 loc) · 17.9 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
package org.usfirst.frc.team3238.robot;
import java.util.ArrayList;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.BuiltInAccelerometer;
import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Servo;
/**
* 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
{
Autonomous autonomous;
Chassis chassis;
PIController piControllerGrabber;
PIController piControllerLifterLeft, piControllerLifterRight;
CANTalon leftFrontTalon, rightFrontTalon,
leftRearTalon, rightRearTalon;
CANTalon grabberVerticalTalon, grabberHorizontalTalon;
CANTalon leftLifterTalon, rightLifterTalon;
DigitalInput reflectSensorFront, reflectSensorRear;
AnalogInput gyroSensor;
BuiltInAccelerometer accelerometer;
AnalogInput infraredSensor, sonarSensor;
ToteLifter toteLifter;
Grabber grabber;
ArrayList<String> fileContents;
AnalogInput grabberVerticalPot, grabberHorizontalPot;
AnalogInput lifterLeftPot, lifterRightPot;
Joystick joystickZero, joystickOne, joystickTwo;
Servo leftLifterServo, rightLifterServo;
//Subsytem Constants
double m_lifterLeftP;
double m_lifterLeftI;
double m_lifterRightP;
double m_lifterRightI;
double m_lifterAccuracyThreshold;
double m_grabberHorizontalP;
double m_grabberHorizontalI;
double m_grabberVerticalP;
double m_grabberVerticalI;
double m_spinThreshold;
double m_infraredDistanceTrigger;
long m_timeIgnore;
int m_timeDriveBack;
int m_timeDriveAway;
double m_moveBackSpeed;
double m_canExtendHeight;
double m_canGrabHeight;
double m_toteExtendHeight;
double m_toteGrabHeight;
double m_stepCanExtendHeight;
double m_stepCanGrabHeight;
double m_retractedLocation;
double m_horizontalThreshold;
double m_verticalThreshold;
double m_pauseDistanceFromObject;
double m_grabberHorizontalGentleP;
double m_grabberHorizontalGentleI;
double m_grabberHorizontalHome;
double m_grabberVerticalHome;
double m_lifterHomeHeight;
double m_lifterWaitLiftPosition;
double m_lifterOpenDogsLiftPosition;
double m_lifterCloseDogsLiftPosition;
double m_slowDownRetractThreshold;
double m_homeOffset;
double m_gyroPConstant;
double m_gyroIConstant;
int m_topHatValue;
int m_previousTopHatValue;
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit()
{
//Talon Ports
//Chassis
final int CHASSISLEFTFRONTTALONID = 2;
final int CHASSISLEFTREARTALONID = 4;
final int CHASSISRIGHTFRONTTALONID = 8;
final int CHASSISRIGHTREARTALONID = 6;
//Grabber
final int GRABBERVERTICALTALONID = 1;
final int GRABBERHORIZONTALTALONID = 3;
//Lifter
final int LIFTERLEFTTALONID = 7;
final int LIFTERRIGHTTALONID = 5;
// Digital Inputs
final int REFLECTSENSORFRONTPORT = 5;
final int REFLECTSENSORREARPORT = 6;
// Analog Inputs
//Autonomous
final int IRSENSORPORT = 1;
final int GYROSENSORPORT = 2;
//Grabber
final int SONARSENSORPORT = 0;
final int GRABBERVERTICALPOTPORT = 4;
final int GRABBERHORIZONTALPOTPORT = 5;
//Lifter
final int LIFTERLEFTPOTPORT = 7;
final int LIFTERRIGHTPOTPORT = 6;
// Servo Ports
final int SERVOLEFTPORT = 0;
final int SERVORIGHTPORT = 1;
// Driver Station Inputs
final int JOYSTICKPORT = 0;
joystickZero = new Joystick(JOYSTICKPORT);
joystickOne = new Joystick(1);
joystickTwo = new Joystick(2);
leftFrontTalon = new CANTalon(CHASSISLEFTFRONTTALONID);
rightFrontTalon = new CANTalon(CHASSISRIGHTFRONTTALONID);
leftRearTalon = new CANTalon(CHASSISLEFTREARTALONID);
rightRearTalon = new CANTalon(CHASSISRIGHTREARTALONID);
grabberVerticalTalon = new CANTalon(GRABBERVERTICALTALONID);
grabberHorizontalTalon = new CANTalon(GRABBERHORIZONTALTALONID);
grabberVerticalPot = new AnalogInput(GRABBERVERTICALPOTPORT);
grabberHorizontalPot = new AnalogInput(GRABBERHORIZONTALPOTPORT);
grabberVerticalPot.setAverageBits(3);
grabberHorizontalPot.setAverageBits(3);
reflectSensorFront = new DigitalInput(REFLECTSENSORFRONTPORT);
reflectSensorRear = new DigitalInput(REFLECTSENSORREARPORT);
gyroSensor = new AnalogInput(GYROSENSORPORT);
gyroSensor.setAverageBits(2);
infraredSensor = new AnalogInput(IRSENSORPORT);
infraredSensor.setAverageBits(2);
sonarSensor = new AnalogInput(SONARSENSORPORT);
accelerometer = new BuiltInAccelerometer();
leftLifterTalon = new CANTalon(LIFTERLEFTTALONID);
rightLifterTalon = new CANTalon(LIFTERRIGHTTALONID);
lifterLeftPot = new AnalogInput(LIFTERLEFTPOTPORT);
lifterRightPot = new AnalogInput(LIFTERRIGHTPOTPORT);
lifterLeftPot.setAverageBits(2);
lifterRightPot.setAverageBits(2);
leftLifterServo = new Servo(SERVOLEFTPORT);
rightLifterServo = new Servo(SERVORIGHTPORT);
setSubsystemConstants();
chassis = new Chassis(leftFrontTalon,
leftRearTalon, rightFrontTalon,
rightRearTalon);
grabber = new Grabber(grabberVerticalTalon, grabberHorizontalTalon,
grabberVerticalPot, grabberHorizontalPot, sonarSensor,
m_grabberVerticalP, m_grabberVerticalI, m_grabberHorizontalP,
m_grabberHorizontalI, m_grabberHorizontalGentleP,
m_grabberHorizontalGentleI, m_horizontalThreshold,
m_verticalThreshold, m_canExtendHeight, m_canGrabHeight,
m_toteExtendHeight, m_toteGrabHeight, m_stepCanExtendHeight,
m_stepCanGrabHeight, m_retractedLocation,
m_pauseDistanceFromObject, m_grabberHorizontalHome,
m_grabberVerticalHome, m_slowDownRetractThreshold, infraredSensor);
toteLifter = new ToteLifter(leftLifterTalon, rightLifterTalon,
leftLifterServo, rightLifterServo, lifterLeftPot,
lifterRightPot, m_lifterLeftP, m_lifterLeftI, m_lifterRightP,
m_lifterRightI, m_lifterAccuracyThreshold, m_lifterHomeHeight,
m_lifterWaitLiftPosition, m_lifterOpenDogsLiftPosition,
m_lifterCloseDogsLiftPosition, m_homeOffset, infraredSensor);
autonomous = new Autonomous(chassis);
}
public void setSubsystemConstants()
{
fileContents = FileReader.readFile("RobotConstants.txt");
m_lifterLeftP = Double.parseDouble(fileContents.get(27));
m_lifterLeftI = Double.parseDouble(fileContents.get(30));
m_lifterRightP = Double.parseDouble(fileContents.get(33));
m_lifterRightI = Double.parseDouble(fileContents.get(36));
m_lifterAccuracyThreshold =
Double.parseDouble(fileContents.get(39));
m_grabberHorizontalP = Double.parseDouble(fileContents.get(56));
m_grabberHorizontalI = Double.parseDouble(fileContents.get(59));
m_grabberVerticalP = Double.parseDouble(fileContents.get(50));
m_grabberVerticalI = Double.parseDouble(fileContents.get(53));
m_spinThreshold = Double.parseDouble(fileContents.get(22));
m_infraredDistanceTrigger =
Double.parseDouble(fileContents.get(5));
m_timeIgnore = Long.parseLong(fileContents.get(8));
m_timeDriveBack = Integer.parseInt(fileContents.get(11));
m_timeDriveAway = Integer.parseInt(fileContents.get(14));
m_moveBackSpeed = Double.parseDouble(fileContents.get(17));
m_canExtendHeight = Double.parseDouble(fileContents.get(62));
m_canGrabHeight = Double.parseDouble(fileContents.get(65));
m_toteExtendHeight = Double.parseDouble(fileContents.get(68));
m_toteGrabHeight = Double.parseDouble(fileContents.get(71));
m_stepCanExtendHeight = Double.parseDouble(fileContents.get(74));
m_stepCanGrabHeight = Double.parseDouble(fileContents.get(77));
m_retractedLocation = Double.parseDouble(fileContents.get(80));
m_horizontalThreshold = Double.parseDouble(fileContents.get(83));
m_verticalThreshold = Double.parseDouble(fileContents.get(86));
m_pauseDistanceFromObject =
Double.parseDouble(fileContents.get(89));
m_grabberHorizontalGentleP =
Double.parseDouble(fileContents.get(92));
m_grabberHorizontalGentleI =
Double.parseDouble(fileContents.get(95));
m_grabberHorizontalHome =
Double.parseDouble(fileContents.get(98));
m_grabberVerticalHome = Double.parseDouble(fileContents.get(101));
m_lifterHomeHeight = Double.parseDouble(fileContents.get(104));
m_lifterWaitLiftPosition = Double.parseDouble
(fileContents.get(107));
m_lifterOpenDogsLiftPosition = Double.parseDouble
(fileContents.get(110));
m_lifterCloseDogsLiftPosition = Double.parseDouble
(fileContents.get(113));
m_slowDownRetractThreshold =
Double.parseDouble(fileContents.get(116));
m_homeOffset = Double.parseDouble(fileContents.get(119));
m_gyroPConstant = Double.parseDouble(fileContents.get(122));
m_gyroIConstant = Double.parseDouble(fileContents.get(125));
}
public void reinputConstants()
{
setSubsystemConstants();
grabber.inputConstants(m_grabberVerticalP, m_grabberVerticalI,
m_grabberHorizontalP, m_grabberHorizontalI,
m_grabberHorizontalGentleP, m_grabberHorizontalGentleI,
m_horizontalThreshold, m_verticalThreshold, m_canExtendHeight,
m_canGrabHeight, m_toteExtendHeight, m_toteGrabHeight,
m_stepCanExtendHeight, m_stepCanGrabHeight, m_retractedLocation,
m_pauseDistanceFromObject, m_grabberHorizontalHome,
m_grabberVerticalHome, m_slowDownRetractThreshold);
toteLifter.inputConstants(m_lifterLeftP, m_lifterLeftI,
m_lifterRightP, m_lifterRightI, m_lifterAccuracyThreshold,
m_lifterHomeHeight, m_lifterWaitLiftPosition,
m_lifterOpenDogsLiftPosition, m_lifterCloseDogsLiftPosition,
m_homeOffset);
//autonomous.inputConstants(m_infraredDistanceTrigger, m_timeIgnore,
// m_timeDriveBack, m_timeDriveAway, m_moveBackSpeed);
}
public void autonomousInit()
{
reinputConstants();
//System.out.println(toteLifter.m_homeHeight);
//toteLifter.zeroPots();
leftLifterServo.setAngle(0);
rightLifterServo.setAngle(180);
//toteLifter.reinitPIControllers();
//toteLifter.zeroPots();
//grabber.zeroPots();
//grabber.grabStepCan();
grabber.reset();
autonomous.init();
grabber.zeroPots();
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic()
{
//toteLifter.mapPots();
//System.out.print("Left: " + toteLifter.m_leftHeight);
//System.out.println(" Right: " + toteLifter.m_rightHeight);
//chassis.setJoystickData(0, 0, 0);
autonomous.idle(chassis, gyroSensor.getAverageVoltage(), m_spinThreshold,
toteLifter, grabber, m_gyroPConstant, m_gyroIConstant);
chassis.idle();
grabber.idle();
System.out.println(grabber.horizontalTalon.getOutputCurrent());
//grabber.idle();
//System.out.println(grabber.m_canHorizontalState);
//System.out.println(grabber.horizontalTalon.getOutputCurrent());
// grabber.goToLength(0.689);
// System.out.println(toteLifter.goToHeight(0.4));
}
/**
*
*/
public void teleopInit()
{
reinputConstants();
grabber.reset();
System.out.println(grabber.m_horizontalP);
System.out.println(grabber.m_horizontalI);
toteLifter.disable();
GyroDrive.reinit();
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic()
{
// toteLifter.mapPots();
toteLifter.mapDistance();
//System.out.println(toteLifter.m_infraredDistance);
// System.out.print("Left: " + toteLifter.m_leftHeight);
// System.out.println(" Right: " + toteLifter.m_rightHeight);
m_previousTopHatValue = m_topHatValue;
m_topHatValue = joystickZero.getPOV();
if(joystickZero.getRawButton(1))
{
toteLifter.dropTotes();
}
if(joystickZero.getRawButton(2))
{
toteLifter.addTote();
}
if(joystickZero.getRawButton(7))
{
toteLifter.closeDogs();
}
if(joystickZero.getRawButton(8))
{
toteLifter.openDogs();
}
if(joystickZero.getRawButton(5))
{
toteLifter.dropFullTotes();
}
if(joystickZero.getRawButton(9))
{
grabber.goToAvoidTotePosition();
}
if(joystickZero.getRawButton(3))
{
grabber.grabTote();
}
if(joystickZero.getRawButton(4))
{
grabber.grabCan();
}
if(joystickZero.getRawButton(6))
{
grabber.grabStepCan();
}
if(joystickZero.getRawButton(10))
{
toteLifter.approachHome();
}
if(joystickZero.getRawButton(11))
{
grabber.zeroPots();
}
if(joystickZero.getRawButton(12))
{
grabber.goHome();
}
//System.out.println(0.2680762026*Math.pow(infraredSensor.getAverageVoltage(),
// -1.130124285));
///System.out.println(toteLifter.leftTalon.getOutputCurrent());
//System.out.println(toteLifter.leftTalon.getOutputCurrent());
//System.out.println(grabber.horizontalTalon.getOutputCurrent());
System.out.println(grabber.m_horizontalPotDistance);
toteLifter.idle();
//System.out.println("Tote = "+ grabber.m_toteHorizontalState );
//System.out.println(" Can = "+grabber.m_canHorizontalState);
//System.out.println(" Vertical = "+grabber.m_verticalState);
if(m_topHatValue == -1)
{
if(m_previousTopHatValue != -1)
{
grabber.horizontalTalon.set(0);
}
grabber.idle();
}
else
{
grabber.reset();
//grabber.horizontalTalon.set(0);
//grabber.verticalTalon.set(0);
//System.out.println(m_topHatValue);
grabber.verticalTalon.set(1*Math.sin((m_topHatValue+90)*(Math.PI/180.0)));
grabber.horizontalTalon.set(0.75*Math.cos((m_topHatValue+270)*(Math.PI/180.0)));
}
double x = joystickZero.getX();
double y = joystickZero.getY();
double twist = joystickZero.getTwist();
double deadzone = 0.15;
if(twist < 0)
{
if (twist > -deadzone)
{
twist = 0;
}
else
{
twist += deadzone;
twist = twist*(1/(1-deadzone));
}
}
else
{
if (twist < deadzone)
{
twist = 0;
}
else
{
twist -= deadzone;
twist = twist*(1/(1-deadzone));
}
}
/* Maps the value of the joystick using the current position of the
throttle slider for safety and ease of driver control. */
double throttleMapping = Math.abs((joystickZero.getThrottle() - 1));
double adjustedRotation = GyroDrive.getAdjustedRotationValue(
x * throttleMapping, y * throttleMapping,
twist * throttleMapping, m_gyroPConstant, m_gyroIConstant,
m_spinThreshold, gyroSensor.getAverageVoltage() - 2.37);
chassis.setJoystickData(x * throttleMapping, y * throttleMapping,
adjustedRotation);
chassis.idle();
}
public void disabledPeriodic()
{
// grabber.mapSensors();
// System.out.print("Horizontal: " + grabber.m_horizontalPotDistance);
// System.out.print(" Vertical: " + grabber.m_verticalPotDistance);
// System.out.println(" Sonar: " + grabber.m_sonarDistance);
}
public void testInit()
{
reinputConstants();
leftLifterServo.setAngle(0);
rightLifterServo.setAngle(180);
}
/**
* This function is called periodically during test mode
*/
public void testPeriodic()
{
double twist = joystickZero.getTwist();
if(twist < 0)
{
if(twist > -0.35)
{
twist = 0;
}
else
{
twist += 0.35;
twist = twist*(1/.65);
}
}
leftLifterTalon.set(-joystickZero.getY());
rightLifterTalon.set(joystickOne.getY());
if(joystickZero.getRawButton(1))
{
leftLifterServo.setAngle(0);
rightLifterServo.setAngle(180);
}
if(joystickZero.getRawButton(2))
{
leftLifterServo.setAngle(170);
rightLifterServo.setAngle(10);
}
}
}