forked from TheRealStayman/AISUMechanicalDragonsRoboPong2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlastYearsCode.java
375 lines (331 loc) · 11.4 KB
/
lastYearsCode.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
package org.usfirst.frc.team5974.robot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.CameraServer; //The Camera
//import edu.wpi.first.wpilibj.vision.USBCamera;
import edu.wpi.first.wpilibj.DigitalInput; //Digital inputs
import edu.wpi.first.wpilibj.Timer; // The Timer
import edu.wpi.first.wpilibj.IterativeRobot; //Guess
import edu.wpi.first.wpilibj.Joystick; //The Controller
import edu.wpi.first.wpilibj.VictorSP; //The Motor Controller
import edu.wpi.first.wpilibj.interfaces.Accelerometer;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; //The Dashboard
//import edu.wpi.first.wpilibj.interfaces.Gyro; //The Gyro
import edu.wpi.first.wpilibj.ADXL362; //The Accelerometer
import edu.wpi.first.wpilibj.ADXRS450_Gyro;
public class Robot extends IterativeRobot {
//Defining Variables
RobotDrive myDrive;
Joystick controller;
CameraServer cameraServer;
//USBCamera camera;
int mode; //Steps for Autonomous Code
double j1X_axis; //Left joystick X and Y axis
double j1Y_axis;
double leftTrigger;
double rightTrigger;
double j2X_axis; //Right joystick X and Y axis
double j2Y_axis;
double newTime = 0; //Beginning and end of loop.
double oldTime = 0;
double dT; //Delta Time
double velocity = 0;
double pos = 0; //Position
double accelAdjust = 0; //Adjusting the accelerometer
boolean leftBumper;
boolean yButton;
boolean xButton;
double robotDirection = 0;
double robotSpeed = 0;
int over9000 = 9001;
Timer timer = new Timer(); //Defining variables
ADXRS450_Gyro gyro = new ADXRS450_Gyro();
ADXL362 accel = new ADXL362(Accelerometer.Range.k2G); //Accelerometer
DigitalInput limitSwitch = new DigitalInput(0); //limit switch
VictorSP upperLeftMotor = new VictorSP(0); //Upper left Motor
VictorSP lowerRightMotor = new VictorSP(1); //Lower Right Motor
VictorSP upperRightMotor = new VictorSP(3); //Upper Right Motor
VictorSP lowerLeftMotor = new VictorSP(2); //Lower Left Motor
VictorSP beltMotor1 = new VictorSP(4); //Ball picker-upper Motor Controller
//VictorSP beltMotor2 = new VictorSP(5);
public void updateStuff() { //Defining variables that need to be updated
j1X_axis = controller.getRawAxis(0); //left joystick
j1Y_axis = controller.getRawAxis(1);
leftTrigger = controller.getRawAxis(2); //ball picker-upper Motors
rightTrigger = controller.getRawAxis(3);
j2X_axis = controller.getRawAxis(4); //right joystick
j2Y_axis = controller.getRawAxis(5);
leftBumper = controller.getRawButton(5); //Speed Boost
xButton = controller.getRawButton(3);
yButton = controller.getRawButton(4);
cameraServer = CameraServer.getInstance(); //Camera
//camera = new USBCamera("cam0");
deadZone(); //See below
directionOfRobot();
speedOfRobot();
smartDashboardWidgets();
}
public void deadZone() { //The dead Zones for the Joysitcks and accelerometer
/* Joysticks when not touched may not be exactly zero. This code *
* tells the joysticks to be set to zero when they are not *
* touched. Accelerometers can get values from gravity and *
* rumbling. That is why we set dead zones for that as well */
if (j1Y_axis <= 0.15 && j1Y_axis >= -0.15) { //For joysticks
j1Y_axis = 0;
}
if (j2Y_axis <= 0.15 && j2Y_axis >= -0.15) {
j2Y_axis = 0;
}
//For Accelerometer
if (accel.getY() <= 0.010 && accel.getY() >= -0.010) {
accelAdjust = 0;
}
else {
accelAdjust = accel.getY();
}
}
public void ballStopper() { //Stops the motors from spinning when Limit Switch is pressed
if (limitSwitch.get()) {
beltMotor1.set(0);
//beltMotor2.set(0);
}
}
public void directionOfRobot() { //Calculates where the Robot is facing
robotDirection = robotDirection + (gyro.getAngle());
if (robotDirection > 130) {
robotDirection = -129;
}
else if (robotDirection < -130) {
robotDirection = 129;
}
}
public void speedOfRobot() { //Calculates the speed of the robot
robotSpeed = (Math.abs(j1Y_axis) + Math.abs(j2Y_axis)) * 100;
}
public void yMovement() { //Calculates the distance the robot has gone
oldTime = newTime;
newTime = timer.get();
dT = newTime - oldTime;
velocity += dT * accelAdjust;
pos += dT * velocity; //1 ~= 9.8 Meters
}
public void smartDashboardWidgets() { //Gathers data and sends it to the Dashboard
boolean alwaysTrue = true;
SmartDashboard.putBoolean("Limit Switch", limitSwitch.get());
SmartDashboard.putNumber("Direction", robotDirection);
SmartDashboard.putNumber("Speed", velocity);
SmartDashboard.putNumber("Speed X Axis", accel.getX());
SmartDashboard.putNumber("Speed Y Axis", accelAdjust);
SmartDashboard.putNumber("Gyro Value", gyro.getAngle());
SmartDashboard.putNumber("Movement", pos);
}
public void robotInit() { //When the robot turns on
controller = new Joystick(0);
updateStuff(); //Updates variables
newTime = 0; //sets timer to 0
gyro.calibrate();
//Real time camera code
//camera.setSize(640, 480);
//camera.setFPS(15);
//camera.updateSettings();
cameraServer.setQuality(25);
cameraServer.startAutomaticCapture("cam0");
}
/* ___ _____ _
/ _ \ |_ _| |
/ /_\ \_ _| | | |__ ___ _ __ ___ __ _ ___
| _ | | | | | | '_ \ / _ \| '_ ` _ \ / _` / __|
| | | | |_| | | | | | | (_) | | | | | | (_| \__ \
\_| |_/\__,_\_/ |_| |_|\___/|_| |_| |_|\__,_|___/
*/
public void autonomousInit() {
timer.start();
gyro.reset();
pos = 0;
mode = 1;
System.out.println("Let the ankle breaking commence!");
controller.setRumble(Joystick.RumbleType.kRightRumble, 1); //Rumbles the controller
controller.setRumble(Joystick.RumbleType.kLeftRumble, 1);
Timer.delay(0.25);
controller.setRumble(Joystick.RumbleType.kRightRumble, 0);
controller.setRumble(Joystick.RumbleType.kLeftRumble, 0);
Timer.delay(0.25);
controller.setRumble(Joystick.RumbleType.kRightRumble, 1);
controller.setRumble(Joystick.RumbleType.kLeftRumble, 1);
Timer.delay(0.25);
controller.setRumble(Joystick.RumbleType.kRightRumble, 0);
controller.setRumble(Joystick.RumbleType.kLeftRumble, 0);
}
public void autonomousPeriodic() {
yMovement(); //calling the functions
updateStuff();
/* while (pos <= 0.818) { //Go forward until we reach this distance
upperLeftMotor.set(-0.5);
upperRightMotor.set(-0.5);
}
upperLeftMotor.set(0.5); //Stop
upperRightMotor.set(0.5);
Timer.delay(0.01);
upperLeftMotor.set(0);
upperRightMotor.set(0);
Timer.delay(1);
while (gyro.getAngle() <= 65) {
upperLeftMotor.set(-0.5); //Turn Right
upperRightMotor.set(0.5);
}
Timer.delay(0.1);
upperLeftMotor.set(0.375);
upperRightMotor.set(-0.375);
Timer.delay(0.01);
upperLeftMotor.set(0);
upperRightMotor.set(0);
*/
if (mode == 0) {
upperLeftMotor.set(0);
upperRightMotor.set(0);
beltMotor1.set(0);
}
if (mode == 1) {
//if (pos <= 0.734) { //Go forward until this distance is reached
upperLeftMotor.set(-0.52);
upperRightMotor.set(-0.5);
System.out.println(pos);
smartDashboardWidgets();
Timer.delay(5);
//yMovement();
//}
//else {
//Timer.delay(0.1);
upperLeftMotor.set(0.5); //Stop
upperRightMotor.set(0.5);
Timer.delay(0.01);
mode = 2;
//}
}
if (mode == 2) {
upperLeftMotor.set(0);
upperRightMotor.set(0);
Timer.delay(0.1);
upperLeftMotor.set(0.5); //Go backwards a bit
upperRightMotor.set(0.5);
Timer.delay(0.5);
upperLeftMotor.set(0);
upperRightMotor.set(0);
Timer.delay(0.1);
pos = 0;
gyro.reset();
yMovement();
mode = 3;
}
if (mode == 3) {
if (gyro.getAngle() <= 60) {
upperLeftMotor.set(-0.3); //Turn Right
upperRightMotor.set(0.3);
smartDashboardWidgets();
}
else {
//Timer.delay(0.1);
upperLeftMotor.set(0.375);
upperRightMotor.set(-0.375);
Timer.delay(0.01);
upperLeftMotor.set(0);
upperRightMotor.set(0);
pos = 0;
yMovement();
gyro.reset();
mode = 4;
}
}
if (mode == 4) {
//if (pos <= 0.339) { //Go forward a little bit longer
upperLeftMotor.set(-0.5);
upperRightMotor.set(-0.5);
Timer.delay(3);
//yMovement();
//}
//else {
//upperLeftMotor.set(0); //Stop
//upperRightMotor.set(0);
//Timer.delay(0.1);
//mode = 5;
//}
}
if (mode == 5) {
beltMotor1.set(1); //Spit out the ball
//beltMotor2.set(1);
Timer.delay(5);
beltMotor1.set(0);
//beltMotor2.set(0);
timer.stop();
mode = 0;
}
}
/* _____ _
|_ _| | |
| | ___| | ___ ___ _ __
| |/ _ \ |/ _ \/ _ \| '_ \
| | __/ | __/ (_) | |_) |
\_/\___|_|\___|\___/| .__/
| |
|_|
*/
public void teleopInit(){
System.out.println("Test Started!");
controller.setRumble(Joystick.RumbleType.kRightRumble, 1); //Rumbles the controller for 1 second
controller.setRumble(Joystick.RumbleType.kLeftRumble, 1);
Timer.delay(1);
controller.setRumble(Joystick.RumbleType.kRightRumble, 0);
controller.setRumble(Joystick.RumbleType.kLeftRumble, 0);
pos = 0;
gyro.reset();
timer.start();
}
public void teleopPeriodic(){
//Updating values
updateStuff();
smartDashboardWidgets();
System.out.println("Ready to drive!");
//Controller buttons
if(leftBumper == true) {
System.out.println("Left Bumper Pressed!"); //Go full speed when the leftBumper is pressed
}
else {
j1Y_axis = j1Y_axis/2; //Else, Go only half speed
j2Y_axis = j2Y_axis/2;
robotSpeed = robotSpeed/2;
}
if(xButton == true && yButton == true) {
System.out.println("Do a Barrel Roll");
upperLeftMotor.set(1);
lowerLeftMotor.set(1);
upperRightMotor.set(-1);
lowerRightMotor.set(-1);
Timer.delay(1);
upperLeftMotor.set(0);
lowerLeftMotor.set(0);
upperRightMotor.set(0);
lowerRightMotor.set(0);
}
//Controlling the Robot with Tank Drive
upperLeftMotor.set(j1Y_axis); //Control the left motors with the first joystick
lowerLeftMotor.set(j1Y_axis);
upperRightMotor.set(j2Y_axis); //Control the right motors with the second joystick
lowerRightMotor.set(j2Y_axis);
//Loading and Unloading Function
if (leftTrigger > 0) { //When left trigger is pressed load a ball
beltMotor1.set(leftTrigger);
//beltMotor2.set(leftTrigger);
ballStopper();
}
else if (rightTrigger > 0) { //When right trigger is pressed unload a ball
beltMotor1.set(-1 * rightTrigger);
//beltMotor2.set(-1 * rightTrigger);
}
else {
beltMotor1.set(0);
//beltMotor2.set(0);
}
SmartDashboard.putNumber("Loop", timer.get()); //Timer loop
yMovement();
SmartDashboard.putNumber("Y Position", pos); //Distance the robot has gone
}
}