SCRIPT Move cc files

This commit is contained in:
PJ Reiniger
2025-11-07 19:55:39 -05:00
committed by Peter Johnson
parent 10b4a0c971
commit 7ca1be9bae
1197 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
// 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 <frc/PneumaticHub.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 busId, int module, PneumaticsModuleType moduleType)
: m_module{PneumaticsBase::GetForType(busId, module, moduleType)},
m_moduleType{moduleType} {
if (!m_module->ReserveCompressor()) {
throw FRC_MakeError(err::ResourceAlreadyAllocated, "{}", module);
}
m_module->EnableCompressorDigital();
m_module->ReportUsage("Compressor", "");
wpi::SendableRegistry::Add(this, "Compressor", module);
}
Compressor::Compressor(int busId, PneumaticsModuleType moduleType)
: Compressor{busId, PneumaticsBase::GetDefaultForType(moduleType),
moduleType} {}
Compressor::~Compressor() {
if (m_module) {
m_module->UnreserveCompressor();
}
}
bool Compressor::IsEnabled() const {
return m_module->GetCompressor();
}
bool Compressor::GetPressureSwitchValue() const {
return m_module->GetPressureSwitch();
}
units::ampere_t Compressor::GetCurrent() const {
return m_module->GetCompressorCurrent();
}
units::volt_t Compressor::GetAnalogVoltage() const {
return m_module->GetAnalogVoltage(0);
}
units::pounds_per_square_inch_t Compressor::GetPressure() const {
return m_module->GetPressure(0);
}
void Compressor::Disable() {
m_module->DisableCompressor();
}
void Compressor::EnableDigital() {
m_module->EnableCompressorDigital();
}
void Compressor::EnableAnalog(units::pounds_per_square_inch_t minPressure,
units::pounds_per_square_inch_t maxPressure) {
m_module->EnableCompressorAnalog(minPressure, maxPressure);
}
void Compressor::EnableHybrid(units::pounds_per_square_inch_t minPressure,
units::pounds_per_square_inch_t maxPressure) {
m_module->EnableCompressorHybrid(minPressure, maxPressure);
}
CompressorConfigType Compressor::GetConfigType() const {
return m_module->GetCompressorConfigType();
}
void Compressor::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("Compressor");
builder.AddBooleanProperty(
"Enabled", [this] { return IsEnabled(); }, nullptr);
builder.AddBooleanProperty(
"Pressure switch", [this] { return GetPressureSwitchValue(); }, nullptr);
builder.AddDoubleProperty(
"Current (A)", [this] { return GetCurrent().value(); }, nullptr);
// These are not supported by the CTRE PCM
if (m_moduleType == PneumaticsModuleType::REVPH) {
builder.AddDoubleProperty(
"Analog Voltage", [this] { return GetAnalogVoltage().value(); },
nullptr);
builder.AddDoubleProperty(
"Pressure (PSI)", [this] { return GetPressure().value(); }, nullptr);
}
}

View File

