[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:
Thad House
2021-09-16 18:50:27 -07:00
committed by GitHub
parent 906bfc8464
commit 60ede67abd
43 changed files with 1016 additions and 317 deletions

View File

@@ -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);
}