-
Notifications
You must be signed in to change notification settings - Fork 35
/
LimelightManager.java
163 lines (136 loc) · 4.98 KB
/
LimelightManager.java
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
package com.team254.frc2019.subsystems;
import com.team254.frc2019.Constants;
import com.team254.frc2019.GamePiece;
import com.team254.frc2019.RobotState;
import com.team254.frc2019.loops.ILooper;
import com.team254.frc2019.loops.Loop;
import java.util.List;
/**
* Class that manages using multiple Limelight 2's, one at a time
*
* @see Limelight
*/
public class LimelightManager extends Subsystem {
private static LimelightManager sInstance = null;
private Limelight mTopLimelight;
private Limelight mBottomLimelight;
private Elevator mElevator;
private List<Limelight> mAllLimelights;
enum ActiveLimelight {
TOP,
BOTTOM,
}
private ActiveLimelight mActiveLimelight = ActiveLimelight.TOP;
private LimelightManager() {
mTopLimelight = new Limelight(Constants.kTopLimelightConstants);
mBottomLimelight = new Limelight(Constants.kBottomLimelightConstants);
mAllLimelights = List.of(mTopLimelight, mBottomLimelight);
mElevator = Elevator.getInstance();
}
public static LimelightManager getInstance() {
if (sInstance == null) {
sInstance = new LimelightManager();
}
return sInstance;
}
@Override
public void registerEnabledLoops(ILooper mEnabledLooper) {
Loop mLoop = new Loop() {
@Override
public void onStart(double timestamp) {
mAllLimelights.forEach(limelight -> limelight.setLed(Limelight.LedMode.OFF));
RobotState.getInstance().resetVision();
}
@Override
public void onLoop(double timestamp) {
synchronized (LimelightManager.this) {
Limelight limelight;
if (mActiveLimelight == ActiveLimelight.TOP) {
limelight = mTopLimelight;
} else {
limelight = mBottomLimelight;
}
if (mActiveLimelight == ActiveLimelight.TOP &&
mElevator.getPosition() > Constants.kMaxTopLimelightHeight) {
RobotState.getInstance().addVisionUpdate(
timestamp - limelight.getLatency(),
null, getActiveLimelightObject());
} else {
RobotState.getInstance().addVisionUpdate(
timestamp - limelight.getLatency(),
limelight.getTarget(), getActiveLimelightObject());
}
}
}
@Override
public void onStop(double timestamp) {
stop();
}
};
mEnabledLooper.register(mLoop);
}
@Override
public synchronized void readPeriodicInputs() {
mAllLimelights.forEach(limelight -> limelight.readPeriodicInputs());
}
@Override
public synchronized void writePeriodicOutputs() {
mAllLimelights.forEach(limelight -> limelight.writePeriodicOutputs());
}
@Override
public synchronized void stop() {
mAllLimelights.forEach(limelight -> limelight.stop());
}
@Override
public boolean checkSystem() {
return true;
}
@Override
public void outputTelemetry() {
mAllLimelights.forEach(limelight -> limelight.outputTelemetry());
}
public synchronized ActiveLimelight getActiveLimelight() {
return mActiveLimelight;
}
public synchronized void setUseTopLimelight(boolean useTop) {
mActiveLimelight = useTop ? ActiveLimelight.TOP : ActiveLimelight.BOTTOM;
getInactiveLimelightObject().setLed(Limelight.LedMode.OFF);
getActiveLimelightObject().setLed(Limelight.LedMode.PIPELINE);
}
private synchronized Limelight getActiveLimelightObject() {
if (mActiveLimelight == ActiveLimelight.TOP) {
return mTopLimelight;
} else {
return mBottomLimelight;
}
}
private synchronized Limelight getInactiveLimelightObject() {
if (mActiveLimelight == ActiveLimelight.TOP) {
return mBottomLimelight;
} else {
return mTopLimelight;
}
}
public Limelight getTopLimelight() {
return mTopLimelight;
}
public Limelight getBottomLimelight() {
return mBottomLimelight;
}
public synchronized void setPipeline(int mode) {
mAllLimelights.forEach(limelight -> limelight.setPipeline(mode));
}
public synchronized void triggerOutputs() {
mAllLimelights.forEach(limelight -> limelight.triggerOutputs());
}
public synchronized void setAllLeds(Limelight.LedMode mode) {
mAllLimelights.forEach(limelight -> limelight.setLed(mode));
}
public synchronized void updatePipeline(GamePiece gamePiece) {
if (gamePiece == GamePiece.BALL) {
setPipeline(Limelight.kSortTopPipeline);
} else {
setPipeline(Limelight.kDefaultPipeline);
}
}
}