Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2014-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Compressor.h" |
| 9 | #include "HAL/Compressor.h" |
| 10 | |
| 11 | #include "HAL/HAL.h" |
| 12 | #include "HAL/Ports.h" |
| 13 | #include "HAL/Solenoid.h" |
| 14 | #include "WPIErrors.h" |
| 15 | |
| 16 | using namespace frc; |
| 17 | |
| 18 | /** |
| 19 | * Constructor. |
| 20 | * |
| 21 | * @param module The PCM ID to use (0-62) |
| 22 | */ |
| 23 | Compressor::Compressor(int pcmID) : m_module(pcmID) { |
| 24 | int32_t status = 0; |
| 25 | m_compressorHandle = HAL_InitializeCompressor(m_module, &status); |
| 26 | if (status != 0) { |
| 27 | wpi_setErrorWithContextRange(status, 0, HAL_GetNumPCMModules(), pcmID, |
| 28 | HAL_GetErrorMessage(status)); |
| 29 | return; |
| 30 | } |
| 31 | SetClosedLoopControl(true); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Starts closed-loop control. Note that closed loop control is enabled by |
| 36 | * default. |
| 37 | */ |
| 38 | void Compressor::Start() { |
| 39 | if (StatusIsFatal()) return; |
| 40 | SetClosedLoopControl(true); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Stops closed-loop control. Note that closed loop control is enabled by |
| 45 | * default. |
| 46 | */ |
| 47 | void Compressor::Stop() { |
| 48 | if (StatusIsFatal()) return; |
| 49 | SetClosedLoopControl(false); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Check if compressor output is active. |
| 54 | * |
| 55 | * @return true if the compressor is on |
| 56 | */ |
| 57 | bool Compressor::Enabled() const { |
| 58 | if (StatusIsFatal()) return false; |
| 59 | int32_t status = 0; |
| 60 | bool value; |
| 61 | |
| 62 | value = HAL_GetCompressor(m_compressorHandle, &status); |
| 63 | |
| 64 | if (status) { |
| 65 | wpi_setWPIError(Timeout); |
| 66 | } |
| 67 | |
| 68 | return value; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check if the pressure switch is triggered. |
| 73 | * |
| 74 | * @return true if pressure is low |
| 75 | */ |
| 76 | bool Compressor::GetPressureSwitchValue() const { |
| 77 | if (StatusIsFatal()) return false; |
| 78 | int32_t status = 0; |
| 79 | bool value; |
| 80 | |
| 81 | value = HAL_GetCompressorPressureSwitch(m_compressorHandle, &status); |
| 82 | |
| 83 | if (status) { |
| 84 | wpi_setWPIError(Timeout); |
| 85 | } |
| 86 | |
| 87 | return value; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Query how much current the compressor is drawing. |
| 92 | * |
| 93 | * @return The current through the compressor, in amps |
| 94 | */ |
| 95 | double Compressor::GetCompressorCurrent() const { |
| 96 | if (StatusIsFatal()) return 0; |
| 97 | int32_t status = 0; |
| 98 | double value; |
| 99 | |
| 100 | value = HAL_GetCompressorCurrent(m_compressorHandle, &status); |
| 101 | |
| 102 | if (status) { |
| 103 | wpi_setWPIError(Timeout); |
| 104 | } |
| 105 | |
| 106 | return value; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Enables or disables automatically turning the compressor on when the |
| 111 | * pressure is low. |
| 112 | * |
| 113 | * @param on Set to true to enable closed loop control of the compressor. False |
| 114 | * to disable. |
| 115 | */ |
| 116 | void Compressor::SetClosedLoopControl(bool on) { |
| 117 | if (StatusIsFatal()) return; |
| 118 | int32_t status = 0; |
| 119 | |
| 120 | HAL_SetCompressorClosedLoopControl(m_compressorHandle, on, &status); |
| 121 | |
| 122 | if (status) { |
| 123 | wpi_setWPIError(Timeout); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns true if the compressor will automatically turn on when the |
| 129 | * pressure is low. |
| 130 | * |
| 131 | * @return True if closed loop control of the compressor is enabled. False if |
| 132 | * disabled. |
| 133 | */ |
| 134 | bool Compressor::GetClosedLoopControl() const { |
| 135 | if (StatusIsFatal()) return false; |
| 136 | int32_t status = 0; |
| 137 | bool value; |
| 138 | |
| 139 | value = HAL_GetCompressorClosedLoopControl(m_compressorHandle, &status); |
| 140 | |
| 141 | if (status) { |
| 142 | wpi_setWPIError(Timeout); |
| 143 | } |
| 144 | |
| 145 | return value; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Query if the compressor output has been disabled due to high current draw. |
| 150 | * |
| 151 | * @return true if PCM is in fault state : Compressor Drive is |
| 152 | * disabled due to compressor current being too high. |
| 153 | */ |
| 154 | bool Compressor::GetCompressorCurrentTooHighFault() const { |
| 155 | if (StatusIsFatal()) return false; |
| 156 | int32_t status = 0; |
| 157 | bool value; |
| 158 | |
| 159 | value = HAL_GetCompressorCurrentTooHighFault(m_compressorHandle, &status); |
| 160 | |
| 161 | if (status) { |
| 162 | wpi_setWPIError(Timeout); |
| 163 | } |
| 164 | |
| 165 | return value; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Query if the compressor output has been disabled due to high current draw |
| 170 | * (sticky). |
| 171 | * |
| 172 | * A sticky fault will not clear on device reboot, it must be cleared through |
| 173 | * code or the webdash. |
| 174 | * |
| 175 | * @return true if PCM sticky fault is set : Compressor Drive is |
| 176 | * disabled due to compressor current being too high. |
| 177 | */ |
| 178 | bool Compressor::GetCompressorCurrentTooHighStickyFault() const { |
| 179 | if (StatusIsFatal()) return false; |
| 180 | int32_t status = 0; |
| 181 | bool value; |
| 182 | |
| 183 | value = |
| 184 | HAL_GetCompressorCurrentTooHighStickyFault(m_compressorHandle, &status); |
| 185 | |
| 186 | if (status) { |
| 187 | wpi_setWPIError(Timeout); |
| 188 | } |
| 189 | |
| 190 | return value; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Query if the compressor output has been disabled due to a short circuit |
| 195 | * (sticky). |
| 196 | * |
| 197 | * A sticky fault will not clear on device reboot, it must be cleared through |
| 198 | * code or the webdash. |
| 199 | * |
| 200 | * @return true if PCM sticky fault is set : Compressor output |
| 201 | * appears to be shorted. |
| 202 | */ |
| 203 | bool Compressor::GetCompressorShortedStickyFault() const { |
| 204 | if (StatusIsFatal()) return false; |
| 205 | int32_t status = 0; |
| 206 | bool value; |
| 207 | |
| 208 | value = HAL_GetCompressorShortedStickyFault(m_compressorHandle, &status); |
| 209 | |
| 210 | if (status) { |
| 211 | wpi_setWPIError(Timeout); |
| 212 | } |
| 213 | |
| 214 | return value; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Query if the compressor output has been disabled due to a short circuit. |
| 219 | * |
| 220 | * @return true if PCM is in fault state : Compressor output |
| 221 | * appears to be shorted. |
| 222 | */ |
| 223 | bool Compressor::GetCompressorShortedFault() const { |
| 224 | if (StatusIsFatal()) return false; |
| 225 | int32_t status = 0; |
| 226 | bool value; |
| 227 | |
| 228 | value = HAL_GetCompressorShortedFault(m_compressorHandle, &status); |
| 229 | |
| 230 | if (status) { |
| 231 | wpi_setWPIError(Timeout); |
| 232 | } |
| 233 | |
| 234 | return value; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Query if the compressor output does not appear to be wired (sticky). |
| 239 | * |
| 240 | * A sticky fault will not clear on device reboot, it must be cleared through |
| 241 | * code or the webdash. |
| 242 | * |
| 243 | * @return true if PCM sticky fault is set : Compressor does not |
| 244 | * appear to be wired, i.e. compressor is not drawing enough current. |
| 245 | */ |
| 246 | bool Compressor::GetCompressorNotConnectedStickyFault() const { |
| 247 | if (StatusIsFatal()) return false; |
| 248 | int32_t status = 0; |
| 249 | bool value; |
| 250 | |
| 251 | value = HAL_GetCompressorNotConnectedStickyFault(m_compressorHandle, &status); |
| 252 | |
| 253 | if (status) { |
| 254 | wpi_setWPIError(Timeout); |
| 255 | } |
| 256 | |
| 257 | return value; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Query if the compressor output does not appear to be wired. |
| 262 | * |
| 263 | * @return true if PCM is in fault state : Compressor does not |
| 264 | * appear to be wired, i.e. compressor is not drawing enough current. |
| 265 | */ |
| 266 | bool Compressor::GetCompressorNotConnectedFault() const { |
| 267 | if (StatusIsFatal()) return false; |
| 268 | int32_t status = 0; |
| 269 | bool value; |
| 270 | |
| 271 | value = HAL_GetCompressorNotConnectedFault(m_compressorHandle, &status); |
| 272 | |
| 273 | if (status) { |
| 274 | wpi_setWPIError(Timeout); |
| 275 | } |
| 276 | |
| 277 | return value; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Clear ALL sticky faults inside PCM that Compressor is wired to. |
| 282 | * |
| 283 | * If a sticky fault is set, then it will be persistently cleared. Compressor |
| 284 | * drive maybe momentarily disable while flags are being cleared. Care should |
| 285 | * be taken to not call this too frequently, otherwise normal compressor |
| 286 | * functionality may be prevented. |
| 287 | * |
| 288 | * If no sticky faults are set then this call will have no effect. |
| 289 | */ |
| 290 | void Compressor::ClearAllPCMStickyFaults() { |
| 291 | if (StatusIsFatal()) return; |
| 292 | int32_t status = 0; |
| 293 | |
| 294 | HAL_ClearAllPCMStickyFaults(m_module, &status); |
| 295 | |
| 296 | if (status) { |
| 297 | wpi_setWPIError(Timeout); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | void Compressor::UpdateTable() { |
| 302 | if (m_table) { |
| 303 | m_table->PutBoolean("Enabled", Enabled()); |
| 304 | m_table->PutBoolean("Pressure switch", GetPressureSwitchValue()); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void Compressor::StartLiveWindowMode() {} |
| 309 | |
| 310 | void Compressor::StopLiveWindowMode() {} |
| 311 | |
| 312 | std::string Compressor::GetSmartDashboardType() const { return "Compressor"; } |
| 313 | |
| 314 | void Compressor::InitTable(std::shared_ptr<ITable> subTable) { |
| 315 | m_table = subTable; |
| 316 | UpdateTable(); |
| 317 | } |
| 318 | |
| 319 | std::shared_ptr<ITable> Compressor::GetTable() const { return m_table; } |
| 320 | |
| 321 | void Compressor::ValueChanged(ITable* source, llvm::StringRef key, |
| 322 | std::shared_ptr<nt::Value> value, bool isNew) { |
| 323 | if (!value->IsBoolean()) return; |
| 324 | if (value->GetBoolean()) |
| 325 | Start(); |
| 326 | else |
| 327 | Stop(); |
| 328 | } |