2021-10-10 20:04:50 -07:00
|
|
|
// 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.
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
#include "frc/PneumaticHub.h"
|
2021-10-10 20:04:50 -07:00
|
|
|
|
|
|
|
|
#include <hal/REVPH.h>
|
|
|
|
|
#include <wpi/NullDeleter.h>
|
|
|
|
|
#include <wpi/StackTrace.h>
|
|
|
|
|
|
|
|
|
|
#include "frc/Compressor.h"
|
|
|
|
|
#include "frc/DoubleSolenoid.h"
|
|
|
|
|
#include "frc/Errors.h"
|
|
|
|
|
#include "frc/SensorUtil.h"
|
|
|
|
|
#include "frc/Solenoid.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
wpi::mutex PneumaticHub::m_handleLock;
|
|
|
|
|
std::unique_ptr<wpi::DenseMap<int, std::weak_ptr<PneumaticHub::DataStore>>>
|
|
|
|
|
PneumaticHub::m_handleMap = nullptr;
|
2021-10-10 20:04:50 -07:00
|
|
|
|
|
|
|
|
// Always called under lock, so we can avoid the double lock from the magic
|
|
|
|
|
// static
|
2021-10-29 00:00:31 -07:00
|
|
|
std::weak_ptr<PneumaticHub::DataStore>& PneumaticHub::GetDataStore(int module) {
|
2021-10-10 20:04:50 -07:00
|
|
|
if (!m_handleMap) {
|
|
|
|
|
m_handleMap = std::make_unique<
|
2021-10-29 00:00:31 -07:00
|
|
|
wpi::DenseMap<int, std::weak_ptr<PneumaticHub::DataStore>>>();
|
2021-10-10 20:04:50 -07:00
|
|
|
}
|
|
|
|
|
return (*m_handleMap)[module];
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
class PneumaticHub::DataStore {
|
2021-10-10 20:04:50 -07:00
|
|
|
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);
|
2021-10-29 00:00:31 -07:00
|
|
|
m_moduleObject = PneumaticHub{handle, module};
|
2021-10-10 20:04:50 -07:00
|
|
|
m_moduleObject.m_dataStore =
|
|
|
|
|
std::shared_ptr<DataStore>{this, wpi::NullDeleter<DataStore>()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~DataStore() noexcept { HAL_FreeREVPH(m_moduleObject.m_handle); }
|
|
|
|
|
|
|
|
|
|
DataStore(DataStore&&) = delete;
|
|
|
|
|
DataStore& operator=(DataStore&&) = delete;
|
|
|
|
|
|
|
|
|
|
private:
|
2021-10-29 00:00:31 -07:00
|
|
|
friend class PneumaticHub;
|
2021-10-10 20:04:50 -07:00
|
|
|
uint32_t m_reservedMask{0};
|
|
|
|
|
bool m_compressorReserved{false};
|
|
|
|
|
wpi::mutex m_reservedLock;
|
2021-10-29 00:00:31 -07:00
|
|
|
PneumaticHub m_moduleObject{HAL_kInvalidHandle, 0};
|
2021-10-10 20:04:50 -07:00
|
|
|
};
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
PneumaticHub::PneumaticHub()
|
|
|
|
|
: PneumaticHub{SensorUtil::GetDefaultREVPHModule()} {}
|
2021-10-10 20:04:50 -07:00
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
PneumaticHub::PneumaticHub(int module) {
|
2021-10-10 20:04:50 -07:00
|
|
|
std::string stackTrace = wpi::GetStackTrace(1);
|
|
|
|
|
std::scoped_lock lock(m_handleLock);
|
|
|
|
|
auto& res = GetDataStore(module);
|
|
|
|
|
m_dataStore = res.lock();
|
|
|
|
|
if (!m_dataStore) {
|
|
|
|
|
m_dataStore = std::make_shared<DataStore>(module, stackTrace.c_str());
|
|
|
|
|
res = m_dataStore;
|
|
|
|
|
}
|
|
|
|
|
m_handle = m_dataStore->m_moduleObject.m_handle;
|
|
|
|
|
m_module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
PneumaticHub::PneumaticHub(HAL_REVPHHandle handle, int module)
|
2021-10-10 20:04:50 -07:00
|
|
|
: m_handle{handle}, m_module{module} {}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
bool PneumaticHub::GetCompressor() const {
|
2021-10-10 20:04:50 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetREVPHCompressor(m_handle, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 20:32:02 -08:00
|
|
|
void PneumaticHub::DisableCompressor() {
|
2021-10-10 20:04:50 -07:00
|
|
|
int32_t status = 0;
|
2021-11-23 20:32:02 -08:00
|
|
|
HAL_SetREVPHClosedLoopControlDisabled(m_handle, &status);
|
2021-10-10 20:04:50 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 20:32:02 -08:00
|
|
|
void PneumaticHub::EnableCompressorDigital() {
|
2021-10-10 20:04:50 -07:00
|
|
|
int32_t status = 0;
|
2021-11-23 20:32:02 -08:00
|
|
|
HAL_SetREVPHClosedLoopControlDigital(m_handle, &status);
|
2021-10-10 20:04:50 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
2021-11-23 20:32:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PneumaticHub::EnableCompressorAnalog(double minAnalogVoltage,
|
|
|
|
|
double maxAnalogVoltage) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetREVPHClosedLoopControlAnalog(m_handle, minAnalogVoltage,
|
|
|
|
|
maxAnalogVoltage, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PneumaticHub::EnableCompressorHybrid(double minAnalogVoltage,
|
|
|
|
|
double maxAnalogVoltage) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetREVPHClosedLoopControlHybrid(m_handle, minAnalogVoltage,
|
|
|
|
|
maxAnalogVoltage, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompressorConfigType PneumaticHub::GetCompressorConfigType() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetREVPHCompressorConfig(m_handle, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
return static_cast<CompressorConfigType>(result);
|
2021-10-10 20:04:50 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
bool PneumaticHub::GetPressureSwitch() const {
|
2021-10-10 20:04:50 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetREVPHPressureSwitch(m_handle, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
double PneumaticHub::GetCompressorCurrent() const {
|
2021-10-10 20:04:50 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetREVPHCompressorCurrent(m_handle, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
void PneumaticHub::SetSolenoids(int mask, int values) {
|
2021-10-10 20:04:50 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetREVPHSolenoids(m_handle, mask, values, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
int PneumaticHub::GetSolenoids() const {
|
2021-10-10 20:04:50 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetREVPHSolenoids(m_handle, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
int PneumaticHub::GetModuleNumber() const {
|
2021-10-10 20:04:50 -07:00
|
|
|
return m_module;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
int PneumaticHub::GetSolenoidDisabledList() const {
|
2021-10-10 20:04:50 -07:00
|
|
|
return 0;
|
|
|
|
|
// TODO Fix me
|
|
|
|
|
// int32_t status = 0;
|
|
|
|
|
// auto result = HAL_GetREVPHSolenoidDisabledList(m_handle, &status);
|
|
|
|
|
// FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
// return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
void PneumaticHub::FireOneShot(int index) {
|
2021-10-10 20:04:50 -07:00
|
|
|
// TODO Fix me
|
|
|
|
|
// int32_t status = 0;
|
|
|
|
|
// HAL_FireREVPHOneShot(m_handle, index, &status);
|
|
|
|
|
// FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
void PneumaticHub::SetOneShotDuration(int index, units::second_t duration) {
|
2021-10-10 20:04:50 -07:00
|
|
|
// TODO Fix me
|
|
|
|
|
// int32_t status = 0;
|
|
|
|
|
// units::millisecond_t millis = duration;
|
|
|
|
|
// HAL_SetREVPHOneShotDuration(m_handle, index, millis.to<int32_t>(),
|
|
|
|
|
// &status); FRC_CheckErrorStatus(status, "Module {}", m_module);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
bool PneumaticHub::CheckSolenoidChannel(int channel) const {
|
2021-10-10 20:04:50 -07:00
|
|
|
return HAL_CheckREVPHSolenoidChannel(channel);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
int PneumaticHub::CheckAndReserveSolenoids(int mask) {
|
2021-10-10 20:04:50 -07:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
void PneumaticHub::UnreserveSolenoids(int mask) {
|
2021-10-10 20:04:50 -07:00
|
|
|
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
|
|
|
|
m_dataStore->m_reservedMask &= ~(static_cast<uint32_t>(mask));
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
bool PneumaticHub::ReserveCompressor() {
|
2021-10-10 20:04:50 -07:00
|
|
|
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
|
|
|
|
if (m_dataStore->m_compressorReserved) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
m_dataStore->m_compressorReserved = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
void PneumaticHub::UnreserveCompressor() {
|
2021-10-10 20:04:50 -07:00
|
|
|
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
|
|
|
|
m_dataStore->m_compressorReserved = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
Solenoid PneumaticHub::MakeSolenoid(int channel) {
|
2021-10-10 20:04:50 -07:00
|
|
|
return Solenoid{m_module, PneumaticsModuleType::REVPH, channel};
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
DoubleSolenoid PneumaticHub::MakeDoubleSolenoid(int forwardChannel,
|
|
|
|
|
int reverseChannel) {
|
2021-10-10 20:04:50 -07:00
|
|
|
return DoubleSolenoid{m_module, PneumaticsModuleType::REVPH, forwardChannel,
|
|
|
|
|
reverseChannel};
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
Compressor PneumaticHub::MakeCompressor() {
|
2021-10-10 20:04:50 -07:00
|
|
|
return Compressor{m_module, PneumaticsModuleType::REVPH};
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:00:31 -07:00
|
|
|
std::shared_ptr<PneumaticsBase> PneumaticHub::GetForModule(int module) {
|
2021-10-10 20:04:50 -07:00
|
|
|
std::string stackTrace = wpi::GetStackTrace(1);
|
|
|
|
|
std::scoped_lock lock(m_handleLock);
|
|
|
|
|
auto& res = GetDataStore(module);
|
|
|
|
|
std::shared_ptr<DataStore> dataStore = res.lock();
|
|
|
|
|
if (!dataStore) {
|
|
|
|
|
dataStore = std::make_shared<DataStore>(module, stackTrace.c_str());
|
|
|
|
|
res = dataStore;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::shared_ptr<PneumaticsBase>{dataStore, &dataStore->m_moduleObject};
|
|
|
|
|
}
|