[sim] Add DoubleSolenoidSim and SolenoidSim classes (#3177)

This commit is contained in:
Starlight220
2021-02-17 04:03:57 +02:00
committed by GitHub
parent cb7f39afa1
commit d241bc81ae
15 changed files with 747 additions and 0 deletions

View File

@@ -74,6 +74,20 @@ class DoubleSolenoid : public SolenoidBase,
*/
void Toggle();
/**
* Get the forward channel.
*
* @return the forward channel.
*/
int GetFwdChannel() const;
/**
* Get the reverse channel.
*
* @return the reverse channel.
*/
int GetRevChannel() const;
/**
* Check if the forward solenoid is blacklisted.
*

View File

@@ -66,6 +66,11 @@ class Solenoid : public SolenoidBase,
*/
void Toggle();
/**
* Get the channel this solenoid is connected to.
*/
int GetChannel() const;
/**
* Check if solenoid is blacklisted.
*

View File

@@ -14,6 +14,13 @@ namespace frc {
*/
class SolenoidBase : public ErrorBase {
public:
/**
* Get the CAN ID of the module this solenoid is connected to.
*
* @return the module number.
*/
int GetModuleNumber() const;
/**
* Read all 8 solenoids as a single byte
*

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 "frc/DoubleSolenoid.h"
#include "frc/simulation/CallbackStore.h"
#include "frc/simulation/PCMSim.h"
namespace frc::sim {
/**
* Class to control a simulated Pneumatic Control Module (PCM).
*/
class DoubleSolenoidSim {
public:
/**
* Constructs for a solenoid on the default PCM.
*
* @param channel the solenoid channel.
*/
DoubleSolenoidSim(int fwd, int rev);
/**
* Constructs for a solenoid on the given PCM.
*
* @param pcm the PCM the solenoid is connected to.
* @param channel the solenoid channel.
*/
DoubleSolenoidSim(int module, int fwd, int rev);
/**
* Constructs from a PCMSim object.
*
* @param pcm the PCM the solenoid is connected to.
*/
DoubleSolenoidSim(PCMSim& pcm, int fwd, int rev);
/**
* Constructs for the given solenoid.
*
* @param solenoid the solenoid to simulate.
*/
explicit DoubleSolenoidSim(DoubleSolenoid& solenoid);
/**
* Register a callback to be run when the forward solenoid is initialized.
*
* @param callback the callback
* @param initialNotify should the callback be run with the initial state
* @return the {@link CallbackStore} object associated with this callback.
*/
[[nodiscard]] std::unique_ptr<CallbackStore> RegisterFwdInitializedCallback(
NotifyCallback callback, bool initialNotify);
/**
* Check if the forward solenoid has been initialized.
*
* @return true if initialized
*/
bool GetFwdInitialized() const;
/**
* Define whether the forward solenoid has been initialized.
*
* @param initialized whether the solenoid is intiialized.
*/
void SetFwdInitialized(bool initialized);
/**
* Register a callback to be run when the reverse solenoid is initialized.
*
* @param callback the callback
* @param initialNotify should the callback be run with the initial state
* @return the {@link CallbackStore} object associated with this callback.
*/
[[nodiscard]] std::unique_ptr<CallbackStore> RegisterRevInitializedCallback(
NotifyCallback callback, bool initialNotify);
/**
* Define whether the reverse solenoid has been initialized.
*
* @param initialized whether the solenoid is intiialized.
*/
void SetRevInitialized(bool initialized);
/**
* Check if the reverse solenoid has been initialized.
*
* @return true if initialized
*/
bool GetRevInitialized() const;
/**
* Set the value of the double solenoid output.
*
* @param value The value to set (Off, Forward, Reverse)
*/
void Set(DoubleSolenoid::Value value);
/**
* Check the value of the double solenoid output.
*
* @return the output value of the double solenoid.
*/
DoubleSolenoid::Value Get() const;
private:
PCMSim m_pcm;
int m_fwd;
int m_rev;
};
} // namespace frc::sim

View File

@@ -0,0 +1,104 @@
// 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 "frc/Solenoid.h"
#include "frc/simulation/CallbackStore.h"
#include "frc/simulation/PCMSim.h"
namespace frc::sim {
/**
* Class to control a simulated Pneumatic Control Module (PCM).
*/
class SolenoidSim {
public:
/**
* Constructs for a solenoid on the default PCM.
*
* @param channel the solenoid channel.
*/
explicit SolenoidSim(int channel);
/**
* Constructs for the given solenoid.
*
* @param doubleSolenoid the solenoid to simulate.
*/
explicit SolenoidSim(Solenoid& solenoid);
/**
* Constructs for a solenoid.
*
* @param module the CAN ID of the PCM the solenoid is connected to.
* @param channel the solenoid channel.
*
* @see PCMSim#PCMSim(int)
*/
SolenoidSim(int module, int channel);
/**
* Constructs for a solenoid on the given PCM.
*
* @param pcm the PCM the solenoid is connected to.
* @param channel the solenoid channel.
*/
SolenoidSim(PCMSim& pcm, int channel);
/**
* Register a callback to be run when this solenoid is initialized.
*
* @param callback the callback
* @param initialNotify should the callback be run with the initial state
* @return the {@link CallbackStore} object associated with this callback.
*/
[[nodiscard]] std::unique_ptr<CallbackStore> RegisterInitializedCallback(
NotifyCallback callback, bool initialNotify);
/**
* Check if this solenoid has been initialized.
*
* @return true if initialized
*/
bool GetInitialized() const;
/**
* Define whether this solenoid has been initialized.
*
* @param initialized whether the solenoid is intiialized.
*/
void SetInitialized(bool initialized);
/**
* Register a callback to be run when the output of this solenoid has changed.
*
* @param callback the callback
* @param initialNotify should the callback be run with the initial value
* @return the {@link CallbackStore} object associated with this callback.
*/
[[nodiscard]] std::unique_ptr<CallbackStore> RegisterOutputCallback(
NotifyCallback callback, bool initialNotify);
/**
* Check the solenoid output.
*
* @return the solenoid output
*/
bool GetOutput() const;
/**
* Change the solenoid output.
*
* @param output the new solenoid output
*/
void SetOutput(bool output);
private:
PCMSim m_pcm;
int m_channel;
};
} // namespace frc::sim