-
Notifications
You must be signed in to change notification settings - Fork 35
/
Vacuum.java
177 lines (145 loc) · 5.31 KB
/
Vacuum.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.team254.frc2019.subsystems;
import com.ctre.phoenix.motorcontrol.ControlMode;
import com.ctre.phoenix.motorcontrol.can.TalonSRX;
import com.team254.frc2019.Constants;
import com.team254.frc2019.loops.ILooper;
import com.team254.frc2019.loops.Loop;
import com.team254.lib.drivers.TalonSRXFactory;
import com.team254.lib.util.ReflectingCSVWriter;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* Controls a 775pro which drives a GM UP28 vacuum pump
* <p>
* Feedback on the pressure build up is given through a ProSense QPS digital
* pressure switch/transmitter, set on hysteresis mode
*/
public class Vacuum extends Subsystem {
private static Vacuum mInstance;
public synchronized static Vacuum getInstance() {
if (mInstance == null) {
mInstance = new Vacuum();
}
return mInstance;
}
private final TalonSRX mMaster;
private boolean mOn = false;
private CarriageCanifier mCanifier = CarriageCanifier.getInstance();
enum ThresholdState {
ABOVE, BETWEEN, BELOW
}
public static class PeriodicIO {
// INPUTS
public boolean lowPressureSensor;
public boolean highPressureSensor;
public ThresholdState thresholdState = ThresholdState.BELOW;
}
private PeriodicIO mPeriodicIO = new PeriodicIO();
private ReflectingCSVWriter<PeriodicIO> mCSVWriter = null;
private final double kFullSetpoint = -1.0;
private final double kSteadySetpoint = -0.8;
private Vacuum() {
mMaster = TalonSRXFactory.createDefaultTalon(Constants.kVacuumMasterId);
mMaster.overrideSoftLimitsEnable(false);
mMaster.configPeakCurrentLimit(30);
}
@Override
public void registerEnabledLoops(ILooper mEnabledLooper) {
mEnabledLooper.register(new Loop() {
@Override
public void onStart(double timestamp) {
if (mCSVWriter == null) {
// mCSVWriter = new ReflectingCSVWriter<>("/home/lvuser/VACUUM-LOGS.csv", PeriodicIO.class);
}
}
@Override
public void onLoop(double timestamp) {}
@Override
public void onStop(double timestamp) {
if (mCSVWriter != null) {
mCSVWriter.flush();
mCSVWriter = null;
}
stop();
}
});
}
public synchronized void setOn(boolean on) {
mOn = on;
}
/**
* @return whether we are actually at climbing pressure to auto climb
*/
public synchronized boolean isAtClimbingPressure() {
return mPeriodicIO.thresholdState == ThresholdState.ABOVE;
}
/**
* @return whether we are within the climable range to allow drivers to trigger climb
*/
public synchronized boolean isAlmostAtClimbingPressure() {
return mPeriodicIO.thresholdState == ThresholdState.BETWEEN
|| mPeriodicIO.thresholdState == ThresholdState.ABOVE;
}
private boolean getLowerPressureSensor() {
return !mCanifier.getLowPressureSensor(); // NPN sensor
}
private boolean getHigherPressureSensor() {
return !mCanifier.getHighPressureSensor(); // NPN sensor
}
@Override
public synchronized void readPeriodicInputs() {
mPeriodicIO.lowPressureSensor = getLowerPressureSensor();
mPeriodicIO.highPressureSensor = getHigherPressureSensor();
if (mPeriodicIO.highPressureSensor) {
mPeriodicIO.thresholdState = ThresholdState.ABOVE;
} else if (mPeriodicIO.lowPressureSensor && !mPeriodicIO.highPressureSensor) {
mPeriodicIO.thresholdState = ThresholdState.BETWEEN;
} else {
mPeriodicIO.thresholdState = ThresholdState.BELOW;
}
if (mCSVWriter != null) {
mCSVWriter.add(mPeriodicIO);
}
}
@Override
public synchronized void writePeriodicOutputs() {
double output = 0.0;
if (mOn) {
if (mPeriodicIO.thresholdState == ThresholdState.BELOW || mPeriodicIO.thresholdState == ThresholdState.BETWEEN) {
output = kFullSetpoint;
} else { // ABOVE
output = kSteadySetpoint;
}
}
mMaster.set(ControlMode.PercentOutput, output);
}
@Override
public synchronized void stop() {
mOn = false;
// Make sure the vacuum is off
mMaster.set(ControlMode.PercentOutput, 0.0);
}
@Override
public boolean checkSystem() {
return true;
}
@Override
public void outputTelemetry() {
SmartDashboard.putBoolean("Vacuum: Low sensor", getLowerPressureSensor());
SmartDashboard.putBoolean("Vacuum: High sensor", getHigherPressureSensor());
String state = "default";
if (mPeriodicIO.thresholdState == ThresholdState.ABOVE) {
state = "Above";
} else if (mPeriodicIO.thresholdState == ThresholdState.BETWEEN) {
state = "Between";
} else if (mPeriodicIO.thresholdState == ThresholdState.BELOW) {
state = "Below";
}
SmartDashboard.putString("Vacuum: Threshold state", state);
if (mCSVWriter != null) {
mCSVWriter.write();
}
}
public ThresholdState getVacuumState() {
return mPeriodicIO.thresholdState;
}
}