-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHubTracker.java
More file actions
161 lines (136 loc) · 4.59 KB
/
Copy pathHubTracker.java
File metadata and controls
161 lines (136 loc) · 4.59 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
package frc.robot.util;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.DriverStation.Alliance;
import frc.robot.config.RobotConstants;
import frc.robot.robotcontainer.enums.MatchPeriod;
import java.util.Optional;
public class HubTracker {
private static Optional<Alliance> generatedAutoWinner = Optional.empty();
private static Optional<Alliance> autoWinner = Optional.empty();
public static double getCurrentMatchTime() {
return DriverStation.getMatchTime();
}
public static boolean isBlue() {
return DriverStation.getAlliance().equals(Optional.of(Alliance.Blue));
}
public static boolean isAuton() {
return DriverStation.isAutonomous();
}
public static boolean isEnableShoot() {
return currentShiftIsOurs() || timeLeftInShiftSeconds() < 3.0;
}
public static MatchPeriod getMatchPeriod() {
if (getCurrentMatchTime() < 20 && getCurrentMatchTime() >= 0 && isAuton()) {
return MatchPeriod.AUTO;
} else if (getCurrentMatchTime() < 140 && getCurrentMatchTime() >= 130) {
return MatchPeriod.TRANSITION;
} else if (getCurrentMatchTime() < 130 && getCurrentMatchTime() >= 105) {
return MatchPeriod.SHIFT_ONE;
} else if (getCurrentMatchTime() < 105 && getCurrentMatchTime() >= 80) {
return MatchPeriod.SHIFT_TWO;
} else if (getCurrentMatchTime() < 80 && getCurrentMatchTime() >= 55) {
return MatchPeriod.SHIFT_THREE;
} else if (getCurrentMatchTime() < 55 && getCurrentMatchTime() >= 30) {
return MatchPeriod.SHIFT_FOUR;
} else if (getCurrentMatchTime() < 30 && getCurrentMatchTime() >= 0) {
return MatchPeriod.ENDGAME;
} else {
return MatchPeriod.NONE;
}
}
public static Optional<Alliance> getAutoWinner() {
if (!RobotConstants.isCompRobot) {
if (getMatchPeriod() == MatchPeriod.AUTO || getMatchPeriod() == MatchPeriod.NONE) {
return Optional.empty();
}
if (generatedAutoWinner.isEmpty()) {
resetGeneratedAutoWinner();
}
return generatedAutoWinner;
}
if (!autoWinner.isEmpty()) {
return autoWinner;
}
String gameData = DriverStation.getGameSpecificMessage();
if (gameData.length() > 0) {
switch (gameData.charAt(0)) {
case 'B':
autoWinner = Optional.of(Alliance.Blue);
break;
case 'R':
autoWinner = Optional.of(Alliance.Red);
break;
default:
System.err.println("THIS MESSAGE IS CORRUPT");
break;
}
}
return autoWinner;
}
public static double timeLeftInShiftSeconds() {
switch (getMatchPeriod()) {
case AUTO:
return DriverStation.getMatchTime();
case TRANSITION:
if (!weWonAuto()) {
return DriverStation.getMatchTime() - 105;
} else {
return DriverStation.getMatchTime() - 130;
}
case SHIFT_ONE:
return DriverStation.getMatchTime() - 105;
case SHIFT_TWO:
return DriverStation.getMatchTime() - 80;
case SHIFT_THREE:
return DriverStation.getMatchTime() - 55;
case SHIFT_FOUR:
if (weWonAuto()) {
return DriverStation.getMatchTime();
} else {
return DriverStation.getMatchTime() - 30;
}
case ENDGAME:
return DriverStation.getMatchTime();
default:
return 0.0;
}
}
public static void resetGeneratedAutoWinner() {
if (Math.random() < 0.5) {
generatedAutoWinner = Optional.of(Alliance.Blue);
} else {
generatedAutoWinner = Optional.of(Alliance.Red);
}
}
public static boolean weWonAuto() {
if (DriverStation.getAlliance().isPresent() && getAutoWinner().isPresent()) {
return getAutoWinner().get().equals(DriverStation.getAlliance().get());
}
return false;
}
public static Alliance currentShift() {
Alliance ourAlliance = DriverStation.getAlliance().get();
if (getAutoWinner().isEmpty()) {
return ourAlliance;
}
Alliance autoWinner = getAutoWinner().get();
Alliance other = autoWinner == Alliance.Blue ? Alliance.Red : Alliance.Blue;
MatchPeriod period = getMatchPeriod();
if (period == MatchPeriod.AUTO
|| period == MatchPeriod.TRANSITION
|| period == MatchPeriod.ENDGAME) {
return ourAlliance;
}
if (period == MatchPeriod.SHIFT_TWO || period == MatchPeriod.SHIFT_FOUR) {
return autoWinner;
}
return other;
}
public static boolean currentShiftIsOurs() {
Optional<Alliance> alliance = DriverStation.getAlliance();
if (alliance.isEmpty()) {
return true;
}
return currentShift().equals(alliance.get());
}
}