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

319 lines
11 KiB
C++
Raw Normal View History

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.
2025-11-07 19:56:21 -05:00
#include "wpi/hardware/pneumatic/PneumaticsControlModule.hpp"
2021-06-05 22:36:39 -07:00
2024-09-20 17:43:39 -07:00
#include <memory>
#include <string>
#include <fmt/format.h>
2025-11-07 19:57:55 -05:00
2025-11-07 19:56:21 -05:00
#include "wpi/hal/CTREPCM.h"
#include "wpi/hal/Ports.h"
#include "wpi/hal/UsageReporting.h"
#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"
2025-11-07 19:56:21 -05:00
#include "wpi/system/Errors.hpp"
2025-11-07 19:57:55 -05:00
#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"
2021-06-05 22:36:39 -07:00
2025-11-07 20:00:05 -05:00
using namespace wpi;
2021-06-05 22:36:39 -07:00
2025-11-07 20:00:05 -05:00
wpi::util::mutex PneumaticsControlModule::m_handleLock;
std::unique_ptr<
2025-11-07 20:00:05 -05:00
wpi::util::DenseMap<int, std::weak_ptr<PneumaticsControlModule::DataStore>>[]>
2025-02-25 19:07:01 -08:00
PneumaticsControlModule::m_handleMaps = nullptr;
// Always called under lock, so we can avoid the double lock from the magic
// static
std::weak_ptr<PneumaticsControlModule::DataStore>&
2025-02-25 19:07:01 -08:00
PneumaticsControlModule::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-02-25 19:07:01 -08:00
"Bus {} out of range. Must be [0-{}).", busId, numBuses);
if (!m_handleMaps) {
2025-11-07 20:00:05 -05:00
m_handleMaps = std::make_unique<wpi::util::DenseMap<
2025-02-25 19:07:01 -08:00
int, std::weak_ptr<PneumaticsControlModule::DataStore>>[]>(numBuses);
}
2025-02-25 19:07:01 -08:00
return m_handleMaps[busId][module];
}
class PneumaticsControlModule::DataStore {
public:
2025-02-25 19:07:01 -08:00
explicit DataStore(int busId, int module, const char* stackTrace) {
int32_t status = 0;
HAL_CTREPCMHandle handle =
2025-02-25 19:07:01 -08:00
HAL_InitializeCTREPCM(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 = PneumaticsControlModule{busId, handle, module};
m_moduleObject.m_dataStore =
2025-11-07 20:00:05 -05:00
std::shared_ptr<DataStore>{this, wpi::util::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};
2025-11-07 20:00:05 -05:00
wpi::util::mutex m_reservedLock;
2025-02-25 19:07:01 -08:00
PneumaticsControlModule m_moduleObject{0, HAL_kInvalidHandle, 0};
};
2025-02-25 19:07:01 -08:00
PneumaticsControlModule::PneumaticsControlModule(int busId)
: PneumaticsControlModule{busId, SensorUtil::GetDefaultCTREPCMModule()} {}
2021-06-05 22:36:39 -07:00
2025-02-25 19:07:01 -08:00
PneumaticsControlModule::PneumaticsControlModule(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;
2021-06-05 22:36:39 -07:00
m_module = module;
}
2025-02-25 19:07:01 -08:00
PneumaticsControlModule::PneumaticsControlModule(int /* busId */,
HAL_CTREPCMHandle handle,
int module)
: m_handle{handle}, m_module{module} {}
2021-06-05 22:36:39 -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);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
return result;
}
void PneumaticsControlModule::DisableCompressor() {
2021-06-05 22:36:39 -07:00
int32_t status = 0;
HAL_SetCTREPCMClosedLoopControl(m_handle, false, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
}
void PneumaticsControlModule::EnableCompressorDigital() {
int32_t status = 0;
HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
}
void PneumaticsControlModule::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) {
int32_t status = 0;
HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
}
void PneumaticsControlModule::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) {
int32_t status = 0;
HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
}
CompressorConfigType PneumaticsControlModule::GetCompressorConfigType() const {
2021-06-05 22:36:39 -07:00
int32_t status = 0;
auto result = HAL_GetCTREPCMClosedLoopControl(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
return result ? CompressorConfigType::Digital
: CompressorConfigType::Disabled;
2021-06-05 22:36:39 -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);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
return result;
}
2025-11-07 20:00:05 -05:00
wpi::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);
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};
2021-06-05 22:36:39 -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);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
return result;
}
bool PneumaticsControlModule::GetCompressorCurrentTooHighStickyFault() const {
2021-06-05 22:36:39 -07:00
int32_t status = 0;
auto result =
HAL_GetCTREPCMCompressorCurrentTooHighStickyFault(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
return result;
}
bool PneumaticsControlModule::GetCompressorShortedFault() const {
2021-06-05 22:36:39 -07:00
int32_t status = 0;
auto result = HAL_GetCTREPCMCompressorShortedFault(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
return result;
}
bool PneumaticsControlModule::GetCompressorShortedStickyFault() const {
2021-06-05 22:36:39 -07:00
int32_t status = 0;
auto result = HAL_GetCTREPCMCompressorShortedStickyFault(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
return result;
}
bool PneumaticsControlModule::GetCompressorNotConnectedFault() const {
2021-06-05 22:36:39 -07:00
int32_t status = 0;
auto result = HAL_GetCTREPCMCompressorNotConnectedFault(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
return result;
}
bool PneumaticsControlModule::GetCompressorNotConnectedStickyFault() const {
2021-06-05 22:36:39 -07:00
int32_t status = 0;
auto result =
HAL_GetCTREPCMCompressorNotConnectedStickyFault(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
return result;
}
bool PneumaticsControlModule::GetSolenoidVoltageFault() const {
2021-06-05 22:36:39 -07:00
int32_t status = 0;
auto result = HAL_GetCTREPCMSolenoidVoltageFault(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
return result;
}
bool PneumaticsControlModule::GetSolenoidVoltageStickyFault() const {
2021-06-05 22:36:39 -07:00
int32_t status = 0;
auto result = HAL_GetCTREPCMSolenoidVoltageStickyFault(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_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);
2025-11-07 20:00:43 -05:00
WPILIB_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);
2025-11-07 20:00:43 -05:00
WPILIB_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);
2025-11-07 20:00:43 -05:00
WPILIB_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);
2025-11-07 20:00:43 -05:00
WPILIB_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);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
}
void PneumaticsControlModule::SetOneShotDuration(int index,
2025-11-07 20:00:05 -05:00
wpi::units::second_t duration) {
2021-06-05 22:36:39 -07:00
int32_t status = 0;
2025-11-07 20:00:05 -05:00
wpi::units::millisecond_t millis = duration;
2021-06-05 22:36:39 -07:00
HAL_SetCTREPCMOneShotDuration(m_handle, index, millis.to<int32_t>(), &status);
2025-11-07 20:00:43 -05:00
WPILIB_ReportError(status, "Module {}", m_module);
2021-06-05 22:36:39 -07:00
}
bool PneumaticsControlModule::CheckSolenoidChannel(int channel) const {
return HAL_CheckCTREPCMSolenoidChannel(channel);
}
int PneumaticsControlModule::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 PneumaticsControlModule::UnreserveSolenoids(int mask) {
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;
}
2025-11-07 20:00:05 -05:00
wpi::units::volt_t PneumaticsControlModule::GetAnalogVoltage(int channel) const {
2024-08-14 10:44:00 -07:00
return 0_V;
}
2025-11-07 20:00:05 -05:00
wpi::units::pounds_per_square_inch_t PneumaticsControlModule::GetPressure(
int channel) const {
return 0_psi;
}
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};
}
void PneumaticsControlModule::ReportUsage(std::string_view device,
std::string_view data) {
HAL_ReportUsage(fmt::format("PCM[{}]/{}", m_module, device), data);
}
std::shared_ptr<PneumaticsBase> PneumaticsControlModule::GetForModule(
2025-02-25 19:07:01 -08:00
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};
}