-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHubShiftUtil.java
More file actions
231 lines (198 loc) · 8.18 KB
/
Copy pathHubShiftUtil.java
File metadata and controls
231 lines (198 loc) · 8.18 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
// Copyright (c) 2025-2026 Littleton Robotics
// http://github.com/Mechanical-Advantage
//
// MIT License
// Copyright (c) 2025-2026 Littleton Robotics
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package frc.robot.util;
import java.util.Optional;
import java.util.function.Supplier;
import org.littletonrobotics.junction.AutoLogOutput;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.DriverStation.Alliance;
import edu.wpi.first.wpilibj.Timer;
public class HubShiftUtil {
public enum ShiftEnum {
TRANSITION,
SHIFT1,
SHIFT2,
SHIFT3,
SHIFT4,
ENDGAME,
AUTO,
DISABLED;
}
public record ShiftInfo(
ShiftEnum currentShift, double elapsedTime, double remainingTime, boolean active) {}
private static Timer shiftTimer = new Timer();
private static final ShiftEnum[] shiftsEnums = ShiftEnum.values();
private static final double[] shiftStartTimes = {0.0, 10.0, 35.0, 60.0, 85.0, 110.0};
private static final double[] shiftEndTimes = {10.0, 35.0, 60.0, 85.0, 110.0, 140.0};
// private static final double minFuelCountDelay = 1.0;
// private static final double maxFuelCountDelay = 2.0;
// private static final double shiftEndFuelCountExtension = 3.0;
public static final double autoEndTime = 20.0;
public static final double teleopDuration = 140.0;
private static final boolean[] activeSchedule = {true, true, false, true, false, true};
private static final boolean[] inactiveSchedule = {true, false, true, false, true, true};
private static final double timeResetThreshold = 1.0;
private static double shiftTimerOffset = 0.0;
private static Supplier<Optional<Boolean>> allianceWinOverride = () -> Optional.empty();
public static Optional<Boolean> getAllianceWinOverride() {
return allianceWinOverride.get();
}
public static void setAllianceWinOverride(Supplier<Optional<Boolean>> win) {
allianceWinOverride = win;
}
public static Alliance getFirstActiveAlliance() {
var alliance = DriverStation.getAlliance().orElse(Alliance.Blue);
// Return override value
var winOverride = getAllianceWinOverride();
if (!winOverride.isEmpty()) {
return winOverride.get()
? (alliance == Alliance.Blue ? Alliance.Red : Alliance.Blue)
: (alliance == Alliance.Blue ? Alliance.Blue : Alliance.Red);
}
// Return FMS value
String message = DriverStation.getGameSpecificMessage();
if (message.length() > 0) {
char character = message.charAt(0);
if (character == 'R') {
return Alliance.Blue;
} else if (character == 'B') {
return Alliance.Red;
}
}
// Return default value
return alliance == Alliance.Blue ? Alliance.Red : Alliance.Blue;
}
/** Starts the timer at the begining of teleop. */
public static void initialize() {
shiftTimerOffset = 0;
shiftTimer.restart();
}
private static boolean[] getSchedule() {
boolean[] currentSchedule;
Alliance startAlliance = getFirstActiveAlliance();
currentSchedule =
startAlliance == DriverStation.getAlliance().orElse(Alliance.Blue)
? activeSchedule
: inactiveSchedule;
return currentSchedule;
}
@AutoLogOutput
private static ShiftInfo getShiftInfo(
boolean[] currentSchedule, double[] shiftStartTimes, double[] shiftEndTimes) {
double timerValue = shiftTimer.get();
double currentTime = timerValue - shiftTimerOffset;
double stateTimeElapsed = currentTime;
double stateTimeRemaining = 0.0;
boolean active = false;
ShiftEnum currentShift = ShiftEnum.DISABLED;
double fieldTeleopTime = 140.0 - DriverStation.getMatchTime();
if (DriverStation.isAutonomousEnabled()) {
stateTimeElapsed = currentTime;
stateTimeRemaining = autoEndTime - currentTime;
active = true;
currentShift = ShiftEnum.AUTO;
} else if (DriverStation.isEnabled()) {
// Adjust the current offset if the time difference above the theshold
if (Math.abs(fieldTeleopTime - currentTime) >= timeResetThreshold
&& fieldTeleopTime <= 135
&& DriverStation.isFMSAttached()) {
// shiftTimerOffset += currentTime - fieldTeleopTime;
// currentTime = timerValue + shiftTimerOffset;
shiftTimerOffset = timerValue - fieldTeleopTime;
currentTime = fieldTeleopTime;
}
int currentShiftIndex = -1;
for (int i = 0; i < shiftStartTimes.length; i++) {
if (currentTime >= shiftStartTimes[i] && currentTime < shiftEndTimes[i]) {
currentShiftIndex = i;
break;
}
}
if (currentShiftIndex < 0) {
// After last shift, so assume endgame
currentShiftIndex = shiftStartTimes.length - 1;
}
// Calculate elapsed and remaining time in the current shift, ignoring combined shifts
stateTimeElapsed = currentTime - shiftStartTimes[currentShiftIndex];
stateTimeRemaining = shiftEndTimes[currentShiftIndex] - currentTime;
// If the state is the same as the last shift, combine the elapsed time
if (currentShiftIndex > 0) {
if (currentSchedule[currentShiftIndex] == currentSchedule[currentShiftIndex - 1]) {
stateTimeElapsed = currentTime - shiftStartTimes[currentShiftIndex - 1];
}
}
// If the state is the same as the next shift, combine the remaining time
if (currentShiftIndex < shiftEndTimes.length - 1) {
if (currentSchedule[currentShiftIndex] == currentSchedule[currentShiftIndex + 1]) {
stateTimeRemaining = shiftEndTimes[currentShiftIndex + 1] - currentTime;
}
}
active = currentSchedule[currentShiftIndex];
currentShift = shiftsEnums[currentShiftIndex];
}
ShiftInfo shiftInfo = new ShiftInfo(currentShift, stateTimeElapsed, stateTimeRemaining, active);
return shiftInfo;
}
public static ShiftInfo getOfficialShiftInfo() {
return getShiftInfo(getSchedule(), shiftStartTimes, shiftEndTimes);
}
// public static ShiftInfo getShiftedShiftInfo() {
// boolean[] shiftSchedule = getSchedule();
// // Starting active
// if (shiftSchedule[1] == true) {
// double[] shiftedShiftStartTimes = {
// 0.0,
// 10.0,
// 35.0 + endingActiveFudge,
// 60.0 + approachingActiveFudge,
// 85.0 + endingActiveFudge,
// 110.0 + approachingActiveFudge
// };
// double[] shiftedShiftEndTimes = {
// 10.0,
// 35.0 + endingActiveFudge,
// 60.0 + approachingActiveFudge,
// 85.0 + endingActiveFudge,
// 110.0 + approachingActiveFudge,
// 140.0
// };
// return getShiftInfo(shiftSchedule, shiftedShiftStartTimes, shiftedShiftEndTimes);
// }
// double[] shiftedShiftStartTimes = {
// 0.0,
// 10.0 + endingActiveFudge,
// 35.0 + approachingActiveFudge,
// 60.0 + endingActiveFudge,
// 85.0 + approachingActiveFudge,
// 110.0
// };
// double[] shiftedShiftEndTimes = {
// 10.0 + endingActiveFudge,
// 35.0 + approachingActiveFudge,
// 60.0 + endingActiveFudge,
// 85.0 + approachingActiveFudge,
// 110.0,
// 140.0
// };
// return getShiftInfo(shiftSchedule, shiftedShiftStartTimes, shiftedShiftEndTimes);
// // }
// }
}