-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.java
More file actions
752 lines (674 loc) · 31.1 KB
/
Copy pathRobot.java
File metadata and controls
752 lines (674 loc) · 31.1 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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
// A project by Ricardo Santamaria-Sarcos, Dario Zaccagnino, Christian Kolowich and David James
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import com.ctre.phoenix.motorcontrol.can.TalonSRX;
import com.ctre.phoenix.motorcontrol.ControlMode;
import com.ctre.phoenix.motorcontrol.FeedbackDevice;
//import com.ctre.phoenix.motorcontrol.can.BaseMotorController;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.ADXRS450_Gyro;
import edu.wpi.first.networktables.NetworkTableInstance;
import com.ctre.phoenix.motorcontrol.can.VictorSPX;
import com.ctre.phoenix.motorcontrol.NeutralMode;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.GenericHID.Hand;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import edu.wpi.first.wpilibj.command.Scheduler;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the TimedRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the build.gradle file in the
* project.
*/
public class Robot extends TimedRobot {
private static final String kDefaultAuto = "Default";
private static final String kCustomAuto = "My Auto";
private String m_autoSelected;
private final SendableChooser<String> m_chooser = new SendableChooser<>();
//Define Motors Controllers
//DRIVE TRAIN
//Right Side Motors
public TalonSRX rightMasterMotor1 = new TalonSRX (3);
public VictorSPX rightSlaveMotor2 = new VictorSPX (5);
public VictorSPX rightSlaveMotor3 = new VictorSPX (7);
//Left Side Motors
public TalonSRX leftMasterMotor1 = new TalonSRX (2);
public VictorSPX leftSlaveMotor2 = new VictorSPX (4);
public VictorSPX leftSlaveMotor3 = new VictorSPX (6);
//ELEVATOR
//Left GB
public static TalonSRX elevLeftMaster = new TalonSRX (8);
public static VictorSPX elevLeftSlave = new VictorSPX (11);
//Right GB
public static TalonSRX elevRightMaster = new TalonSRX (9);
public static VictorSPX elevRightSlave = new VictorSPX (10);
//WRIST
public static TalonSRX wristMaster = new TalonSRX (13);
public static VictorSPX wristSlave = new VictorSPX (12);
//INTAKE
public static VictorSPX intakeMotor = new VictorSPX (14);
//Climbing
public static TalonSRX climbMotorMaster = new TalonSRX(17);
public static VictorSPX climbMotorSlave = new VictorSPX(18);
//Joysticks
public static Joystick leftJoy;
public static Joystick rightJoy;
public static XboxController operatorController;
/**
* A Button - 1
* B Button - 2
* X button - 3
* Y Button - 4
* Left Bumper - 5
* Right Bumber - 6
* Select Button - 7
* Start Button - 8
*/
/*
JoystickButton aButton = new JoystickButton(operatorController, 1);
JoystickButton bButton = new JoystickButton(operatorController, 2);
JoystickButton xButton = new JoystickButton(operatorController, 3);
JoystickButton yButton = new JoystickButton(operatorController, 4);
JoystickButton LeftBumper = new JoystickButton(operatorController, 5);
JoystickButton RightBumper = new JoystickButton(operatorController, 6);
JoystickButton rightTrigger = new JoystickButton(rightJoy, 1);
JoystickButton button2 = new JoystickButton(rightJoy, 2);
*/
//Usually Variables
//Encoder Counts per Revolution
final private double countPerRev = 4096;
//wheel Radius
final private double wheelRadius = 3;
//Wheel Circumference
final private double wheelCircumference = 2 * Math.PI * wheelRadius; //Circumference (in inches) (2*r*pi)
final public static double sprocketPitchCircumference = 1.79 * Math.PI;
final private double armRatio = 44/18;
double distance;
double velocity;
//Gyro
private ADXRS450_Gyro onboardGyro;
//Limelight
private boolean m_LimelightHasValidTarget = false;
private double m_LimelightDriveCommand = 0.0;
private double m_LimelightSteerCommand = 0.0;
//LED - Test to see if the light is on/off during auto?
boolean ledStatus = true;
/*
//Wrist Commands
MoveWrist wristIntake = new MoveWrist(Constants.wristDown);
MoveWrist wristRest = new MoveWrist(Constants.wristStraight);
MoveWrist wristHatch = new MoveWrist(Constants.wristHatch);
//Elevator Commands
MoveElev elevBot = new MoveElev(Constants.elevBotGoal);
MoveElev elevLow = new MoveElev(Constants.elevLowGoal);
MoveElev elevMid = new MoveElev(Constants.elevMidGoal);
MoveElev elevTop = new MoveElev(Constants.elevTopGoal);
*/
/*
//Climb Commands
MoveClimb extendClimb = new MoveClimb(Constants.climbExtend);
MoveClimb retractClimb = new MoveClimb(Constants.climbRetract);
*/
// public enum POv {
// NORTH(0), EAST(90), SOUTH(180), WEST(270);
// final int angle;
// public POV()
// }
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
@Override
public void robotInit() {
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
m_chooser.addOption("My Auto", kCustomAuto);
SmartDashboard.putData("Auto choices", m_chooser);
//CONTROLLERS
leftJoy = new Joystick(0);
rightJoy = new Joystick(1);
operatorController = new XboxController(2);
//operatorController.getPOV()
//END OF CONTROLLERS
//GYRO
onboardGyro = new ADXRS450_Gyro();
onboardGyro.calibrate();//Calibrates the gyro
onboardGyro.reset();//Sets gyro to 0 degrees
//END OF GYRO
//DRIVE TRAIN
//Sets up motor controller settings for right side
rightMasterMotor1.setInverted(false);
rightSlaveMotor2.setInverted(false);
rightSlaveMotor3.setInverted(false);
rightMasterMotor1.setNeutralMode(NeutralMode.Brake);
rightSlaveMotor2.setNeutralMode(NeutralMode.Brake);
rightSlaveMotor3.setNeutralMode(NeutralMode.Brake);
//Encoder Right Side
rightMasterMotor1.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Absolute, Constants.PID_PRIMARY, Constants.kTimeoutMs);
//Reverse Direction of Encoder
rightMasterMotor1.setSensorPhase(true);
//Config PID F Gains
rightMasterMotor1.config_kP(Constants.kSlot_Distanc, Constants.kGains_Distanc.kP, Constants.kTimeoutMs);
rightMasterMotor1.config_kI(Constants.kSlot_Distanc, Constants.kGains_Distanc.kI, Constants.kTimeoutMs);
rightMasterMotor1.config_kD(Constants.kSlot_Distanc, Constants.kGains_Distanc.kD, Constants.kTimeoutMs);
//Sets up motor controller settings for left side
leftMasterMotor1.setInverted(true);
leftSlaveMotor2.setInverted(true);
leftSlaveMotor3.setInverted(true);
leftMasterMotor1.setNeutralMode(NeutralMode.Brake);
leftSlaveMotor2.setNeutralMode(NeutralMode.Brake);
leftSlaveMotor3.setNeutralMode(NeutralMode.Brake);
//Encoder Left Side
leftMasterMotor1.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Absolute, Constants.PID_PRIMARY, Constants.kTimeoutMs);
//Reverse Direction of Encoder
leftMasterMotor1.setSensorPhase(true);
//Config PID F Gains
leftMasterMotor1.config_kP(Constants.kSlot_Distanc, Constants.kGains_Distanc.kP, Constants.kTimeoutMs);
leftMasterMotor1.config_kI(Constants.kSlot_Distanc, Constants.kGains_Distanc.kI, Constants.kTimeoutMs);
leftMasterMotor1.config_kD(Constants.kSlot_Distanc, Constants.kGains_Distanc.kD, Constants.kTimeoutMs);
//END OF DRIVE TRAIN
//ELEVATOR
//Left
elevLeftMaster.setInverted(false); //Reverse direction
elevLeftSlave .setInverted(true); //Reverse direction
elevLeftMaster.setNeutralMode(NeutralMode.Brake);
elevLeftSlave .setNeutralMode(NeutralMode.Brake);
//Encoder
elevLeftMaster.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, Constants.PID_PRIMARY, Constants.kTimeoutMs);
elevLeftMaster.setSensorPhase(false); //Reverse direction
//Config PID F Gains
elevLeftMaster.selectProfileSlot(1, 0);
elevLeftMaster.config_kP(1, 1.5, Constants.kTimeoutMs);
elevLeftMaster.config_kI(1, Constants.kGains_Elev.kI, Constants.kTimeoutMs);
elevLeftMaster.config_kD(1, Constants.kGains_Elev.kD, Constants.kTimeoutMs);
elevLeftMaster.config_kF(1, 0.3, Constants.kTimeoutMs);
elevLeftMaster.configMotionAcceleration(Constants.kElevAccel, Constants.kTimeoutMs);
elevLeftMaster.configMotionCruiseVelocity(Constants.kElevVel, Constants.kTimeoutMs);
elevLeftMaster.setSelectedSensorPosition(0, Constants.PID_PRIMARY, Constants.kTimeoutMs);
//Right
elevRightMaster.setInverted(false); //Reverse direction
elevRightSlave .setInverted(true); //Reverse direction
elevRightMaster.setNeutralMode(NeutralMode.Brake);
elevRightSlave .setNeutralMode(NeutralMode.Brake);
//Encoder
elevRightMaster.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, Constants.PID_PRIMARY, Constants.kTimeoutMs);
elevRightMaster.setSensorPhase(false); //Reverse direction
//Config PID F Gains
elevRightMaster.selectProfileSlot(1, 0);
elevRightMaster.config_kP(1, 1.5, Constants.kTimeoutMs);
elevRightMaster.config_kI(1, Constants.kGains_Elev.kI, Constants.kTimeoutMs);
elevRightMaster.config_kD(1, Constants.kGains_Elev.kD, Constants.kTimeoutMs);
elevRightMaster.config_kF(1, 0.3, Constants.kTimeoutMs);
elevRightMaster.configMotionAcceleration(Constants.kElevAccel, Constants.kTimeoutMs);
elevRightMaster.configMotionCruiseVelocity(Constants.kElevVel, Constants.kTimeoutMs);
elevRightMaster.setSelectedSensorPosition(0, Constants.PID_PRIMARY, Constants.kTimeoutMs);
//END OF ELEVATOR
//WRIST
//Left
wristMaster.setInverted(false); //Reverse direction
wristSlave .setInverted(true); //Reverse direction
wristMaster.setNeutralMode(NeutralMode.Brake);
wristSlave .setNeutralMode(NeutralMode.Brake);
//Encoder
wristMaster.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Absolute, Constants.PID_PRIMARY, Constants.kTimeoutMs);
wristMaster.setSensorPhase(true); //Reverse direction
//Config PID F Gains
wristMaster.selectProfileSlot(1 , Constants.PID_PRIMARY);
wristMaster.config_kP(1, 2.5, Constants.kTimeoutMs);
wristMaster.config_kI(1, Constants.kGains_Wrist.kI, Constants.kTimeoutMs);
wristMaster.config_kD(1, Constants.kGains_Wrist.kD, Constants.kTimeoutMs);
wristMaster.config_kF(1, 2, Constants.kTimeoutMs);
wristMaster.configMotionAcceleration(Constants.kWristAccel, Constants.kTimeoutMs);
wristMaster.configMotionCruiseVelocity(Constants.kWristVel, Constants.kTimeoutMs);
wristMaster.setSelectedSensorPosition(0);
//END OF WRIST
//INTAKE
intakeMotor.setInverted(true);
intakeMotor.setNeutralMode(NeutralMode.Brake);
//END OF INTAKE
//Climbing
climbMotorMaster.setInverted(false);
climbMotorSlave.setInverted(false);
climbMotorMaster.setNeutralMode(NeutralMode.Brake);
climbMotorSlave.setNeutralMode(NeutralMode.Brake);
//Encoder
climbMotorMaster.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, Constants.PID_PRIMARY, Constants.kTimeoutMs);
climbMotorMaster.setSensorPhase(false);
//Config PID F Gains
climbMotorMaster.selectProfileSlot(Constants.kSlot_Climb, Constants.PID_PRIMARY);
climbMotorMaster.config_kP(Constants.kSlot_Climb, Constants.kGains_Climb.kP, Constants.kTimeoutMs);
climbMotorMaster.config_kI(Constants.kSlot_Climb, Constants.kGains_Climb.kI, Constants.kTimeoutMs);
climbMotorMaster.config_kD(Constants.kSlot_Climb, Constants.kGains_Climb.kD, Constants.kTimeoutMs);
climbMotorMaster.config_kF(Constants.kSlot_Climb, Constants.kGains_Climb.kF, Constants.kTimeoutMs);
climbMotorMaster.configMotionAcceleration(Constants.kClimbAccel, Constants.kTimeoutMs);
climbMotorMaster.configMotionCruiseVelocity(Constants.kClimbVel, Constants.kTimeoutMs);
climbMotorMaster.setSelectedSensorPosition(0, Constants.PID_PRIMARY, Constants.kTimeoutMs);
//END OF CLIMBING
/*
rightTrigger.cancelWhenPressed(retractClimb);
button2.cancelWhenPressed(extendClimb);
*/
}
@Override
public void disabledPeriodic() {
}
@Override
public void disabledInit() {
}
/**
* This function is called every robot packet, no matter the mode. Use
* this for items like diagnostics that you want ran during disabled,
* autonomous, teleoperated and test.
*
* <p>This runs after the mode specific periodic functions, but before
* LiveWindow and SmartDashboard integrated updating.
*/
@Override
public void robotPeriodic() {
}
/**
* 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 line to get the auto name from the text box below the Gyro
*
* <p>You can add additional auto modes by adding additional comparisons to
* the switch structure below with additional strings. If using the
* SendableChooser make sure to add them to the chooser code above as well.
*/
@Override
public void autonomousInit() {
m_autoSelected = m_chooser.getSelected();
m_autoSelected = SmartDashboard.getString("Auto Selector", kDefaultAuto);
System.out.println("Auto selected: " + m_autoSelected);
}
/**
* This function is called periodically during autonomous.
*/
@Override
public void autonomousPeriodic() {
switch (m_autoSelected) {
case kCustomAuto:
// Put custom auto code here
break;
case kDefaultAuto:
default:
SmartDashboard.putNumber("Wrist Postion (encoder ticks): ", wristMaster.getSelectedSensorPosition(Constants.PID_PRIMARY));
SmartDashboard.putNumber("Elev R Postion (encoder ticks): ", elevRightMaster.getSelectedSensorPosition(Constants.PID_PRIMARY));
SmartDashboard.putNumber("Elev L Postion (encoder ticks): ", elevLeftMaster.getSelectedSensorPosition(Constants.PID_PRIMARY));
//Drive Train Controls
//Tank Drive
//Right
rightMasterMotor1.set(ControlMode.PercentOutput, -rightJoy.getY());
rightSlaveMotor2.set(ControlMode.PercentOutput, -rightJoy.getY());
rightSlaveMotor3.set(ControlMode.PercentOutput, -rightJoy.getY());
//Left
leftMasterMotor1.set(ControlMode.PercentOutput, -leftJoy.getY());
leftSlaveMotor2.set(ControlMode.PercentOutput, -leftJoy.getY());
leftSlaveMotor3.set(ControlMode.PercentOutput, -leftJoy.getY());
/*
//Reset Gyro
if(rightJoy.getRawButtonPressed(4)){
onboardGyro.reset();
}
//PID Drive Straight
if(rightJoy.getRawButton(4)){
PIDControl(0, 1.0);
}
*/
// turn on/off Vision Tracking
if(leftJoy.getRawButtonPressed(7)){
if(ledStatus){ //turn off
NetworkTableInstance.getDefault().getTable("limelight").getEntry("camMode").setNumber(1);
NetworkTableInstance.getDefault().getTable("limelight").getEntry("ledMode").setNumber(1);
ledStatus = false;
} else { //turn on
NetworkTableInstance.getDefault().getTable("limelight").getEntry("camMode").setNumber(0);
NetworkTableInstance.getDefault().getTable("limelight").getEntry("ledMode").setNumber(3);
ledStatus = true;
}
}
if(operatorController.getAButton()){
elevLeftMaster.set(ControlMode.MotionMagic, 0); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.MotionMagic, 0); //Find out direction
elevRightSlave.follow(elevRightMaster);
} else if(operatorController.getBButton()) {
elevLeftMaster.set(ControlMode.MotionMagic, -5000); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.MotionMagic, 5000); //Find out direction
elevRightSlave.follow(elevRightMaster);
} else if(operatorController.getYButton()){
elevLeftMaster.set(ControlMode.MotionMagic, -10000); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.MotionMagic, 10000); //Find out direction
elevRightSlave.follow(elevRightMaster);
} else if(operatorController.getXButton()){
elevLeftMaster.set(ControlMode.PercentOutput, operatorController.getY(Hand.kLeft)); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.PercentOutput, -operatorController.getY(Hand.kLeft)); //Find out direction
elevRightSlave.follow(elevRightMaster);
} else {
elevLeftMaster.set(ControlMode.PercentOutput, 0); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.PercentOutput, 0); //Find out direction
elevRightSlave.follow(elevRightMaster);
}
if(rightJoy.getRawButton(3)){
intakeMotor.set(ControlMode.PercentOutput, -0.5);//out take
} else {
intakeMotor.set(ControlMode.PercentOutput, 0.25);
}
if(operatorController.getBumper(Hand.kLeft)){
//wristMaster.set(ControlMode.MotionMagic, 2100);
//wristSlave.follow(wristMaster);
} else if(operatorController.getBumper(Hand.kRight)){
wristMaster.set(ControlMode.MotionMagic, 2900);
wristSlave.follow(wristMaster);
intakeMotor.set(ControlMode.PercentOutput, 0.35);
} else if(operatorController.getBackButton()){
wristMaster.set(ControlMode.MotionMagic, 1000);
wristSlave.follow(wristMaster);
} else if(operatorController.getStartButton()){
wristMaster.set(ControlMode.MotionMagic, 100);
wristSlave.follow(wristMaster);
} else {
wristMaster.set(ControlMode.PercentOutput, 0);
wristSlave.follow(wristMaster);
}
break;
}
}
@Override
public void teleopInit() {
}
/**
* This function is called periodically during operator control.
*/
@Override
public void teleopPeriodic() {
//Scheduler.getInstance().run();
Update_Limelight_Tracking();
double driveForwardPower;
double turnPower;
SmartDashboard.putNumber("Drivetrain Position (encoder ticks)", rightMasterMotor1.getSelectedSensorPosition(Constants.PID_PRIMARY));
SmartDashboard.putNumber("Wrist Postion (encoder ticks): ", wristMaster.getSelectedSensorPosition(Constants.PID_PRIMARY));
SmartDashboard.putNumber("Elev R Postion (encoder ticks): ", elevRightMaster.getSelectedSensorPosition(Constants.PID_PRIMARY));
SmartDashboard.putNumber("Elev L Postion (encoder ticks): ", elevLeftMaster.getSelectedSensorPosition(Constants.PID_PRIMARY));
if (leftJoy.getTrigger()){
if (m_LimelightHasValidTarget){
//Limelight vision procesing control
SmartDashboard.putString("Valid Target", "True");
driveForwardPower = m_LimelightDriveCommand;
turnPower = m_LimelightSteerCommand;
rightMasterMotor1.set(ControlMode.PercentOutput, driveForwardPower-turnPower);
rightSlaveMotor2.follow(rightMasterMotor1);
rightSlaveMotor3.follow(rightMasterMotor1);
leftMasterMotor1.set(ControlMode.PercentOutput, driveForwardPower+turnPower);
leftSlaveMotor2.follow(leftMasterMotor1);
leftSlaveMotor3.follow(leftMasterMotor1);
} else {
SmartDashboard.putString("Valid Target", "False");
}
}else {
//Tank Drive
//Right
rightMasterMotor1.set(ControlMode.PercentOutput, -rightJoy.getY());
rightSlaveMotor2.set(ControlMode.PercentOutput, -rightJoy.getY());
rightSlaveMotor3.set(ControlMode.PercentOutput, -rightJoy.getY());
//Left
leftMasterMotor1.set(ControlMode.PercentOutput, -leftJoy.getY());
leftSlaveMotor2.set(ControlMode.PercentOutput, -leftJoy.getY());
leftSlaveMotor3.set(ControlMode.PercentOutput, -leftJoy.getY());
}
/*
//Reset Gyro
if(rightJoy.getRawButtonPressed(4)){
onboardGyro.reset();
}
//PID Drive Straight
if(rightJoy.getRawButton(4)){
PIDControl(0, 1.0);
}
*/
// turn on/off Vision Tracking
if(leftJoy.getRawButtonPressed(7)){
if(ledStatus){ //turn off
NetworkTableInstance.getDefault().getTable("limelight").getEntry("camMode").setNumber(1);
NetworkTableInstance.getDefault().getTable("limelight").getEntry("ledMode").setNumber(1);
ledStatus = false;
} else { //turn on
NetworkTableInstance.getDefault().getTable("limelight").getEntry("camMode").setNumber(0);
NetworkTableInstance.getDefault().getTable("limelight").getEntry("ledMode").setNumber(3);
ledStatus = true;
}
}
/**
* A Button - 1 - elevator Bottom Position
* B Button - 2 - elevator Low Position
* X button - 3 - elevator Mid Position
* Y Button - 4 - elevator Top Position
* Left Bumper - 5 - Wrist down to intake
* Right Bumber - 6 - Wrist up to resting position
* Select Button - 7
* Start Button - 8
*/
/*
//Active
LeftBumper.whenPressed(wristIntake);
RightBumper.whenPressed(wristHatch);
aButton.whenPressed(elevBot);
bButton.whenPressed(elevLow);
xButton.whenPressed(elevMid);
yButton.whenPressed(elevTop);
*/
/*
rightTrigger.whenPressed(extendClimb);
button2.whenPressed(retractClimb);
*/
//DeActivate
/*
LeftBumper.cancelWhenPressed(wristRest);
RightBumper.cancelWhenPressed(wristIntake);
aButton.cancelWhenPressed(elevLow);
aButton.cancelWhenPressed(elevMid);
aButton.cancelWhenPressed(elevTop);
bButton.cancelWhenPressed(elevBot);
bButton.cancelWhenPressed(elevMid);
bButton.cancelWhenPressed(elevTop);
xButton.cancelWhenPressed(elevBot);
xButton.cancelWhenPressed(elevLow);
xButton.cancelWhenPressed(elevTop);
yButton.cancelWhenPressed(elevBot);
yButton.cancelWhenPressed(elevLow);
yButton.cancelWhenPressed(elevMid);
*/
//Tuning
if(operatorController.getAButton()){
elevLeftMaster.set(ControlMode.MotionMagic, 0); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.MotionMagic, 0); //Find out direction
elevRightSlave.follow(elevRightMaster);
wristMaster.set(ControlMode.MotionMagic, 200);
wristSlave.follow(wristMaster);
} else if(operatorController.getBButton()) {
elevLeftMaster.set(ControlMode.MotionMagic, -5000); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.MotionMagic, 5000); //Find out direction
elevRightSlave.follow(elevRightMaster);
wristMaster.set(ControlMode.MotionMagic, 2100);
wristSlave.follow(wristMaster);
} else if(operatorController.getYButton()){
elevLeftMaster.set(ControlMode.MotionMagic, -10000); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.MotionMagic, 10000); //Find out direction
elevRightSlave.follow(elevRightMaster);
wristMaster.set(ControlMode.MotionMagic, 1000);
wristSlave.follow(wristMaster);
} else if(operatorController.getXButton()){
elevLeftMaster.set(ControlMode.PercentOutput, operatorController.getY(Hand.kLeft)); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.PercentOutput, -operatorController.getY(Hand.kLeft)); //Find out direction
elevRightSlave.follow(elevRightMaster);
} else if(operatorController.getBumper(Hand.kRight)){
wristMaster.set(ControlMode.MotionMagic, 4500);
wristSlave.follow(wristMaster);
elevLeftMaster.set(ControlMode.MotionMagic, -10000); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.MotionMagic, 10000); //Find out direction
elevRightSlave.follow(elevRightMaster);
} else if(operatorController.getBumper(Hand.kLeft)){
wristMaster.set(ControlMode.MotionMagic, 2900);
wristSlave.follow(wristMaster);
//intakeMotor.set(ControlMode.PercentOutput, 0.35);
elevLeftMaster.set(ControlMode.MotionMagic, 0); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.MotionMagic, 0); //Find out direction
elevRightSlave.follow(elevRightMaster);
} else {
elevLeftMaster.set(ControlMode.PercentOutput, 0); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.PercentOutput, 0); //Find out direction
elevRightSlave.follow(elevRightMaster);
wristMaster.set(ControlMode.PercentOutput, 0);
wristSlave.follow(wristMaster);
}
/*
elevLeftMaster.set(ControlMode.PercentOutput, operatorController.getY(Hand.kLeft)); //Find out direction
elevLeftSlave.follow(elevLeftMaster);
elevRightMaster.set(ControlMode.PercentOutput, -operatorController.getY(Hand.kLeft)); //Find out direction
elevRightSlave.follow(elevRightMaster);
*/
if(rightJoy.getRawButton(3)){
//intakeMotor.set(ControlMode.PercentOutput, -0.5);//out take
} else if(!(operatorController.getBumper(Hand.kLeft))) {
//intakeMotor.set(ControlMode.PercentOutput, 0.25);
}
climbMotorMaster.set(ControlMode.PercentOutput, -operatorController.getY(Hand.kRight));
climbMotorSlave.follow(climbMotorMaster);
intakeMotor.set(ControlMode.PercentOutput, -1);
}
/**
* This function is called periodically during test mode.
*/
@Override
public void testPeriodic() {
}
/*
//Drive set amount - in inches
public void driveToTargetDist(double dist){
double targetDist = dist; //Inches
double setPoint;
setPoint = (dist * countPerRev)/wheelCircumference;
System.out.println("Button Pressed. Moving to: " + targetDist);
rightMasterMotor1.set(ControlMode.Position, setPoint);
}
//Move Elev set amount - in inches
public void MoveElevToTarget(double dist){
double targetDist = dist; //Inches
double setPoint;
setPoint = (dist * countPerRev)/sprocketPitchCircumference;
System.out.println("Button Pressed. Moving to: " + targetDist);
elevRightMaster.set(ControlMode.MotionMagic, setPoint);
elevRightSlave.follow(elevRightMaster);
elevLeftMaster.set(ControlMode.MotionMagic, setPoint);
elevLeftSlave.follow(elevLeftMaster);
}
//Move Elev set amount - in talon native units
public void MoveElevToRawPosition(double position){
elevRightMaster.set(ControlMode.MotionMagic, position);
elevRightSlave.follow(elevRightMaster);
elevLeftMaster.set(ControlMode.MotionMagic, position);
elevLeftSlave.follow(elevLeftMaster);
}
//Move wrist to raw postion - in degree
public void MoveWristToRawAngle(double angle){
wristMaster.set(ControlMode.MotionMagic, angle);
wristSlave.follow(wristMaster);
}
//Move wrist to a postion - in degree
public void MoveWristToAngle(double angle){
double targetAngle = angle;
double setPoint;
setPoint = (angle/360)*(countPerRev * (armRatio));
System.out.println("Button Pressed. Moving to angle: " + targetAngle);
wristMaster.set(ControlMode.MotionMagic, setPoint);
wristSlave.follow(wristMaster);
}
*/
/**
* This function implements a simple method of generating driving and steering commands
* based on the tracking data from a limelight camera.
*/
public void Update_Limelight_Tracking() {
final double STEER_P = 0.03/2; // how hard to turn toward the target
final double DRIVE_P = 0; // how hard to drive fwd toward the target
final double DESIRED_TARGET_AREA = 3.75; // Area of the target when the robot reaches the wall
final double MAX_DRIVE = 0.5; // Simple speed limit so we don't drive too fast
final double STEER_I = 0;
final double DRIVE_I = 0;
final double xError;
final double aError;
double STEER_INTEGRAL = 0;
double DRIVE_INTEGRAL = 0;
double tv = NetworkTableInstance.getDefault().getTable("limelight").getEntry("tv").getDouble(0);
double tx = NetworkTableInstance.getDefault().getTable("limelight").getEntry("tx").getDouble(0);
double ta = NetworkTableInstance.getDefault().getTable("limelight").getEntry("ta").getDouble(0);
xError = tx;
aError = DESIRED_TARGET_AREA - ta;
STEER_INTEGRAL = STEER_INTEGRAL + (xError*0.02);
DRIVE_INTEGRAL = DRIVE_INTEGRAL + (aError * 0.02);
if (tv < 1.0)
{
m_LimelightHasValidTarget = false;
m_LimelightDriveCommand = 0.0;
m_LimelightSteerCommand = 0.0;
return;
}
m_LimelightHasValidTarget = true;
// Start with proportional steering
double steer_cmd = (tx * STEER_P) + (STEER_INTEGRAL * STEER_I);
m_LimelightSteerCommand = steer_cmd;
// try to drive forward until the target area reaches our desired area
double drive_cmd = (aError * DRIVE_P) + (DRIVE_INTEGRAL * DRIVE_I);
// don't let the robot drive too fast into the goal
if (drive_cmd > MAX_DRIVE)
{
drive_cmd = MAX_DRIVE;
}
m_LimelightDriveCommand = drive_cmd;
}
/**
*
* @param angle target angle
* @param direction forward or backwards; 1.0 for forward, and -1.0 for backwards
*/
/*
public void PIDControl(int angle, double direction){
double desiredAngle = angle;
double max_speed = direction * 0.5;
double turn_kP = 0.025;
double turn_kI = 0.1;
double integral = 0;
double curentAngle = onboardGyro.getAngle();
double error = desiredAngle - curentAngle;
integral = integral + (error*0.02);
double turnCmd = (error * turn_kP) + (turn_kI * integral) ;
rightMasterMotor1.set(ControlMode.PercentOutput, max_speed + turnCmd);
rightSlaveMotor2.follow(rightMasterMotor1);
rightSlaveMotor3.follow(rightMasterMotor1);
leftMasterMotor1.set(ControlMode.PercentOutput, max_speed - turnCmd);
leftSlaveMotor2.follow(leftMasterMotor1);
leftSlaveMotor3.follow(leftMasterMotor1);
}
*/
}