mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
@@ -2,7 +2,7 @@
|
||||
// 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.
|
||||
|
||||
#include "frc/PneumaticsHub.h"
|
||||
#include "frc/PneumaticHub.h"
|
||||
|
||||
#include <hal/REVPH.h>
|
||||
#include <wpi/NullDeleter.h>
|
||||
@@ -16,28 +16,27 @@
|
||||
|
||||
using namespace frc;
|
||||
|
||||
wpi::mutex PneumaticsHub::m_handleLock;
|
||||
std::unique_ptr<wpi::DenseMap<int, std::weak_ptr<PneumaticsHub::DataStore>>>
|
||||
PneumaticsHub::m_handleMap = nullptr;
|
||||
wpi::mutex PneumaticHub::m_handleLock;
|
||||
std::unique_ptr<wpi::DenseMap<int, std::weak_ptr<PneumaticHub::DataStore>>>
|
||||
PneumaticHub::m_handleMap = nullptr;
|
||||
|
||||
// Always called under lock, so we can avoid the double lock from the magic
|
||||
// static
|
||||
std::weak_ptr<PneumaticsHub::DataStore>& PneumaticsHub::GetDataStore(
|
||||
int module) {
|
||||
std::weak_ptr<PneumaticHub::DataStore>& PneumaticHub::GetDataStore(int module) {
|
||||
if (!m_handleMap) {
|
||||
m_handleMap = std::make_unique<
|
||||
wpi::DenseMap<int, std::weak_ptr<PneumaticsHub::DataStore>>>();
|
||||
wpi::DenseMap<int, std::weak_ptr<PneumaticHub::DataStore>>>();
|
||||
}
|
||||
return (*m_handleMap)[module];
|
||||
}
|
||||
|
||||
class PneumaticsHub::DataStore {
|
||||
class PneumaticHub::DataStore {
|
||||
public:
|
||||
explicit DataStore(int module, const char* stackTrace) {
|
||||
int32_t status = 0;
|
||||
HAL_REVPHHandle handle = HAL_InitializeREVPH(module, stackTrace, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", module);
|
||||
m_moduleObject = PneumaticsHub{handle, module};
|
||||
m_moduleObject = PneumaticHub{handle, module};
|
||||
m_moduleObject.m_dataStore =
|
||||
std::shared_ptr<DataStore>{this, wpi::NullDeleter<DataStore>()};
|
||||
}
|
||||
@@ -48,17 +47,17 @@ class PneumaticsHub::DataStore {
|
||||
DataStore& operator=(DataStore&&) = delete;
|
||||
|
||||
private:
|
||||
friend class PneumaticsHub;
|
||||
friend class PneumaticHub;
|
||||
uint32_t m_reservedMask{0};
|
||||
bool m_compressorReserved{false};
|
||||
wpi::mutex m_reservedLock;
|
||||
PneumaticsHub m_moduleObject{HAL_kInvalidHandle, 0};
|
||||
PneumaticHub m_moduleObject{HAL_kInvalidHandle, 0};
|
||||
};
|
||||
|
||||
PneumaticsHub::PneumaticsHub()
|
||||
: PneumaticsHub{SensorUtil::GetDefaultREVPHModule()} {}
|
||||
PneumaticHub::PneumaticHub()
|
||||
: PneumaticHub{SensorUtil::GetDefaultREVPHModule()} {}
|
||||
|
||||
PneumaticsHub::PneumaticsHub(int module) {
|
||||
PneumaticHub::PneumaticHub(int module) {
|
||||
std::string stackTrace = wpi::GetStackTrace(1);
|
||||
std::scoped_lock lock(m_handleLock);
|
||||
auto& res = GetDataStore(module);
|
||||
@@ -71,61 +70,61 @@ PneumaticsHub::PneumaticsHub(int module) {
|
||||
m_module = module;
|
||||
}
|
||||
|
||||
PneumaticsHub::PneumaticsHub(HAL_REVPHHandle handle, int module)
|
||||
PneumaticHub::PneumaticHub(HAL_REVPHHandle handle, int module)
|
||||
: m_handle{handle}, m_module{module} {}
|
||||
|
||||
bool PneumaticsHub::GetCompressor() const {
|
||||
bool PneumaticHub::GetCompressor() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetREVPHCompressor(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
|
||||
void PneumaticsHub::SetClosedLoopControl(bool enabled) {
|
||||
void PneumaticHub::SetClosedLoopControl(bool enabled) {
|
||||
int32_t status = 0;
|
||||
HAL_SetREVPHClosedLoopControl(m_handle, enabled, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
}
|
||||
|
||||
bool PneumaticsHub::GetClosedLoopControl() const {
|
||||
bool PneumaticHub::GetClosedLoopControl() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetREVPHClosedLoopControl(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool PneumaticsHub::GetPressureSwitch() const {
|
||||
bool PneumaticHub::GetPressureSwitch() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetREVPHPressureSwitch(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
|
||||
double PneumaticsHub::GetCompressorCurrent() const {
|
||||
double PneumaticHub::GetCompressorCurrent() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetREVPHCompressorCurrent(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
|
||||
void PneumaticsHub::SetSolenoids(int mask, int values) {
|
||||
void PneumaticHub::SetSolenoids(int mask, int values) {
|
||||
int32_t status = 0;
|
||||
HAL_SetREVPHSolenoids(m_handle, mask, values, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
}
|
||||
|
||||
int PneumaticsHub::GetSolenoids() const {
|
||||
int PneumaticHub::GetSolenoids() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetREVPHSolenoids(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
|
||||
int PneumaticsHub::GetModuleNumber() const {
|
||||
int PneumaticHub::GetModuleNumber() const {
|
||||
return m_module;
|
||||
}
|
||||
|
||||
int PneumaticsHub::GetSolenoidDisabledList() const {
|
||||
int PneumaticHub::GetSolenoidDisabledList() const {
|
||||
return 0;
|
||||
// TODO Fix me
|
||||
// int32_t status = 0;
|
||||
@@ -134,14 +133,14 @@ int PneumaticsHub::GetSolenoidDisabledList() const {
|
||||
// return result;
|
||||
}
|
||||
|
||||
void PneumaticsHub::FireOneShot(int index) {
|
||||
void PneumaticHub::FireOneShot(int index) {
|
||||
// TODO Fix me
|
||||
// int32_t status = 0;
|
||||
// HAL_FireREVPHOneShot(m_handle, index, &status);
|
||||
// FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
}
|
||||
|
||||
void PneumaticsHub::SetOneShotDuration(int index, units::second_t duration) {
|
||||
void PneumaticHub::SetOneShotDuration(int index, units::second_t duration) {
|
||||
// TODO Fix me
|
||||
// int32_t status = 0;
|
||||
// units::millisecond_t millis = duration;
|
||||
@@ -149,11 +148,11 @@ void PneumaticsHub::SetOneShotDuration(int index, units::second_t duration) {
|
||||
// &status); FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
}
|
||||
|
||||
bool PneumaticsHub::CheckSolenoidChannel(int channel) const {
|
||||
bool PneumaticHub::CheckSolenoidChannel(int channel) const {
|
||||
return HAL_CheckREVPHSolenoidChannel(channel);
|
||||
}
|
||||
|
||||
int PneumaticsHub::CheckAndReserveSolenoids(int mask) {
|
||||
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) {
|
||||
@@ -163,12 +162,12 @@ int PneumaticsHub::CheckAndReserveSolenoids(int mask) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void PneumaticsHub::UnreserveSolenoids(int mask) {
|
||||
void PneumaticHub::UnreserveSolenoids(int mask) {
|
||||
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
||||
m_dataStore->m_reservedMask &= ~(static_cast<uint32_t>(mask));
|
||||
}
|
||||
|
||||
bool PneumaticsHub::ReserveCompressor() {
|
||||
bool PneumaticHub::ReserveCompressor() {
|
||||
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
||||
if (m_dataStore->m_compressorReserved) {
|
||||
return false;
|
||||
@@ -177,26 +176,26 @@ bool PneumaticsHub::ReserveCompressor() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void PneumaticsHub::UnreserveCompressor() {
|
||||
void PneumaticHub::UnreserveCompressor() {
|
||||
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
||||
m_dataStore->m_compressorReserved = false;
|
||||
}
|
||||
|
||||
Solenoid PneumaticsHub::MakeSolenoid(int channel) {
|
||||
Solenoid PneumaticHub::MakeSolenoid(int channel) {
|
||||
return Solenoid{m_module, PneumaticsModuleType::REVPH, channel};
|
||||
}
|
||||
|
||||
DoubleSolenoid PneumaticsHub::MakeDoubleSolenoid(int forwardChannel,
|
||||
int reverseChannel) {
|
||||
DoubleSolenoid PneumaticHub::MakeDoubleSolenoid(int forwardChannel,
|
||||
int reverseChannel) {
|
||||
return DoubleSolenoid{m_module, PneumaticsModuleType::REVPH, forwardChannel,
|
||||
reverseChannel};
|
||||
}
|
||||
|
||||
Compressor PneumaticsHub::MakeCompressor() {
|
||||
Compressor PneumaticHub::MakeCompressor() {
|
||||
return Compressor{m_module, PneumaticsModuleType::REVPH};
|
||||
}
|
||||
|
||||
std::shared_ptr<PneumaticsBase> PneumaticsHub::GetForModule(int module) {
|
||||
std::shared_ptr<PneumaticsBase> PneumaticHub::GetForModule(int module) {
|
||||
std::string stackTrace = wpi::GetStackTrace(1);
|
||||
std::scoped_lock lock(m_handleLock);
|
||||
auto& res = GetDataStore(module);
|
||||
@@ -5,8 +5,8 @@
|
||||
#include "frc/PneumaticsBase.h"
|
||||
|
||||
#include "frc/Errors.h"
|
||||
#include "frc/PneumaticHub.h"
|
||||
#include "frc/PneumaticsControlModule.h"
|
||||
#include "frc/PneumaticsHub.h"
|
||||
#include "frc/SensorUtil.h"
|
||||
|
||||
using namespace frc;
|
||||
@@ -16,7 +16,7 @@ std::shared_ptr<PneumaticsBase> PneumaticsBase::GetForType(
|
||||
if (moduleType == PneumaticsModuleType::CTREPCM) {
|
||||
return PneumaticsControlModule::GetForModule(module);
|
||||
} else if (moduleType == PneumaticsModuleType::REVPH) {
|
||||
return PneumaticsHub::GetForModule(module);
|
||||
return PneumaticHub::GetForModule(module);
|
||||
}
|
||||
throw FRC_MakeError(err::InvalidParameter, "{}", moduleType);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user