[glass] Add more widgets (#2947)

This adds the following widgets:
- Speed Controller
- Gyroscope
- Command
- Subsystem
- PIDController
- Scheduler
This commit is contained in:
Prateek Machiraju
2020-12-23 00:07:44 -05:00
committed by GitHub
parent 581b7ec553
commit 27b67deca6
25 changed files with 1101 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "glass/hardware/Gyro.h"
#include <cmath>
#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui.h>
#include <imgui_internal.h>
#include <wpi/math>
#include "glass/Context.h"
#include "glass/DataSource.h"
using namespace glass;
void glass::DisplayGyro(GyroModel* m) {
static const auto kColorWhite = IM_COL32(255, 255, 255, 255);
static const auto kColorGray = IM_COL32(200, 200, 200, 255);
static const auto kColorPurple = IM_COL32(200, 200, 255, 255);
auto angle = m->GetAngleData();
if (!angle || !m->Exists()) {
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
ImGui::Text("Unknown Gyro");
ImGui::PopStyleColor();
return;
}
// Display the numeric angle value. This can be editable in some cases (i.e.
// running from HALSIM).
auto flags =
m->IsReadOnly() ? ImGuiInputTextFlags_ReadOnly : ImGuiInputTextFlags_None;
auto value = angle->GetValue();
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
if (ImGui::InputDouble("Gyro Angle (Deg)", &value, 0.0, 0.0, "%.4f", flags))
m->SetAngle(value);
// Draw the gyro indicator.
ImDrawList* draw = ImGui::GetWindowDrawList();
ImVec2 window = ImGui::GetWindowPos();
float w = ImGui::GetWindowWidth();
float h = ImGui::GetWindowHeight();
float radius = (w < h) ? w * 0.3 : h * 0.3;
ImVec2 center = window + ImVec2(w / 2, h / 2 + ImGui::GetFontSize());
// Add the primary circle.
draw->AddCircle(center, radius, kColorWhite, 100, 1.5);
// Draw the spokes at every 5 degrees and a "major" spoke every 45 degrees.
for (int i = -175; i <= 180; i += 5) {
double radians = i * 2 * wpi::math::pi / 360.0;
ImVec2 direction(std::sin(radians), -std::cos(radians));
bool major = i % 45 == 0;
auto color = major ? kColorWhite : kColorGray;
draw->AddLine(center + (direction * radius),
center + (direction * radius * (major ? 1.07f : 1.03f)),
color, 1.2f);
if (major) {
char txt[16];
std::snprintf(txt, sizeof(txt), "%d°", i);
draw->AddText(
center + (direction * radius * 1.25) - ImGui::CalcTextSize(txt) * 0.5,
kColorWhite, txt, nullptr);
}
}
draw->AddCircleFilled(center, radius * 0.075, kColorPurple, 50);
double radians = value * 2 * wpi::math::pi / 360.0;
draw->AddLine(
center - ImVec2(1, 0),
center + ImVec2(std::sin(radians), -std::cos(radians)) * radius * 0.95f,
kColorPurple, 3);
}

View File

@@ -0,0 +1,36 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "glass/hardware/SpeedController.h"
#include <imgui.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
using namespace glass;
void glass::DisplaySpeedController(SpeedControllerModel* m) {
// Get duty cycle data from the model and do not display anything if the data
// is null.
auto dc = m->GetPercentData();
if (!dc || !m->Exists()) {
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
ImGui::Text("Unknown SpeedController");
ImGui::PopStyleColor();
return;
}
// Add button to zero output.
if (ImGui::Button("Zero")) m->SetPercent(0.0);
ImGui::SameLine();
// Display a slider for the data.
float value = dc->GetValue();
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
if (dc->SliderFloat("% Output", &value, -1.0f, 1.0f)) m->SetPercent(value);
}

View File

@@ -0,0 +1,41 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "glass/other/CommandScheduler.h"
#include <imgui.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
using namespace glass;
void glass::DisplayCommandScheduler(CommandSchedulerModel* m) {
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 20);
ImGui::Text("Scheduled Commands: ");
ImGui::Separator();
ImGui::Spacing();
if (m->Exists()) {
float pos = ImGui::GetContentRegionAvail().x * 0.97f -
ImGui::CalcTextSize("Cancel").x;
const auto& commands = m->GetCurrentCommands();
for (size_t i = 0; i < commands.size(); ++i) {
ImGui::Text("%s", commands[i].c_str());
ImGui::SameLine(pos);
ImGui::PushID(i);
if (ImGui::Button("Cancel")) m->CancelCommand(i);
ImGui::PopID();
}
} else {
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
ImGui::Text("Unknown Scheduler");
ImGui::PopStyleColor();
}
}

View File

@@ -0,0 +1,34 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "glass/other/CommandSelector.h"
#include <imgui.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
using namespace glass;
void glass::DisplayCommandSelector(CommandSelectorModel* m) {
if (auto name = m->GetName()) ImGui::Text("%s", name);
if (m->Exists()) {
if (auto run = m->GetRunningData()) {
bool running = run->GetValue();
if (ImGui::Button(running ? "Cancel" : "Run")) {
running = !running;
m->SetRunning(running);
}
ImGui::SameLine();
if (running) ImGui::Text("Running...");
}
} else {
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
ImGui::Text("Unknown Command");
ImGui::PopStyleColor();
}
}

View File

@@ -0,0 +1,54 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "glass/other/PIDController.h"
#include <string>
#include <imgui.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
using namespace glass;
void glass::DisplayPIDController(PIDControllerModel* m) {
if (auto name = m->GetName()) {
ImGui::Text("%s", name);
ImGui::Separator();
}
if (m->Exists()) {
auto createTuningParameter = [](const char* name, double* v,
std::function<void(double)> callback) {
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 4);
if (ImGui::InputDouble(name, v, 0.0, 0.0, "%.3f")) callback(*v);
};
if (auto p = m->GetPData()) {
double value = p->GetValue();
createTuningParameter("P", &value, [=](auto v) { m->SetP(v); });
}
if (auto i = m->GetIData()) {
double value = i->GetValue();
createTuningParameter("I", &value, [=](auto v) { m->SetI(v); });
}
if (auto d = m->GetDData()) {
double value = d->GetValue();
createTuningParameter("D", &value, [=](auto v) { m->SetD(v); });
}
if (auto s = m->GetSetpointData()) {
double value = s->GetValue();
createTuningParameter("Setpoint", &value,
[=](auto v) { m->SetSetpoint(v); });
}
} else {
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
ImGui::Text("Unknown PID Controller");
ImGui::PopStyleColor();
}
}

View File

@@ -0,0 +1,32 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "glass/other/Subsystem.h"
#include <imgui.h>
#include "glass/Context.h"
#include "glass/DataSource.h"
using namespace glass;
void glass::DisplaySubsystem(SubsystemModel* m) {
if (auto name = m->GetName()) {
ImGui::Text("%s", name);
ImGui::Separator();
}
if (m->Exists()) {
std::string defaultCommand = m->GetDefaultCommand();
std::string currentCommand = m->GetCurrentCommand();
ImGui::Text("%s", ("Default Command: " + defaultCommand).c_str());
ImGui::Text("%s", ("Current Command: " + currentCommand).c_str());
} else {
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
ImGui::Text("Unknown Subsystem");
ImGui::PopStyleColor();
}
}