-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.java
More file actions
299 lines (250 loc) · 11.1 KB
/
Robot.java
File metadata and controls
299 lines (250 loc) · 11.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
// 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.cameraserver.CameraServer;
import edu.wpi.first.cscore.UsbCamera;
import edu.wpi.first.cscore.VideoSource;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.DriverStation.Alliance;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.autochooser.FieldLocation;
import frc.robot.constants.Constants;
import frc.robot.utils.diag.Diagnostics;
import frc.robot.utils.logging.TimeoutLogger;
import frc.robot.utils.logging.commands.CommandLogger;
import org.littletonrobotics.junction.LogFileUtil;
import org.littletonrobotics.junction.LoggedRobot;
import org.littletonrobotics.junction.Logger;
import org.littletonrobotics.junction.networktables.NT4Publisher;
import org.littletonrobotics.junction.wpilog.WPILOGReader;
import org.littletonrobotics.junction.wpilog.WPILOGWriter;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
/**
* 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.
*/
public class Robot extends LoggedRobot {
private Command autonomousCommand;
private static final Diagnostics diagnostics = new Diagnostics();
private final RobotContainer robotContainer;
private static final AtomicReference<RobotMode> mode = new AtomicReference<>(RobotMode.DISABLED);
private String autonomousWinner;
private boolean hubActive;
private static Alliance autoWinner;
private static Optional<DriverStation.Alliance> allianceColor = Optional.empty();
final CommandXboxController driverXbox = new CommandXboxController(0);
/**
* This function is run when the robot is first started up and should be used for any
* initialization code.
*/
public Robot() {
// Set up data receivers & replay source
switch (Constants.currentMode) {
case REAL:
// Running on a real robot, log to a USB stick ("/U/logs")
Logger.addDataReceiver(new WPILOGWriter());
Logger.addDataReceiver(new NT4Publisher());
break;
case SIM:
// Running a physics simulator, log to NT
Logger.addDataReceiver(new NT4Publisher());
Logger.addDataReceiver(new WPILOGWriter());
break;
case REPLAY:
// Replaying a log, set up replay source
setUseTiming(false); // Run as fast as possible
String logPath = LogFileUtil.findReplayLog();
Logger.setReplaySource(new WPILOGReader(logPath));
Logger.addDataReceiver(new WPILOGWriter(LogFileUtil.addPathSuffix(logPath, "_sim")));
break;
}
// Start AdvantageKit logger
Logger.start();
CommandLogger.get().init();
UsbCamera camera = CameraServer.startAutomaticCapture("DriverCam", 0);
camera.setConnectionStrategy(VideoSource.ConnectionStrategy.kKeepOpen);
CameraServer.addCamera(camera);
// Instantiate our RobotContainer. This will perform all our button bindings, and put our
// autonomous chooser on the dashboard.
robotContainer = new RobotContainer();
}
public static RobotMode getMode() {
return mode.get();
}
/**
* 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.
*
* <p>This runs after the mode specific periodic functions, but before LiveWindow and
* SmartDashboard integrated updating.
*/
@Override
public void robotPeriodic() {
// Runs the Scheduler. This is responsible for polling buttons, adding newly-scheduled
// commands, running already-scheduled commands, removing finished or interrupted commands,
// and running subsystem periodic() methods. This must be called from the robot's periodic
// block in order for anything in the Command-based framework to work.
if (getMode() != RobotMode.TEST) {
// Runs the Scheduler. This is responsible for polling buttons, adding newly-scheduled
// commands, running already-scheduled commands, removing finished or interrupted commands,
// and running subsystem periodic() methods. This must be called from the robot's periodic
// block in order for anything in the Command-based framework to work.
CommandScheduler.getInstance().run();
}
Logger.recordOutput("shootingState", robotContainer.getShootingState().getShootState().toString());
Logger.recordOutput("totalTimeouts", TimeoutLogger.getTotalTimeouts());
if (Constants.currentMode.equals(Constants.Mode.SIM)) {
robotContainer.getRobotVisualizer().logMechanism();
}
if (Constants.ENABLE_LOGGING) {
CommandLogger.get().log();
}
// Gets the alliance color.
if (DriverStation.isDSAttached() && allianceColor.isEmpty()) {
allianceColor = DriverStation.getAlliance();
}
if (Constants.DEBUG) {
SmartDashboard.putNumber("driverXbox.getLeftY()", driverXbox.getLeftY());
SmartDashboard.putNumber("driverXbox::getRightX", driverXbox.getRightX());
SmartDashboard.putString("DeploymentState", robotContainer.getDeployer().getDeploymentState().toString());
if (!Constants.TESTBED) {
Logger.recordOutput("MyPose", robotContainer.getDriveBase().getPose());
// Puts data on the elastic dashboard
SmartDashboard.putString("Alliance Color", Robot.allianceColorString());
SmartDashboard.putBoolean("Hub Active?", hubActive());
}
// Puts data on the elastic dashboard
SmartDashboard.putString("Alliance Color", Robot.allianceColorString());
SmartDashboard.putBoolean("Hub Active?", hubActive());
}
SmartDashboard.putString("Selected Action",
robotContainer.getAutoChooser().getCommandDescription());
}
/** This function is called once each time the robot enters Disabled mode. */
@Override
public void disabledInit() {
mode.set(RobotMode.DISABLED);
}
@Override
public void disabledPeriodic() {
}
/** This autonomous runs the autonomous command selected by your {@link RobotContainer} class. */
@Override
public void autonomousInit() {
//m_autonomousCommand = m_robotContainer.getAutonomousCommand();
// schedule the autonomous command (example)
mode.set(RobotMode.AUTONOMOUS);
// Hub is always active during autonomous.
hubActive = true;
}
/** This function is called periodically during autonomous. */
@Override
public void autonomousPeriodic() {
// schedule the autonomous command
if (this.autonomousCommand == null) {
autonomousCommand = robotContainer.getAutonomousCommand();
if (autonomousCommand != null) {
CommandScheduler.getInstance().schedule(autonomousCommand);
}
}
}
@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.
diagnostics.reset();
mode.set(RobotMode.TELEOP);
if (autonomousCommand != null) {
autonomousCommand.cancel();
}
}
/** This function is called periodically during operator control. */
@Override
public void teleopPeriodic() {
// Check who won autonomous.
if (autonomousWinner == null) {
determineAutonomousWinner();
} else {
determineHubActive();
}
}
private void determineAutonomousWinner() {
autonomousWinner = DriverStation.getGameSpecificMessage();
if (autonomousWinner != null) {
autoWinner = switch (autonomousWinner.toUpperCase()) {
case "R" -> Alliance.Red;
case "B" -> Alliance.Blue;
default -> null;
};
} else {
hubActive = true; // If game data has not been recieved,
}
// it is transition period and the hub is active.
}
private void determineHubActive() {
// Determine whether the hub is active.
double timeLeft = DriverStation.getMatchTime();
if (timeLeft < 0) return; // Match has not started.
if (timeLeft <= Constants.ENDGAME_START) {
hubActive = true; // Hub is always active during endgame and transition
} else if (timeLeft <= Constants.SHIFT_4_START) {
hubActive = (allianceColor.get() == autoWinner);
// Only the hub of the team that won autonomous is active during shifts 2 and 4.
} else if (timeLeft <= Constants.SHIFT_3_START) {
hubActive = (allianceColor.get() != autoWinner);
// Only the hub of the team that didn't win autonomous is active during shifts 1 and 3.
} else if (timeLeft <= Constants.SHIFT_2_START) {
hubActive = (allianceColor.get() == autoWinner);
} else if (timeLeft <= Constants.SHIFT_1_START) {
hubActive = (allianceColor.get() != autoWinner);
} else hubActive = true; // transition
}
@Override
public void testInit() {
diagnostics.reset();
mode.set(RobotMode.TEST);
// Cancels all running commands at the start of test mode.
CommandScheduler.getInstance().cancelAll();
}
/** This function is called periodically during test mode. */
@Override
public void testPeriodic() {
diagnostics.refresh();
}
/** 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 static Diagnostics getDiagnostics() {
return diagnostics;
}
// Getters
public boolean hubActive() {
return hubActive;
}
public static Optional<Alliance> allianceColor() {
return allianceColor;
}
public static String allianceColorString() {
return String.valueOf(allianceColor.orElse(null));
}
public FieldLocation location() {
return robotContainer.getAutoChooser().getLocation();
}
public Pose2d getStartingLocation() {
return location().getLocation();
}
}