-
Notifications
You must be signed in to change notification settings - Fork 35
/
SubsystemManager.java
102 lines (79 loc) · 2.61 KB
/
SubsystemManager.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
package com.team254.frc2019;
import com.team254.frc2019.loops.ILooper;
import com.team254.frc2019.loops.Loop;
import com.team254.frc2019.loops.Looper;
import com.team254.frc2019.subsystems.Subsystem;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Used to reset, start, stop, and update all subsystems at once
*/
public class SubsystemManager implements ILooper {
public static SubsystemManager mInstance = null;
private List<Subsystem> mAllSubsystems;
private List<Loop> mLoops = new ArrayList<>();
private SubsystemManager() {}
public static SubsystemManager getInstance() {
if (mInstance == null) {
mInstance = new SubsystemManager();
}
return mInstance;
}
public void outputToSmartDashboard() {
mAllSubsystems.forEach(Subsystem::outputTelemetry);
}
public boolean checkSubsystems() {
boolean ret_val = true;
for (Subsystem s : mAllSubsystems) {
ret_val &= s.checkSystem();
}
return ret_val;
}
public void stop() {
mAllSubsystems.forEach(Subsystem::stop);
}
public List<Subsystem> getSubsystems() {
return mAllSubsystems;
}
public void setSubsystems(Subsystem... allSubsystems) {
mAllSubsystems = Arrays.asList(allSubsystems);
}
private class EnabledLoop implements Loop {
@Override
public void onStart(double timestamp) {
mLoops.forEach(l -> l.onStart(timestamp));
}
@Override
public void onLoop(double timestamp) {
mAllSubsystems.forEach(Subsystem::readPeriodicInputs);
mLoops.forEach(l -> l.onLoop(timestamp));
mAllSubsystems.forEach(Subsystem::writePeriodicOutputs);
}
@Override
public void onStop(double timestamp) {
mLoops.forEach(l -> l.onStop(timestamp));
}
}
private class DisabledLoop implements Loop {
@Override
public void onStart(double timestamp) {}
@Override
public void onLoop(double timestamp) {
mAllSubsystems.forEach(Subsystem::readPeriodicInputs);
}
@Override
public void onStop(double timestamp) {}
}
public void registerEnabledLoops(Looper enabledLooper) {
mAllSubsystems.forEach(s -> s.registerEnabledLoops(this));
enabledLooper.register(new EnabledLoop());
}
public void registerDisabledLoops(Looper disabledLooper) {
disabledLooper.register(new DisabledLoop());
}
@Override
public void register(Loop loop) {
mLoops.add(loop);
}
}