[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

@@ -7,8 +7,8 @@
using namespace HatchConstants;
HatchSubsystem::HatchSubsystem()
: m_hatchSolenoid{m_pneumaticsModule, kHatchSolenoidPorts[0],
kHatchSolenoidPorts[1]} {}
: m_hatchSolenoid{frc::PneumaticsModuleType::CTREPCM,
kHatchSolenoidPorts[0], kHatchSolenoidPorts[1]} {}
void HatchSubsystem::GrabHatch() {
m_hatchSolenoid.Set(frc::DoubleSolenoid::kForward);

View File

@@ -29,6 +29,5 @@ class HatchSubsystem : public frc2::SubsystemBase {
private:
// Components (e.g. motor controllers and sensors) should generally be
// declared private and exposed only through public methods.
frc::PneumaticsControlModule m_pneumaticsModule;
frc::DoubleSolenoid m_hatchSolenoid;
};

View File

@@ -7,8 +7,8 @@
using namespace HatchConstants;
HatchSubsystem::HatchSubsystem()
: m_hatchSolenoid{m_pneumaticsModule, kHatchSolenoidPorts[0],
kHatchSolenoidPorts[1]} {}
: m_hatchSolenoid{frc::PneumaticsModuleType::CTREPCM,
kHatchSolenoidPorts[0], kHatchSolenoidPorts[1]} {}
void HatchSubsystem::GrabHatch() {
m_hatchSolenoid.Set(frc::DoubleSolenoid::kForward);

View File

@@ -29,6 +29,5 @@ class HatchSubsystem : public frc2::SubsystemBase {
private:
// Components (e.g. motor controllers and sensors) should generally be
// declared private and exposed only through public methods.
frc::PneumaticsControlModule m_pneumaticsModule;
frc::DoubleSolenoid m_hatchSolenoid;
};

View File

@@ -10,8 +10,6 @@
#include <frc/livewindow/LiveWindow.h>
#include <frc/smartdashboard/SmartDashboard.h>
#include "PneumaticsModule.h"
DriveTrain Robot::drivetrain;
Pivot Robot::pivot;
Collector Robot::collector;
@@ -83,11 +81,6 @@ void Robot::Log() {
drivetrain.GetRightEncoder().GetDistance());
}
frc::PneumaticsControlModule* pac::GetPneumatics() {
static frc::PneumaticsControlModule pcm;
return &pcm;
}
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();

View File

@@ -6,8 +6,6 @@
#include <frc/smartdashboard/SmartDashboard.h>
#include "PneumaticsModule.h"
Pneumatics::Pneumatics() : frc::Subsystem("Pneumatics") {
AddChild("Pressure Sensor", m_pressureSensor);
}
@@ -18,7 +16,7 @@ void Pneumatics::InitDefaultCommand() {
void Pneumatics::Start() {
#ifndef SIMULATION
pac::GetPneumatics()->SetClosedLoopControl(true);
m_compressor.Start();
#endif
}

View File

@@ -1,11 +0,0 @@
// 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 <frc/PneumaticsControlModule.h>
namespace pac {
frc::PneumaticsControlModule* GetPneumatics();
} // namespace pac

View File

@@ -4,7 +4,6 @@
#pragma once
#include <frc/PneumaticsControlModule.h>
#include <frc/TimedRobot.h>
#include <frc/commands/Command.h>
#include <frc/smartdashboard/SendableChooser.h>

View File

@@ -9,8 +9,6 @@
#include <frc/commands/Subsystem.h>
#include <frc/motorcontrol/PWMSparkMax.h>
#include "PneumaticsModule.h"
/**
* The Collector subsystem has one motor for the rollers, a limit switch for
* ball
@@ -70,6 +68,6 @@ class Collector : public frc::Subsystem {
// Subsystem devices
frc::PWMSparkMax m_rollerMotor{6};
frc::DigitalInput m_ballDetector{10};
frc::Solenoid m_piston{pac::GetPneumatics(), 1};
frc::Solenoid m_piston{frc::PneumaticsModuleType::CTREPCM, 1};
frc::DigitalInput m_openDetector{6};
};

View File

@@ -5,6 +5,7 @@
#pragma once
#include <frc/AnalogInput.h>
#include <frc/Compressor.h>
#include <frc/commands/Subsystem.h>
/**
@@ -41,5 +42,10 @@ class Pneumatics : public frc::Subsystem {
private:
frc::AnalogInput m_pressureSensor{3};
#ifndef SIMULATION
frc::Compressor m_compressor{
1, frc::PneumaticsModuleType::CTREPCM}; // TODO: (1, 14, 1, 8);
#endif
static constexpr double kMaxPressure = 2.55;
};

View File

@@ -9,8 +9,6 @@
#include <frc/Solenoid.h>
#include <frc/commands/Subsystem.h>
#include "PneumaticsModule.h"
/**
* The Shooter subsystem handles shooting. The mechanism for shooting is
* slightly complicated because it has to pneumatic cylinders for shooting, and
@@ -116,9 +114,9 @@ class Shooter : public frc::Subsystem {
private:
// Devices
frc::DoubleSolenoid m_piston1{pac::GetPneumatics(), 3, 4};
frc::DoubleSolenoid m_piston2{pac::GetPneumatics(), 5, 6};
frc::Solenoid m_latchPiston{pac::GetPneumatics(), 2};
frc::DoubleSolenoid m_piston1{frc::PneumaticsModuleType::CTREPCM, 3, 4};
frc::DoubleSolenoid m_piston2{frc::PneumaticsModuleType::CTREPCM, 5, 6};
frc::Solenoid m_latchPiston{1, frc::PneumaticsModuleType::CTREPCM, 2};
frc::DigitalInput m_piston1ReedSwitchFront{9};
frc::DigitalInput m_piston1ReedSwitchBack{11};
frc::DigitalInput m_hotGoalSensor{

View File

@@ -54,13 +54,12 @@ class Robot : public frc::TimedRobot {
private:
frc::Joystick m_stick{0};
frc::PneumaticsControlModule m_pneumaticsModule;
// Solenoid corresponds to a single solenoid.
frc::Solenoid m_solenoid{m_pneumaticsModule, 0};
frc::Solenoid m_solenoid{frc::PneumaticsModuleType::CTREPCM, 0};
// DoubleSolenoid corresponds to a double solenoid.
frc::DoubleSolenoid m_doubleSolenoid{m_pneumaticsModule, 1, 2};
frc::DoubleSolenoid m_doubleSolenoid{frc::PneumaticsModuleType::CTREPCM, 1,
2};
static constexpr int kSolenoidButton = 1;
static constexpr int kDoubleSolenoidForward = 2;