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/PneumaticHub.h" |
| 6 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 7 | #include <array> |
| 8 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 9 | #include <fmt/format.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 10 | #include <hal/REVPH.h> |
| 11 | #include <wpi/NullDeleter.h> |
| 12 | #include <wpi/StackTrace.h> |
| 13 | |
| 14 | #include "frc/Compressor.h" |
| 15 | #include "frc/DoubleSolenoid.h" |
| 16 | #include "frc/Errors.h" |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 17 | #include "frc/RobotBase.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 18 | #include "frc/SensorUtil.h" |
| 19 | #include "frc/Solenoid.h" |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 20 | #include "frc/fmt/Units.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 21 | |
| 22 | using namespace frc; |
| 23 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 24 | /** Converts volts to PSI per the REV Analog Pressure Sensor datasheet. */ |
| 25 | units::pounds_per_square_inch_t VoltsToPSI(units::volt_t sensorVoltage, |
| 26 | units::volt_t supplyVoltage) { |
| 27 | auto pressure = 250 * (sensorVoltage.value() / supplyVoltage.value()) - 25; |
| 28 | return units::pounds_per_square_inch_t{pressure}; |
| 29 | } |
| 30 | |
| 31 | /** Converts PSI to volts per the REV Analog Pressure Sensor datasheet. */ |
| 32 | units::volt_t PSIToVolts(units::pounds_per_square_inch_t pressure, |
| 33 | units::volt_t supplyVoltage) { |
| 34 | auto voltage = supplyVoltage.value() * (0.004 * pressure.value() + 0.1); |
| 35 | return units::volt_t{voltage}; |
| 36 | } |
| 37 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 38 | wpi::mutex PneumaticHub::m_handleLock; |
| 39 | std::unique_ptr<wpi::DenseMap<int, std::weak_ptr<PneumaticHub::DataStore>>> |
| 40 | PneumaticHub::m_handleMap = nullptr; |
| 41 | |
| 42 | // Always called under lock, so we can avoid the double lock from the magic |
| 43 | // static |
| 44 | std::weak_ptr<PneumaticHub::DataStore>& PneumaticHub::GetDataStore(int module) { |
| 45 | if (!m_handleMap) { |
| 46 | m_handleMap = std::make_unique< |
| 47 | wpi::DenseMap<int, std::weak_ptr<PneumaticHub::DataStore>>>(); |
| 48 | } |
| 49 | return (*m_handleMap)[module]; |
| 50 | } |
| 51 | |
| 52 | class PneumaticHub::DataStore { |
| 53 | public: |
| 54 | explicit DataStore(int module, const char* stackTrace) { |
| 55 | int32_t status = 0; |
| 56 | HAL_REVPHHandle handle = HAL_InitializeREVPH(module, stackTrace, &status); |
| 57 | FRC_CheckErrorStatus(status, "Module {}", module); |
| 58 | m_moduleObject = PneumaticHub{handle, module}; |
| 59 | m_moduleObject.m_dataStore = |
| 60 | std::shared_ptr<DataStore>{this, wpi::NullDeleter<DataStore>()}; |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 61 | |
| 62 | auto version = m_moduleObject.GetVersion(); |
| 63 | |
| 64 | if (version.FirmwareMajor > 0 && RobotBase::IsReal()) { |
| 65 | // Write PH firmware version to roboRIO |
| 66 | std::FILE* file = nullptr; |
| 67 | file = std::fopen( |
| 68 | fmt::format("/tmp/frc_versions/REV_PH_{:0>2}_WPILib_Version.ini", |
| 69 | module) |
| 70 | .c_str(), |
| 71 | "w"); |
| 72 | if (file != nullptr) { |
| 73 | std::fputs("[Version]\n", file); |
| 74 | std::fputs(fmt::format("model=REV PH\n").c_str(), file); |
| 75 | std::fputs(fmt::format("deviceID={:x}\n", (0x9052600 | module)).c_str(), |
| 76 | file); |
| 77 | std::fputs(fmt::format("currentVersion={}.{}.{}", version.FirmwareMajor, |
| 78 | version.FirmwareMinor, version.FirmwareFix) |
| 79 | .c_str(), |
| 80 | file); |
| 81 | std::fclose(file); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Check PH firmware version |
| 86 | if (version.FirmwareMajor > 0 && version.FirmwareMajor < 22) { |
| 87 | throw FRC_MakeError( |
| 88 | err::AssertionFailure, |
| 89 | "The Pneumatic Hub has firmware version {}.{}.{}, and must be " |
| 90 | "updated to version 2022.0.0 or later using the REV Hardware Client", |
| 91 | version.FirmwareMajor, version.FirmwareMinor, version.FirmwareFix); |
| 92 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | ~DataStore() noexcept { HAL_FreeREVPH(m_moduleObject.m_handle); } |
| 96 | |
| 97 | DataStore(DataStore&&) = delete; |
| 98 | DataStore& operator=(DataStore&&) = delete; |
| 99 | |
| 100 | private: |
| 101 | friend class PneumaticHub; |
| 102 | uint32_t m_reservedMask{0}; |
| 103 | bool m_compressorReserved{false}; |
| 104 | wpi::mutex m_reservedLock; |
| 105 | PneumaticHub m_moduleObject{HAL_kInvalidHandle, 0}; |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 106 | std::array<units::millisecond_t, 16> m_oneShotDurMs{0_ms}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | PneumaticHub::PneumaticHub() |
| 110 | : PneumaticHub{SensorUtil::GetDefaultREVPHModule()} {} |
| 111 | |
| 112 | PneumaticHub::PneumaticHub(int module) { |
| 113 | std::string stackTrace = wpi::GetStackTrace(1); |
| 114 | std::scoped_lock lock(m_handleLock); |
| 115 | auto& res = GetDataStore(module); |
| 116 | m_dataStore = res.lock(); |
| 117 | if (!m_dataStore) { |
| 118 | m_dataStore = std::make_shared<DataStore>(module, stackTrace.c_str()); |
| 119 | res = m_dataStore; |
| 120 | } |
| 121 | m_handle = m_dataStore->m_moduleObject.m_handle; |
| 122 | m_module = module; |
| 123 | } |
| 124 | |
| 125 | PneumaticHub::PneumaticHub(HAL_REVPHHandle handle, int module) |
| 126 | : m_handle{handle}, m_module{module} {} |
| 127 | |
| 128 | bool PneumaticHub::GetCompressor() const { |
| 129 | int32_t status = 0; |
| 130 | auto result = HAL_GetREVPHCompressor(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 131 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 132 | return result; |
| 133 | } |
| 134 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 135 | void PneumaticHub::DisableCompressor() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 136 | int32_t status = 0; |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 137 | HAL_SetREVPHClosedLoopControlDisabled(m_handle, &status); |
| 138 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 141 | void PneumaticHub::EnableCompressorDigital() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 142 | int32_t status = 0; |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 143 | HAL_SetREVPHClosedLoopControlDigital(m_handle, &status); |
| 144 | FRC_ReportError(status, "Module {}", m_module); |
| 145 | } |
| 146 | |
| 147 | void PneumaticHub::EnableCompressorAnalog( |
| 148 | units::pounds_per_square_inch_t minPressure, |
| 149 | units::pounds_per_square_inch_t maxPressure) { |
| 150 | if (minPressure >= maxPressure) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 151 | throw FRC_MakeError(err::InvalidParameter, |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 152 | "maxPressure must be greater than minPresure"); |
| 153 | } |
| 154 | if (minPressure < 0_psi || minPressure > 120_psi) { |
| 155 | throw FRC_MakeError(err::ParameterOutOfRange, |
| 156 | "minPressure must be between 0 and 120 PSI, got {}", |
| 157 | minPressure); |
| 158 | } |
| 159 | if (maxPressure < 0_psi || maxPressure > 120_psi) { |
| 160 | throw FRC_MakeError(err::ParameterOutOfRange, |
| 161 | "maxPressure must be between 0 and 120 PSI, got {}", |
| 162 | maxPressure); |
| 163 | } |
| 164 | int32_t status = 0; |
| 165 | units::volt_t minAnalogVoltage = PSIToVolts(minPressure, 5_V); |
| 166 | units::volt_t maxAnalogVoltage = PSIToVolts(maxPressure, 5_V); |
| 167 | HAL_SetREVPHClosedLoopControlAnalog(m_handle, minAnalogVoltage.value(), |
| 168 | maxAnalogVoltage.value(), &status); |
| 169 | FRC_ReportError(status, "Module {}", m_module); |
| 170 | } |
| 171 | |
| 172 | void PneumaticHub::EnableCompressorHybrid( |
| 173 | units::pounds_per_square_inch_t minPressure, |
| 174 | units::pounds_per_square_inch_t maxPressure) { |
| 175 | if (minPressure >= maxPressure) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 176 | throw FRC_MakeError(err::InvalidParameter, |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 177 | "maxPressure must be greater than minPresure"); |
| 178 | } |
| 179 | if (minPressure < 0_psi || minPressure > 120_psi) { |
| 180 | throw FRC_MakeError(err::ParameterOutOfRange, |
| 181 | "minPressure must be between 0 and 120 PSI, got {}", |
| 182 | minPressure); |
| 183 | } |
| 184 | if (maxPressure < 0_psi || maxPressure > 120_psi) { |
| 185 | throw FRC_MakeError(err::ParameterOutOfRange, |
| 186 | "maxPressure must be between 0 and 120 PSI, got {}", |
| 187 | maxPressure); |
| 188 | } |
| 189 | int32_t status = 0; |
| 190 | units::volt_t minAnalogVoltage = PSIToVolts(minPressure, 5_V); |
| 191 | units::volt_t maxAnalogVoltage = PSIToVolts(maxPressure, 5_V); |
| 192 | HAL_SetREVPHClosedLoopControlHybrid(m_handle, minAnalogVoltage.value(), |
| 193 | maxAnalogVoltage.value(), &status); |
| 194 | FRC_ReportError(status, "Module {}", m_module); |
| 195 | } |
| 196 | |
| 197 | CompressorConfigType PneumaticHub::GetCompressorConfigType() const { |
| 198 | int32_t status = 0; |
| 199 | auto result = HAL_GetREVPHCompressorConfig(m_handle, &status); |
| 200 | FRC_ReportError(status, "Module {}", m_module); |
| 201 | return static_cast<CompressorConfigType>(result); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | bool PneumaticHub::GetPressureSwitch() const { |
| 205 | int32_t status = 0; |
| 206 | auto result = HAL_GetREVPHPressureSwitch(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 207 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 208 | return result; |
| 209 | } |
| 210 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 211 | units::ampere_t PneumaticHub::GetCompressorCurrent() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 212 | int32_t status = 0; |
| 213 | auto result = HAL_GetREVPHCompressorCurrent(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 214 | FRC_ReportError(status, "Module {}", m_module); |
| 215 | return units::ampere_t{result}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void PneumaticHub::SetSolenoids(int mask, int values) { |
| 219 | int32_t status = 0; |
| 220 | HAL_SetREVPHSolenoids(m_handle, mask, values, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 221 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | int PneumaticHub::GetSolenoids() const { |
| 225 | int32_t status = 0; |
| 226 | auto result = HAL_GetREVPHSolenoids(m_handle, &status); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 227 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 228 | return result; |
| 229 | } |
| 230 | |
| 231 | int PneumaticHub::GetModuleNumber() const { |
| 232 | return m_module; |
| 233 | } |
| 234 | |
| 235 | int PneumaticHub::GetSolenoidDisabledList() const { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 236 | int32_t status = 0; |
| 237 | HAL_REVPHStickyFaults faults; |
| 238 | std::memset(&faults, 0, sizeof(faults)); |
| 239 | HAL_GetREVPHStickyFaults(m_handle, &faults, &status); |
| 240 | FRC_ReportError(status, "Module {}", m_module); |
| 241 | uint32_t intFaults = 0; |
| 242 | static_assert(sizeof(faults) == sizeof(intFaults)); |
| 243 | std::memcpy(&intFaults, &faults, sizeof(faults)); |
| 244 | return intFaults & 0xFFFF; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void PneumaticHub::FireOneShot(int index) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 248 | int32_t status = 0; |
| 249 | HAL_FireREVPHOneShot(m_handle, index, |
| 250 | m_dataStore->m_oneShotDurMs[index].value(), &status); |
| 251 | FRC_ReportError(status, "Module {}", m_module); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void PneumaticHub::SetOneShotDuration(int index, units::second_t duration) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 255 | m_dataStore->m_oneShotDurMs[index] = duration; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | bool PneumaticHub::CheckSolenoidChannel(int channel) const { |
| 259 | return HAL_CheckREVPHSolenoidChannel(channel); |
| 260 | } |
| 261 | |
| 262 | int PneumaticHub::CheckAndReserveSolenoids(int mask) { |
| 263 | std::scoped_lock lock{m_dataStore->m_reservedLock}; |
| 264 | uint32_t uMask = static_cast<uint32_t>(mask); |
| 265 | if ((m_dataStore->m_reservedMask & uMask) != 0) { |
| 266 | return m_dataStore->m_reservedMask & uMask; |
| 267 | } |
| 268 | m_dataStore->m_reservedMask |= uMask; |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | void PneumaticHub::UnreserveSolenoids(int mask) { |
| 273 | std::scoped_lock lock{m_dataStore->m_reservedLock}; |
| 274 | m_dataStore->m_reservedMask &= ~(static_cast<uint32_t>(mask)); |
| 275 | } |
| 276 | |
| 277 | bool PneumaticHub::ReserveCompressor() { |
| 278 | std::scoped_lock lock{m_dataStore->m_reservedLock}; |
| 279 | if (m_dataStore->m_compressorReserved) { |
| 280 | return false; |
| 281 | } |
| 282 | m_dataStore->m_compressorReserved = true; |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | void PneumaticHub::UnreserveCompressor() { |
| 287 | std::scoped_lock lock{m_dataStore->m_reservedLock}; |
| 288 | m_dataStore->m_compressorReserved = false; |
| 289 | } |
| 290 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 291 | PneumaticHub::Version PneumaticHub::GetVersion() const { |
| 292 | int32_t status = 0; |
| 293 | HAL_REVPHVersion halVersions; |
| 294 | std::memset(&halVersions, 0, sizeof(halVersions)); |
| 295 | HAL_GetREVPHVersion(m_handle, &halVersions, &status); |
| 296 | FRC_ReportError(status, "Module {}", m_module); |
| 297 | PneumaticHub::Version versions; |
| 298 | static_assert(sizeof(halVersions) == sizeof(versions)); |
| 299 | static_assert(std::is_standard_layout_v<decltype(versions)>); |
| 300 | static_assert(std::is_trivial_v<decltype(versions)>); |
| 301 | std::memcpy(&versions, &halVersions, sizeof(versions)); |
| 302 | return versions; |
| 303 | } |
| 304 | |
| 305 | PneumaticHub::Faults PneumaticHub::GetFaults() const { |
| 306 | int32_t status = 0; |
| 307 | HAL_REVPHFaults halFaults; |
| 308 | std::memset(&halFaults, 0, sizeof(halFaults)); |
| 309 | HAL_GetREVPHFaults(m_handle, &halFaults, &status); |
| 310 | FRC_ReportError(status, "Module {}", m_module); |
| 311 | PneumaticHub::Faults faults; |
| 312 | static_assert(sizeof(halFaults) == sizeof(faults)); |
| 313 | static_assert(std::is_standard_layout_v<decltype(faults)>); |
| 314 | static_assert(std::is_trivial_v<decltype(faults)>); |
| 315 | std::memcpy(&faults, &halFaults, sizeof(faults)); |
| 316 | return faults; |
| 317 | } |
| 318 | |
| 319 | PneumaticHub::StickyFaults PneumaticHub::GetStickyFaults() const { |
| 320 | int32_t status = 0; |
| 321 | HAL_REVPHStickyFaults halStickyFaults; |
| 322 | std::memset(&halStickyFaults, 0, sizeof(halStickyFaults)); |
| 323 | HAL_GetREVPHStickyFaults(m_handle, &halStickyFaults, &status); |
| 324 | FRC_ReportError(status, "Module {}", m_module); |
| 325 | PneumaticHub::StickyFaults stickyFaults; |
| 326 | static_assert(sizeof(halStickyFaults) == sizeof(stickyFaults)); |
| 327 | static_assert(std::is_standard_layout_v<decltype(stickyFaults)>); |
| 328 | static_assert(std::is_trivial_v<decltype(stickyFaults)>); |
| 329 | std::memcpy(&stickyFaults, &halStickyFaults, sizeof(stickyFaults)); |
| 330 | return stickyFaults; |
| 331 | } |
| 332 | |
| 333 | void PneumaticHub::ClearStickyFaults() { |
| 334 | int32_t status = 0; |
| 335 | HAL_ClearREVPHStickyFaults(m_handle, &status); |
| 336 | FRC_ReportError(status, "Module {}", m_module); |
| 337 | } |
| 338 | |
| 339 | units::volt_t PneumaticHub::GetInputVoltage() const { |
| 340 | int32_t status = 0; |
| 341 | auto voltage = HAL_GetREVPHVoltage(m_handle, &status); |
| 342 | FRC_ReportError(status, "Module {}", m_module); |
| 343 | return units::volt_t{voltage}; |
| 344 | } |
| 345 | |
| 346 | units::volt_t PneumaticHub::Get5VRegulatedVoltage() const { |
| 347 | int32_t status = 0; |
| 348 | auto voltage = HAL_GetREVPH5VVoltage(m_handle, &status); |
| 349 | FRC_ReportError(status, "Module {}", m_module); |
| 350 | return units::volt_t{voltage}; |
| 351 | } |
| 352 | |
| 353 | units::ampere_t PneumaticHub::GetSolenoidsTotalCurrent() const { |
| 354 | int32_t status = 0; |
| 355 | auto current = HAL_GetREVPHSolenoidCurrent(m_handle, &status); |
| 356 | FRC_ReportError(status, "Module {}", m_module); |
| 357 | return units::ampere_t{current}; |
| 358 | } |
| 359 | |
| 360 | units::volt_t PneumaticHub::GetSolenoidsVoltage() const { |
| 361 | int32_t status = 0; |
| 362 | auto voltage = HAL_GetREVPHSolenoidVoltage(m_handle, &status); |
| 363 | FRC_ReportError(status, "Module {}", m_module); |
| 364 | return units::volt_t{voltage}; |
| 365 | } |
| 366 | |
| 367 | units::volt_t PneumaticHub::GetAnalogVoltage(int channel) const { |
| 368 | int32_t status = 0; |
| 369 | auto voltage = HAL_GetREVPHAnalogVoltage(m_handle, channel, &status); |
| 370 | FRC_ReportError(status, "Module {}", m_module); |
| 371 | return units::volt_t{voltage}; |
| 372 | } |
| 373 | |
| 374 | units::pounds_per_square_inch_t PneumaticHub::GetPressure(int channel) const { |
| 375 | int32_t status = 0; |
| 376 | auto sensorVoltage = HAL_GetREVPHAnalogVoltage(m_handle, channel, &status); |
| 377 | FRC_ReportError(status, "Module {}", m_module); |
| 378 | auto supplyVoltage = HAL_GetREVPH5VVoltage(m_handle, &status); |
| 379 | FRC_ReportError(status, "Module {}", m_module); |
| 380 | return VoltsToPSI(units::volt_t{sensorVoltage}, units::volt_t{supplyVoltage}); |
| 381 | } |
| 382 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 383 | Solenoid PneumaticHub::MakeSolenoid(int channel) { |
| 384 | return Solenoid{m_module, PneumaticsModuleType::REVPH, channel}; |
| 385 | } |
| 386 | |
| 387 | DoubleSolenoid PneumaticHub::MakeDoubleSolenoid(int forwardChannel, |
| 388 | int reverseChannel) { |
| 389 | return DoubleSolenoid{m_module, PneumaticsModuleType::REVPH, forwardChannel, |
| 390 | reverseChannel}; |
| 391 | } |
| 392 | |
| 393 | Compressor PneumaticHub::MakeCompressor() { |
| 394 | return Compressor{m_module, PneumaticsModuleType::REVPH}; |
| 395 | } |
| 396 | |
| 397 | std::shared_ptr<PneumaticsBase> PneumaticHub::GetForModule(int module) { |
| 398 | std::string stackTrace = wpi::GetStackTrace(1); |
| 399 | std::scoped_lock lock(m_handleLock); |
| 400 | auto& res = GetDataStore(module); |
| 401 | std::shared_ptr<DataStore> dataStore = res.lock(); |
| 402 | if (!dataStore) { |
| 403 | dataStore = std::make_shared<DataStore>(module, stackTrace.c_str()); |
| 404 | res = dataStore; |
| 405 | } |
| 406 | |
| 407 | return std::shared_ptr<PneumaticsBase>{dataStore, &dataStore->m_moduleObject}; |
| 408 | } |