@@ -0,0 +1,151 @@
// 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/DoubleSolenoid.h"
#include <utility>
#include <hal/Ports.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;
DoubleSolenoid::DoubleSolenoid(int busId, int module,
PneumaticsModuleType moduleType,
int forwardChannel, int reverseChannel)
: m_module{PneumaticsBase::GetForType(busId, module, moduleType)},
m_forwardChannel{forwardChannel},
m_reverseChannel{reverseChannel} {
if (!m_module->CheckSolenoidChannel(m_forwardChannel)) {
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}",
m_forwardChannel);
}
if (!m_module->CheckSolenoidChannel(m_reverseChannel)) {
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}",
m_reverseChannel);
}
m_forwardMask = 1 << forwardChannel;
m_reverseMask = 1 << reverseChannel;
m_mask = m_forwardMask | m_reverseMask;
int allocMask = m_module->CheckAndReserveSolenoids(m_mask);
if (allocMask != 0) {
if (allocMask == m_mask) {
throw FRC_MakeError(err::ResourceAlreadyAllocated, "Channels {} and {}",
m_forwardChannel, m_reverseChannel);
} else if (allocMask == m_forwardMask) {
throw FRC_MakeError(err::ResourceAlreadyAllocated, "Channel {}",
m_forwardChannel);
} else {
throw FRC_MakeError(err::ResourceAlreadyAllocated, "Channel {}",
m_reverseChannel);
}
}
m_module->ReportUsage(
fmt::format("Solenoid[{},{}]", m_forwardChannel, m_reverseChannel),
"DoubleSolenoid");
wpi::SendableRegistry::Add(this, "DoubleSolenoid",
m_module->GetModuleNumber(), m_forwardChannel);
}
DoubleSolenoid::DoubleSolenoid(int busId, PneumaticsModuleType moduleType,
int forwardChannel, int reverseChannel)
: DoubleSolenoid{busId, PneumaticsBase::GetDefaultForType(moduleType),
moduleType, forwardChannel, reverseChannel} {}
DoubleSolenoid::~DoubleSolenoid() {
if (m_module) {
m_module->UnreserveSolenoids(m_mask);
}
}
void DoubleSolenoid::Set(Value value) {
int setValue = 0;
switch (value) {
case kOff:
setValue = 0;
break;
case kForward:
setValue = m_forwardMask;
break;
case kReverse:
setValue = m_reverseMask;
break;
}
m_module->SetSolenoids(m_mask, setValue);
}
DoubleSolenoid::Value DoubleSolenoid::Get() const {
auto values = m_module->GetSolenoids();
if ((values & m_forwardMask) != 0) {
return Value::kForward;
} else if ((values & m_reverseMask) != 0) {
return Value::kReverse;
} else {
return Value::kOff;
}
}
void DoubleSolenoid::Toggle() {
Value value = Get();
if (value == kForward) {
Set(kReverse);
} else if (value == kReverse) {
Set(kForward);
}
}
int DoubleSolenoid::GetFwdChannel() const {
return m_forwardChannel;
}
int DoubleSolenoid::GetRevChannel() const {
return m_reverseChannel;
}
bool DoubleSolenoid::IsFwdSolenoidDisabled() const {
return (m_module->GetSolenoidDisabledList() & m_forwardMask) != 0;
}
bool DoubleSolenoid::IsRevSolenoidDisabled() const {
return (m_module->GetSolenoidDisabledList() & m_reverseMask) != 0;
}
void DoubleSolenoid::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("Double Solenoid");
builder.SetActuator(true);
builder.AddSmallStringProperty(
"Value",
[=, this](wpi::SmallVectorImpl<char>& buf) -> std::string_view {
switch (Get()) {
case kForward:
return "Forward";
case kReverse:
return "Reverse";
default:
return "Off";
}
},
[=, this](std::string_view value) {
Value lvalue = kOff;
if (value == "Forward") {
lvalue = kForward;
} else if (value == "Reverse") {
lvalue = kReverse;
}
Set(lvalue);
});
}

View File

