2021-08-04 20:31:17 -07: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.
|
|
|
|
|
|
|
|
|
|
#include "PowerDistributionSimGui.h"
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <glass/hardware/PowerDistribution.h>
|
2021-08-04 20:31:17 -07:00
|
|
|
#include <hal/Ports.h>
|
|
|
|
|
#include <hal/simulation/PowerDistributionData.h>
|
|
|
|
|
|
|
|
|
|
#include "HALDataSource.h"
|
|
|
|
|
#include "HALSimGui.h"
|
|
|
|
|
|
|
|
|
|
using namespace halsimgui;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(PowerDistributionTemperature,
|
|
|
|
|
"Power Distribution Temp");
|
|
|
|
|
HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(PowerDistributionVoltage,
|
|
|
|
|
"Power Distribution Voltage");
|
|
|
|
|
HALSIMGUI_DATASOURCE_DOUBLE_INDEXED2(PowerDistributionCurrent,
|
|
|
|
|
"Power Distribution Current");
|
|
|
|
|
|
|
|
|
|
class PowerDistributionSimModel : public glass::PowerDistributionModel {
|
|
|
|
|
public:
|
|
|
|
|
explicit PowerDistributionSimModel(int32_t index)
|
|
|
|
|
: m_index{index}, m_temp{index}, m_voltage{index} {
|
2021-08-14 11:44:56 -07:00
|
|
|
// TODO make this better
|
|
|
|
|
const int numChannels = HAL_GetNumREVPDHChannels();
|
2021-08-04 20:31:17 -07:00
|
|
|
m_currents.reserve(numChannels);
|
|
|
|
|
for (int i = 0; i < numChannels; ++i) {
|
|
|
|
|
m_currents.emplace_back(
|
|
|
|
|
std::make_unique<PowerDistributionCurrentSource>(index, i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update() override {}
|
|
|
|
|
|
|
|
|
|
bool Exists() override {
|
|
|
|
|
return HALSIM_GetPowerDistributionInitialized(m_index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetNumChannels() const override { return m_currents.size(); }
|
|
|
|
|
|
|
|
|
|
glass::DataSource* GetTemperatureData() override { return &m_temp; }
|
|
|
|
|
glass::DataSource* GetVoltageData() override { return &m_voltage; }
|
|
|
|
|
glass::DataSource* GetCurrentData(int channel) override {
|
|
|
|
|
return m_currents[channel].get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetTemperature(double val) override {
|
|
|
|
|
HALSIM_SetPowerDistributionTemperature(m_index, val);
|
|
|
|
|
}
|
|
|
|
|
void SetVoltage(double val) override {
|
|
|
|
|
HALSIM_SetPowerDistributionVoltage(m_index, val);
|
|
|
|
|
}
|
|
|
|
|
void SetCurrent(int channel, double val) override {
|
|
|
|
|
HALSIM_SetPowerDistributionCurrent(m_index, channel, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int32_t m_index;
|
|
|
|
|
PowerDistributionTemperatureSource m_temp;
|
|
|
|
|
PowerDistributionVoltageSource m_voltage;
|
|
|
|
|
std::vector<std::unique_ptr<PowerDistributionCurrentSource>> m_currents;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class PowerDistributionsSimModel : public glass::PowerDistributionsModel {
|
|
|
|
|
public:
|
2021-08-14 11:44:56 -07:00
|
|
|
PowerDistributionsSimModel() : m_models(HAL_GetNumREVPDHModules()) {}
|
2021-08-04 20:31:17 -07:00
|
|
|
|
|
|
|
|
void Update() override;
|
|
|
|
|
|
|
|
|
|
bool Exists() override { return true; }
|
|
|
|
|
|
|
|
|
|
void ForEachPowerDistribution(
|
|
|
|
|
wpi::function_ref<void(glass::PowerDistributionModel& model, int index)>
|
|
|
|
|
func) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<std::unique_ptr<PowerDistributionSimModel>> m_models;
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void PowerDistributionsSimModel::Update() {
|
|
|
|
|
for (int32_t i = 0, iend = static_cast<int32_t>(m_models.size()); i < iend;
|
|
|
|
|
++i) {
|
|
|
|
|
auto& model = m_models[i];
|
|
|
|
|
if (HALSIM_GetPowerDistributionInitialized(i)) {
|
|
|
|
|
if (!model) {
|
|
|
|
|
model = std::make_unique<PowerDistributionSimModel>(i);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
model.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowerDistributionsSimModel::ForEachPowerDistribution(
|
|
|
|
|
wpi::function_ref<void(glass::PowerDistributionModel& model, int index)>
|
|
|
|
|
func) {
|
|
|
|
|
for (int32_t i = 0, iend = static_cast<int32_t>(m_models.size()); i < iend;
|
|
|
|
|
++i) {
|
|
|
|
|
if (auto model = m_models[i].get()) {
|
|
|
|
|
func(*model, i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool PowerDistributionsAnyInitialized() {
|
2021-08-14 11:44:56 -07:00
|
|
|
static const int32_t num = HAL_GetNumREVPDHModules();
|
2021-08-04 20:31:17 -07:00
|
|
|
for (int32_t i = 0; i < num; ++i) {
|
|
|
|
|
if (HALSIM_GetPowerDistributionInitialized(i)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowerDistributionSimGui::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->Register(
|
2021-08-04 20:31:17 -07:00
|
|
|
"PowerDistributions", PowerDistributionsAnyInitialized,
|
|
|
|
|
[] { return std::make_unique<PowerDistributionsSimModel>(); },
|
|
|
|
|
[](glass::Window* win, glass::Model* model) {
|
|
|
|
|
win->SetDefaultPos(245, 155);
|
|
|
|
|
return glass::MakeFunctionView([=] {
|
|
|
|
|
DisplayPowerDistributions(
|
|
|
|
|
static_cast<PowerDistributionsSimModel*>(model));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|