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 "EncoderSimGui.h"
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
#include <memory>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <string_view>
|
2020-09-12 10:55:46 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2025-01-03 13:36:40 -08:00
|
|
|
#include <glass/DataSource.h>
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <glass/hardware/Encoder.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
#include <hal/Ports.h>
|
|
|
|
|
#include <hal/simulation/EncoderData.h>
|
|
|
|
|
#include <hal/simulation/SimDeviceData.h>
|
|
|
|
|
|
|
|
|
|
#include "HALSimGui.h"
|
|
|
|
|
|
|
|
|
|
using namespace halsimgui;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class EncoderSimModel : public glass::EncoderModel {
|
|
|
|
|
public:
|
2021-06-06 16:13:58 -07:00
|
|
|
EncoderSimModel(std::string_view id, int32_t index, int channelA,
|
2020-09-12 10:55:46 -07:00
|
|
|
int channelB)
|
2021-06-06 16:13:58 -07:00
|
|
|
: m_distancePerPulse(fmt::format("{} Dist/Count", id)),
|
|
|
|
|
m_count(fmt::format("{} Count", id)),
|
|
|
|
|
m_period(fmt::format("{} Period", id)),
|
|
|
|
|
m_direction(fmt::format("{} Direction", id)),
|
|
|
|
|
m_distance(fmt::format("{} Distance", id)),
|
|
|
|
|
m_rate(fmt::format("{} Rate", id)),
|
2020-09-12 10:55:46 -07:00
|
|
|
m_index{index},
|
|
|
|
|
m_channelA{channelA},
|
|
|
|
|
m_channelB{channelB},
|
|
|
|
|
m_distancePerPulseCallback{
|
|
|
|
|
HALSIM_RegisterEncoderDistancePerPulseCallback(
|
|
|
|
|
index, DistancePerPulseCallbackFunc, this, true)},
|
|
|
|
|
m_countCallback{HALSIM_RegisterEncoderCountCallback(
|
|
|
|
|
index, CountCallbackFunc, this, true)},
|
|
|
|
|
m_periodCallback{HALSIM_RegisterEncoderPeriodCallback(
|
|
|
|
|
index, PeriodCallbackFunc, this, true)},
|
|
|
|
|
m_directionCallback{HALSIM_RegisterEncoderDirectionCallback(
|
2025-01-03 13:36:40 -08:00
|
|
|
index, DirectionCallbackFunc, this, true)} {}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
EncoderSimModel(int32_t index, int channelA, int channelB)
|
2021-06-06 16:13:58 -07:00
|
|
|
: EncoderSimModel(fmt::format("Encoder[{},{}]", channelA, channelB),
|
2020-09-12 10:55:46 -07:00
|
|
|
index, channelA, channelB) {}
|
|
|
|
|
|
|
|
|
|
explicit EncoderSimModel(int32_t index)
|
|
|
|
|
: EncoderSimModel(index, HALSIM_GetEncoderDigitalChannelA(index),
|
|
|
|
|
HALSIM_GetEncoderDigitalChannelB(index)) {}
|
|
|
|
|
|
2020-12-28 00:10:13 -08:00
|
|
|
~EncoderSimModel() override {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_distancePerPulseCallback != 0) {
|
2020-09-12 10:55:46 -07:00
|
|
|
HALSIM_CancelEncoderDistancePerPulseCallback(m_index,
|
|
|
|
|
m_distancePerPulseCallback);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
|
|
|
|
if (m_countCallback != 0) {
|
2020-09-12 10:55:46 -07:00
|
|
|
HALSIM_CancelEncoderCountCallback(m_index, m_countCallback);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
|
|
|
|
if (m_periodCallback != 0) {
|
2020-09-12 10:55:46 -07:00
|
|
|
HALSIM_CancelEncoderCountCallback(m_index, m_periodCallback);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
|
|
|
|
if (m_directionCallback != 0) {
|
2020-09-12 10:55:46 -07:00
|
|
|
HALSIM_CancelEncoderCountCallback(m_index, m_directionCallback);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update() override {}
|
|
|
|
|
|
|
|
|
|
bool Exists() override { return HALSIM_GetEncoderInitialized(m_index); }
|
|
|
|
|
|
|
|
|
|
int32_t GetIndex() const { return m_index; }
|
|
|
|
|
|
|
|
|
|
const char* GetSimDevice() const override {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (auto simDevice = HALSIM_GetEncoderSimDevice(m_index)) {
|
2020-09-12 10:55:46 -07:00
|
|
|
return HALSIM_GetSimDeviceName(simDevice);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2020-09-12 10:55:46 -07:00
|
|
|
return nullptr;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetChannelA() const override { return m_channelA; }
|
|
|
|
|
int GetChannelB() const override { return m_channelB; }
|
|
|
|
|
|
2025-01-03 13:36:40 -08:00
|
|
|
glass::DoubleSource* GetDistancePerPulseData() override {
|
2020-09-12 10:55:46 -07:00
|
|
|
return &m_distancePerPulse;
|
|
|
|
|
}
|
2025-01-03 13:36:40 -08:00
|
|
|
glass::IntegerSource* GetCountData() override { return &m_count; }
|
|
|
|
|
glass::DoubleSource* GetPeriodData() override { return &m_period; }
|
|
|
|
|
glass::BooleanSource* GetDirectionData() override { return &m_direction; }
|
|
|
|
|
glass::DoubleSource* GetDistanceData() override { return &m_distance; }
|
|
|
|
|
glass::DoubleSource* GetRateData() override { return &m_rate; }
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
double GetMaxPeriod() override { return HALSIM_GetEncoderMaxPeriod(m_index); }
|
|
|
|
|
bool GetReverseDirection() override {
|
|
|
|
|
return HALSIM_GetEncoderReverseDirection(m_index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetDistancePerPulse(double val) override {
|
|
|
|
|
HALSIM_SetEncoderDistancePerPulse(m_index, val);
|
|
|
|
|
}
|
|
|
|
|
void SetCount(int val) override { HALSIM_SetEncoderCount(m_index, val); }
|
|
|
|
|
void SetPeriod(double val) override { HALSIM_SetEncoderPeriod(m_index, val); }
|
|
|
|
|
void SetDirection(bool val) override {
|
|
|
|
|
HALSIM_SetEncoderDirection(m_index, val);
|
|
|
|
|
}
|
|
|
|
|
void SetDistance(double val) override {
|
|
|
|
|
HALSIM_SetEncoderDistance(m_index, val);
|
|
|
|
|
}
|
|
|
|
|
void SetRate(double val) override { HALSIM_SetEncoderRate(m_index, val); }
|
|
|
|
|
|
|
|
|
|
void SetMaxPeriod(double val) override {
|
|
|
|
|
HALSIM_SetEncoderMaxPeriod(m_index, val);
|
|
|
|
|
}
|
|
|
|
|
void SetReverseDirection(bool val) override {
|
|
|
|
|
HALSIM_SetEncoderReverseDirection(m_index, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static void DistancePerPulseCallbackFunc(const char*, void* param,
|
|
|
|
|
const HAL_Value* value) {
|
|
|
|
|
if (value->type == HAL_DOUBLE) {
|
|
|
|
|
auto self = static_cast<EncoderSimModel*>(param);
|
|
|
|
|
double distPerPulse = value->data.v_double;
|
|
|
|
|
self->m_distancePerPulse.SetValue(distPerPulse);
|
|
|
|
|
self->m_distance.SetValue(self->m_count.GetValue() * distPerPulse);
|
|
|
|
|
double period = self->m_period.GetValue();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (period == 0) {
|
2020-09-12 10:55:46 -07:00
|
|
|
self->m_rate.SetValue(std::numeric_limits<double>::infinity());
|
2020-12-28 12:58:06 -08:00
|
|
|
} else if (period == std::numeric_limits<double>::infinity()) {
|
2020-09-12 10:55:46 -07:00
|
|
|
self->m_rate.SetValue(0);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2020-09-12 10:55:46 -07:00
|
|
|
self->m_rate.SetValue(static_cast<float>(distPerPulse / period));
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void CountCallbackFunc(const char*, void* param,
|
|
|
|
|
const HAL_Value* value) {
|
|
|
|
|
if (value->type == HAL_INT) {
|
|
|
|
|
auto self = static_cast<EncoderSimModel*>(param);
|
|
|
|
|
double count = value->data.v_int;
|
|
|
|
|
self->m_count.SetValue(count);
|
|
|
|
|
self->m_distance.SetValue(count * self->m_distancePerPulse.GetValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void PeriodCallbackFunc(const char*, void* param,
|
|
|
|
|
const HAL_Value* value) {
|
|
|
|
|
if (value->type == HAL_DOUBLE) {
|
|
|
|
|
auto self = static_cast<EncoderSimModel*>(param);
|
|
|
|
|
double period = value->data.v_double;
|
|
|
|
|
self->m_period.SetValue(period);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (period == 0) {
|
2020-09-12 10:55:46 -07:00
|
|
|
self->m_rate.SetValue(std::numeric_limits<double>::infinity());
|
2020-12-28 12:58:06 -08:00
|
|
|
} else if (period == std::numeric_limits<double>::infinity()) {
|
2020-09-12 10:55:46 -07:00
|
|
|
self->m_rate.SetValue(0);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2020-09-12 10:55:46 -07:00
|
|
|
self->m_rate.SetValue(
|
|
|
|
|
static_cast<float>(self->m_distancePerPulse.GetValue() / period));
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void DirectionCallbackFunc(const char*, void* param,
|
|
|
|
|
const HAL_Value* value) {
|
|
|
|
|
if (value->type == HAL_BOOLEAN) {
|
|
|
|
|
static_cast<EncoderSimModel*>(param)->m_direction.SetValue(
|
|
|
|
|
value->data.v_boolean);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-03 13:36:40 -08:00
|
|
|
glass::DoubleSource m_distancePerPulse;
|
|
|
|
|
glass::IntegerSource m_count;
|
|
|
|
|
glass::DoubleSource m_period;
|
|
|
|
|
glass::BooleanSource m_direction;
|
|
|
|
|
glass::DoubleSource m_distance;
|
|
|
|
|
glass::DoubleSource m_rate;
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
int32_t m_index;
|
|
|
|
|
int m_channelA;
|
|
|
|
|
int m_channelB;
|
|
|
|
|
int32_t m_distancePerPulseCallback;
|
|
|
|
|
int32_t m_countCallback;
|
|
|
|
|
int32_t m_periodCallback;
|
|
|
|
|
int32_t m_directionCallback;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class EncodersSimModel : public glass::EncodersModel {
|
|
|
|
|
public:
|
|
|
|
|
EncodersSimModel() : m_models(HAL_GetNumEncoders()) {}
|
|
|
|
|
|
|
|
|
|
void Update() override;
|
|
|
|
|
|
|
|
|
|
bool Exists() override { return true; }
|
|
|
|
|
|
|
|
|
|
void ForEachEncoder(
|
|
|
|
|
wpi::function_ref<void(glass::EncoderModel& model, int index)> func)
|
|
|
|
|
override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<std::unique_ptr<EncoderSimModel>> m_models;
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void EncodersSimModel::Update() {
|
|
|
|
|
for (int32_t i = 0, iend = static_cast<int32_t>(m_models.size()); i < iend;
|
|
|
|
|
++i) {
|
|
|
|
|
auto& model = m_models[i];
|
|
|
|
|
if (HALSIM_GetEncoderInitialized(i)) {
|
|
|
|
|
if (!model) {
|
|
|
|
|
model = std::make_unique<EncoderSimModel>(i);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
model.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EncodersSimModel::ForEachEncoder(
|
|
|
|
|
wpi::function_ref<void(glass::EncoderModel& 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 EncodersAnyInitialized() {
|
|
|
|
|
static const int32_t num = HAL_GetNumEncoders();
|
|
|
|
|
for (int32_t i = 0; i < num; ++i) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (HALSIM_GetEncoderInitialized(i)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EncoderSimGui::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(
|
2020-09-12 10:55:46 -07:00
|
|
|
"Encoders", EncodersAnyInitialized,
|
|
|
|
|
[] { return std::make_unique<EncodersSimModel>(); },
|
|
|
|
|
[](glass::Window* win, glass::Model* model) {
|
|
|
|
|
win->SetFlags(ImGuiWindowFlags_AlwaysAutoResize);
|
|
|
|
|
win->SetDefaultPos(5, 250);
|
|
|
|
|
return glass::MakeFunctionView(
|
|
|
|
|
[=] { DisplayEncoders(static_cast<EncodersSimModel*>(model)); });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glass::EncodersModel& EncoderSimGui::GetEncodersModel() {
|
[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
|
|
|
static auto model = HALSimGui::halProvider->GetModel("Encoders");
|
2020-09-12 10:55:46 -07:00
|
|
|
assert(model);
|
|
|
|
|
return *static_cast<glass::EncodersModel*>(model);
|
|
|
|
|
}
|