Files
allwpilib/wpilibc/src/main/native/cpp/hardware/pneumatic/PneumaticHub.cpp

452 lines
16 KiB
C++
Raw Normal View History

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
2025-11-07 19:56:21 -05:00
#include "wpi/hardware/pneumatic/PneumaticHub.hpp"
2022-04-30 00:07:37 -07:00
#include <array>
2024-09-20 17:43:39 -07:00
#include <cstdio>
#include <memory>
#include <string>
2022-04-30 00:07:37 -07:00
#include <fmt/format.h>
2025-11-07 19:57:55 -05:00
2025-11-07 19:56:21 -05:00
#include "wpi/hal/Ports.h"
#include "wpi/hal/REVPH.h"
#include "wpi/hal/UsageReporting.hpp"
2025-11-07 19:56:21 -05:00
#include "wpi/hardware/pneumatic/Compressor.hpp"
#include "wpi/hardware/pneumatic/DoubleSolenoid.hpp"
2025-11-07 19:57:55 -05:00
#include "wpi/hardware/pneumatic/Solenoid.hpp"
#include "wpi/system/Errors.hpp"
#include "wpi/util/NullDeleter.hpp"
2025-11-07 19:56:21 -05:00
#include "wpi/util/SensorUtil.hpp"
2025-11-07 19:57:55 -05:00
#include "wpi/util/StackTrace.hpp"
2025-11-07 20:00:05 -05:00
using namespace wpi;
/** Converts volts to PSI per the REV Analog Pressure Sensor datasheet. */
2025-11-07 20:01:58 -05:00
wpi::units::pounds_per_square_inch_t VoltsToPSI(
wpi::units::volt_t sensorVoltage, wpi::units::volt_t supplyVoltage) {
2025-11-07 20:00:05 -05:00
return wpi::units::pounds_per_square_inch_t{
250 * (sensorVoltage.value() / supplyVoltage.value()) - 25};
}
/** Converts PSI to volts per the REV Analog Pressure Sensor datasheet. */
2025-11-07 20:00:05 -05:00
wpi::units::volt_t PSIToVolts(wpi::units::pounds_per_square_inch_t pressure,
2025-11-07 20:01:58 -05:00
wpi::units::volt_t supplyVoltage) {
2025-11-07 20:00:05 -05:00
return wpi::units::volt_t{supplyVoltage.value() *
2025-11-07 20:01:58 -05:00
(0.004 * pressure.value() + 0.1)};
}
2025-11-07 20:00:05 -05:00
wpi::util::mutex PneumaticHub::m_handleLock;
2025-11-07 20:01:58 -05:00
std::unique_ptr<
wpi::util::DenseMap<int, std::weak_ptr<PneumaticHub::DataStore>>[]>
2025-02-25 19:07:01 -08:00
PneumaticHub::m_handleMaps = nullptr;
// Always called under lock, so we can avoid the double lock from the magic
// static
2025-02-25 19:07:01 -08:00
std::weak_ptr<PneumaticHub::DataStore>& PneumaticHub::GetDataStore(int busId,
int module) {
int32_t numBuses = HAL_GetNumCanBuses();
2025-11-07 20:00:43 -05:00
WPILIB_AssertMessage(busId >= 0 && busId < numBuses,
2025-11-07 20:01:58 -05:00
"Bus {} out of range. Must be [0-{}).", busId, numBuses);
2025-02-25 19:07:01 -08:00
if (!m_handleMaps) {
m_handleMaps = std::make_unique<
2025-11-07 20:01:58 -05:00
wpi::util::DenseMap<int, std::weak_ptr<PneumaticHub::DataStore>>[]>(
numBuses);
}
2025-02-25 19:07:01 -08:00
return m_handleMaps[busId][module];
}
class PneumaticHub::DataStore {
public:
2025-02-25 19:07:01 -08:00
explicit DataStore(int busId, int module, const char* stackTrace) {
int32_t status = 0;
2025-02-25 19:07:01 -08:00
HAL_REVPHHandle handle =
HAL_InitializeREVPH(busId, module, stackTrace, &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "Module {}", module);
2025-02-25 19:07:01 -08:00
m_moduleObject = PneumaticHub{busId, handle, module};
m_moduleObject.m_dataStore =
2025-11-07 20:00:05 -05:00
std::shared_ptr<DataStore>{this, wpi::util::NullDeleter<DataStore>()};
auto version = m_moduleObject.GetVersion();
// Check PH firmware version
if (version.FirmwareMajor > 0 && version.FirmwareMajor < 22) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(
err::AssertionFailure,
"The Pneumatic Hub has firmware version {}.{}.{}, and must be "
"updated to version 2022.0.0 or later using the REV Hardware Client",
version.FirmwareMajor, version.FirmwareMinor, version.FirmwareFix);
}
}
~DataStore() noexcept { HAL_FreeREVPH(m_moduleObject.m_handle); }
DataStore(DataStore&&) = delete;
DataStore& operator=(DataStore&&) = delete;
private:
friend class PneumaticHub;
uint32_t m_reservedMask{0};
bool m_compressorReserved{false};
2025-11-07 20:00:05 -05:00
wpi::util::mutex m_reservedLock;
PneumaticHub m_moduleObject{0, HAL_INVALID_HANDLE, 0};
2025-11-07 20:00:05 -05:00
std::array<wpi::units::millisecond_t, 16> m_oneShotDurMs{0_ms};
};
2025-02-25 19:07:01 -08:00
PneumaticHub::PneumaticHub(int busId)
: PneumaticHub{busId, SensorUtil::GetDefaultREVPHModule()} {}
2025-02-25 19:07:01 -08:00
PneumaticHub::PneumaticHub(int busId, int module) {
2025-11-07 20:00:05 -05:00
std::string stackTrace = wpi::util::GetStackTrace(1);
std::scoped_lock lock(m_handleLock);
2025-02-25 19:07:01 -08:00
auto& res = GetDataStore(busId, module);
m_dataStore = res.lock();
if (!m_dataStore) {
2025-02-25 19:07:01 -08:00
m_dataStore =
std::make_shared<DataStore>(busId, module, stackTrace.c_str());
res = m_dataStore;
}
m_handle = m_dataStore->m_moduleObject.m_handle;
m_module = module;
}
2025-02-25 19:07:01 -08:00
PneumaticHub::PneumaticHub(int /* busId */, HAL_REVPHHandle handle, int module)
: m_handle{handle}, m_module{module} {}
bool PneumaticHub::GetCompressor() const {
int32_t status = 0;
auto result = HAL_GetREVPHCompressor(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
return result;
}
void PneumaticHub::DisableCompressor() {
int32_t status = 0;
HAL_SetREVPHClosedLoopControlDisabled(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
}
void PneumaticHub::EnableCompressorDigital() {
int32_t status = 0;
HAL_SetREVPHClosedLoopControlDigital(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
}
void PneumaticHub::EnableCompressorAnalog(
2025-11-07 20:00:05 -05:00
wpi::units::pounds_per_square_inch_t minPressure,
wpi::units::pounds_per_square_inch_t maxPressure) {
if (minPressure >= maxPressure) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::InvalidParameter,
2025-11-07 20:01:58 -05:00
"maxPressure must be greater than minPressure");
}
if (minPressure < 0_psi || minPressure > 120_psi) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::ParameterOutOfRange,
2025-11-07 20:01:58 -05:00
"minPressure must be between 0 and 120 PSI, got {}",
minPressure);
}
if (maxPressure < 0_psi || maxPressure > 120_psi) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::ParameterOutOfRange,
2025-11-07 20:01:58 -05:00
"maxPressure must be between 0 and 120 PSI, got {}",
maxPressure);
}
// Send the voltage as it would be if the 5V rail was at exactly 5V.
// The firmware will compensate for the real 5V rail voltage, which
// can fluctuate somewhat over time.
2025-11-07 20:00:05 -05:00
wpi::units::volt_t minAnalogVoltage = PSIToVolts(minPressure, 5_V);
wpi::units::volt_t maxAnalogVoltage = PSIToVolts(maxPressure, 5_V);
int32_t status = 0;
HAL_SetREVPHClosedLoopControlAnalog(m_handle, minAnalogVoltage.value(),
maxAnalogVoltage.value(), &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
}
void PneumaticHub::EnableCompressorHybrid(
2025-11-07 20:00:05 -05:00
wpi::units::pounds_per_square_inch_t minPressure,
wpi::units::pounds_per_square_inch_t maxPressure) {
if (minPressure >= maxPressure) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::InvalidParameter,
2025-11-07 20:01:58 -05:00
"maxPressure must be greater than minPressure");
}
if (minPressure < 0_psi || minPressure > 120_psi) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::ParameterOutOfRange,
2025-11-07 20:01:58 -05:00
"minPressure must be between 0 and 120 PSI, got {}",
minPressure);
}
if (maxPressure < 0_psi || maxPressure > 120_psi) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::ParameterOutOfRange,
2025-11-07 20:01:58 -05:00
"maxPressure must be between 0 and 120 PSI, got {}",
maxPressure);
}
// Send the voltage as it would be if the 5V rail was at exactly 5V.
// The firmware will compensate for the real 5V rail voltage, which
// can fluctuate somewhat over time.
2025-11-07 20:00:05 -05:00
wpi::units::volt_t minAnalogVoltage = PSIToVolts(minPressure, 5_V);
wpi::units::volt_t maxAnalogVoltage = PSIToVolts(maxPressure, 5_V);
int32_t status = 0;
HAL_SetREVPHClosedLoopControlHybrid(m_handle, minAnalogVoltage.value(),
maxAnalogVoltage.value(), &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
}
CompressorConfigType PneumaticHub::GetCompressorConfigType() const {
int32_t status = 0;
auto result = HAL_GetREVPHCompressorConfig(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
return static_cast<CompressorConfigType>(result);
}
bool PneumaticHub::GetPressureSwitch() const {
int32_t status = 0;
auto result = HAL_GetREVPHPressureSwitch(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
return result;
}
2025-11-07 20:00:05 -05:00
wpi::units::ampere_t PneumaticHub::GetCompressorCurrent() const {
int32_t status = 0;
auto result = HAL_GetREVPHCompressorCurrent(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2025-11-07 20:00:05 -05:00
return wpi::units::ampere_t{result};
}
void PneumaticHub::SetSolenoids(int mask, int values) {
int32_t status = 0;
HAL_SetREVPHSolenoids(m_handle, mask, values, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
}
int PneumaticHub::GetSolenoids() const {
int32_t status = 0;
auto result = HAL_GetREVPHSolenoids(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
return result;
}
int PneumaticHub::GetModuleNumber() const {
return m_module;
}
int PneumaticHub::GetSolenoidDisabledList() const {
int32_t status = 0;
auto result = HAL_GetREVPHSolenoidDisabledList(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
return result;
}
void PneumaticHub::FireOneShot(int index) {
int32_t status = 0;
HAL_FireREVPHOneShot(m_handle, index,
m_dataStore->m_oneShotDurMs[index].value(), &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
}
2025-11-07 20:01:58 -05:00
void PneumaticHub::SetOneShotDuration(int index,
wpi::units::second_t duration) {
m_dataStore->m_oneShotDurMs[index] = duration;
}
bool PneumaticHub::CheckSolenoidChannel(int channel) const {
return HAL_CheckREVPHSolenoidChannel(channel);
}
int PneumaticHub::CheckAndReserveSolenoids(int mask) {
std::scoped_lock lock{m_dataStore->m_reservedLock};
uint32_t uMask = static_cast<uint32_t>(mask);
if ((m_dataStore->m_reservedMask & uMask) != 0) {
return m_dataStore->m_reservedMask & uMask;
}
m_dataStore->m_reservedMask |= uMask;
return 0;
}
void PneumaticHub::UnreserveSolenoids(int mask) {
std::scoped_lock lock{m_dataStore->m_reservedLock};
m_dataStore->m_reservedMask &= ~(static_cast<uint32_t>(mask));
}
bool PneumaticHub::ReserveCompressor() {
std::scoped_lock lock{m_dataStore->m_reservedLock};
if (m_dataStore->m_compressorReserved) {
return false;
}
m_dataStore->m_compressorReserved = true;
return true;
}
void PneumaticHub::UnreserveCompressor() {
std::scoped_lock lock{m_dataStore->m_reservedLock};
m_dataStore->m_compressorReserved = false;
}
PneumaticHub::Version PneumaticHub::GetVersion() const {
int32_t status = 0;
HAL_REVPHVersion halVersions;
std::memset(&halVersions, 0, sizeof(halVersions));
HAL_GetREVPHVersion(m_handle, &halVersions, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
PneumaticHub::Version versions;
static_assert(sizeof(halVersions) == sizeof(versions));
static_assert(std::is_standard_layout_v<decltype(versions)>);
static_assert(std::is_trivial_v<decltype(versions)>);
std::memcpy(&versions, &halVersions, sizeof(versions));
return versions;
}
PneumaticHub::Faults PneumaticHub::GetFaults() const {
int32_t status = 0;
HAL_REVPHFaults halFaults;
std::memset(&halFaults, 0, sizeof(halFaults));
HAL_GetREVPHFaults(m_handle, &halFaults, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
PneumaticHub::Faults faults;
static_assert(sizeof(halFaults) == sizeof(faults));
static_assert(std::is_standard_layout_v<decltype(faults)>);
static_assert(std::is_trivial_v<decltype(faults)>);
std::memcpy(&faults, &halFaults, sizeof(faults));
return faults;
}
PneumaticHub::StickyFaults PneumaticHub::GetStickyFaults() const {
int32_t status = 0;
HAL_REVPHStickyFaults halStickyFaults;
std::memset(&halStickyFaults, 0, sizeof(halStickyFaults));
HAL_GetREVPHStickyFaults(m_handle, &halStickyFaults, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
PneumaticHub::StickyFaults stickyFaults;
static_assert(sizeof(halStickyFaults) == sizeof(stickyFaults));
static_assert(std::is_standard_layout_v<decltype(stickyFaults)>);
static_assert(std::is_trivial_v<decltype(stickyFaults)>);
std::memcpy(&stickyFaults, &halStickyFaults, sizeof(stickyFaults));
return stickyFaults;
}
bool PneumaticHub::Faults::GetChannelFault(int channel) const {
switch (channel) {
case 0:
return Channel0Fault != 0;
case 1:
return Channel1Fault != 0;
case 2:
return Channel2Fault != 0;
case 3:
return Channel3Fault != 0;
case 4:
return Channel4Fault != 0;
case 5:
return Channel5Fault != 0;
case 6:
return Channel6Fault != 0;
case 7:
return Channel7Fault != 0;
case 8:
return Channel8Fault != 0;
case 9:
return Channel9Fault != 0;
case 10:
return Channel10Fault != 0;
case 11:
return Channel11Fault != 0;
case 12:
return Channel12Fault != 0;
case 13:
return Channel13Fault != 0;
case 14:
return Channel14Fault != 0;
case 15:
return Channel15Fault != 0;
default:
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::ChannelIndexOutOfRange,
2025-11-07 20:01:58 -05:00
"Pneumatics fault channel out of bounds!");
}
}
void PneumaticHub::ClearStickyFaults() {
int32_t status = 0;
HAL_ClearREVPHStickyFaults(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
}
2025-11-07 20:00:05 -05:00
wpi::units::volt_t PneumaticHub::GetInputVoltage() const {
int32_t status = 0;
auto voltage = HAL_GetREVPHVoltage(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2025-11-07 20:00:05 -05:00
return wpi::units::volt_t{voltage};
}
2025-11-07 20:00:05 -05:00
wpi::units::volt_t PneumaticHub::Get5VRegulatedVoltage() const {
int32_t status = 0;
auto voltage = HAL_GetREVPH5VVoltage(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2025-11-07 20:00:05 -05:00
return wpi::units::volt_t{voltage};
}
2025-11-07 20:00:05 -05:00
wpi::units::ampere_t PneumaticHub::GetSolenoidsTotalCurrent() const {
int32_t status = 0;
auto current = HAL_GetREVPHSolenoidCurrent(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2025-11-07 20:00:05 -05:00
return wpi::units::ampere_t{current};
}
2025-11-07 20:00:05 -05:00
wpi::units::volt_t PneumaticHub::GetSolenoidsVoltage() const {
int32_t status = 0;
auto voltage = HAL_GetREVPHSolenoidVoltage(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2025-11-07 20:00:05 -05:00
return wpi::units::volt_t{voltage};
}
2025-11-07 20:00:05 -05:00
wpi::units::volt_t PneumaticHub::GetAnalogVoltage(int channel) const {
int32_t status = 0;
auto voltage = HAL_GetREVPHAnalogVoltage(m_handle, channel, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2025-11-07 20:00:05 -05:00
return wpi::units::volt_t{voltage};
}
2025-11-07 20:01:58 -05:00
wpi::units::pounds_per_square_inch_t PneumaticHub::GetPressure(
int channel) const {
int32_t status = 0;
auto sensorVoltage = HAL_GetREVPHAnalogVoltage(m_handle, channel, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
auto supplyVoltage = HAL_GetREVPH5VVoltage(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2025-11-07 20:01:58 -05:00
return VoltsToPSI(wpi::units::volt_t{sensorVoltage},
wpi::units::volt_t{supplyVoltage});
}
Solenoid PneumaticHub::MakeSolenoid(int channel) {
return Solenoid{m_module, PneumaticsModuleType::REVPH, channel};
}
DoubleSolenoid PneumaticHub::MakeDoubleSolenoid(int forwardChannel,
int reverseChannel) {
return DoubleSolenoid{m_module, PneumaticsModuleType::REVPH, forwardChannel,
reverseChannel};
}
Compressor PneumaticHub::MakeCompressor() {
return Compressor{m_module, PneumaticsModuleType::REVPH};
}
void PneumaticHub::ReportUsage(std::string_view device, std::string_view data) {
HAL_ReportUsage(fmt::format("PH[{}]/{}", m_module, device), data);
}
2025-02-25 19:07:01 -08:00
std::shared_ptr<PneumaticsBase> PneumaticHub::GetForModule(int busId,
int module) {
2025-11-07 20:00:05 -05:00
std::string stackTrace = wpi::util::GetStackTrace(1);
std::scoped_lock lock(m_handleLock);
2025-02-25 19:07:01 -08:00
auto& res = GetDataStore(busId, module);
std::shared_ptr<DataStore> dataStore = res.lock();
if (!dataStore) {
2025-02-25 19:07:01 -08:00
dataStore = std::make_shared<DataStore>(busId, module, stackTrace.c_str());
res = dataStore;
}
return std::shared_ptr<PneumaticsBase>{dataStore, &dataStore->m_moduleObject};
}