mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[sim] Add plotting to simulation GUI
This commit is contained in:
@@ -7,40 +7,66 @@
|
||||
|
||||
#include "AnalogOutGui.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/AnalogOutData.h>
|
||||
#include <imgui.h>
|
||||
|
||||
#include "GuiDataSource.h"
|
||||
#include "HALSimGui.h"
|
||||
#include "IniSaver.h"
|
||||
#include "IniSaverInfo.h"
|
||||
#include "SimDeviceGui.h"
|
||||
|
||||
using namespace halsimgui;
|
||||
|
||||
namespace {
|
||||
HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(AnalogOutVoltage, "AOut");
|
||||
} // namespace
|
||||
|
||||
static IniSaver<NameInfo> gAnalogOuts{"AnalogOut"}; // indexed by channel
|
||||
static std::vector<std::unique_ptr<AnalogOutVoltageSource>> gAnalogOutSources;
|
||||
|
||||
static void UpdateAnalogOutSources() {
|
||||
for (int i = 0, iend = gAnalogOutSources.size(); i < iend; ++i) {
|
||||
auto& source = gAnalogOutSources[i];
|
||||
if (HALSIM_GetAnalogOutInitialized(i)) {
|
||||
if (!source) {
|
||||
source = std::make_unique<AnalogOutVoltageSource>(i);
|
||||
source->SetName(gAnalogOuts[i].GetName());
|
||||
}
|
||||
} else {
|
||||
source.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void DisplayAnalogOutputs() {
|
||||
static const int numAnalog = HAL_GetNumAnalogOutputs();
|
||||
static auto init = std::make_unique<bool[]>(numAnalog);
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < numAnalog; ++i) {
|
||||
init[i] = HALSIM_GetAnalogOutInitialized(i);
|
||||
if (init[i]) ++count;
|
||||
for (auto&& source : gAnalogOutSources) {
|
||||
if (source) ++count;
|
||||
}
|
||||
|
||||
if (count == 0) return;
|
||||
|
||||
if (SimDeviceGui::StartDevice("Analog Outputs")) {
|
||||
for (int i = 0; i < numAnalog; ++i) {
|
||||
if (!init[i]) continue;
|
||||
for (int i = 0, iend = gAnalogOutSources.size(); i < iend; ++i) {
|
||||
if (auto source = gAnalogOutSources[i].get()) {
|
||||
ImGui::PushID(i);
|
||||
|
||||
auto& info = gAnalogOuts[i];
|
||||
char name[128];
|
||||
info.GetName(name, sizeof(name), "Out", i);
|
||||
HAL_Value value = HAL_MakeDouble(HALSIM_GetAnalogOutVoltage(i));
|
||||
SimDeviceGui::DisplayValue(name, true, &value);
|
||||
info.PopupEditName(i);
|
||||
auto& info = gAnalogOuts[i];
|
||||
char label[128];
|
||||
info.GetLabel(label, sizeof(label), "Out", i);
|
||||
HAL_Value value = HAL_MakeDouble(source->GetValue());
|
||||
SimDeviceGui::DisplayValueSource(label, true, &value, source);
|
||||
|
||||
if (info.PopupEditName(i)) {
|
||||
if (source) source->SetName(info.GetName());
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
}
|
||||
|
||||
SimDeviceGui::FinishDevice();
|
||||
@@ -49,5 +75,7 @@ static void DisplayAnalogOutputs() {
|
||||
|
||||
void AnalogOutGui::Initialize() {
|
||||
gAnalogOuts.Initialize();
|
||||
gAnalogOutSources.resize(HAL_GetNumAnalogOutputs());
|
||||
HALSimGui::AddExecute(UpdateAnalogOutSources);
|
||||
SimDeviceGui::Add(DisplayAnalogOutputs);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user