mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
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>
82 lines
2.3 KiB
C++
82 lines
2.3 KiB
C++
// 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/Solenoid.h"
|
|
|
|
#include <utility>
|
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
#include <wpi/NullDeleter.h>
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
#include <wpi/sendable/SendableRegistry.h>
|
|
|
|
#include "frc/Errors.h"
|
|
#include "frc/SensorUtil.h"
|
|
|
|
using namespace frc;
|
|
|
|
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_mask = 1 << channel;
|
|
|
|
if (m_module->CheckAndReserveSolenoids(m_mask) != 0) {
|
|
throw FRC_MakeError(err::ResourceAlreadyAllocated, "Channel {}", m_channel);
|
|
}
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_channel + 1,
|
|
m_module->GetModuleNumber() + 1);
|
|
wpi::SendableRegistry::AddLW(this, "Solenoid", m_module->GetModuleNumber(),
|
|
m_channel);
|
|
}
|
|
|
|
Solenoid::Solenoid(PneumaticsModuleType moduleType, int channel)
|
|
: Solenoid{PneumaticsBase::GetDefaultForType(moduleType), moduleType,
|
|
channel} {}
|
|
|
|
Solenoid::~Solenoid() {
|
|
m_module->UnreserveSolenoids(m_mask);
|
|
}
|
|
|
|
void Solenoid::Set(bool on) {
|
|
int value = on ? (0xFFFF & m_mask) : 0;
|
|
m_module->SetSolenoids(m_mask, value);
|
|
}
|
|
|
|
bool Solenoid::Get() const {
|
|
int currentAll = m_module->GetSolenoids();
|
|
return (currentAll & m_mask) != 0;
|
|
}
|
|
|
|
void Solenoid::Toggle() {
|
|
Set(!Get());
|
|
}
|
|
|
|
int Solenoid::GetChannel() const {
|
|
return m_channel;
|
|
}
|
|
|
|
bool Solenoid::IsDisabled() const {
|
|
return (m_module->GetSolenoidDisabledList() & m_mask) != 0;
|
|
}
|
|
|
|
void Solenoid::SetPulseDuration(units::second_t duration) {
|
|
m_module->SetOneShotDuration(m_channel, duration);
|
|
}
|
|
|
|
void Solenoid::StartPulse() {
|
|
m_module->FireOneShot(m_channel);
|
|
}
|
|
|
|
void Solenoid::InitSendable(wpi::SendableBuilder& builder) {
|
|
builder.SetSmartDashboardType("Solenoid");
|
|
builder.SetActuator(true);
|
|
builder.SetSafeState([=] { Set(false); });
|
|
builder.AddBooleanProperty(
|
|
"Value", [=] { return Get(); }, [=](bool value) { Set(value); });
|
|
}
|