-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicRobot.java
More file actions
324 lines (282 loc) · 11.7 KB
/
Copy pathBasicRobot.java
File metadata and controls
324 lines (282 loc) · 11.7 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
/*----------------------------------------------------------------------------*/
/* 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.XboxController;
import edu.wpi.first.wpilibj.GenericHID.Hand;
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.can.BaseMotorController;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.cscore.CameraServerJNI;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.first.wpilibj.CameraServer;
import com.ctre.phoenix.motorcontrol.can.VictorSPX;
import com.ctre.phoenix.motorcontrol.NeutralMode;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.first.wpilibj.ADXRS450_Gyro;
import edu.wpi.first.networktables.NetworkTableInstance;
/**
* 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
TalonSRX rightMasterMotor1 = new TalonSRX (3);
VictorSPX rightSlaveMotor2 = new VictorSPX (5);
VictorSPX rightSlaveMotor3 = new VictorSPX (7);
//Left Side Motors
TalonSRX leftMasterMotor1 = new TalonSRX (2);
VictorSPX leftSlaveMotor2 = new VictorSPX (4);
VictorSPX leftSlaveMotor3 = new VictorSPX (6);
//ELEVATOR
//Left GB
TalonSRX elevLeftMaster = new TalonSRX (8);
VictorSPX elevLeftSlave = new VictorSPX (11);
//Right GB
TalonSRX elevRightMaster = new TalonSRX (9);
VictorSPX elevRightSlave = new VictorSPX (10);
//WRIST
TalonSRX wristMaster = new TalonSRX (13);
VictorSPX wristSlave = new VictorSPX (12);
//INTAKE
VictorSPX intakeMotor = new VictorSPX (14);
//Joysticks
private XboxController operatorController;
private Joystick leftJoy;
private Joystick rghtJoy;
private ADXRS450_Gyro onboardGyro;
private boolean m_LimelightHasValidTarget = false;
private double m_LimelightDriveCommand = 0.0;
private double m_LimelightSteerCommand = 0.0;
/**
* 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
driveJoy = new Joystick(0);
operatorController = new XboxController(1);
//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(true);
rightSlaveMotor2.setInverted(true);
rightSlaveMotor3.setInverted(true);
rightMasterMotor1.configOpenloopRamp(1, 0);
rightMasterMotor1.setNeutralMode(NeutralMode.Brake);
rightSlaveMotor2.setNeutralMode(NeutralMode.Brake);
rightSlaveMotor3.setNeutralMode(NeutralMode.Brake);
//Sets up motor controller settings for left side
leftMasterMotor1.setInverted(false);
leftSlaveMotor2.setInverted(false);
leftSlaveMotor3.setInverted(false);
leftMasterMotor1.configOpenloopRamp(1, 0);
leftMasterMotor1.setNeutralMode(NeutralMode.Brake);
leftSlaveMotor2.setNeutralMode(NeutralMode.Brake);
leftSlaveMotor3.setNeutralMode(NeutralMode.Brake);
//END OF DRIVE TRAIN
//ELEVATOR
//Left
elevLeftMaster.setInverted(true); //Reverse direction
elevLeftSlave .setInverted(true); //Reverse direction
elevLeftMaster.setNeutralMode(NeutralMode.Brake);
elevLeftSlave .setNeutralMode(NeutralMode.Brake);
elevLeftMaster.configOpenloopRamp(1.5);
//Right
elevRightMaster.setInverted(false); //Reverse direction
elevRightSlave .setInverted(false); //Reverse direction
elevRightMaster.setNeutralMode(NeutralMode.Brake);
elevRightSlave .setNeutralMode(NeutralMode.Brake);
elevRightMaster.configOpenloopRamp(1.5);
//END OF ELEVATOR
//WRIST
//Left
wristMaster.setInverted(false); //Reverse direction
wristSlave .setInverted(true); //Reverse direction
wristMaster.setNeutralMode(NeutralMode.Brake);
wristSlave .setNeutralMode(NeutralMode.Brake);
//END OF WRIST
//INTAKE
intakeMotor.setInverted(true);
intakeMotor.setNeutralMode(NeutralMode.Brake);
//END OF INTAKE
}
/**
* 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:
// Put default auto code here
break;
}
}
/**
* This function is called periodically during operator control.
*/
@Override
public void teleopPeriodic() {
Update_Limelight_Tracking();
double driveForwardPower;
double turnPower;
//Drive Train Controls
if (driveJoy.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
{
//Percent Control Mode w/ Arcade Drive
//Right
rightMasterMotor1.set(ControlMode.PercentOutput, -driveJoy.getY() - driveJoy.getX()/2);
rightSlaveMotor2.follow(rightMasterMotor1);
rightSlaveMotor3.follow(rightMasterMotor1);
//Left
leftMasterMotor1.set(ControlMode.PercentOutput, -driveJoy.getY() + driveJoy.getX()/2);
leftSlaveMotor2.follow(leftMasterMotor1);
leftSlaveMotor3.follow(leftMasterMotor1);
}
//Elevator
elevLeftMaster.set(ControlMode.PercentOutput, (operatorController.getY(Hand.kLeft)/2));
elevRightMaster.set(ControlMode.PercentOutput, (operatorController.getY(Hand.kLeft)/2));
elevLeftSlave.follow(elevLeftMaster);
elevRightSlave.follow(elevRightMaster);
//Wrist
wristMaster.set(ControlMode.PercentOutput, operatorController.getY(Hand.kRight));
wristSlave.follow(wristMaster);
//Intake
if(operatorController.getYButton()){
intakeMotor.set(ControlMode.PercentOutput, 1.0);
} else if(operatorController.getAButton()){
intakeMotor.set(ControlMode.PercentOutput, -1.0);
} else {
intakeMotor.set(ControlMode.PercentOutput, 0.0);
}
}
/**
* This function is called periodically during test mode.
*/
@Override
public void testPeriodic() {
}
/**
* 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()
{
// These numbers must be tuned for your Robot! Be careful!
final double STEER_P = 0.03/2; // how hard to turn toward the target
final double DRIVE_P = 0.15; // 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.75; // Simple speed limit so we don't drive too fast
final double STEER_I = 0.15;
final double DRIVE_I = .75;
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;
}
}