mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
[hal, wpilib] Switch PCM to be a single object that is allowed to be duplicated (#3475)
Having PCM as a singleton is a problem, as multiple things need to use it, and that gets really ugly. This changes PCM's to be a reference counted object, that can be passed around and constructed from multiple places. In Java, this is using a map to hold a data store with a ref count, and allocating new objects any time a duplicate is requested. In C++, this uses a trick constructor to store a PCM instance in the data store itself. This instance can then be passed to base objects using std::shared_ptr's aliasing constructor, which means constructing a solenoid from a PCM is not allocating after the 1st one. This did require removing sendable from PCM. A compressor class was added back in to act as sendable for the PCM. After this change is finished, the only change RobotBuilder and Team Code would require is passing a module type to solenoid constructors. Co-authored-by: sciencewhiz <sciencewhiz@users.noreply.github.com>
This commit is contained in:
72
wpilibc/src/main/native/cpp/Compressor.cpp
Normal file
72
wpilibc/src/main/native/cpp/Compressor.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
// 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/Compressor.h"
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
#include <hal/Ports.h>
|
||||
#include <wpi/sendable/SendableBuilder.h>
|
||||
#include <wpi/sendable/SendableRegistry.h>
|
||||
|
||||
#include "frc/Errors.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
Compressor::Compressor(int module, PneumaticsModuleType moduleType)
|
||||
: m_module{PneumaticsBase::GetForType(module, moduleType)} {
|
||||
if (!m_module->ReserveCompressor()) {
|
||||
throw FRC_MakeError(err::ResourceAlreadyAllocated, "{}", module);
|
||||
}
|
||||
|
||||
SetClosedLoopControl(true);
|
||||
|
||||
HAL_Report(HALUsageReporting::kResourceType_Compressor, module + 1);
|
||||
wpi::SendableRegistry::AddLW(this, "Compressor", module);
|
||||
}
|
||||
|
||||
Compressor::Compressor(PneumaticsModuleType moduleType)
|
||||
: Compressor{PneumaticsBase::GetDefaultForType(moduleType), moduleType} {}
|
||||
|
||||
Compressor::~Compressor() {
|
||||
m_module->UnreserveCompressor();
|
||||
}
|
||||
|
||||
void Compressor::Start() {
|
||||
SetClosedLoopControl(true);
|
||||
}
|
||||
|
||||
void Compressor::Stop() {
|
||||
SetClosedLoopControl(false);
|
||||
}
|
||||
|
||||
bool Compressor::Enabled() const {
|
||||
return m_module->GetCompressor();
|
||||
}
|
||||
|
||||
bool Compressor::GetPressureSwitchValue() const {
|
||||
return m_module->GetPressureSwitch();
|
||||
}
|
||||
|
||||
double Compressor::GetCompressorCurrent() const {
|
||||
return m_module->GetCompressorCurrent();
|
||||
}
|
||||
|
||||
void Compressor::SetClosedLoopControl(bool on) {
|
||||
m_module->SetClosedLoopControl(on);
|
||||
}
|
||||
|
||||
bool Compressor::GetClosedLoopControl() const {
|
||||
return m_module->GetClosedLoopControl();
|
||||
}
|
||||
|
||||
void Compressor::InitSendable(wpi::SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Compressor");
|
||||
builder.AddBooleanProperty(
|
||||
"Closed Loop Control", [=]() { return GetClosedLoopControl(); },
|
||||
[=](bool value) { SetClosedLoopControl(value); });
|
||||
builder.AddBooleanProperty(
|
||||
"Enabled", [=] { return Enabled(); }, nullptr);
|
||||
builder.AddBooleanProperty(
|
||||
"Pressure switch", [=]() { return GetPressureSwitchValue(); }, nullptr);
|
||||
}
|
||||
@@ -18,33 +18,20 @@
|
||||
|
||||
using namespace frc;
|
||||
|
||||
DoubleSolenoid::DoubleSolenoid(PneumaticsBase& module, int forwardChannel,
|
||||
int reverseChannel)
|
||||
: DoubleSolenoid{std::shared_ptr<PneumaticsBase>{
|
||||
&module, wpi::NullDeleter<PneumaticsBase>()},
|
||||
forwardChannel, reverseChannel} {}
|
||||
|
||||
DoubleSolenoid::DoubleSolenoid(PneumaticsBase* module, int forwardChannel,
|
||||
int reverseChannel)
|
||||
: DoubleSolenoid{std::shared_ptr<PneumaticsBase>{
|
||||
module, wpi::NullDeleter<PneumaticsBase>()},
|
||||
forwardChannel, reverseChannel} {}
|
||||
|
||||
DoubleSolenoid::DoubleSolenoid(std::shared_ptr<PneumaticsBase> module,
|
||||
DoubleSolenoid::DoubleSolenoid(int module, PneumaticsModuleType moduleType,
|
||||
int forwardChannel, int reverseChannel)
|
||||
: m_module{std::move(module)} {
|
||||
if (!m_module->CheckSolenoidChannel(forwardChannel)) {
|
||||
: m_module{PneumaticsBase::GetForType(module, moduleType)},
|
||||
m_forwardChannel{forwardChannel},
|
||||
m_reverseChannel{reverseChannel} {
|
||||
if (!m_module->CheckSolenoidChannel(m_forwardChannel)) {
|
||||
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}",
|
||||
forwardChannel);
|
||||
m_forwardChannel);
|
||||
}
|
||||
if (!m_module->CheckSolenoidChannel(reverseChannel)) {
|
||||
if (!m_module->CheckSolenoidChannel(m_reverseChannel)) {
|
||||
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}",
|
||||
reverseChannel);
|
||||
m_reverseChannel);
|
||||
}
|
||||
|
||||
m_forwardChannel = forwardChannel;
|
||||
m_reverseChannel = reverseChannel;
|
||||
|
||||
m_forwardMask = 1 << forwardChannel;
|
||||
m_reverseMask = 1 << reverseChannel;
|
||||
m_mask = m_forwardMask | m_reverseMask;
|
||||
@@ -72,6 +59,11 @@ DoubleSolenoid::DoubleSolenoid(std::shared_ptr<PneumaticsBase> module,
|
||||
m_module->GetModuleNumber(), m_forwardChannel);
|
||||
}
|
||||
|
||||
DoubleSolenoid::DoubleSolenoid(PneumaticsModuleType moduleType,
|
||||
int forwardChannel, int reverseChannel)
|
||||
: DoubleSolenoid{PneumaticsBase::GetDefaultForType(moduleType), moduleType,
|
||||
forwardChannel, reverseChannel} {}
|
||||
|
||||
DoubleSolenoid::~DoubleSolenoid() {
|
||||
m_module->UnreserveSolenoids(m_mask);
|
||||
}
|
||||
|
||||
26
wpilibc/src/main/native/cpp/PneumaticsBase.cpp
Normal file
26
wpilibc/src/main/native/cpp/PneumaticsBase.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
// 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/PneumaticsBase.h"
|
||||
|
||||
#include "frc/Errors.h"
|
||||
#include "frc/PneumaticsControlModule.h"
|
||||
#include "frc/SensorUtil.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
std::shared_ptr<PneumaticsBase> PneumaticsBase::GetForType(
|
||||
int module, PneumaticsModuleType moduleType) {
|
||||
if (moduleType == PneumaticsModuleType::CTREPCM) {
|
||||
return PneumaticsControlModule::GetForModule(module);
|
||||
}
|
||||
throw FRC_MakeError(err::InvalidParameter, "{}", moduleType);
|
||||
}
|
||||
|
||||
int PneumaticsBase::GetDefaultForType(PneumaticsModuleType moduleType) {
|
||||
if (moduleType == PneumaticsModuleType::CTREPCM) {
|
||||
return SensorUtil::GetDefaultCTREPCMModule();
|
||||
}
|
||||
throw FRC_MakeError(err::InvalidParameter, "{}", moduleType);
|
||||
}
|
||||
@@ -5,32 +5,79 @@
|
||||
#include "frc/PneumaticsControlModule.h"
|
||||
|
||||
#include <hal/CTREPCM.h>
|
||||
#include <wpi/NullDeleter.h>
|
||||
#include <wpi/StackTrace.h>
|
||||
#include <wpi/sendable/SendableBuilder.h>
|
||||
#include <wpi/sendable/SendableRegistry.h>
|
||||
|
||||
#include "frc/Compressor.h"
|
||||
#include "frc/DoubleSolenoid.h"
|
||||
#include "frc/Errors.h"
|
||||
#include "frc/SensorUtil.h"
|
||||
#include "frc/Solenoid.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
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};
|
||||
};
|
||||
|
||||
PneumaticsControlModule::PneumaticsControlModule()
|
||||
: PneumaticsControlModule{SensorUtil::GetDefaultCTREPCMModule()} {}
|
||||
|
||||
PneumaticsControlModule::PneumaticsControlModule(int module) {
|
||||
int32_t status = 0;
|
||||
std::string stackTrace = wpi::GetStackTrace(1);
|
||||
m_handle = HAL_InitializeCTREPCM(module, stackTrace.c_str(), &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", module);
|
||||
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;
|
||||
wpi::SendableRegistry::AddLW(this, "Compressor", module);
|
||||
}
|
||||
|
||||
PneumaticsControlModule::~PneumaticsControlModule() {
|
||||
HAL_FreeCTREPCM(m_handle);
|
||||
}
|
||||
PneumaticsControlModule::PneumaticsControlModule(HAL_CTREPCMHandle handle,
|
||||
int module)
|
||||
: m_handle{handle}, m_module{module} {}
|
||||
|
||||
bool PneumaticsControlModule::GetCompressor() {
|
||||
bool PneumaticsControlModule::GetCompressor() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetCTREPCMCompressor(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
@@ -43,59 +90,59 @@ void PneumaticsControlModule::SetClosedLoopControl(bool enabled) {
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
}
|
||||
|
||||
bool PneumaticsControlModule::GetClosedLoopControl() {
|
||||
bool PneumaticsControlModule::GetClosedLoopControl() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetCTREPCMClosedLoopControl(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool PneumaticsControlModule::GetPressureSwitch() {
|
||||
bool PneumaticsControlModule::GetPressureSwitch() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetCTREPCMPressureSwitch(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
|
||||
double PneumaticsControlModule::GetCompressorCurrent() {
|
||||
double PneumaticsControlModule::GetCompressorCurrent() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetCTREPCMCompressorCurrent(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool PneumaticsControlModule::GetCompressorCurrentTooHighFault() {
|
||||
bool PneumaticsControlModule::GetCompressorCurrentTooHighFault() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetCTREPCMCompressorCurrentTooHighFault(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
bool PneumaticsControlModule::GetCompressorCurrentTooHighStickyFault() {
|
||||
bool PneumaticsControlModule::GetCompressorCurrentTooHighStickyFault() const {
|
||||
int32_t status = 0;
|
||||
auto result =
|
||||
HAL_GetCTREPCMCompressorCurrentTooHighStickyFault(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
bool PneumaticsControlModule::GetCompressorShortedFault() {
|
||||
bool PneumaticsControlModule::GetCompressorShortedFault() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetCTREPCMCompressorShortedFault(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
bool PneumaticsControlModule::GetCompressorShortedStickyFault() {
|
||||
bool PneumaticsControlModule::GetCompressorShortedStickyFault() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetCTREPCMCompressorShortedStickyFault(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
bool PneumaticsControlModule::GetCompressorNotConnectedFault() {
|
||||
bool PneumaticsControlModule::GetCompressorNotConnectedFault() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetCTREPCMCompressorNotConnectedFault(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
bool PneumaticsControlModule::GetCompressorNotConnectedStickyFault() {
|
||||
bool PneumaticsControlModule::GetCompressorNotConnectedStickyFault() const {
|
||||
int32_t status = 0;
|
||||
auto result =
|
||||
HAL_GetCTREPCMCompressorNotConnectedStickyFault(m_handle, &status);
|
||||
@@ -103,13 +150,13 @@ bool PneumaticsControlModule::GetCompressorNotConnectedStickyFault() {
|
||||
return result;
|
||||
}
|
||||
|
||||
bool PneumaticsControlModule::GetSolenoidVoltageFault() {
|
||||
bool PneumaticsControlModule::GetSolenoidVoltageFault() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetCTREPCMSolenoidVoltageFault(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
return result;
|
||||
}
|
||||
bool PneumaticsControlModule::GetSolenoidVoltageStickyFault() {
|
||||
bool PneumaticsControlModule::GetSolenoidVoltageStickyFault() const {
|
||||
int32_t status = 0;
|
||||
auto result = HAL_GetCTREPCMSolenoidVoltageStickyFault(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Module {}", m_module);
|
||||
@@ -165,27 +212,58 @@ bool PneumaticsControlModule::CheckSolenoidChannel(int channel) const {
|
||||
}
|
||||
|
||||
int PneumaticsControlModule::CheckAndReserveSolenoids(int mask) {
|
||||
std::scoped_lock lock{m_reservedLock};
|
||||
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
||||
uint32_t uMask = static_cast<uint32_t>(mask);
|
||||
if ((m_reservedMask & uMask) != 0) {
|
||||
return m_reservedMask & uMask;
|
||||
if ((m_dataStore->m_reservedMask & uMask) != 0) {
|
||||
return m_dataStore->m_reservedMask & uMask;
|
||||
}
|
||||
m_reservedMask |= uMask;
|
||||
m_dataStore->m_reservedMask |= uMask;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void PneumaticsControlModule::UnreserveSolenoids(int mask) {
|
||||
std::scoped_lock lock{m_reservedLock};
|
||||
m_reservedMask &= ~(static_cast<uint32_t>(mask));
|
||||
std::scoped_lock lock{m_dataStore->m_reservedLock};
|
||||
m_dataStore->m_reservedMask &= ~(static_cast<uint32_t>(mask));
|
||||
}
|
||||
|
||||
void PneumaticsControlModule::InitSendable(wpi::SendableBuilder& builder) {
|
||||
builder.SetSmartDashboardType("Compressor");
|
||||
builder.AddBooleanProperty(
|
||||
"Closed Loop Control", [=]() { return GetClosedLoopControl(); },
|
||||
[=](bool value) { SetClosedLoopControl(value); });
|
||||
builder.AddBooleanProperty(
|
||||
"Enabled", [=] { return GetCompressor(); }, nullptr);
|
||||
builder.AddBooleanProperty(
|
||||
"Pressure switch", [=]() { return GetPressureSwitch(); }, nullptr);
|
||||
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;
|
||||
}
|
||||
|
||||
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};
|
||||
}
|
||||
|
||||
@@ -16,22 +16,12 @@
|
||||
|
||||
using namespace frc;
|
||||
|
||||
Solenoid::Solenoid(PneumaticsBase& module, int channel)
|
||||
: Solenoid{std::shared_ptr<PneumaticsBase>{
|
||||
&module, wpi::NullDeleter<PneumaticsBase>()},
|
||||
channel} {}
|
||||
|
||||
Solenoid::Solenoid(PneumaticsBase* module, int channel)
|
||||
: Solenoid{std::shared_ptr<PneumaticsBase>{
|
||||
module, wpi::NullDeleter<PneumaticsBase>()},
|
||||
channel} {}
|
||||
|
||||
Solenoid::Solenoid(std::shared_ptr<PneumaticsBase> module, int channel)
|
||||
: m_module{std::move(module)} {
|
||||
if (!m_module->CheckSolenoidChannel(channel)) {
|
||||
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
|
||||
Solenoid::Solenoid(int module, PneumaticsModuleType moduleType, int channel)
|
||||
: m_module{PneumaticsBase::GetForType(module, moduleType)},
|
||||
m_channel{channel} {
|
||||
if (!m_module->CheckSolenoidChannel(m_channel)) {
|
||||
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", m_channel);
|
||||
}
|
||||
m_channel = channel;
|
||||
m_mask = 1 << channel;
|
||||
|
||||
if (m_module->CheckAndReserveSolenoids(m_mask) != 0) {
|
||||
@@ -44,6 +34,10 @@ Solenoid::Solenoid(std::shared_ptr<PneumaticsBase> module, int channel)
|
||||
m_channel);
|
||||
}
|
||||
|
||||
Solenoid::Solenoid(PneumaticsModuleType moduleType, int channel)
|
||||
: Solenoid{PneumaticsBase::GetDefaultForType(moduleType), moduleType,
|
||||
channel} {}
|
||||
|
||||
Solenoid::~Solenoid() {
|
||||
m_module->UnreserveSolenoids(m_mask);
|
||||
}
|
||||
|
||||
116
wpilibc/src/main/native/include/frc/Compressor.h
Normal file
116
wpilibc/src/main/native/include/frc/Compressor.h
Normal file
@@ -0,0 +1,116 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <hal/Types.h>
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
#include "frc/PneumaticsBase.h"
|
||||
#include "frc/PneumaticsModuleType.h"
|
||||
#include "frc/SensorUtil.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Class for operating a compressor connected to a pneumatics module.
|
||||
*
|
||||
* The module will automatically run in closed loop mode by default whenever a
|
||||
* Solenoid object is created. For most cases, a Compressor object does not need
|
||||
* to be instantiated or used in a robot program. This class is only required in
|
||||
* cases where the robot program needs a more detailed status of the compressor
|
||||
* or to enable/disable closed loop control.
|
||||
*
|
||||
* Note: you cannot operate the compressor directly from this class as doing so
|
||||
* would circumvent the safety provided by using the pressure switch and closed
|
||||
* loop control. You can only turn off closed loop control, thereby stopping
|
||||
* the compressor from operating.
|
||||
*/
|
||||
class Compressor : public wpi::Sendable,
|
||||
public wpi::SendableHelper<Compressor> {
|
||||
public:
|
||||
/**
|
||||
* Constructs a compressor for a specified module and type.
|
||||
*
|
||||
* @param module The module ID to use.
|
||||
* @param moduleType The module type to use.
|
||||
*/
|
||||
Compressor(int module, PneumaticsModuleType moduleType);
|
||||
|
||||
/**
|
||||
* Constructs a compressor for a default module and specified type.
|
||||
*
|
||||
* @param moduleType The module type to use.
|
||||
*/
|
||||
explicit Compressor(PneumaticsModuleType moduleType);
|
||||
|
||||
~Compressor() override;
|
||||
|
||||
Compressor(const Compressor&) = delete;
|
||||
Compressor& operator=(const Compressor&) = delete;
|
||||
|
||||
Compressor(Compressor&&) = default;
|
||||
Compressor& operator=(Compressor&&) = default;
|
||||
|
||||
/**
|
||||
* Starts closed-loop control. Note that closed loop control is enabled by
|
||||
* default.
|
||||
*/
|
||||
void Start();
|
||||
|
||||
/**
|
||||
* Stops closed-loop control. Note that closed loop control is enabled by
|
||||
* default.
|
||||
*/
|
||||
void Stop();
|
||||
|
||||
/**
|
||||
* Check if compressor output is active.
|
||||
*
|
||||
* @return true if the compressor is on
|
||||
*/
|
||||
bool Enabled() const;
|
||||
|
||||
/**
|
||||
* Check if the pressure switch is triggered.
|
||||
*
|
||||
* @return true if pressure is low
|
||||
*/
|
||||
bool GetPressureSwitchValue() const;
|
||||
|
||||
/**
|
||||
* Query how much current the compressor is drawing.
|
||||
*
|
||||
* @return The current through the compressor, in amps
|
||||
*/
|
||||
double GetCompressorCurrent() const;
|
||||
|
||||
/**
|
||||
* Enables or disables automatically turning the compressor on when the
|
||||
* pressure is low.
|
||||
*
|
||||
* @param on Set to true to enable closed loop control of the compressor.
|
||||
* False to disable.
|
||||
*/
|
||||
void SetClosedLoopControl(bool on);
|
||||
|
||||
/**
|
||||
* Returns true if the compressor will automatically turn on when the
|
||||
* pressure is low.
|
||||
*
|
||||
* @return True if closed loop control of the compressor is enabled. False if
|
||||
* disabled.
|
||||
*/
|
||||
bool GetClosedLoopControl() const;
|
||||
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<PneumaticsBase> m_module;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -11,12 +11,13 @@
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
#include "frc/PneumaticsBase.h"
|
||||
#include "frc/PneumaticsModuleType.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* DoubleSolenoid class for running 2 channels of high voltage Digital Output
|
||||
* (PCM).
|
||||
* on a pneumatics module.
|
||||
*
|
||||
* The DoubleSolenoid class is typically used for pneumatics solenoids that
|
||||
* have two positions controlled by two separate channels.
|
||||
@@ -26,11 +27,27 @@ class DoubleSolenoid : public wpi::Sendable,
|
||||
public:
|
||||
enum Value { kOff, kForward, kReverse };
|
||||
|
||||
DoubleSolenoid(PneumaticsBase& module, int forwardChannel,
|
||||
int reverseChannel);
|
||||
DoubleSolenoid(PneumaticsBase* module, int forwardChannel,
|
||||
int reverseChannel);
|
||||
DoubleSolenoid(std::shared_ptr<PneumaticsBase> module, int forwardChannel,
|
||||
/**
|
||||
* Constructs a double solenoid for a specified module of a specific module
|
||||
* type.
|
||||
*
|
||||
* @param module The module of the solenoid module to use.
|
||||
* @param moduleType The module type to use.
|
||||
* @param forwardChannel The forward channel on the module to control.
|
||||
* @param reverseChannel The reverse channel on the module to control.
|
||||
*/
|
||||
DoubleSolenoid(int module, PneumaticsModuleType moduleType,
|
||||
int forwardChannel, int reverseChannel);
|
||||
|
||||
/**
|
||||
* Constructs a double solenoid for a default module of a specific module
|
||||
* type.
|
||||
*
|
||||
* @param moduleType The module type to use.
|
||||
* @param forwardChannel The forward channel on the module to control.
|
||||
* @param reverseChannel The reverse channel on the module to control.
|
||||
*/
|
||||
DoubleSolenoid(PneumaticsModuleType moduleType, int forwardChannel,
|
||||
int reverseChannel);
|
||||
|
||||
~DoubleSolenoid() override;
|
||||
@@ -100,12 +117,12 @@ class DoubleSolenoid : public wpi::Sendable,
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<PneumaticsBase> m_module;
|
||||
int m_forwardChannel; // The forward channel on the module to control.
|
||||
int m_reverseChannel; // The reverse channel on the module to control.
|
||||
int m_forwardMask; // The mask for the forward channel.
|
||||
int m_reverseMask; // The mask for the reverse channel.
|
||||
int m_mask;
|
||||
std::shared_ptr<PneumaticsBase> m_module;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -4,13 +4,30 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <units/time.h>
|
||||
|
||||
#include "frc/PneumaticsModuleType.h"
|
||||
|
||||
namespace frc {
|
||||
class Solenoid;
|
||||
class DoubleSolenoid;
|
||||
class Compressor;
|
||||
class PneumaticsBase {
|
||||
public:
|
||||
virtual ~PneumaticsBase() = default;
|
||||
|
||||
virtual bool GetCompressor() const = 0;
|
||||
|
||||
virtual bool GetPressureSwitch() const = 0;
|
||||
|
||||
virtual double GetCompressorCurrent() const = 0;
|
||||
|
||||
virtual void SetClosedLoopControl(bool on) = 0;
|
||||
|
||||
virtual bool GetClosedLoopControl() const = 0;
|
||||
|
||||
virtual void SetSolenoids(int mask, int values) = 0;
|
||||
|
||||
virtual int GetSolenoids() const = 0;
|
||||
@@ -28,5 +45,18 @@ class PneumaticsBase {
|
||||
virtual int CheckAndReserveSolenoids(int mask) = 0;
|
||||
|
||||
virtual void UnreserveSolenoids(int mask) = 0;
|
||||
|
||||
virtual bool ReserveCompressor() = 0;
|
||||
|
||||
virtual void UnreserveCompressor() = 0;
|
||||
|
||||
virtual Solenoid MakeSolenoid(int channel) = 0;
|
||||
virtual DoubleSolenoid MakeDoubleSolenoid(int forwardChannel,
|
||||
int reverseChannel) = 0;
|
||||
virtual Compressor MakeCompressor() = 0;
|
||||
|
||||
static std::shared_ptr<PneumaticsBase> GetForType(
|
||||
int module, PneumaticsModuleType moduleType);
|
||||
static int GetDefaultForType(PneumaticsModuleType moduleType);
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
@@ -4,43 +4,41 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <hal/Types.h>
|
||||
#include <wpi/DenseMap.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
#include "PneumaticsBase.h"
|
||||
|
||||
namespace frc {
|
||||
class PneumaticsControlModule
|
||||
: public PneumaticsBase,
|
||||
public wpi::Sendable,
|
||||
public wpi::SendableHelper<PneumaticsControlModule> {
|
||||
class PneumaticsControlModule : public PneumaticsBase {
|
||||
public:
|
||||
PneumaticsControlModule();
|
||||
explicit PneumaticsControlModule(int module);
|
||||
|
||||
~PneumaticsControlModule() override;
|
||||
~PneumaticsControlModule() override = default;
|
||||
|
||||
bool GetCompressor();
|
||||
bool GetCompressor() const override;
|
||||
|
||||
void SetClosedLoopControl(bool enabled);
|
||||
void SetClosedLoopControl(bool enabled) override;
|
||||
|
||||
bool GetClosedLoopControl();
|
||||
bool GetClosedLoopControl() const override;
|
||||
|
||||
bool GetPressureSwitch();
|
||||
bool GetPressureSwitch() const override;
|
||||
|
||||
double GetCompressorCurrent();
|
||||
double GetCompressorCurrent() const override;
|
||||
|
||||
bool GetCompressorCurrentTooHighFault();
|
||||
bool GetCompressorCurrentTooHighStickyFault();
|
||||
bool GetCompressorShortedFault();
|
||||
bool GetCompressorShortedStickyFault();
|
||||
bool GetCompressorNotConnectedFault();
|
||||
bool GetCompressorNotConnectedStickyFault();
|
||||
bool GetCompressorCurrentTooHighFault() const;
|
||||
bool GetCompressorCurrentTooHighStickyFault() const;
|
||||
bool GetCompressorShortedFault() const;
|
||||
bool GetCompressorShortedStickyFault() const;
|
||||
bool GetCompressorNotConnectedFault() const;
|
||||
bool GetCompressorNotConnectedStickyFault() const;
|
||||
|
||||
bool GetSolenoidVoltageFault();
|
||||
bool GetSolenoidVoltageStickyFault();
|
||||
bool GetSolenoidVoltageFault() const;
|
||||
bool GetSolenoidVoltageStickyFault() const;
|
||||
|
||||
void ClearAllStickyFaults();
|
||||
|
||||
@@ -62,12 +60,30 @@ class PneumaticsControlModule
|
||||
|
||||
void UnreserveSolenoids(int mask) override;
|
||||
|
||||
void InitSendable(wpi::SendableBuilder& builder) override;
|
||||
bool ReserveCompressor() override;
|
||||
|
||||
void UnreserveCompressor() override;
|
||||
|
||||
Solenoid MakeSolenoid(int channel) override;
|
||||
DoubleSolenoid MakeDoubleSolenoid(int forwardChannel,
|
||||
int reverseChannel) override;
|
||||
Compressor MakeCompressor() override;
|
||||
|
||||
private:
|
||||
class DataStore;
|
||||
friend class DataStore;
|
||||
friend class PneumaticsBase;
|
||||
PneumaticsControlModule(HAL_CTREPCMHandle handle, int module);
|
||||
|
||||
static std::shared_ptr<PneumaticsBase> GetForModule(int module);
|
||||
|
||||
std::shared_ptr<DataStore> m_dataStore;
|
||||
HAL_CTREPCMHandle m_handle;
|
||||
int m_module;
|
||||
hal::Handle<HAL_CTREPCMHandle> m_handle;
|
||||
uint32_t m_reservedMask{0};
|
||||
wpi::mutex m_reservedLock;
|
||||
|
||||
static wpi::mutex m_handleLock;
|
||||
static std::unique_ptr<wpi::DenseMap<int, std::weak_ptr<DataStore>>>
|
||||
m_handleMap;
|
||||
static std::weak_ptr<DataStore>& GetDataStore(int module);
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace frc {
|
||||
enum class PneumaticsModuleType { CTREPCM, REVPH };
|
||||
} // namespace frc
|
||||
@@ -12,20 +12,35 @@
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
|
||||
#include "frc/PneumaticsBase.h"
|
||||
#include "frc/PneumaticsModuleType.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Solenoid class for running high voltage Digital Output (PCM).
|
||||
* Solenoid class for running high voltage Digital Output on a pneumatics
|
||||
* module.
|
||||
*
|
||||
* The Solenoid class is typically used for pneumatics solenoids, but could be
|
||||
* used for any device within the current spec of the PCM.
|
||||
* used for any device within the current spec of the module.
|
||||
*/
|
||||
class Solenoid : public wpi::Sendable, public wpi::SendableHelper<Solenoid> {
|
||||
public:
|
||||
Solenoid(PneumaticsBase& module, int channel);
|
||||
Solenoid(PneumaticsBase* module, int channel);
|
||||
Solenoid(std::shared_ptr<PneumaticsBase> module, int channel);
|
||||
/**
|
||||
* Constructs a solenoid for a specified module and type.
|
||||
*
|
||||
* @param module The module ID to use.
|
||||
* @param moduleType The module type to use.
|
||||
* @param channel The channel the solenoid is on.
|
||||
*/
|
||||
Solenoid(int module, PneumaticsModuleType moduleType, int channel);
|
||||
|
||||
/**
|
||||
* Constructs a solenoid for a default module and specified type.
|
||||
*
|
||||
* @param moduleType The module type to use.
|
||||
* @param channel The channel the solenoid is on.
|
||||
*/
|
||||
Solenoid(PneumaticsModuleType moduleType, int channel);
|
||||
|
||||
~Solenoid() override;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user