@@ -0,0 +1,447 @@
// 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/PneumaticHub.h"
#include <array>
#include <cstdio>
#include <memory>
#include <string>
#include <fmt/format.h>
#include <hal/Ports.h>
#include <hal/REVPH.h>
#include <hal/UsageReporting.h>
#include <wpi/NullDeleter.h>
#include <wpi/StackTrace.h>
#include "frc/Compressor.h"
#include "frc/DoubleSolenoid.h"
#include "frc/Errors.h"
#include "frc/RobotBase.h"
#include "frc/SensorUtil.h"
#include "frc/Solenoid.h"
using namespace frc;
/** Converts volts to PSI per the REV Analog Pressure Sensor datasheet. */
units::pounds_per_square_inch_t VoltsToPSI(units::volt_t sensorVoltage,
units::volt_t supplyVoltage) {
return units::pounds_per_square_inch_t{
250 * (sensorVoltage.value() / supplyVoltage.value()) - 25};
}
/** Converts PSI to volts per the REV Analog Pressure Sensor datasheet. */
units::volt_t PSIToVolts(units::pounds_per_square_inch_t pressure,
units::volt_t supplyVoltage) {
return units::volt_t{supplyVoltage.value() *
(0.004 * pressure.value() + 0.1)};
}
wpi::mutex PneumaticHub::m_handleLock;
std::unique_ptr<wpi::DenseMap<int, std::weak_ptr<PneumaticHub::DataStore>>[]>
PneumaticHub::m_handleMaps = nullptr;
// Always called under lock, so we can avoid the double lock from the magic
// static
std::weak_ptr<PneumaticHub::DataStore>& PneumaticHub::GetDataStore(int busId,
int module) {
int32_t numBuses = HAL_GetNumCanBuses();
FRC_AssertMessage(busId >= 0 && busId < numBuses,
"Bus {} out of range. Must be [0-{}).", busId, numBuses);
if (!m_handleMaps) {
m_handleMaps = std::make_unique<
wpi::DenseMap<int, std::weak_ptr<PneumaticHub::DataStore>>[]>(numBuses);
}
return m_handleMaps[busId][module];
}
class PneumaticHub::DataStore {
public:
explicit DataStore(int busId, int module, const char* stackTrace) {
int32_t status = 0;
HAL_REVPHHandle handle =
HAL_InitializeREVPH(busId, module, stackTrace, &status);
FRC_CheckErrorStatus(status, "Module {}", module);
m_moduleObject = PneumaticHub{busId, handle, module};
m_moduleObject.m_dataStore =
std::shared_ptr<DataStore>{this, wpi::NullDeleter<DataStore>()};
auto version = m_moduleObject.GetVersion();
// Check PH firmware version
if (version.FirmwareMajor > 0 && version.FirmwareMajor < 22) {
throw FRC_MakeError(
err::AssertionFailure,
"The Pneumatic Hub has firmware version {}.{}.{}, and must be "
"updated to version 2022.0.0 or later using the REV Hardware Client",
version.FirmwareMajor, version.FirmwareMinor, version.FirmwareFix);
}
}
~DataStore() noexcept { HAL_FreeREVPH(m_moduleObject.m_handle); }
DataStore(DataStore&&) = delete;
DataStore& operator=(DataStore&&) = delete;
private:
friend class PneumaticHub;
uint32_t m_reservedMask{0};
bool m_compressorReserved{false};
wpi::mutex m_reservedLock;
PneumaticHub m_moduleObject{0, HAL_kInvalidHandle, 0};
std::array<units::millisecond_t, 16> m_oneShotDurMs{0_ms};
};
PneumaticHub::PneumaticHub(int busId)
: PneumaticHub{busId, SensorUtil::GetDefaultREVPHModule()} {}
PneumaticHub::PneumaticHub(int busId, int module) {
std::string stackTrace = wpi::GetStackTrace(1);
std::scoped_lock lock(m_handleLock);
auto& res = GetDataStore(busId, module);
m_dataStore = res.lock();
if (!m_dataStore) {
m_dataStore =
std::make_shared<DataStore>(busId, module, stackTrace.c_str());
res = m_dataStore;
}
m_handle = m_dataStore->m_moduleObject.m_handle;
m_module = module;
}
PneumaticHub::PneumaticHub(int /* busId */, HAL_REVPHHandle handle, int module)
: m_handle{handle}, m_module{module} {}
bool PneumaticHub::GetCompressor() const {
int32_t status = 0;
auto result = HAL_GetREVPHCompressor(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
void PneumaticHub::DisableCompressor() {
int32_t status = 0;
HAL_SetREVPHClosedLoopControlDisabled(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
}
void PneumaticHub::EnableCompressorDigital() {
int32_t status = 0;
HAL_SetREVPHClosedLoopControlDigital(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
}
void PneumaticHub::EnableCompressorAnalog(
units::pounds_per_square_inch_t minPressure,
units::pounds_per_square_inch_t maxPressure) {
if (minPressure >= maxPressure) {
throw FRC_MakeError(err::InvalidParameter,
"maxPressure must be greater than minPressure");
}
if (minPressure < 0_psi || minPressure > 120_psi) {
throw FRC_MakeError(err::ParameterOutOfRange,
"minPressure must be between 0 and 120 PSI, got {}",
minPressure);
}
if (maxPressure < 0_psi || maxPressure > 120_psi) {
throw FRC_MakeError(err::ParameterOutOfRange,
"maxPressure must be between 0 and 120 PSI, got {}",
maxPressure);
}
// Send the voltage as it would be if the 5V rail was at exactly 5V.
// The firmware will compensate for the real 5V rail voltage, which
// can fluctuate somewhat over time.
units::volt_t minAnalogVoltage = PSIToVolts(minPressure, 5_V);
units::volt_t maxAnalogVoltage = PSIToVolts(maxPressure, 5_V);
int32_t status = 0;
HAL_SetREVPHClosedLoopControlAnalog(m_handle, minAnalogVoltage.value(),
maxAnalogVoltage.value(), &status);
FRC_ReportError(status, "Module {}", m_module);
}
void PneumaticHub::EnableCompressorHybrid(
units::pounds_per_square_inch_t minPressure,
units::pounds_per_square_inch_t maxPressure) {
if (minPressure >= maxPressure) {
throw FRC_MakeError(err::InvalidParameter,
"maxPressure must be greater than minPressure");
}
if (minPressure < 0_psi || minPressure > 120_psi) {
throw FRC_MakeError(err::ParameterOutOfRange,
"minPressure must be between 0 and 120 PSI, got {}",
minPressure);
}
if (maxPressure < 0_psi || maxPressure > 120_psi) {
throw FRC_MakeError(err::ParameterOutOfRange,
"maxPressure must be between 0 and 120 PSI, got {}",
maxPressure);
}
// Send the voltage as it would be if the 5V rail was at exactly 5V.
// The firmware will compensate for the real 5V rail voltage, which
// can fluctuate somewhat over time.
units::volt_t minAnalogVoltage = PSIToVolts(minPressure, 5_V);
units::volt_t maxAnalogVoltage = PSIToVolts(maxPressure, 5_V);
int32_t status = 0;
HAL_SetREVPHClosedLoopControlHybrid(m_handle, minAnalogVoltage.value(),
maxAnalogVoltage.value(), &status);
FRC_ReportError(status, "Module {}", m_module);
}
CompressorConfigType PneumaticHub::GetCompressorConfigType() const {
int32_t status = 0;
auto result = HAL_GetREVPHCompressorConfig(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return static_cast<CompressorConfigType>(result);
}
bool PneumaticHub::GetPressureSwitch() const {
int32_t status = 0;
auto result = HAL_GetREVPHPressureSwitch(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
units::ampere_t PneumaticHub::GetCompressorCurrent() const {
int32_t status = 0;
auto result = HAL_GetREVPHCompressorCurrent(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return units::ampere_t{result};
}
void PneumaticHub::SetSolenoids(int mask, int values) {
int32_t status = 0;
HAL_SetREVPHSolenoids(m_handle, mask, values, &status);
FRC_ReportError(status, "Module {}", m_module);
}
int PneumaticHub::GetSolenoids() const {
int32_t status = 0;
auto result = HAL_GetREVPHSolenoids(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
int PneumaticHub::GetModuleNumber() const {
return m_module;
}
int PneumaticHub::GetSolenoidDisabledList() const {
int32_t status = 0;
auto result = HAL_GetREVPHSolenoidDisabledList(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
void PneumaticHub::FireOneShot(int index) {
int32_t status = 0;
HAL_FireREVPHOneShot(m_handle, index,
m_dataStore->m_oneShotDurMs[index].value(), &status);
FRC_ReportError(status, "Module {}", m_module);
}
void PneumaticHub::SetOneShotDuration(int index, units::second_t duration) {
m_dataStore->m_oneShotDurMs[index] = duration;
}
bool PneumaticHub::CheckSolenoidChannel(int channel) const {
return HAL_CheckREVPHSolenoidChannel(channel);
}
int PneumaticHub::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 PneumaticHub::UnreserveSolenoids(int mask) {
std::scoped_lock lock{m_dataStore->m_reservedLock};
m_dataStore->m_reservedMask &= ~(static_cast<uint32_t>(mask));
}
bool PneumaticHub::ReserveCompressor() {
std::scoped_lock lock{m_dataStore->m_reservedLock};
if (m_dataStore->m_compressorReserved) {
return false;
}
m_dataStore->m_compressorReserved = true;
return true;
}
void PneumaticHub::UnreserveCompressor() {
std::scoped_lock lock{m_dataStore->m_reservedLock};
m_dataStore->m_compressorReserved = false;
}
PneumaticHub::Version PneumaticHub::GetVersion() const {
int32_t status = 0;
HAL_REVPHVersion halVersions;
std::memset(&halVersions, 0, sizeof(halVersions));
HAL_GetREVPHVersion(m_handle, &halVersions, &status);
FRC_ReportError(status, "Module {}", m_module);
PneumaticHub::Version versions;
static_assert(sizeof(halVersions) == sizeof(versions));
static_assert(std::is_standard_layout_v<decltype(versions)>);
static_assert(std::is_trivial_v<decltype(versions)>);
std::memcpy(&versions, &halVersions, sizeof(versions));
return versions;
}
PneumaticHub::Faults PneumaticHub::GetFaults() const {
int32_t status = 0;
HAL_REVPHFaults halFaults;
std::memset(&halFaults, 0, sizeof(halFaults));
HAL_GetREVPHFaults(m_handle, &halFaults, &status);
FRC_ReportError(status, "Module {}", m_module);
PneumaticHub::Faults faults;
static_assert(sizeof(halFaults) == sizeof(faults));
static_assert(std::is_standard_layout_v<decltype(faults)>);
static_assert(std::is_trivial_v<decltype(faults)>);
std::memcpy(&faults, &halFaults, sizeof(faults));
return faults;
}
PneumaticHub::StickyFaults PneumaticHub::GetStickyFaults() const {
int32_t status = 0;
HAL_REVPHStickyFaults halStickyFaults;
std::memset(&halStickyFaults, 0, sizeof(halStickyFaults));
HAL_GetREVPHStickyFaults(m_handle, &halStickyFaults, &status);
FRC_ReportError(status, "Module {}", m_module);
PneumaticHub::StickyFaults stickyFaults;
static_assert(sizeof(halStickyFaults) == sizeof(stickyFaults));
static_assert(std::is_standard_layout_v<decltype(stickyFaults)>);
static_assert(std::is_trivial_v<decltype(stickyFaults)>);
std::memcpy(&stickyFaults, &halStickyFaults, sizeof(stickyFaults));
return stickyFaults;
}
bool PneumaticHub::Faults::GetChannelFault(int channel) const {
switch (channel) {
case 0:
return Channel0Fault != 0;
case 1:
return Channel1Fault != 0;
case 2:
return Channel2Fault != 0;
case 3:
return Channel3Fault != 0;
case 4:
return Channel4Fault != 0;
case 5:
return Channel5Fault != 0;
case 6:
return Channel6Fault != 0;
case 7:
return Channel7Fault != 0;
case 8:
return Channel8Fault != 0;
case 9:
return Channel9Fault != 0;
case 10:
return Channel10Fault != 0;
case 11:
return Channel11Fault != 0;
case 12:
return Channel12Fault != 0;
case 13:
return Channel13Fault != 0;
case 14:
return Channel14Fault != 0;
case 15:
return Channel15Fault != 0;
default:
throw FRC_MakeError(err::ChannelIndexOutOfRange,
"Pneumatics fault channel out of bounds!");
}
}
void PneumaticHub::ClearStickyFaults() {
int32_t status = 0;
HAL_ClearREVPHStickyFaults(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
}
units::volt_t PneumaticHub::GetInputVoltage() const {
int32_t status = 0;
auto voltage = HAL_GetREVPHVoltage(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return units::volt_t{voltage};
}
units::volt_t PneumaticHub::Get5VRegulatedVoltage() const {
int32_t status = 0;
auto voltage = HAL_GetREVPH5VVoltage(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return units::volt_t{voltage};
}
units::ampere_t PneumaticHub::GetSolenoidsTotalCurrent() const {
int32_t status = 0;
auto current = HAL_GetREVPHSolenoidCurrent(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return units::ampere_t{current};
}
units::volt_t PneumaticHub::GetSolenoidsVoltage() const {
int32_t status = 0;
auto voltage = HAL_GetREVPHSolenoidVoltage(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return units::volt_t{voltage};
}
units::volt_t PneumaticHub::GetAnalogVoltage(int channel) const {
int32_t status = 0;
auto voltage = HAL_GetREVPHAnalogVoltage(m_handle, channel, &status);
FRC_ReportError(status, "Module {}", m_module);
return units::volt_t{voltage};
}
units::pounds_per_square_inch_t PneumaticHub::GetPressure(int channel) const {
int32_t status = 0;
auto sensorVoltage = HAL_GetREVPHAnalogVoltage(m_handle, channel, &status);
FRC_ReportError(status, "Module {}", m_module);
auto supplyVoltage = HAL_GetREVPH5VVoltage(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return VoltsToPSI(units::volt_t{sensorVoltage}, units::volt_t{supplyVoltage});
}
Solenoid PneumaticHub::MakeSolenoid(int channel) {
return Solenoid{m_module, PneumaticsModuleType::REVPH, channel};
}
DoubleSolenoid PneumaticHub::MakeDoubleSolenoid(int forwardChannel,
int reverseChannel) {
return DoubleSolenoid{m_module, PneumaticsModuleType::REVPH, forwardChannel,
reverseChannel};
}
Compressor PneumaticHub::MakeCompressor() {
return Compressor{m_module, PneumaticsModuleType::REVPH};
}
void PneumaticHub::ReportUsage(std::string_view device, std::string_view data) {
HAL_ReportUsage(fmt::format("PH[{}]/{}", m_module, device), data);
}
std::shared_ptr<PneumaticsBase> PneumaticHub::GetForModule(int busId,
int module) {
std::string stackTrace = wpi::GetStackTrace(1);
std::scoped_lock lock(m_handleLock);
auto& res = GetDataStore(busId, module);
std::shared_ptr<DataStore> dataStore = res.lock();
if (!dataStore) {
dataStore = std::make_shared<DataStore>(busId, module, stackTrace.c_str());
res = dataStore;
}
return std::shared_ptr<PneumaticsBase>{dataStore, &dataStore->m_moduleObject};
}

View File

@@ -0,0 +1,50 @@
// 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 <memory>
#include <hal/REVPH.h>
#include "frc/Errors.h"
#include "frc/PneumaticHub.h"
#include "frc/PneumaticsControlModule.h"
#include "frc/SensorUtil.h"
using namespace frc;
static_assert(
static_cast<int>(CompressorConfigType::Disabled) ==
HAL_REVPHCompressorConfigType::HAL_REVPHCompressorConfigType_kDisabled);
static_assert(
static_cast<int>(CompressorConfigType::Digital) ==
HAL_REVPHCompressorConfigType::HAL_REVPHCompressorConfigType_kDigital);
static_assert(
static_cast<int>(CompressorConfigType::Analog) ==
HAL_REVPHCompressorConfigType::HAL_REVPHCompressorConfigType_kAnalog);
static_assert(
static_cast<int>(CompressorConfigType::Hybrid) ==
HAL_REVPHCompressorConfigType::HAL_REVPHCompressorConfigType_kHybrid);
std::shared_ptr<PneumaticsBase> PneumaticsBase::GetForType(
int busId, int module, PneumaticsModuleType moduleType) {
if (moduleType == PneumaticsModuleType::CTREPCM) {
return PneumaticsControlModule::GetForModule(busId, module);
} else if (moduleType == PneumaticsModuleType::REVPH) {
return PneumaticHub::GetForModule(busId, module);
}
throw FRC_MakeError(err::InvalidParameter, "{}",
static_cast<int>(moduleType));
}
int PneumaticsBase::GetDefaultForType(PneumaticsModuleType moduleType) {
if (moduleType == PneumaticsModuleType::CTREPCM) {
return SensorUtil::GetDefaultCTREPCMModule();
} else if (moduleType == PneumaticsModuleType::REVPH) {
return SensorUtil::GetDefaultREVPHModule();
}
throw FRC_MakeError(err::InvalidParameter, "{}",
static_cast<int>(moduleType));
}

View File

@@ -0,0 +1,318 @@
// 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 <memory>
#include <string>
#include <fmt/format.h>
#include <hal/CTREPCM.h>
#include <hal/Ports.h>
#include <hal/UsageReporting.h>
#include <wpi/NullDeleter.h>
#include <wpi/StackTrace.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_handleMaps = nullptr;
// Always called under lock, so we can avoid the double lock from the magic
// static
std::weak_ptr<PneumaticsControlModule::DataStore>&
PneumaticsControlModule::GetDataStore(int busId, int module) {
int32_t numBuses = HAL_GetNumCanBuses();
FRC_AssertMessage(busId >= 0 && busId < numBuses,
"Bus {} out of range. Must be [0-{}).", busId, numBuses);
if (!m_handleMaps) {
m_handleMaps = std::make_unique<wpi::DenseMap<
int, std::weak_ptr<PneumaticsControlModule::DataStore>>[]>(numBuses);
}
return m_handleMaps[busId][module];
}
class PneumaticsControlModule::DataStore {
public:
explicit DataStore(int busId, int module, const char* stackTrace) {
int32_t status = 0;
HAL_CTREPCMHandle handle =
HAL_InitializeCTREPCM(busId, module, stackTrace, &status);
FRC_CheckErrorStatus(status, "Module {}", module);
m_moduleObject = PneumaticsControlModule{busId, 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{0, HAL_kInvalidHandle, 0};
};
PneumaticsControlModule::PneumaticsControlModule(int busId)
: PneumaticsControlModule{busId, SensorUtil::GetDefaultCTREPCMModule()} {}
PneumaticsControlModule::PneumaticsControlModule(int busId, int module) {
std::string stackTrace = wpi::GetStackTrace(1);
std::scoped_lock lock(m_handleLock);
auto& res = GetDataStore(busId, module);
m_dataStore = res.lock();
if (!m_dataStore) {
m_dataStore =
std::make_shared<DataStore>(busId, module, stackTrace.c_str());
res = m_dataStore;
}
m_handle = m_dataStore->m_moduleObject.m_handle;
m_module = module;
}
PneumaticsControlModule::PneumaticsControlModule(int /* busId */,
HAL_CTREPCMHandle handle,
int module)
: m_handle{handle}, m_module{module} {}
bool PneumaticsControlModule::GetCompressor() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMCompressor(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
void PneumaticsControlModule::DisableCompressor() {
int32_t status = 0;
HAL_SetCTREPCMClosedLoopControl(m_handle, false, &status);
FRC_ReportError(status, "Module {}", m_module);
}
void PneumaticsControlModule::EnableCompressorDigital() {
int32_t status = 0;
HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status);
FRC_ReportError(status, "Module {}", m_module);
}
void PneumaticsControlModule::EnableCompressorAnalog(
units::pounds_per_square_inch_t minPressure,
units::pounds_per_square_inch_t maxPressure) {
int32_t status = 0;
HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status);
FRC_ReportError(status, "Module {}", m_module);
}
void PneumaticsControlModule::EnableCompressorHybrid(
units::pounds_per_square_inch_t minPressure,
units::pounds_per_square_inch_t maxPressure) {
int32_t status = 0;
HAL_SetCTREPCMClosedLoopControl(m_handle, true, &status);
FRC_ReportError(status, "Module {}", m_module);
}
CompressorConfigType PneumaticsControlModule::GetCompressorConfigType() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMClosedLoopControl(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result ? CompressorConfigType::Digital
: CompressorConfigType::Disabled;
}
bool PneumaticsControlModule::GetPressureSwitch() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMPressureSwitch(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
units::ampere_t PneumaticsControlModule::GetCompressorCurrent() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMCompressorCurrent(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return units::ampere_t{result};
}
bool PneumaticsControlModule::GetCompressorCurrentTooHighFault() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMCompressorCurrentTooHighFault(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
bool PneumaticsControlModule::GetCompressorCurrentTooHighStickyFault() const {
int32_t status = 0;
auto result =
HAL_GetCTREPCMCompressorCurrentTooHighStickyFault(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
bool PneumaticsControlModule::GetCompressorShortedFault() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMCompressorShortedFault(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
bool PneumaticsControlModule::GetCompressorShortedStickyFault() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMCompressorShortedStickyFault(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
bool PneumaticsControlModule::GetCompressorNotConnectedFault() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMCompressorNotConnectedFault(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
bool PneumaticsControlModule::GetCompressorNotConnectedStickyFault() const {
int32_t status = 0;
auto result =
HAL_GetCTREPCMCompressorNotConnectedStickyFault(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
bool PneumaticsControlModule::GetSolenoidVoltageFault() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMSolenoidVoltageFault(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
bool PneumaticsControlModule::GetSolenoidVoltageStickyFault() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMSolenoidVoltageStickyFault(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
void PneumaticsControlModule::ClearAllStickyFaults() {
int32_t status = 0;
HAL_ClearAllCTREPCMStickyFaults(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
}
void PneumaticsControlModule::SetSolenoids(int mask, int values) {
int32_t status = 0;
HAL_SetCTREPCMSolenoids(m_handle, mask, values, &status);
FRC_ReportError(status, "Module {}", m_module);
}
int PneumaticsControlModule::GetSolenoids() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMSolenoids(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
int PneumaticsControlModule::GetModuleNumber() const {
return m_module;
}
int PneumaticsControlModule::GetSolenoidDisabledList() const {
int32_t status = 0;
auto result = HAL_GetCTREPCMSolenoidDisabledList(m_handle, &status);
FRC_ReportError(status, "Module {}", m_module);
return result;
}
void PneumaticsControlModule::FireOneShot(int index) {
int32_t status = 0;
HAL_FireCTREPCMOneShot(m_handle, index, &status);
FRC_ReportError(status, "Module {}", m_module);
}
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);
FRC_ReportError(status, "Module {}", m_module);
}
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;
}
units::volt_t PneumaticsControlModule::GetAnalogVoltage(int channel) const {
return 0_V;
}
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(
int busId, int module) {
std::string stackTrace = wpi::GetStackTrace(1);
std::scoped_lock lock(m_handleLock);
auto& res = GetDataStore(busId, module);
std::shared_ptr<DataStore> dataStore = res.lock();
if (!dataStore) {
dataStore = std::make_shared<DataStore>(busId, module, stackTrace.c_str());
res = dataStore;
}
return std::shared_ptr<PneumaticsBase>{dataStore, &dataStore->m_moduleObject};
}

View File

@@ -0,0 +1,82 @@
// 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 <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 busId, int module, PneumaticsModuleType moduleType,
int channel)
: m_module{PneumaticsBase::GetForType(busId, 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);
}
m_module->ReportUsage(fmt::format("Solenoid[{}]", m_channel), "Solenoid");
wpi::SendableRegistry::Add(this, "Solenoid", m_module->GetModuleNumber(),
m_channel);
}
Solenoid::Solenoid(int busId, PneumaticsModuleType moduleType, int channel)
: Solenoid{busId, PneumaticsBase::GetDefaultForType(moduleType), moduleType,
channel} {}
Solenoid::~Solenoid() {
if (m_module) {
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.AddBooleanProperty(
"Value", [=, this] { return Get(); },
[=, this](bool value) { Set(value); });
}