2021-06-05 22:36:39 -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.
|
|
|
|
|
|
|
|
|
|
#include "frc/PneumaticsControlModule.h"
|
|
|
|
|
|
|
|
|
|
#include <hal/CTREPCM.h>
|
2021-09-16 18:50:27 -07:00
|
|
|
#include <wpi/NullDeleter.h>
|
2021-06-05 22:36:39 -07:00
|
|
|
#include <wpi/StackTrace.h>
|
|
|
|
|
|
2021-09-16 18:50:27 -07:00
|
|
|
#include "frc/Compressor.h"
|
|
|
|
|
#include "frc/DoubleSolenoid.h"
|
2021-06-05 22:36:39 -07:00
|
|
|
#include "frc/Errors.h"
|
|
|
|
|
#include "frc/SensorUtil.h"
|
2021-09-16 18:50:27 -07:00
|
|
|
#include "frc/Solenoid.h"
|
2021-06-05 22:36:39 -07:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2021-09-16 18:50:27 -07:00
|
|
|
wpi::mutex PneumaticsControlModule::m_handleLock;
|
|
|
|
|
std::unique_ptr<
|
|
|
|
|
wpi::DenseMap<int, std::weak_ptr<PneumaticsControlModule::DataStore>>>
|
|
|
|
|
PneumaticsControlModule::m_handleMap = nullptr;
|
|
|
|
|
|
|
|
|
|
// Always called under lock, so we can avoid the double lock from the magic
|
|
|
|
|
// static
|
|
|
|
|
std::weak_ptr<PneumaticsControlModule::DataStore>&
|
|
|
|
|
PneumaticsControlModule::GetDataStore(int module) {
|
|
|
|
|
if (!m_handleMap) {
|
|
|
|
|
m_handleMap = std::make_unique<wpi::DenseMap<
|
|
|
|
|
int, std::weak_ptr<PneumaticsControlModule::DataStore>>>();
|
|
|
|
|
}
|
|
|
|
|
return (*m_handleMap)[module];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class PneumaticsControlModule::DataStore {
|
|
|
|
|
public:
|
|
|
|
|
explicit DataStore(int module, const char* stackTrace) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_CTREPCMHandle handle =
|
|
|
|
|
HAL_InitializeCTREPCM(module, stackTrace, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Module {}", module);
|
|
|
|
|
m_moduleObject = PneumaticsControlModule{handle, module};
|
|
|
|
|
m_moduleObject.m_dataStore =
|
|
|
|
|
std::shared_ptr<DataStore>{this, wpi::NullDeleter<DataStore>()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~DataStore() noexcept { HAL_FreeCTREPCM(m_moduleObject.m_handle); }
|
|
|
|
|
|
|
|
|
|
DataStore(DataStore&&) = delete;
|
|
|
|
|
DataStore& operator=(DataStore&&) = delete;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
friend class PneumaticsControlModule;
|
|
|
|
|
uint32_t m_reservedMask{0};
|
|
|
|
|
bool m_compressorReserved{false};
|
|
|
|
|
wpi::mutex m_reservedLock;
|
|
|
|
|
PneumaticsControlModule m_moduleObject{HAL_kInvalidHandle, 0};
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-05 22:36:39 -07:00
|
|
|
PneumaticsControlModule::PneumaticsControlModule()
|
|
|
|
|
: PneumaticsControlModule{SensorUtil::GetDefaultCTREPCMModule()} {}
|
|
|
|
|
|
|
|
|
|
PneumaticsControlModule::PneumaticsControlModule(int module) {
|
|
|
|
|
std::string stackTrace = wpi::GetStackTrace(1);
|
2021-09-16 18:50:27 -07:00
|
|
|
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;
|
2021-06-05 22:36:39 -07:00
|
|
|
m_module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 18:50:27 -07:00
|
|
|
PneumaticsControlModule::PneumaticsControlModule(HAL_CTREPCMHandle handle,
|
|
|
|
|
int module)
|
|
|
|
|
: m_handle{handle}, m_module{module} {}
|
2021-06-05 22:36:39 -07:00
|
|
|
|
2021-09-16 18:50:27 -07:00
|
|
|
bool PneumaticsControlModule::GetCompressor() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMCompressor(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 20:32:02 -08:00
|
|
|
void PneumaticsControlModule::DisableCompressor() {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
2021-11-23 20:32:02 -08:00
|
|
|
HAL_SetCTREPCMClosedLoopControl(m_handle, false, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
2021-11-23 20:32:02 -08:00
|
|
|
void PneumaticsControlModule::EnableCompressorDigital() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-11-23 20:32:02 -08:00
|
|
|
}
|
|
|
|
|
|
2021-12-19 13:41:35 -08:00
|
|
|
void PneumaticsControlModule::EnableCompressorAnalog(
|
2021-12-31 22:04:56 -07:00
|
|
|
units::pounds_per_square_inch_t minPressure,
|
|
|
|
|
units::pounds_per_square_inch_t maxPressure) {
|
2021-11-23 20:32:02 -08:00
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-11-23 20:32:02 -08:00
|
|
|
}
|
|
|
|
|
|
2021-12-19 13:41:35 -08:00
|
|
|
void PneumaticsControlModule::EnableCompressorHybrid(
|
2021-12-31 22:04:56 -07:00
|
|
|
units::pounds_per_square_inch_t minPressure,
|
|
|
|
|
units::pounds_per_square_inch_t maxPressure) {
|
2021-11-23 20:32:02 -08:00
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-11-23 20:32:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompressorConfigType PneumaticsControlModule::GetCompressorConfigType() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMClosedLoopControl(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-11-23 20:32:02 -08:00
|
|
|
return result ? CompressorConfigType::Digital
|
|
|
|
|
: CompressorConfigType::Disabled;
|
2021-06-05 22:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-16 18:50:27 -07:00
|
|
|
bool PneumaticsControlModule::GetPressureSwitch() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMPressureSwitch(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 13:41:35 -08:00
|
|
|
units::ampere_t PneumaticsControlModule::GetCompressorCurrent() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMCompressorCurrent(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-12-19 13:41:35 -08:00
|
|
|
return units::ampere_t{result};
|
2021-06-05 22:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-16 18:50:27 -07:00
|
|
|
bool PneumaticsControlModule::GetCompressorCurrentTooHighFault() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMCompressorCurrentTooHighFault(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
2021-09-16 18:50:27 -07:00
|
|
|
bool PneumaticsControlModule::GetCompressorCurrentTooHighStickyFault() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result =
|
|
|
|
|
HAL_GetCTREPCMCompressorCurrentTooHighStickyFault(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
2021-09-16 18:50:27 -07:00
|
|
|
bool PneumaticsControlModule::GetCompressorShortedFault() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMCompressorShortedFault(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
2021-09-16 18:50:27 -07:00
|
|
|
bool PneumaticsControlModule::GetCompressorShortedStickyFault() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMCompressorShortedStickyFault(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
2021-09-16 18:50:27 -07:00
|
|
|
bool PneumaticsControlModule::GetCompressorNotConnectedFault() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMCompressorNotConnectedFault(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
2021-09-16 18:50:27 -07:00
|
|
|
bool PneumaticsControlModule::GetCompressorNotConnectedStickyFault() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result =
|
|
|
|
|
HAL_GetCTREPCMCompressorNotConnectedStickyFault(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 18:50:27 -07:00
|
|
|
bool PneumaticsControlModule::GetSolenoidVoltageFault() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMSolenoidVoltageFault(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
2021-09-16 18:50:27 -07:00
|
|
|
bool PneumaticsControlModule::GetSolenoidVoltageStickyFault() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMSolenoidVoltageStickyFault(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PneumaticsControlModule::ClearAllStickyFaults() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_ClearAllCTREPCMStickyFaults(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PneumaticsControlModule::SetSolenoids(int mask, int values) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetCTREPCMSolenoids(m_handle, mask, values, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PneumaticsControlModule::GetSolenoids() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMSolenoids(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PneumaticsControlModule::GetModuleNumber() const {
|
|
|
|
|
return m_module;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PneumaticsControlModule::GetSolenoidDisabledList() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
auto result = HAL_GetCTREPCMSolenoidDisabledList(m_handle, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PneumaticsControlModule::FireOneShot(int index) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_FireCTREPCMOneShot(m_handle, index, &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PneumaticsControlModule::SetOneShotDuration(int index,
|
|
|
|
|
units::second_t duration) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
units::millisecond_t millis = duration;
|
|
|
|
|
HAL_SetCTREPCMOneShotDuration(m_handle, index, millis.to<int32_t>(), &status);
|
2022-01-18 00:58:26 -08:00
|
|
|
FRC_ReportError(status, "Module {}", m_module);
|
2021-06-05 22:36:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PneumaticsControlModule::CheckSolenoidChannel(int channel) const {
|
|
|
|
|
return HAL_CheckCTREPCMSolenoidChannel(channel);
|
|
|
|
|
}
|
2021-07-09 15:11:12 -07:00
|
|
|
|
|
|
|
|
int PneumaticsControlModule::CheckAndReserveSolenoids(int mask) {
|
2021-09-16 18:50:27 -07:00
|
|
|
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
2021-07-09 15:11:12 -07:00
|
|
|
uint32_t uMask = static_cast<uint32_t>(mask);
|
2021-09-16 18:50:27 -07:00
|
|
|
if ((m_dataStore->m_reservedMask & uMask) != 0) {
|
|
|
|
|
return m_dataStore->m_reservedMask & uMask;
|
2021-07-09 15:11:12 -07:00
|
|
|
}
|
2021-09-16 18:50:27 -07:00
|
|
|
m_dataStore->m_reservedMask |= uMask;
|
2021-07-09 15:11:12 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PneumaticsControlModule::UnreserveSolenoids(int mask) {
|
2021-09-16 18:50:27 -07:00
|
|
|
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
|
|
|
|
m_dataStore->m_reservedMask &= ~(static_cast<uint32_t>(mask));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PneumaticsControlModule::ReserveCompressor() {
|
|
|
|
|
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
|
|
|
|
if (m_dataStore->m_compressorReserved) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
m_dataStore->m_compressorReserved = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PneumaticsControlModule::UnreserveCompressor() {
|
|
|
|
|
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
|
|
|
|
m_dataStore->m_compressorReserved = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 13:41:35 -08:00
|
|
|
units::volt_t PneumaticsControlModule::GetAnalogVoltage(int channel) const {
|
|
|
|
|
return units::volt_t{0};
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-31 22:04:56 -07:00
|
|
|
units::pounds_per_square_inch_t PneumaticsControlModule::GetPressure(
|
|
|
|
|
int channel) const {
|
|
|
|
|
return 0_psi;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 18:50:27 -07:00
|
|
|
Solenoid PneumaticsControlModule::MakeSolenoid(int channel) {
|
|
|
|
|
return Solenoid{m_module, PneumaticsModuleType::CTREPCM, channel};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DoubleSolenoid PneumaticsControlModule::MakeDoubleSolenoid(int forwardChannel,
|
|
|
|
|
int reverseChannel) {
|
|
|
|
|
return DoubleSolenoid{m_module, PneumaticsModuleType::CTREPCM, forwardChannel,
|
|
|
|
|
reverseChannel};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Compressor PneumaticsControlModule::MakeCompressor() {
|
|
|
|
|
return Compressor{m_module, PneumaticsModuleType::CTREPCM};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<PneumaticsBase> PneumaticsControlModule::GetForModule(
|
|
|
|
|
int module) {
|
|
|
|
|
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};
|
2021-07-09 15:11:12 -07:00
|
|
|
}
|