|
| 1 | + |
| 2 | +package Team4450.Lib; |
| 3 | + |
| 4 | +import edu.wpi.first.util.sendable.Sendable; |
| 5 | +import edu.wpi.first.util.sendable.SendableBuilder; |
| 6 | +import edu.wpi.first.util.sendable.SendableRegistry; |
| 7 | +import edu.wpi.first.wpilibj.DriverStation; |
| 8 | +import edu.wpi.first.wpilibj.PneumaticsModuleType; |
| 9 | +import edu.wpi.first.wpilibj.Timer; |
| 10 | +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
| 11 | +import edu.wpi.first.wpilibj.Compressor; |
| 12 | + |
| 13 | +/** |
| 14 | + * Compressor monitoring task. For REV Pneumatics Hub only. |
| 15 | + * Runs as a separate thread from our MyRobot class. Runs until our |
| 16 | + * program is terminated from the RoboRio. Displays compressor on/off |
| 17 | + * LED on DS. Can also monitor an air pressure sensor and report the |
| 18 | + * pressure to the DS. Assumes compressor is plugged into the first |
| 19 | + * PH, device id 1. |
| 20 | + */ |
| 21 | + |
| 22 | +public class MonitorCompressorPH extends Thread implements Sendable |
| 23 | +{ |
| 24 | + private final Compressor compressor; |
| 25 | + private double delay = 2.0, lowPressureThreshold = 0.0, correction = 0.0; |
| 26 | + private boolean lowPressureAlarm = false, ledState = false, compressorState; |
| 27 | + private int loopCount; |
| 28 | + |
| 29 | + /** |
| 30 | + * Static reference to the internal MonitorCompressorPH instance created by |
| 31 | + * getInstance() calls on this class. Must call a getInstance() before using. |
| 32 | + */ |
| 33 | + public static MonitorCompressorPH INSTANCE; |
| 34 | + |
| 35 | + // Create single instance of this class and return that single instance to any callers. |
| 36 | + // This is the singleton class model. You don't use new, you use getInstance. After that |
| 37 | + // you can use the returned instance reference in a variable in your code or use the |
| 38 | + // INSTANCE variable above to access the members of this class. Assumes robot will have |
| 39 | + // only one compressor. |
| 40 | + |
| 41 | + /** |
| 42 | + * Get a reference to global MonitorCompressorPH Thread object. Monitors compressor on/off |
| 43 | + * and sets DS LED named Compressor accordingly. Also monitors pressure on analog I/O port. |
| 44 | + * Pressure is displayed on DS gauge called AirPressure. Can also do an alarm LED called |
| 45 | + * LowPressure if you set the low pressure threshold. |
| 46 | + * @param compressor Reference to a Compressor object of type REVPH. |
| 47 | + * @return Reference to global MonitorCompressorPH object. |
| 48 | + */ |
| 49 | + |
| 50 | + public static MonitorCompressorPH getInstance(Compressor compressor) |
| 51 | + { |
| 52 | + Util.consoleLog(); |
| 53 | + |
| 54 | + if (INSTANCE == null) INSTANCE = new MonitorCompressorPH(compressor); |
| 55 | + |
| 56 | + return INSTANCE; |
| 57 | + } |
| 58 | + |
| 59 | + private MonitorCompressorPH(Compressor compressor) |
| 60 | + { |
| 61 | + Util.consoleLog(); |
| 62 | + |
| 63 | + this.compressor = compressor; |
| 64 | + |
| 65 | + this.setName("MonitorCompressorPH"); |
| 66 | + |
| 67 | + SmartDashboard.putBoolean("LowPressure", false); |
| 68 | + |
| 69 | + SendableRegistry.addLW(this, "MonitorCompressorPH", 1); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * If monitoring pressure, return the current pressure from port 0. |
| 74 | + * @return Current pressure in PSI. |
| 75 | + */ |
| 76 | + public double getPressure() |
| 77 | + { |
| 78 | + Double pressure = compressor.getPressure(); |
| 79 | + |
| 80 | + if (Double.isNaN(pressure)) pressure = 0.0; |
| 81 | + |
| 82 | + return pressure + correction; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Return the pressure sensor current voltage on port 0. |
| 87 | + * @return Sensor voltage. |
| 88 | + */ |
| 89 | + public double getVoltage() |
| 90 | + { |
| 91 | + return compressor.getAnalogVoltage(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Set the delay of the sampling loop. Longer delay works when only monitoring compressor on/off. |
| 96 | + * Shorter delay may be appropriate when monitoring pressure. |
| 97 | + * @param seconds Delay in second between samples. Minimum .5 second. Defaults to 2 seconds. |
| 98 | + */ |
| 99 | + public void setDelay(double seconds) |
| 100 | + { |
| 101 | + if (delay < 0.5) delay = 0.5; |
| 102 | + |
| 103 | + delay = seconds; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Set correction value to be added to the pressure to make it |
| 108 | + * match the gauge. |
| 109 | + * @param psi Correction value in PSI. |
| 110 | + */ |
| 111 | + public void setCorrection(double psi) |
| 112 | + { |
| 113 | + correction = psi; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Enable low pressure alarm LED on DS by setting pressure at which |
| 118 | + * alarm will trigger. Expects LED to be named LowPressure. |
| 119 | + * @param psi The low pressure in PSI. |
| 120 | + */ |
| 121 | + public void SetLowPressureAlarm(double psi) |
| 122 | + { |
| 123 | + if (psi < 0) psi = 0; |
| 124 | + |
| 125 | + lowPressureThreshold = psi; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Start monitoring. Called by Thread.start(). |
| 130 | + */ |
| 131 | + public void run() |
| 132 | + { |
| 133 | + boolean saveState = false; |
| 134 | + double pressure; |
| 135 | + |
| 136 | + try |
| 137 | + { |
| 138 | + Util.consoleLog(); |
| 139 | + |
| 140 | + while (!isInterrupted()) |
| 141 | + { |
| 142 | + compressorState = compressor.isEnabled(); |
| 143 | + |
| 144 | + if (compressorState != saveState) |
| 145 | + { |
| 146 | + saveState = compressorState; |
| 147 | + SmartDashboard.putBoolean("Compressor", saveState); |
| 148 | + Util.consoleLog("compressor on=%b", saveState); |
| 149 | + } |
| 150 | + |
| 151 | + pressure = getPressure(); |
| 152 | + |
| 153 | + |
| 154 | + SmartDashboard.putNumber("AirPressure", (int) pressure); |
| 155 | + |
| 156 | + if (lowPressureThreshold > 0) |
| 157 | + { |
| 158 | + if (pressure <= lowPressureThreshold) |
| 159 | + { |
| 160 | + if (!lowPressureAlarm) DriverStation.reportError(String.format("low air pressure alarm: %dpsi", (int) pressure), false); |
| 161 | + |
| 162 | + lowPressureAlarm = true; |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + if (lowPressureAlarm) DriverStation.reportError("low air pressure alarm cleared", false); |
| 167 | + |
| 168 | + lowPressureAlarm = false; |
| 169 | + } |
| 170 | + |
| 171 | + if (lowPressureAlarm) |
| 172 | + ledState = !ledState; |
| 173 | + else |
| 174 | + ledState = false; |
| 175 | + |
| 176 | + SmartDashboard.putBoolean("LowPressure", ledState); |
| 177 | + |
| 178 | + loopCount++; |
| 179 | + } |
| 180 | + |
| 181 | + Timer.delay(delay); |
| 182 | + } |
| 183 | + } |
| 184 | + catch (Throwable e) {Util.logException(e);} |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void initSendable( SendableBuilder builder ) |
| 189 | + { |
| 190 | + builder.setSmartDashboardType("MonitorCompressorPH"); |
| 191 | + builder.addBooleanProperty(".controllable", () -> false, null); |
| 192 | + builder.addDoubleProperty("PSI", this::getPressure, null); |
| 193 | + builder.addBooleanProperty("On", () -> compressorState, null); |
| 194 | + builder.addBooleanProperty("Alarm", () -> lowPressureAlarm, null); |
| 195 | + builder.addDoubleProperty("Loop", () -> loopCount, null); |
| 196 | + } |
| 197 | +} |
0 commit comments