-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.java
More file actions
377 lines (298 loc) · 12.3 KB
/
Robot.java
File metadata and controls
377 lines (298 loc) · 12.3 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
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
import edu.wpi.first.math.controller.ElevatorFeedforward;
import edu.wpi.first.math.controller.ProfiledPIDController;
import edu.wpi.first.math.filter.SlewRateLimiter;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.kinematics.MecanumDriveKinematics;
import edu.wpi.first.math.kinematics.MecanumDriveOdometry;
import edu.wpi.first.math.kinematics.MecanumDriveWheelPositions;
import edu.wpi.first.math.kinematics.MecanumDriveWheelSpeeds;
import edu.wpi.first.math.trajectory.TrapezoidProfile;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.util.sendable.SendableRegistry;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.drive.MecanumDrive;
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
import com.revrobotics.spark.SparkMax;
import com.revrobotics.spark.SparkLowLevel.MotorType;
import com.studica.frc.AHRS;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import frc.robot.Constants.*;
/**
* The methods in this class are called automatically 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 Main.java file in the project.
*/
@SuppressWarnings("PMD.RedundantFieldInitializer")
public class Robot extends TimedRobot {
// Field- Orientated Drive
//gyro
AHRS ahrs;
//Limelight
private boolean m_LimelightHasValidTarget = false;
private double m_LimelightDriveCommand = 0.0;
private double m_LimelightSteerCommand = 0.0;
private double m_LimelightStrafeCommand = 0.0;
Shuffleboard shuffleboard;
private DigitalInput myInput = new DigitalInput(4);
SlewRateLimiter filter = new SlewRateLimiter(0.5);
//Timed Robot/Autonomous Variables
private static final String kDefaultAuto = "Default";
private static final String kLeftAuto = "Left Auto";
private static final String kRightAuto = "Right Auto";
private String m_autoSelected;
private final SendableChooser<String> m_chooserAuto = new SendableChooser<>();
//Mecanum Drive with Gyro variables
private final MecanumDrive m_robotDrive;
private final XboxController driverController = new XboxController(3);
//Drive subsystem for Path planner
private final Encoder m_frontLeftEncoder = new Encoder(0, 1, 0);
private final Encoder m_frontRightEncoder = new Encoder(0, 1, 0);
private final Encoder m_rearLeftEncoder = new Encoder(0, 1, 0);
private final Encoder m_rearRightEncoder = new Encoder(0, 1, 0);
private final MecanumDriveKinematics m_DriveKinematics =
new MecanumDriveKinematics(
OdometryConstants.frontLeftWheelMeters,
OdometryConstants.frontRightWheelMeters,
OdometryConstants.rearLeftWheelMeters,
OdometryConstants.rearRightWheelMeters);
private final MecanumDriveOdometry m_Odometry;
//Elevator PID
private final XboxController operatorController = new XboxController(2);
private final Encoder m_encoder = new Encoder(6, 7);
private final SparkMax m_motor = new SparkMax(1, MotorType.kBrushless);
//Declare DIO pins
// Create a PID controller whose setpoint's change is subject to maximum
// velocity and acceleration constraints.
private final TrapezoidProfile.Constraints m_constraints =
new TrapezoidProfile.Constraints(ElevatorConstants.kMaxVelocity, ElevatorConstants.kMaxAcceleration);
private final ProfiledPIDController m_controller =
new ProfiledPIDController(ElevatorConstants.kP,
ElevatorConstants.kI,
ElevatorConstants.kD,
m_constraints,
ElevatorConstants.kDt);
private final ElevatorFeedforward m_feedforward = new ElevatorFeedforward(ElevatorConstants.kS,
ElevatorConstants.kG,
ElevatorConstants.kV);
// This function is run when the robot is first started up and should be used for any initialization code.
public Robot() {
// Field Orientated Drive
try {
ahrs = new AHRS(AHRS.NavXComType.kMXP_SPI);
} catch (Exception e) {
System.out.println("Error initializing AHRS: " + e.getMessage());
}
//Timed Robot - useful for picking multiple autonomous
m_chooserAuto.setDefaultOption("Default Auto", kDefaultAuto);
m_chooserAuto.addOption("Left Auto", kLeftAuto);
m_chooserAuto.addOption("Right Auto", kRightAuto);
SmartDashboard.putData("Auto choices", m_chooserAuto);
//Mecanum
SparkMax frontLeft = new SparkMax(DriveConstants.kFrontLeftChannel, MotorType.kBrushless);
SparkMax rearLeft = new SparkMax(DriveConstants.kRearLeftChannel, MotorType.kBrushless);
SparkMax frontRight = new SparkMax(DriveConstants.kFrontRightChannel, MotorType.kBrushless);
SparkMax rearRight = new SparkMax(DriveConstants.kRearRightChannel, MotorType.kBrushless);
// pathplanner set up
//ts pmo
m_frontLeftEncoder.setDistancePerPulse(DriveConstants.driveDistancePerPulse);
m_frontRightEncoder.setDistancePerPulse(DriveConstants.driveDistancePerPulse);
m_rearLeftEncoder.setDistancePerPulse(DriveConstants.driveDistancePerPulse);
m_rearRightEncoder.setDistancePerPulse(DriveConstants.driveDistancePerPulse);
resetOdometry(getPose());
m_Odometry = new MecanumDriveOdometry(m_DriveKinematics,
ahrs.getRotation2d(),
new MecanumDriveWheelPositions(
m_frontLeftEncoder.getDistance(),
m_frontRightEncoder.getDistance(),
m_rearLeftEncoder.getDistance(),
m_rearRightEncoder.getDistance())
);
//mecanum
m_robotDrive = new MecanumDrive(frontLeft::set, rearLeft::set, frontRight::set, rearRight::set);
SendableRegistry.addChild(m_robotDrive, frontLeft);
SendableRegistry.addChild(m_robotDrive, rearLeft);
SendableRegistry.addChild(m_robotDrive, frontRight);
SendableRegistry.addChild(m_robotDrive, rearRight);
//Elevator PID
m_encoder.setDistancePerPulse(1.0 / 360.0 * 2.0 * Math.PI * 1.5);
}
// This function is called every 20 ms, no matter the mode. Use this for items like diagnostics
//that you want ran during disabled, autonomous, teleoperated and test.
@Override
public void robotPeriodic() {
m_Odometry.update(ahrs.getRotation2d(),
new MecanumDriveWheelPositions(
m_frontLeftEncoder.getDistance(),
m_frontRightEncoder.getDistance(),
m_rearLeftEncoder.getDistance(),
m_rearRightEncoder.getDistance())
);
}
@Override
public void autonomousInit() {
m_autoSelected = m_chooserAuto.getSelected();
System.out.println("Auto selected: " + m_autoSelected);
}
/** This function is called periodically during autonomous. */
@Override
public void autonomousPeriodic() {
switch (m_autoSelected) {
case kLeftAuto:
// Put custom auto code here
break;
case kRightAuto:
// Put custom auto code here
m_robotDrive.driveCartesian(0, -0.5, 0);
Timer.delay(2.0);
m_robotDrive.driveCartesian(0, 0, 0);
m_controller.setGoal(5);
break;
case kDefaultAuto:
default:
// Put default auto code here
//strafe
m_robotDrive.driveCartesian(-0.5, 0, 0);
Timer.delay(2.0);
m_robotDrive.driveCartesian(0, 0, 0);
break;
}
}
/** This function is called once when teleop is enabled. */
@Override
public void teleopInit() {
if (m_autoSelected != null)
{
m_autoSelected = null;
}
}
/** This function is called periodically during operator control. */
/** Mecanum drive is used with the gyro angle as an input. */
@Override
public void teleopPeriodic() {
//Limelight
Update_Limelight_Tracking();
boolean auto = driverController.getAButton();
if (auto)
{
if (m_LimelightHasValidTarget)
{
System.out.println("yeah");
m_robotDrive.driveCartesian(-m_LimelightStrafeCommand, -m_LimelightDriveCommand, m_LimelightSteerCommand);
System.out.println("drive: " + m_LimelightDriveCommand);
System.out.println("strafe: " + m_LimelightStrafeCommand);
System.out.println("steer: " + m_LimelightSteerCommand);
}
else
{
m_robotDrive.driveCartesian(0,0,0);
}
}
else
{
m_robotDrive.driveCartesian(
-driverController.getLeftX(),
driverController.getLeftY(),
-driverController.getRightX(),
ahrs.getRotation2d());
}
SmartDashboard.putBoolean("Presence", myInput.get());
//Mecanum
if (driverController.getXButtonPressed()) {
ahrs.zeroYaw();
}
//Elevator
if (operatorController.getAButtonPressed()) {
m_controller.setGoal(5);
} else if (operatorController.getBButtonPressed()) {
m_controller.setGoal(0);
}
// Run controller and update motor output
m_motor.setVoltage(
m_controller.calculate(m_encoder.getDistance())
+ m_feedforward.calculate(m_controller.getSetpoint().velocity));
}
/** This function is called once when the robot is disabled. */
@Override
public void disabledInit() {}
/** This function is called periodically when disabled. */
@Override
public void disabledPeriodic() {}
/** This function is called once when test mode is enabled. */
@Override
public void testInit() {}
/** This function is called periodically during test mode. */
@Override
public void testPeriodic() {}
/** This function is called once when the robot is first started up. */
@Override
public void simulationInit() {}
/** This function is called periodically whilst in simulation. */
@Override
public void simulationPeriodic() {}
public void Update_Limelight_Tracking()
{
double tv = NetworkTableInstance.getDefault().getTable("limelight").getEntry("tv").getDouble(0);
double tx = NetworkTableInstance.getDefault().getTable("limelight").getEntry("tx").getDouble(0);
double ty = NetworkTableInstance.getDefault().getTable("limelight").getEntry("ty").getDouble(0);
double ta = NetworkTableInstance.getDefault().getTable("limelight").getEntry("ta").getDouble(0);
if (tv < 1.0)
{
m_LimelightHasValidTarget = false;
m_LimelightDriveCommand = 0.0;
m_LimelightSteerCommand = 0.0;
return;
}
// System.out.println("tv:" + tv);
// System.out.println("tx:" + tx);
// System.out.println("ta:" + ta);
m_LimelightHasValidTarget = true;
// Start with proportional steering
double steer_cmd = tx * DriveConstants.STEER_K;
m_LimelightSteerCommand = steer_cmd;
double strafe_cmd = ty * DriveConstants.DRIVE_K;
// try to drive forward until the target area reaches our desired area
double drive_cmd = (DriveConstants.DESIRED_TARGET_AREA - ta) * DriveConstants.DRIVE_K;
// don't let the robot drive too fast into the goal
if (drive_cmd > DriveConstants.MAX_DRIVE)
{
drive_cmd = DriveConstants.MAX_DRIVE;
}
if (strafe_cmd > DriveConstants.MAX_DRIVE)
{
strafe_cmd = DriveConstants.MAX_DRIVE;
}
else if(strafe_cmd < -DriveConstants.MAX_DRIVE)
{
strafe_cmd = -DriveConstants.MAX_DRIVE;
}
m_LimelightStrafeCommand = strafe_cmd;
m_LimelightDriveCommand = drive_cmd;
}
public Pose2d getPose() {
return m_Odometry.getPoseMeters();
}
public MecanumDriveWheelSpeeds getWheelSpeeds() {
return new MecanumDriveWheelSpeeds
(m_frontLeftEncoder.getRate(),
m_frontRightEncoder.getRate(),
m_rearLeftEncoder.getRate(),
m_rearRightEncoder.getRate());
}
public void resetOdometry(Pose2d pose) {
System.out.println("Resetted Odometry");
m_Odometry.resetPosition(ahrs.getRotation2d(), new MecanumDriveWheelPositions(
m_frontLeftEncoder.getDistance(),
m_frontRightEncoder.getDistance(),
m_rearLeftEncoder.getDistance(),
m_rearRightEncoder.getDistance()), pose);
}
}