[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

@@ -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