Skip to content

Commit

Permalink
[sim] Add GUI support for the REV PH (wpilibsuite#6704)
Browse files Browse the repository at this point in the history
  • Loading branch information
WispySparks authored Jul 16, 2024
1 parent cd6b70a commit 7d64d4e
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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 "glass/hardware/PCM.h"
#include "glass/hardware/Pneumatic.h"

#include <cstdio>
#include <cstring>
Expand All @@ -20,8 +20,8 @@

using namespace glass;

bool glass::DisplayPCMSolenoids(PCMModel* model, int index,
bool outputsEnabled) {
bool glass::DisplayPneumaticControlSolenoids(PneumaticControlModel* model,
int index, bool outputsEnabled) {
wpi::SmallVector<int, 16> channels;
model->ForEachSolenoid([&](SolenoidModel& solenoid, int j) {
if (auto data = solenoid.GetOutputData()) {
Expand Down Expand Up @@ -50,7 +50,8 @@ bool glass::DisplayPCMSolenoids(PCMModel* model, int index,
wpi::format_to_n_c_str(label, sizeof(label), "{} [{}]###header", name,
index);
} else {
wpi::format_to_n_c_str(label, sizeof(label), "PCM[{}]###header", index);
wpi::format_to_n_c_str(label, sizeof(label), "{}[{}]###header",
model->GetName(), index);
}

// header
Expand Down Expand Up @@ -85,32 +86,29 @@ bool glass::DisplayPCMSolenoids(PCMModel* model, int index,
return true;
}

void glass::DisplayPCMsSolenoids(PCMsModel* model, bool outputsEnabled,
std::string_view noneMsg) {
void glass::DisplayPneumaticControlsSolenoids(PneumaticControlsModel* model,
bool outputsEnabled,
std::string_view noneMsg) {
bool hasAny = false;
model->ForEachPCM([&](PCMModel& pcm, int i) {
PushID(i);
if (DisplayPCMSolenoids(&pcm, i, outputsEnabled)) {
hasAny = true;
}
PopID();
});
model->ForEachPneumaticControl(
[&](PneumaticControlModel& pneumaticControl, int i) {
PushID(i);
if (DisplayPneumaticControlSolenoids(&pneumaticControl, i,
outputsEnabled)) {
hasAny = true;
}
PopID();
});
if (!hasAny && !noneMsg.empty()) {
ImGui::TextUnformatted(noneMsg.data(), noneMsg.data() + noneMsg.size());
}
}

void glass::DisplayCompressorDevice(PCMModel* model, int index,
void glass::DisplayCompressorDevice(CompressorModel* model, int index,
bool outputsEnabled) {
auto compressor = model->GetCompressor();
if (!compressor || !compressor->Exists()) {
if (!model || !model->Exists()) {
return;
}
DisplayCompressorDevice(compressor, index, outputsEnabled);
}

void glass::DisplayCompressorDevice(CompressorModel* model, int index,
bool outputsEnabled) {
char name[32];
wpi::format_to_n_c_str(name, sizeof(name), "Compressor[{}]", index);

Expand Down Expand Up @@ -155,8 +153,11 @@ void glass::DisplayCompressorDevice(CompressorModel* model, int index,
}
}

void glass::DisplayCompressorsDevice(PCMsModel* model, bool outputsEnabled) {
model->ForEachPCM([&](PCMModel& pcm, int i) {
DisplayCompressorDevice(&pcm, i, outputsEnabled);
});
void glass::DisplayCompressorsDevice(PneumaticControlsModel* model,
bool outputsEnabled) {
model->ForEachPneumaticControl(
[&](PneumaticControlModel& pneumaticControl, int i) {
DisplayCompressorDevice(pneumaticControl.GetCompressor(), i,
outputsEnabled);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#pragma once

#include <memory>
#include <string_view>
#include <utility>

#include <wpi/function_ref.h>

Expand Down Expand Up @@ -34,27 +36,45 @@ class SolenoidModel : public Model {
virtual void SetOutput(bool val) = 0;
};

class PCMModel : public Model {
class PneumaticControlModel : public Model {
public:
virtual CompressorModel* GetCompressor() = 0;

virtual void ForEachSolenoid(
wpi::function_ref<void(SolenoidModel& model, int index)> func) = 0;

virtual std::string_view GetName() = 0;
};

class PCMsModel : public Model {
class PneumaticControlsModel : public Model {
public:
virtual void ForEachPCM(
wpi::function_ref<void(PCMModel& model, int index)> func) = 0;
virtual void ForEachPneumaticControl(
wpi::function_ref<void(PneumaticControlModel& model, int index)>
func) = 0;
};

struct AllPneumaticControlsModel : public Model {
AllPneumaticControlsModel(std::unique_ptr<PneumaticControlsModel> pcms,
std::unique_ptr<PneumaticControlsModel> phs)
: pcms{std::move(pcms)}, phs{std::move(phs)} {};
std::unique_ptr<PneumaticControlsModel> pcms;
std::unique_ptr<PneumaticControlsModel> phs;
void Update() override {
pcms->Update();
phs->Update();
};
bool Exists() override { return true; }
};

bool DisplayPCMSolenoids(PCMModel* model, int index, bool outputsEnabled);
void DisplayPCMsSolenoids(PCMsModel* model, bool outputsEnabled,
std::string_view noneMsg = "No solenoids");
bool DisplayPneumaticControlSolenoids(PneumaticControlModel* model, int index,
bool outputsEnabled);
void DisplayPneumaticControlsSolenoids(
PneumaticControlsModel* model, bool outputsEnabled,
std::string_view noneMsg = "No solenoids");

void DisplayCompressorDevice(PCMModel* model, int index, bool outputsEnabled);
void DisplayCompressorDevice(CompressorModel* model, int index,
bool outputsEnabled);
void DisplayCompressorsDevice(PCMsModel* model, bool outputsEnabled);
void DisplayCompressorsDevice(PneumaticControlsModel* model,
bool outputsEnabled);

} // namespace glass
65 changes: 32 additions & 33 deletions simulation/halsim_gui/src/main/native/cpp/PCMSimGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "PCMSimGui.h"

#include <glass/hardware/PCM.h>
#include <glass/hardware/Pneumatic.h>
#include <glass/other/DeviceTree.h>

#include <cstdio>
Expand All @@ -25,7 +25,8 @@ namespace {
HALSIMGUI_DATASOURCE_BOOLEAN_INDEXED(CTREPCMCompressorOn, "Compressor On");
HALSIMGUI_DATASOURCE_BOOLEAN_INDEXED(CTREPCMClosedLoopEnabled, "Closed Loop");
HALSIMGUI_DATASOURCE_BOOLEAN_INDEXED(CTREPCMPressureSwitch, "Pressure Switch");
HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(CTREPCMCompressorCurrent, "Comp Current");
HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(CTREPCMCompressorCurrent,
"Compressor Current");
HALSIMGUI_DATASOURCE_BOOLEAN_INDEXED2(CTREPCMSolenoidOutput, "Solenoid");

class CompressorSimModel : public glass::CompressorModel {
Expand Down Expand Up @@ -90,7 +91,7 @@ class SolenoidSimModel : public glass::SolenoidModel {
CTREPCMSolenoidOutputSource m_output;
};

class PCMSimModel : public glass::PCMModel {
class PCMSimModel : public glass::PneumaticControlModel {
public:
explicit PCMSimModel(int32_t index)
: m_index{index},
Expand All @@ -107,6 +108,8 @@ class PCMSimModel : public glass::PCMModel {
wpi::function_ref<void(glass::SolenoidModel& model, int index)> func)
override;

std::string_view GetName() override { return "PCM"; }

int GetNumSolenoids() const { return m_solenoidInitCount; }

private:
Expand All @@ -116,16 +119,17 @@ class PCMSimModel : public glass::PCMModel {
int m_solenoidInitCount = 0;
};

class PCMsSimModel : public glass::PCMsModel {
class PCMsSimModel : public glass::PneumaticControlsModel {
public:
PCMsSimModel() : m_models(HAL_GetNumCTREPCMModules()) {}

void Update() override;

bool Exists() override { return true; }

void ForEachPCM(
wpi::function_ref<void(glass::PCMModel& model, int index)> func) override;
void ForEachPneumaticControl(
wpi::function_ref<void(glass::PneumaticControlModel& model, int index)>
func) override;

private:
std::vector<std::unique_ptr<PCMSimModel>> m_models;
Expand Down Expand Up @@ -176,8 +180,9 @@ void PCMsSimModel::Update() {
}
}

void PCMsSimModel::ForEachPCM(
wpi::function_ref<void(glass::PCMModel& model, int index)> func) {
void PCMsSimModel::ForEachPneumaticControl(
wpi::function_ref<void(glass::PneumaticControlModel& model, int index)>
func) {
int32_t numCTREPCMs = m_models.size();
for (int32_t i = 0; i < numCTREPCMs; ++i) {
if (auto model = m_models[i].get()) {
Expand All @@ -186,7 +191,7 @@ void PCMsSimModel::ForEachPCM(
}
}

static bool PCMsAnyInitialized() {
bool PCMSimGui::PCMsAnyInitialized() {
static const int32_t num = HAL_GetNumCTREPCMModules();
for (int32_t i = 0; i < num; ++i) {
if (HALSIM_GetCTREPCMInitialized(i)) {
Expand All @@ -196,31 +201,25 @@ static bool PCMsAnyInitialized() {
return false;
}

void PCMSimGui::Initialize() {
HALSimGui::halProvider->RegisterModel("CTREPCMs", PCMsAnyInitialized, [] {
return std::make_unique<PCMsSimModel>();
});
HALSimGui::halProvider->RegisterView(
"Solenoids", "CTREPCMs",
[](glass::Model* model) {
bool any = false;
static_cast<PCMsSimModel*>(model)->ForEachPCM(
[&](glass::PCMModel& CTREPCM, int) {
if (static_cast<PCMSimModel*>(&CTREPCM)->GetNumSolenoids() > 0) {
any = true;
}
});
return any;
},
[](glass::Window* win, glass::Model* model) {
win->SetFlags(ImGuiWindowFlags_AlwaysAutoResize);
win->SetDefaultPos(290, 20);
return glass::MakeFunctionView([=] {
glass::DisplayPCMsSolenoids(
static_cast<PCMsSimModel*>(model),
HALSimGui::halProvider->AreOutputsEnabled());
});
bool PCMSimGui::PCMsAnySolenoids(glass::PneumaticControlsModel* model) {
bool any = false;
static_cast<PCMsSimModel*>(model)->ForEachPneumaticControl(
[&](glass::PneumaticControlModel& CTREPCM, int) {
if (static_cast<PCMSimModel*>(&CTREPCM)->GetNumSolenoids() > 0) {
any = true;
}
});
return any;
}

std::unique_ptr<glass::PneumaticControlsModel> PCMSimGui::GetPCMsModel() {
return std::make_unique<PCMsSimModel>();
}

void PCMSimGui::Initialize() {
HALSimGui::halProvider->RegisterModel(
"CTREPCMs", PCMSimGui::PCMsAnyInitialized,
[] { return std::make_unique<PCMsSimModel>(); });

SimDeviceGui::GetDeviceTree().Add(
HALSimGui::halProvider->GetModel("CTREPCMs"), [](glass::Model* model) {
Expand Down
6 changes: 6 additions & 0 deletions simulation/halsim_gui/src/main/native/cpp/PCMSimGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
// the WPILib BSD license file in the root directory of this project.

#pragma once
#include <glass/hardware/Pneumatic.h>

#include <memory>

namespace halsimgui {

class PCMSimGui {
public:
static void Initialize();
static bool PCMsAnyInitialized();
static bool PCMsAnySolenoids(glass::PneumaticControlsModel* model);
static std::unique_ptr<glass::PneumaticControlsModel> GetPCMsModel();
};

} // namespace halsimgui
Loading

0 comments on commit 7d64d4e

Please sign in to comment.