-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobot.java_USE_STATE_VARIABLES
441 lines (311 loc) · 11.3 KB
/
robot.java_USE_STATE_VARIABLES
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
package org.usfirst.frc.team6418.robot;
import java.util.*;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import edu.wpi.cscore.CvSink;
import edu.wpi.cscore.CvSource;
import org.opencv.imgproc.Imgproc;
import edu.wpi.cscore.UsbCamera;
//import org.usfirst.frc.team6418.robot.commands.ExampleCommand;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.ADXRS450_Gyro;
import org.usfirst.frc.team6418.robot.subsystems.*;
import org.opencv.core.*;
import org.usfirst.frc.team6418.robot.commands.*;
/**
* 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 {
public static final int WINCHMOTOR = 2;
//public static OI oi;
public static RobotDrive driveTrain;
//public static RobotDrive driveTrain;
public static Winch winch;
public static GreenLightSystem greenlight = new GreenLightSystem();
Command autonomousCommand;
SendableChooser<Command> chooser = new SendableChooser<>();
public static CameraServer cameraServer;
//Testing UI, probably should put all joystick and buttons in a separate OI class
public static CameraServer cameraServer2;
public Joystick stick = new Joystick(0);
public Joystick stick2 = new Joystick(1);
//for the second joystick
//need to find the right port
//changed button names-Anya
public JoystickButton TriggerButton = new JoystickButton(stick,1);
public JoystickButton SideButton = new JoystickButton(stick,2);
//changed button to 3 to optimize for joystick (was 8)-Anya
public JoystickButton HighButton = new JoystickButton(stick,5);
public JoystickButton LowButton = new JoystickButton(stick, 3);
//TEST - Anya
public JoystickButton RightButton = new JoystickButton(stick,4);
//use Trigger Button and Side Button , togggleWhenPressed(final Command command)
//to toggle between arcadeDrive and tankDrive
//use TopButton to turn the green light on and off
public static String driveMode;
public static String cameraMode;
public static Timer myTimer = new Timer();
//hk
public static ADXRS450_Gyro gyro = new ADXRS450_Gyro();
//hk
//Sensors
public static UltraSonic s1;
//Encoders
public static Encoder leftEncoder;
public static Encoder rightEncoder;
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
//Anya
public SpeedController left;
public SpeedController right;
//drivesides
public Spark leftSpark;
public Spark rightSpark;
double initialLeftEncoderRead;
double initialRightEncoderRead;
@Override
public void robotInit() {
//oi = new OI();
chooser.addDefault("Default Auto", new ExampleCommand());
// chooser.addObject("My Auto", new MyAutoCommand());
SmartDashboard.putData("Auto mode", chooser);
//cameraServer.getInstance().startAutomaticCapture();
//cameraServer2.getInstance().startAutomaticCapture();
//Anya-- changed drive train to left, right. Was 1,0. Added DriveSide
//double KpR = 2;
winch = new Winch(WINCHMOTOR, 9);
//how to call: public void tankDrive(double x, double y){
//winch.tankDrive(stick.getRawAxis(2), 0);
leftSpark = new Spark(0);
rightSpark = new Spark(1);
left = leftSpark;
right = rightSpark;
leftEncoder = new Encoder(1, 2, true, Encoder.EncodingType.k4X);
rightEncoder = new Encoder(4, 3, false, Encoder.EncodingType.k4X);
//Testing Sensor...
s1 = new UltraSonic(0);
//Testing Video ...
/* new Thread(() -> {
UsbCamera camera = CameraServer.getInstance().startAutomaticCapture();
camera.setResolution(640, 480);
CvSink cvSink = CameraServer.getInstance().getVideo();
CvSource outputStream = CameraServer.getInstance().putVideo("Blur", 640, 480);
Mat source = new Mat();
Mat output = new Mat();
//List<Mat> bgr = new Vector<Mat>();
while(!Thread.interrupted()) {
cvSink.grabFrame(source);
// Core.split(source,bgr );
Imgproc.cvtColor(source, output, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(output, output, 150, 255, Imgproc.THRESH_TOZERO);
outputStream.putFrame(output);
}
}).start(); */
//switching between drive modes - Halie
driveMode ="TANK";
cameraMode="BACK";
//if a is pressed, driveMode = "ARCADE"
//if b is pressed, diveMode= "TANK"
//togglewhenpressed
TriggerButton.whenPressed(new SetTankDrive());
SideButton.whenPressed(new SetArcadeDrive());
RightButton.whenPressed(new SetTankDriveBack());
HighButton.whenPressed(new FrontCamera());
LowButton.whenPressed(new BackCamera());
//TopButton.whenPressed(new GreenLight());
//RightButton.whenPressed(new MoveForward());
}
/**
* This function is called once each time the robot enters Disabled mode.
* You can use it to reset any subsystem information you want to clear when
* the robot is disabled.
*/
@Override
public void disabledInit() {
this.left.disable();
this.right.disable();
}
@Override
public void disabledPeriodic() {
Scheduler.getInstance().run();
}
/**
* This autonomous (along with the chooser code above) shows how to select
* between different autonomous modes using the dashboard. The sendable
* chooser code works with the Java SmartDashboard. If you prefer the
* LabVIEW Dashboard, remove all of the chooser code and uncomment the
* getString code to get the auto name from the text box below the Gyro
*
* You can add additional auto modes by adding additional commands to the
* chooser code above (like the commented example) or additional comparisons
* to the switch structure below with additional strings & commands.
*/
@Override
public void autonomousInit() {
autonomousCommand = chooser.getSelected();
double KpR=9/72.0;
double KpL=9/72.0;
double Ki = 0.01/72.0;
double Kd= 1/72.0;
//double KfR= 1.4/36.0;
//double KfL=1.45/36.0;
double Kf = 1.0;
this.right = new Good_DriveSide(KpR, Ki, Kd, Kf, rightEncoder, rightSpark);
right.setInverted(true);
this.left = new Good_DriveSide(KpL, Ki, Kd, Kf, leftEncoder, leftSpark);
driveTrain = new RobotDrive(left,right);
/*
* String autoSelected = SmartDashboard.getString("Auto Selector",
* "Default"); switch(autoSelected) { case "My Auto": autonomousCommand
* = new MyAutoCommand(); break; case "Default Auto": default:
* autonomousCommand = new ExampleCommand(); break; }
*/
//autonomousCommand = new MoveForward();
// schedule the autonomous command (example)
if (autonomousCommand != null)
autonomousCommand.start();
myTimer.reset();
myTimer.start();
System.out.println("Autonomous!");
System.out.println("StartTime: " + myTimer.get());
System.out.println();
System.out.println();
// initialLeftEncoderRead = ((Good_DriveSide) left).getEncoderCount();
//CASTS
// Encoder leftEncoder = ((Good_DriveSide) left).getEncoder();
//leftEncoder.reset();
gyro.calibrate();
// System.out.println("gyro calibration: " + gyro.getAngle());
}
/**
* This function is called periodically during autonomous
*/
@Override
public void autonomousPeriodic() {
Scheduler.getInstance().run();
// System.out.println("encoder count: "+ ((Good_DriveSide) left).getEncoderCount());
//QUESTION: if i made my own method inside good_driveside, which implements speedController
//do I have to cast left as a goodDriveside instead of speedcontroller?
System.out.println();
System.out.println("\t\tRight motor: " + right.get());
if (myTimer.get() < 3) {
//timer measured in seconds
moveForward(9.5);
}
else if (myTimer.get() > 3 && myTimer.get() < 5) {
left.set(0);
right.set(0);
}
System.out.println("right rate: " + rightEncoder.getRate());
System.out.println("timer :" + myTimer.get());
autoDriveStraight(1.5);
/*
if (leftEncoder.getDistance() < feetToEncUnits(1.5)) {
//rotations: X * 2400 / 360
//feet:
left.set(.3);
right.set(.3);
}
else {
left.set(0);
right.set(0);
}
System.out.println("angle = " + gyro.getAngle());
*/
}
@Override
public void teleopInit() {
// This makes sure that the autonomous stops running when
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
System.out.println("Initiating Teleop Mode");
if (autonomousCommand != null)
autonomousCommand.cancel();
driveTrain = new RobotDrive(leftSpark, rightSpark);
leftEncoder.reset();
}
/**
* This function is called periodically during operator control
*/
@Override
public void teleopPeriodic() {
//needs a String/boolean parameter
Scheduler.getInstance().run();
// cameraServer.getInstance().startAutomaticCapture();
// cameraServer2.getInstance().startAutomaticCapture();
//double v = s1.getDistance();
// System.out.println(v);
/* if (cameraMode.equalsIgnoreCase("BACK")){
cameraServer.getInstance().startAutomaticCapture();
}
else if (cameraMode.equalsIgnoreCase("FRONT")){
cameraServer2.getInstance().startAutomaticCapture();
} */
//tank drive - Aliya / Halie
System.out.println(" enc value: " + leftEncoder.get() + "\t\t rate: " + leftEncoder.getRate());
if (driveMode.equalsIgnoreCase("TANK")) {
//driveTrain.tankDrive(-1*stick.getY(),-1*stick.getRawAxis(5));
driveTrain.tankDrive(-1*stick2.getY(), stick.getY());
//System.out.println("stick one = " + stick.getRawAxis(1));
//System.out.println("stick two = " + stick2.getRawAxis(1));
}
//left axis: stick.getY();
//right axis: stick.getRawAxis(5);
else if(driveMode.equalsIgnoreCase("ARCADE")){
//arcade drive
driveTrain.arcadeDrive(-stick.getY(),stick.getX());
//driveTrain.arcadeDrive(stick);
}
else if(driveMode.equalsIgnoreCase("BACK TANK")){
driveTrain.tankDrive(-1*stick.getY(), stick2.getY());
}
//winch control - Halie
winch.tankDrive(stick.getRawAxis(3), 0);
}
/**
* This function is called periodically during test mode
*/
@Override
public void testPeriodic() {
left.set(0.5);
LiveWindow.run();
}
/*
public double feetToEncUnits(double feet) {
return feet*12/(6*Math.PI)*360/2400;
}
*/
public void moveForward(double feet) {
if (leftEncoder.getDistance() < feetToEncUnits(1.5)) {
//rotations: X * 2400 / 360
//feet:
left.set(.3);
right.set(.3);
}
else {
left.set(0);
right.set(0);
}
}
public void changeAngle(double degrees) {
gyro.
}
}