2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
#include "PCMSimGui.h"
|
|
|
|
|
|
|
|
|
|
#include <glass/hardware/PCM.h>
|
|
|
|
|
#include <glass/other/DeviceTree.h>
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include <hal/Ports.h>
|
|
|
|
|
#include <hal/Value.h>
|
2021-06-05 22:36:39 -07:00
|
|
|
#include <hal/simulation/CTREPCMData.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
#include "HALDataSource.h"
|
|
|
|
|
#include "HALSimGui.h"
|
|
|
|
|
#include "SimDeviceGui.h"
|
|
|
|
|
|
|
|
|
|
using namespace halsimgui;
|
|
|
|
|
|
|
|
|
|
namespace {
|
2021-06-05 22:36:39 -07:00
|
|
|
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_BOOLEAN_INDEXED2(CTREPCMSolenoidOutput, "Solenoid");
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
class CompressorSimModel : public glass::CompressorModel {
|
|
|
|
|
public:
|
|
|
|
|
explicit CompressorSimModel(int32_t index)
|
|
|
|
|
: m_index{index},
|
|
|
|
|
m_running{index},
|
|
|
|
|
m_enabled{index},
|
|
|
|
|
m_pressureSwitch{index},
|
|
|
|
|
m_current{index} {}
|
|
|
|
|
|
|
|
|
|
void Update() override {}
|
|
|
|
|
|
2021-06-05 22:36:39 -07:00
|
|
|
bool Exists() override { return HALSIM_GetCTREPCMInitialized(m_index); }
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
glass::DataSource* GetRunningData() override { return &m_running; }
|
|
|
|
|
glass::DataSource* GetEnabledData() override { return &m_enabled; }
|
|
|
|
|
glass::DataSource* GetPressureSwitchData() override {
|
|
|
|
|
return &m_pressureSwitch;
|
|
|
|
|
}
|
|
|
|
|
glass::DataSource* GetCurrentData() override { return &m_current; }
|
|
|
|
|
|
|
|
|
|
void SetRunning(bool val) override {
|
2021-06-05 22:36:39 -07:00
|
|
|
HALSIM_SetCTREPCMCompressorOn(m_index, val);
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
void SetEnabled(bool val) override {
|
2021-06-05 22:36:39 -07:00
|
|
|
HALSIM_SetCTREPCMClosedLoopEnabled(m_index, val);
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
void SetPressureSwitch(bool val) override {
|
2021-06-05 22:36:39 -07:00
|
|
|
HALSIM_SetCTREPCMPressureSwitch(m_index, val);
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
void SetCurrent(double val) override {
|
2021-06-05 22:36:39 -07:00
|
|
|
HALSIM_SetCTREPCMCompressorCurrent(m_index, val);
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int32_t m_index;
|
2021-06-05 22:36:39 -07:00
|
|
|
CTREPCMCompressorOnSource m_running;
|
|
|
|
|
CTREPCMClosedLoopEnabledSource m_enabled;
|
|
|
|
|
CTREPCMPressureSwitchSource m_pressureSwitch;
|
|
|
|
|
CTREPCMCompressorCurrentSource m_current;
|
2020-09-12 10:55:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SolenoidSimModel : public glass::SolenoidModel {
|
|
|
|
|
public:
|
|
|
|
|
SolenoidSimModel(int32_t index, int32_t channel)
|
|
|
|
|
: m_index{index}, m_channel{channel}, m_output{index, channel} {}
|
|
|
|
|
|
|
|
|
|
void Update() override {}
|
|
|
|
|
|
2021-06-05 22:36:39 -07:00
|
|
|
bool Exists() override { return HALSIM_GetCTREPCMInitialized(m_index); }
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
glass::DataSource* GetOutputData() override { return &m_output; }
|
|
|
|
|
|
|
|
|
|
void SetOutput(bool val) override {
|
2021-06-05 22:36:39 -07:00
|
|
|
HALSIM_SetCTREPCMSolenoidOutput(m_index, m_channel, val);
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int32_t m_index;
|
|
|
|
|
int32_t m_channel;
|
2021-06-05 22:36:39 -07:00
|
|
|
CTREPCMSolenoidOutputSource m_output;
|
2020-09-12 10:55:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class PCMSimModel : public glass::PCMModel {
|
|
|
|
|
public:
|
|
|
|
|
explicit PCMSimModel(int32_t index)
|
|
|
|
|
: m_index{index},
|
|
|
|
|
m_compressor{index},
|
2021-08-14 11:44:56 -07:00
|
|
|
m_solenoids(HAL_GetNumCTRESolenoidChannels()) {}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
void Update() override;
|
|
|
|
|
|
|
|
|
|
bool Exists() override { return true; }
|
|
|
|
|
|
|
|
|
|
CompressorSimModel* GetCompressor() override { return &m_compressor; }
|
|
|
|
|
|
|
|
|
|
void ForEachSolenoid(
|
|
|
|
|
wpi::function_ref<void(glass::SolenoidModel& model, int index)> func)
|
|
|
|
|
override;
|
|
|
|
|
|
|
|
|
|
int GetNumSolenoids() const { return m_solenoidInitCount; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int32_t m_index;
|
|
|
|
|
CompressorSimModel m_compressor;
|
|
|
|
|
std::vector<std::unique_ptr<SolenoidSimModel>> m_solenoids;
|
|
|
|
|
int m_solenoidInitCount = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class PCMsSimModel : public glass::PCMsModel {
|
|
|
|
|
public:
|
2021-06-05 22:36:39 -07:00
|
|
|
PCMsSimModel() : m_models(HAL_GetNumCTREPCMModules()) {}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
void Update() override;
|
|
|
|
|
|
|
|
|
|
bool Exists() override { return true; }
|
|
|
|
|
|
|
|
|
|
void ForEachPCM(
|
|
|
|
|
wpi::function_ref<void(glass::PCMModel& model, int index)> func) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<std::unique_ptr<PCMSimModel>> m_models;
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void PCMSimModel::Update() {
|
|
|
|
|
int32_t numChannels = m_solenoids.size();
|
|
|
|
|
m_solenoidInitCount = 0;
|
|
|
|
|
for (int32_t i = 0; i < numChannels; ++i) {
|
|
|
|
|
auto& model = m_solenoids[i];
|
2021-06-05 22:36:39 -07:00
|
|
|
if (HALSIM_GetCTREPCMInitialized(m_index)) {
|
2020-09-12 10:55:46 -07:00
|
|
|
if (!model) {
|
|
|
|
|
model = std::make_unique<SolenoidSimModel>(m_index, i);
|
|
|
|
|
}
|
|
|
|
|
++m_solenoidInitCount;
|
|
|
|
|
} else {
|
|
|
|
|
model.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PCMSimModel::ForEachSolenoid(
|
|
|
|
|
wpi::function_ref<void(glass::SolenoidModel& model, int index)> func) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_solenoidInitCount == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
int32_t numSolenoids = m_solenoids.size();
|
|
|
|
|
for (int32_t i = 0; i < numSolenoids; ++i) {
|
|
|
|
|
if (auto model = m_solenoids[i].get()) {
|
|
|
|
|
func(*model, i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PCMsSimModel::Update() {
|
|
|
|
|
for (int32_t i = 0, iend = static_cast<int32_t>(m_models.size()); i < iend;
|
|
|
|
|
++i) {
|
|
|
|
|
auto& model = m_models[i];
|
2021-06-05 22:36:39 -07:00
|
|
|
if (HALSIM_GetCTREPCMInitialized(i)) {
|
2020-09-12 10:55:46 -07:00
|
|
|
if (!model) {
|
|
|
|
|
model = std::make_unique<PCMSimModel>(i);
|
|
|
|
|
}
|
|
|
|
|
model->Update();
|
|
|
|
|
} else {
|
|
|
|
|
model.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PCMsSimModel::ForEachPCM(
|
|
|
|
|
wpi::function_ref<void(glass::PCMModel& model, int index)> func) {
|
2021-06-05 22:36:39 -07:00
|
|
|
int32_t numCTREPCMs = m_models.size();
|
|
|
|
|
for (int32_t i = 0; i < numCTREPCMs; ++i) {
|
2020-09-12 10:55:46 -07:00
|
|
|
if (auto model = m_models[i].get()) {
|
|
|
|
|
func(*model, i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool PCMsAnyInitialized() {
|
2021-06-05 22:36:39 -07:00
|
|
|
static const int32_t num = HAL_GetNumCTREPCMModules();
|
2020-09-12 10:55:46 -07:00
|
|
|
for (int32_t i = 0; i < num; ++i) {
|
2021-06-05 22:36:39 -07:00
|
|
|
if (HALSIM_GetCTREPCMInitialized(i)) {
|
2020-09-12 10:55:46 -07:00
|
|
|
return true;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PCMSimGui::Initialize() {
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
HALSimGui::halProvider->RegisterModel("CTREPCMs", PCMsAnyInitialized, [] {
|
2020-09-12 10:55:46 -07:00
|
|
|
return std::make_unique<PCMsSimModel>();
|
|
|
|
|
});
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
HALSimGui::halProvider->RegisterView(
|
2021-06-05 22:36:39 -07:00
|
|
|
"Solenoids", "CTREPCMs",
|
2020-09-12 10:55:46 -07:00
|
|
|
[](glass::Model* model) {
|
|
|
|
|
bool any = false;
|
|
|
|
|
static_cast<PCMsSimModel*>(model)->ForEachPCM(
|
2021-06-05 22:36:39 -07:00
|
|
|
[&](glass::PCMModel& CTREPCM, int) {
|
|
|
|
|
if (static_cast<PCMSimModel*>(&CTREPCM)->GetNumSolenoids() > 0) {
|
2020-09-12 10:55:46 -07:00
|
|
|
any = true;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
});
|
|
|
|
|
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),
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
HALSimGui::halProvider->AreOutputsEnabled());
|
2020-09-12 10:55:46 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
SimDeviceGui::GetDeviceTree().Add(
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
HALSimGui::halProvider->GetModel("CTREPCMs"), [](glass::Model* model) {
|
2020-09-12 10:55:46 -07:00
|
|
|
glass::DisplayCompressorsDevice(
|
|
|
|
|
static_cast<PCMsSimModel*>(model),
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
HALSimGui::halProvider->AreOutputsEnabled());
|
2020-09-12 10:55:46 -07:00
|
|
|
});
|
|
|
|
|
}
|