Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Compressor.cpp |
| 3 | */ |
| 4 | |
| 5 | #include "Compressor.h" |
| 6 | #include "WPIErrors.h" |
| 7 | |
| 8 | /** |
| 9 | * Constructor |
| 10 | * |
| 11 | * @param module The PCM ID to use (0-62) |
| 12 | */ |
| 13 | Compressor::Compressor(uint8_t pcmID) { |
| 14 | m_pcm_pointer = initializeCompressor(pcmID); |
| 15 | SetClosedLoopControl(true); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Starts closed-loop control. Note that closed loop control is enabled by |
| 20 | * default. |
| 21 | */ |
| 22 | void Compressor::Start() { SetClosedLoopControl(true); } |
| 23 | |
| 24 | /** |
| 25 | * Stops closed-loop control. Note that closed loop control is enabled by |
| 26 | * default. |
| 27 | */ |
| 28 | void Compressor::Stop() { SetClosedLoopControl(false); } |
| 29 | |
| 30 | /** |
| 31 | * Check if compressor output is active |
| 32 | * @return true if the compressor is on |
| 33 | */ |
| 34 | bool Compressor::Enabled() const { |
| 35 | int32_t status = 0; |
| 36 | bool value; |
| 37 | |
| 38 | value = getCompressor(m_pcm_pointer, &status); |
| 39 | |
| 40 | if (status) { |
| 41 | wpi_setWPIError(Timeout); |
| 42 | } |
| 43 | |
| 44 | return value; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Check if the pressure switch is triggered |
| 49 | * @return true if pressure is low |
| 50 | */ |
| 51 | bool Compressor::GetPressureSwitchValue() const { |
| 52 | int32_t status = 0; |
| 53 | bool value; |
| 54 | |
| 55 | value = getPressureSwitch(m_pcm_pointer, &status); |
| 56 | |
| 57 | if (status) { |
| 58 | wpi_setWPIError(Timeout); |
| 59 | } |
| 60 | |
| 61 | return value; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Query how much current the compressor is drawing |
| 66 | * @return The current through the compressor, in amps |
| 67 | */ |
| 68 | float Compressor::GetCompressorCurrent() const { |
| 69 | int32_t status = 0; |
| 70 | float value; |
| 71 | |
| 72 | value = getCompressorCurrent(m_pcm_pointer, &status); |
| 73 | |
| 74 | if (status) { |
| 75 | wpi_setWPIError(Timeout); |
| 76 | } |
| 77 | |
| 78 | return value; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Enables or disables automatically turning the compressor on when the |
| 83 | * pressure is low. |
| 84 | * @param on Set to true to enable closed loop control of the compressor. False |
| 85 | * to disable. |
| 86 | */ |
| 87 | void Compressor::SetClosedLoopControl(bool on) { |
| 88 | int32_t status = 0; |
| 89 | |
| 90 | setClosedLoopControl(m_pcm_pointer, on, &status); |
| 91 | |
| 92 | if (status) { |
| 93 | wpi_setWPIError(Timeout); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns true if the compressor will automatically turn on when the |
| 99 | * pressure is low. |
| 100 | * @return True if closed loop control of the compressor is enabled. False if |
| 101 | * disabled. |
| 102 | */ |
| 103 | bool Compressor::GetClosedLoopControl() const { |
| 104 | int32_t status = 0; |
| 105 | bool value; |
| 106 | |
| 107 | value = getClosedLoopControl(m_pcm_pointer, &status); |
| 108 | |
| 109 | if (status) { |
| 110 | wpi_setWPIError(Timeout); |
| 111 | } |
| 112 | |
| 113 | return value; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Query if the compressor output has been disabled due to high current draw. |
| 118 | * @return true if PCM is in fault state : Compressor Drive is |
| 119 | * disabled due to compressor current being too high. |
| 120 | */ |
| 121 | bool Compressor::GetCompressorCurrentTooHighFault() const { |
| 122 | int32_t status = 0; |
| 123 | bool value; |
| 124 | |
| 125 | value = getCompressorCurrentTooHighFault(m_pcm_pointer, &status); |
| 126 | |
| 127 | if (status) { |
| 128 | wpi_setWPIError(Timeout); |
| 129 | } |
| 130 | |
| 131 | return value; |
| 132 | } |
| 133 | /** |
| 134 | * Query if the compressor output has been disabled due to high current draw |
| 135 | * (sticky). |
| 136 | * A sticky fault will not clear on device reboot, it must be cleared through |
| 137 | * code or the webdash. |
| 138 | * @return true if PCM sticky fault is set : Compressor Drive is |
| 139 | * disabled due to compressor current being too high. |
| 140 | */ |
| 141 | bool Compressor::GetCompressorCurrentTooHighStickyFault() const { |
| 142 | int32_t status = 0; |
| 143 | bool value; |
| 144 | |
| 145 | value = getCompressorCurrentTooHighStickyFault(m_pcm_pointer, &status); |
| 146 | |
| 147 | if (status) { |
| 148 | wpi_setWPIError(Timeout); |
| 149 | } |
| 150 | |
| 151 | return value; |
| 152 | } |
| 153 | /** |
| 154 | * Query if the compressor output has been disabled due to a short circuit |
| 155 | * (sticky). |
| 156 | * A sticky fault will not clear on device reboot, it must be cleared through |
| 157 | * code or the webdash. |
| 158 | * @return true if PCM sticky fault is set : Compressor output |
| 159 | * appears to be shorted. |
| 160 | */ |
| 161 | bool Compressor::GetCompressorShortedStickyFault() const { |
| 162 | int32_t status = 0; |
| 163 | bool value; |
| 164 | |
| 165 | value = getCompressorShortedStickyFault(m_pcm_pointer, &status); |
| 166 | |
| 167 | if (status) { |
| 168 | wpi_setWPIError(Timeout); |
| 169 | } |
| 170 | |
| 171 | return value; |
| 172 | } |
| 173 | /** |
| 174 | * Query if the compressor output has been disabled due to a short circuit. |
| 175 | * @return true if PCM is in fault state : Compressor output |
| 176 | * appears to be shorted. |
| 177 | */ |
| 178 | bool Compressor::GetCompressorShortedFault() const { |
| 179 | int32_t status = 0; |
| 180 | bool value; |
| 181 | |
| 182 | value = getCompressorShortedFault(m_pcm_pointer, &status); |
| 183 | |
| 184 | if (status) { |
| 185 | wpi_setWPIError(Timeout); |
| 186 | } |
| 187 | |
| 188 | return value; |
| 189 | } |
| 190 | /** |
| 191 | * Query if the compressor output does not appear to be wired (sticky). |
| 192 | * A sticky fault will not clear on device reboot, it must be cleared through |
| 193 | * code or the webdash. |
| 194 | * @return true if PCM sticky fault is set : Compressor does not |
| 195 | * appear to be wired, i.e. compressor is |
| 196 | * not drawing enough current. |
| 197 | */ |
| 198 | bool Compressor::GetCompressorNotConnectedStickyFault() const { |
| 199 | int32_t status = 0; |
| 200 | bool value; |
| 201 | |
| 202 | value = getCompressorNotConnectedStickyFault(m_pcm_pointer, &status); |
| 203 | |
| 204 | if (status) { |
| 205 | wpi_setWPIError(Timeout); |
| 206 | } |
| 207 | |
| 208 | return value; |
| 209 | } |
| 210 | /** |
| 211 | * Query if the compressor output does not appear to be wired. |
| 212 | * @return true if PCM is in fault state : Compressor does not |
| 213 | * appear to be wired, i.e. compressor is |
| 214 | * not drawing enough current. |
| 215 | */ |
| 216 | bool Compressor::GetCompressorNotConnectedFault() const { |
| 217 | int32_t status = 0; |
| 218 | bool value; |
| 219 | |
| 220 | value = getCompressorNotConnectedFault(m_pcm_pointer, &status); |
| 221 | |
| 222 | if (status) { |
| 223 | wpi_setWPIError(Timeout); |
| 224 | } |
| 225 | |
| 226 | return value; |
| 227 | } |
| 228 | /** |
| 229 | * Clear ALL sticky faults inside PCM that Compressor is wired to. |
| 230 | * |
| 231 | * If a sticky fault is set, then it will be persistently cleared. Compressor |
| 232 | * drive |
| 233 | * maybe momentarily disable while flags are being cleared. Care |
| 234 | * should be |
| 235 | * taken to not call this too frequently, otherwise normal |
| 236 | * compressor |
| 237 | * functionality may be prevented. |
| 238 | * |
| 239 | * If no sticky faults are set then this call will have no effect. |
| 240 | */ |
| 241 | void Compressor::ClearAllPCMStickyFaults() { |
| 242 | int32_t status = 0; |
| 243 | |
| 244 | clearAllPCMStickyFaults(m_pcm_pointer, &status); |
| 245 | |
| 246 | if (status) { |
| 247 | wpi_setWPIError(Timeout); |
| 248 | } |
| 249 | } |
| 250 | void Compressor::UpdateTable() { |
| 251 | if (m_table) { |
| 252 | m_table->PutBoolean("Enabled", Enabled()); |
| 253 | m_table->PutBoolean("Pressure switch", GetPressureSwitchValue()); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void Compressor::StartLiveWindowMode() {} |
| 258 | |
| 259 | void Compressor::StopLiveWindowMode() {} |
| 260 | |
| 261 | std::string Compressor::GetSmartDashboardType() const { return "Compressor"; } |
| 262 | |
| 263 | void Compressor::InitTable(std::shared_ptr<ITable> subTable) { |
| 264 | m_table = subTable; |
| 265 | UpdateTable(); |
| 266 | } |
| 267 | |
| 268 | std::shared_ptr<ITable> Compressor::GetTable() const { return m_table; } |
| 269 | |
| 270 | void Compressor::ValueChanged(ITable* source, llvm::StringRef key, |
| 271 | std::shared_ptr<nt::Value> value, bool isNew) { |
| 272 | if (!value->IsBoolean()) return; |
| 273 | if (value->GetBoolean()) |
| 274 | Start(); |
| 275 | else |
| 276 | Stop(); |
| 277 | } |