-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRobot.java
More file actions
207 lines (182 loc) · 7.4 KB
/
Robot.java
File metadata and controls
207 lines (182 loc) · 7.4 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
// Copyright (c) 2024 Az-FIRST
// http://github.com/AZ-First
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 3 as published by the Free Software Foundation or
// available in the root directory of this project.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
package frc.robot;
import edu.wpi.first.wpilibj.Threads;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import frc.robot.util.VirtualSubsystem;
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;
/**
* 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 LoggedRobot {
private Command m_autoCommandPathPlanner;
private RobotContainer m_robotContainer;
private Timer m_disabledTimer;
/**
* This function is run when the robot is first started up and should be used for any
* initialization code.
*/
@Override
public void robotInit() {
// Record metadata
Logger.recordMetadata("Robot", Constants.getRobot().toString());
Logger.recordMetadata("TuningMode", Boolean.toString(Constants.tuningMode));
Logger.recordMetadata("RuntimeType", getRuntimeType().toString());
Logger.recordMetadata("ProjectName", BuildConstants.MAVEN_NAME);
Logger.recordMetadata("BuildDate", BuildConstants.BUILD_DATE);
Logger.recordMetadata("GitSHA", BuildConstants.GIT_SHA);
Logger.recordMetadata("GitDate", BuildConstants.GIT_DATE);
Logger.recordMetadata("GitBranch", BuildConstants.GIT_BRANCH);
switch (BuildConstants.DIRTY) {
case 0:
Logger.recordMetadata("GitDirty", "All changes committed");
break;
case 1:
Logger.recordMetadata("GitDirty", "Uncomitted changes");
break;
default:
Logger.recordMetadata("GitDirty", "Unknown");
break;
}
// Set up data receivers & replay source
switch (Constants.getMode()) {
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());
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;
}
// See http://bit.ly/3YIzFZ6 for more information on timestamps in AdvantageKit.
// Logger.disableDeterministicTimestamps()
// Start AdvantageKit logger
Logger.start();
// Instantiate our RobotContainer. This will perform all our button bindings,
// and put our autonomous chooser on the dashboard.
m_robotContainer = new RobotContainer();
// Create a timer to disable motor brake a few seconds after disable. This will
// let the robot
// stop immediately when disabled, but then also let it be pushed more
m_disabledTimer = new Timer();
}
/** This function is called periodically during all modes. */
@Override
public void robotPeriodic() {
// Switch thread to high priority to improve loop timing
Threads.setCurrentThreadPriority(true, 99);
// Run all virtual subsystems each time through the loop
VirtualSubsystem.periodicAll();
// 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();
// Return to normal thread priority
Threads.setCurrentThreadPriority(false, 10);
}
/** This function is called once when the robot is disabled. */
@Override
public void disabledInit() {
// Set the brakes to stop robot motion
m_robotContainer.setMotorBrake(true);
m_disabledTimer.reset();
m_disabledTimer.start();
}
/** This function is called periodically when disabled. */
@Override
public void disabledPeriodic() {
// After WHEEL_LOCK_TIME has elapsed, release the drive brakes
if (m_disabledTimer.hasElapsed(Constants.DrivebaseConstants.kWheelLockTime)) {
m_robotContainer.setMotorBrake(false);
m_disabledTimer.stop();
}
}
/** This autonomous runs the autonomous command selected by your {@link RobotContainer} class. */
@Override
public void autonomousInit() {
m_robotContainer.setMotorBrake(true);
switch (Constants.getAutoType()) {
case PATHPLANNER:
m_autoCommandPathPlanner = m_robotContainer.getAutonomousCommandPathPlanner();
// schedule the autonomous command
if (m_autoCommandPathPlanner != null) {
m_autoCommandPathPlanner.schedule();
}
break;
case CHOREO:
m_robotContainer.getAutonomousCommandChoreo();
break;
default:
throw new RuntimeException(
"Incorrect AUTO type selected in Constants: " + Constants.getAutoType());
}
}
/** This function is called periodically during autonomous. */
@Override
public void autonomousPeriodic() {}
/** This function is called once when teleop is enabled. */
@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.
if (m_autoCommandPathPlanner != null) {
m_autoCommandPathPlanner.cancel();
} else {
CommandScheduler.getInstance().cancelAll();
}
m_robotContainer.setDriveMode();
m_robotContainer.setMotorBrake(true);
}
/** This function is called periodically during operator control. */
@Override
public void teleopPeriodic() {}
/** This function is called once when test mode is enabled. */
@Override
public void testInit() {
// Cancels all running commands at the start of test mode.
CommandScheduler.getInstance().cancelAll();
m_robotContainer.setDriveMode();
}
/** 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() {}
}