Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include "frc/PneumaticsControlModule.h" |
| 6 | |
| 7 | #include <hal/CTREPCM.h> |
| 8 | #include <wpi/NullDeleter.h> |
| 9 | #include <wpi/StackTrace.h> |
| 10 | |
| 11 | #include "frc/Compressor.h" |
| 12 | #include "frc/DoubleSolenoid.h" |
| 13 | #include "frc/Errors.h" |
| 14 | #include "frc/SensorUtil.h" |
| 15 | #include "frc/Solenoid.h" |
| 16 | |
| 17 | using namespace frc; |
| 18 | |
| 19 | wpi::mutex PneumaticsControlModule::m_handleLock; |
| 20 | std::unique_ptr< |
| 21 | wpi::DenseMap<int, std::weak_ptr<PneumaticsControlModule::DataStore>>> |
| 22 | PneumaticsControlModule::m_handleMap = nullptr; |
| 23 | |
| 24 | // Always called under lock, so we can avoid the double lock from the magic |
| 25 | // static |
| 26 | std::weak_ptr<PneumaticsControlModule::DataStore>& |
| 27 | PneumaticsControlModule::GetDataStore(int module) { |
| 28 | if (!m_handleMap) { |
| 29 | m_handleMap = std::make_unique<wpi::DenseMap< |
| 30 | int, std::weak_ptr<PneumaticsControlModule::DataStore>>>(); |
| 31 | } |
| 32 | return (*m_handleMap)[module]; |
| 33 | } |
| 34 | |
| 35 | class PneumaticsControlModule::DataStore { |
| 36 | public: |
| 37 | explicit DataStore(int module, const char* stackTrace) { |
| 38 | int32_t status = 0; |
| 39 | HAL_CTREPCMHandle handle = |
| 40 | HAL_InitializeCTREPCM(module, stackTrace, &status); |
| 41 | FRC_CheckErrorStatus(status, "Module {}", module); |
| 42 | m_moduleObject = PneumaticsControlModule{handle, module}; |
| 43 | m_moduleObject.m_dataStore = |
| 44 | std::shared_ptr<DataStore>{this, wpi::NullDeleter<DataStore>()}; |
| 45 | } |
| 46 | |
| 47 | ~DataStore() noexcept { HAL_FreeCTREPCM(m_moduleObject.m_handle); } |
| 48 | |
| 49 | DataStore(DataStore&&) = delete; |
| 50 | DataStore& operator=(DataStore&&) = delete; |
| 51 | |
| 52 | private: |
| 53 | friend class PneumaticsControlModule; |
| 54 | uint32_t m_reservedMask{0}; |
| 55 | bool m_compressorReserved{false}; |
| 56 | wpi::mutex m_reservedLock; |
| 57 | PneumaticsControlModule m_moduleObject{HAL_kInvalidHandle, 0}; |
| 58 | }; |
| 59 | |
| 60 | PneumaticsControlModule::PneumaticsControlModule() |
| 61 | : PneumaticsControlModule{SensorUtil::GetDefaultCTREPCMModule()} {} |
| 62 | |
| 63 | PneumaticsControlModule::PneumaticsControlModule(int module) { |
| 64 | std::string stackTrace = wpi::GetStackTrace(1); |
| 65 | std::scoped_lock lock(m_handleLock); |
| 66 | auto& res = GetDataStore(module); |
| 67 | m_dataStore = res.lock(); |
| 68 | if (!m_dataStore) { |
| 69 | m_dataStore = std::make_shared<DataStore>(module, stackTrace.c_str()); |
| 70 | res = m_dataStore; |
| 71 | } |
| 72 | m_handle = m_dataStore->m_moduleObject.m_handle; |
| 73 | m_module = module; |
| 74 | } |
| 75 | |
| 76 | PneumaticsControlModule::PneumaticsControlModule(HAL_CTREPCMHandle handle, |
| 77 | int module) |
| 78 | : m_handle{handle}, m_module{module} {} |
| 79 | |
| 80 | bool PneumaticsControlModule::GetCompressor() const { |
| 81 | int32_t status = 0; |
| 82 | auto result = HAL_GetCTREPCMCompressor(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 83 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 84 | return result; |
| 85 | } |
| 86 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 87 | void PneumaticsControlModule::DisableCompressor() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 88 | int32_t status = 0; |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 89 | HAL_SetCTREPCMClosedLoopControl(m_handle, false, &status); |
| 90 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 93 | void PneumaticsControlModule::EnableCompressorDigital() { |
| 94 | int32_t status = 0; |
| 95 | HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status); |
| 96 | FRC_ReportError(status, "Module {}", m_module); |
| 97 | } |
| 98 | |
| 99 | void PneumaticsControlModule::EnableCompressorAnalog( |
| 100 | units::pounds_per_square_inch_t minPressure, |
| 101 | units::pounds_per_square_inch_t maxPressure) { |
| 102 | int32_t status = 0; |
| 103 | HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status); |
| 104 | FRC_ReportError(status, "Module {}", m_module); |
| 105 | } |
| 106 | |
| 107 | void PneumaticsControlModule::EnableCompressorHybrid( |
| 108 | units::pounds_per_square_inch_t minPressure, |
| 109 | units::pounds_per_square_inch_t maxPressure) { |
| 110 | int32_t status = 0; |
| 111 | HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status); |
| 112 | FRC_ReportError(status, "Module {}", m_module); |
| 113 | } |
| 114 | |
| 115 | CompressorConfigType PneumaticsControlModule::GetCompressorConfigType() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 116 | int32_t status = 0; |
| 117 | auto result = HAL_GetCTREPCMClosedLoopControl(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 118 | FRC_ReportError(status, "Module {}", m_module); |
| 119 | return result ? CompressorConfigType::Digital |
| 120 | : CompressorConfigType::Disabled; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool PneumaticsControlModule::GetPressureSwitch() const { |
| 124 | int32_t status = 0; |
| 125 | auto result = HAL_GetCTREPCMPressureSwitch(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 126 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 127 | return result; |
| 128 | } |
| 129 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 130 | units::ampere_t PneumaticsControlModule::GetCompressorCurrent() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 131 | int32_t status = 0; |
| 132 | auto result = HAL_GetCTREPCMCompressorCurrent(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 133 | FRC_ReportError(status, "Module {}", m_module); |
| 134 | return units::ampere_t{result}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | bool PneumaticsControlModule::GetCompressorCurrentTooHighFault() const { |
| 138 | int32_t status = 0; |
| 139 | auto result = HAL_GetCTREPCMCompressorCurrentTooHighFault(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 140 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 141 | return result; |
| 142 | } |
| 143 | bool PneumaticsControlModule::GetCompressorCurrentTooHighStickyFault() const { |
| 144 | int32_t status = 0; |
| 145 | auto result = |
| 146 | HAL_GetCTREPCMCompressorCurrentTooHighStickyFault(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 147 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 148 | return result; |
| 149 | } |
| 150 | bool PneumaticsControlModule::GetCompressorShortedFault() const { |
| 151 | int32_t status = 0; |
| 152 | auto result = HAL_GetCTREPCMCompressorShortedFault(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 153 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 154 | return result; |
| 155 | } |
| 156 | bool PneumaticsControlModule::GetCompressorShortedStickyFault() const { |
| 157 | int32_t status = 0; |
| 158 | auto result = HAL_GetCTREPCMCompressorShortedStickyFault(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 159 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 160 | return result; |
| 161 | } |
| 162 | bool PneumaticsControlModule::GetCompressorNotConnectedFault() const { |
| 163 | int32_t status = 0; |
| 164 | auto result = HAL_GetCTREPCMCompressorNotConnectedFault(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 165 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 166 | return result; |
| 167 | } |
| 168 | bool PneumaticsControlModule::GetCompressorNotConnectedStickyFault() const { |
| 169 | int32_t status = 0; |
| 170 | auto result = |
| 171 | HAL_GetCTREPCMCompressorNotConnectedStickyFault(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 172 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 173 | return result; |
| 174 | } |
| 175 | |
| 176 | bool PneumaticsControlModule::GetSolenoidVoltageFault() const { |
| 177 | int32_t status = 0; |
| 178 | auto result = HAL_GetCTREPCMSolenoidVoltageFault(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 179 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 180 | return result; |
| 181 | } |
| 182 | bool PneumaticsControlModule::GetSolenoidVoltageStickyFault() const { |
| 183 | int32_t status = 0; |
| 184 | auto result = HAL_GetCTREPCMSolenoidVoltageStickyFault(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 185 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 186 | return result; |
| 187 | } |
| 188 | |
| 189 | void PneumaticsControlModule::ClearAllStickyFaults() { |
| 190 | int32_t status = 0; |
| 191 | HAL_ClearAllCTREPCMStickyFaults(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 192 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void PneumaticsControlModule::SetSolenoids(int mask, int values) { |
| 196 | int32_t status = 0; |
| 197 | HAL_SetCTREPCMSolenoids(m_handle, mask, values, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 198 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | int PneumaticsControlModule::GetSolenoids() const { |
| 202 | int32_t status = 0; |
| 203 | auto result = HAL_GetCTREPCMSolenoids(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 204 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 205 | return result; |
| 206 | } |
| 207 | |
| 208 | int PneumaticsControlModule::GetModuleNumber() const { |
| 209 | return m_module; |
| 210 | } |
| 211 | |
| 212 | int PneumaticsControlModule::GetSolenoidDisabledList() const { |
| 213 | int32_t status = 0; |
| 214 | auto result = HAL_GetCTREPCMSolenoidDisabledList(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 215 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 216 | return result; |
| 217 | } |
| 218 | |
| 219 | void PneumaticsControlModule::FireOneShot(int index) { |
| 220 | int32_t status = 0; |
| 221 | HAL_FireCTREPCMOneShot(m_handle, index, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 222 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void PneumaticsControlModule::SetOneShotDuration(int index, |
| 226 | units::second_t duration) { |
| 227 | int32_t status = 0; |
| 228 | units::millisecond_t millis = duration; |
| 229 | HAL_SetCTREPCMOneShotDuration(m_handle, index, millis.to<int32_t>(), &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 230 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | bool PneumaticsControlModule::CheckSolenoidChannel(int channel) const { |
| 234 | return HAL_CheckCTREPCMSolenoidChannel(channel); |
| 235 | } |
| 236 | |
| 237 | int PneumaticsControlModule::CheckAndReserveSolenoids(int mask) { |
| 238 | std::scoped_lock lock{m_dataStore->m_reservedLock}; |
| 239 | uint32_t uMask = static_cast<uint32_t>(mask); |
| 240 | if ((m_dataStore->m_reservedMask & uMask) != 0) { |
| 241 | return m_dataStore->m_reservedMask & uMask; |
| 242 | } |
| 243 | m_dataStore->m_reservedMask |= uMask; |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | void PneumaticsControlModule::UnreserveSolenoids(int mask) { |
| 248 | std::scoped_lock lock{m_dataStore->m_reservedLock}; |
| 249 | m_dataStore->m_reservedMask &= ~(static_cast<uint32_t>(mask)); |
| 250 | } |
| 251 | |
| 252 | bool PneumaticsControlModule::ReserveCompressor() { |
| 253 | std::scoped_lock lock{m_dataStore->m_reservedLock}; |
| 254 | if (m_dataStore->m_compressorReserved) { |
| 255 | return false; |
| 256 | } |
| 257 | m_dataStore->m_compressorReserved = true; |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | void PneumaticsControlModule::UnreserveCompressor() { |
| 262 | std::scoped_lock lock{m_dataStore->m_reservedLock}; |
| 263 | m_dataStore->m_compressorReserved = false; |
| 264 | } |
| 265 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 266 | units::volt_t PneumaticsControlModule::GetAnalogVoltage(int channel) const { |
| 267 | return units::volt_t{0}; |
| 268 | } |
| 269 | |
| 270 | units::pounds_per_square_inch_t PneumaticsControlModule::GetPressure( |
| 271 | int channel) const { |
| 272 | return 0_psi; |
| 273 | } |
| 274 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 275 | Solenoid PneumaticsControlModule::MakeSolenoid(int channel) { |
| 276 | return Solenoid{m_module, PneumaticsModuleType::CTREPCM, channel}; |
| 277 | } |
| 278 | |
| 279 | DoubleSolenoid PneumaticsControlModule::MakeDoubleSolenoid(int forwardChannel, |
| 280 | int reverseChannel) { |
| 281 | return DoubleSolenoid{m_module, PneumaticsModuleType::CTREPCM, forwardChannel, |
| 282 | reverseChannel}; |
| 283 | } |
| 284 | |
| 285 | Compressor PneumaticsControlModule::MakeCompressor() { |
| 286 | return Compressor{m_module, PneumaticsModuleType::CTREPCM}; |
| 287 | } |
| 288 | |
| 289 | std::shared_ptr<PneumaticsBase> PneumaticsControlModule::GetForModule( |
| 290 | int module) { |
| 291 | std::string stackTrace = wpi::GetStackTrace(1); |
| 292 | std::scoped_lock lock(m_handleLock); |
| 293 | auto& res = GetDataStore(module); |
| 294 | std::shared_ptr<DataStore> dataStore = res.lock(); |
| 295 | if (!dataStore) { |
| 296 | dataStore = std::make_shared<DataStore>(module, stackTrace.c_str()); |
| 297 | res = dataStore; |
| 298 | } |
| 299 | |
| 300 | return std::shared_ptr<PneumaticsBase>{dataStore, &dataStore->m_moduleObject}; |
| 301 | } |