[glass] Add drive class widgets (#2975)

This adds widgets for DifferentialDrive and MecanumDrive.
This commit is contained in:
Prateek Machiraju
2020-12-30 11:51:55 -05:00
committed by GitHub
parent 00fa91d0d6
commit ee7114a58c
7 changed files with 439 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
// 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 "glass/networktables/NTDifferentialDrive.h"
#include <imgui.h>
#include <wpi/MathExtras.h>
using namespace glass;
NTDifferentialDriveModel::NTDifferentialDriveModel(wpi::StringRef path)
: NTDifferentialDriveModel(nt::GetDefaultInstance(), path) {}
NTDifferentialDriveModel::NTDifferentialDriveModel(NT_Inst instance,
wpi::StringRef path)
: m_nt(instance),
m_name(m_nt.GetEntry(path + "/.name")),
m_lPercent(m_nt.GetEntry(path + "/Left Motor Speed")),
m_rPercent(m_nt.GetEntry(path + "/Right Motor Speed")),
m_nameValue(path.rsplit('/').second),
m_lPercentData("NTDiffDriveL:" + path),
m_rPercentData("NTDiffDriveR:" + path) {
m_nt.AddListener(m_name);
m_nt.AddListener(m_lPercent);
m_nt.AddListener(m_rPercent);
m_wheels.emplace_back("L % Output", &m_lPercentData, [this](auto value) {
nt::SetEntryValue(m_lPercent, nt::NetworkTableValue::MakeDouble(value));
});
m_wheels.emplace_back("R % Output", &m_rPercentData, [this](auto value) {
nt::SetEntryValue(m_rPercent, nt::NetworkTableValue::MakeDouble(value));
});
}
void NTDifferentialDriveModel::Update() {
for (auto&& event : m_nt.PollListener()) {
if (event.entry == m_name && event.value && event.value->IsString()) {
m_nameValue = event.value->GetString();
} else if (event.entry == m_lPercent && event.value &&
event.value->IsDouble()) {
m_lPercentData.SetValue(event.value->GetDouble());
} else if (event.entry == m_rPercent && event.value &&
event.value->IsDouble()) {
m_rPercentData.SetValue(event.value->GetDouble());
}
}
double l = m_lPercentData.GetValue();
double r = m_rPercentData.GetValue();
m_speedVector = ImVec2(0.0, -(l + r) / 2.0);
m_rotation = (l - r) / 2.0;
}
bool NTDifferentialDriveModel::Exists() {
return m_nt.IsConnected() && nt::GetEntryType(m_lPercent) != NT_UNASSIGNED;
}

View File

@@ -0,0 +1,81 @@
// 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 "glass/networktables/NTMecanumDrive.h"
#include <imgui.h>
#include <wpi/MathExtras.h>
using namespace glass;
NTMecanumDriveModel::NTMecanumDriveModel(wpi::StringRef path)
: NTMecanumDriveModel(nt::GetDefaultInstance(), path) {}
NTMecanumDriveModel::NTMecanumDriveModel(NT_Inst instance, wpi::StringRef path)
: m_nt(instance),
m_name(m_nt.GetEntry(path + "/.name")),
m_flPercent(m_nt.GetEntry(path + "/Front Left Motor Speed")),
m_frPercent(m_nt.GetEntry(path + "/Front Right Motor Speed")),
m_rlPercent(m_nt.GetEntry(path + "/Rear Left Motor Speed")),
m_rrPercent(m_nt.GetEntry(path + "/Rear Right Motor Speed")),
m_nameValue(path.rsplit('/').second),
m_flPercentData("NTMcnmDriveFL:" + path),
m_frPercentData("NTMcnmDriveFR:" + path),
m_rlPercentData("NTMcnmDriveRL:" + path),
m_rrPercentData("NTMcnmDriveRR:" + path) {
m_nt.AddListener(m_name);
m_nt.AddListener(m_flPercent);
m_nt.AddListener(m_frPercent);
m_nt.AddListener(m_rlPercent);
m_nt.AddListener(m_rrPercent);
m_wheels.emplace_back("FL % Output", &m_flPercentData, [this](auto value) {
nt::SetEntryValue(m_flPercent, nt::NetworkTableValue::MakeDouble(value));
});
m_wheels.emplace_back("FR % Output", &m_frPercentData, [this](auto value) {
nt::SetEntryValue(m_frPercent, nt::NetworkTableValue::MakeDouble(value));
});
m_wheels.emplace_back("RL % Output", &m_rlPercentData, [this](auto value) {
nt::SetEntryValue(m_rlPercent, nt::NetworkTableValue::MakeDouble(value));
});
m_wheels.emplace_back("RR % Output", &m_rrPercentData, [this](auto value) {
nt::SetEntryValue(m_rrPercent, nt::NetworkTableValue::MakeDouble(value));
});
}
void NTMecanumDriveModel::Update() {
for (auto&& event : m_nt.PollListener()) {
if (event.entry == m_name && event.value && event.value->IsString()) {
m_nameValue = event.value->GetString();
} else if (event.entry == m_flPercent && event.value &&
event.value->IsDouble()) {
m_flPercentData.SetValue(event.value->GetDouble());
} else if (event.entry == m_frPercent && event.value &&
event.value->IsDouble()) {
m_frPercentData.SetValue(event.value->GetDouble());
} else if (event.entry == m_rlPercent && event.value &&
event.value->IsDouble()) {
m_rlPercentData.SetValue(event.value->GetDouble());
} else if (event.entry == m_rrPercent && event.value &&
event.value->IsDouble()) {
m_rrPercentData.SetValue(event.value->GetDouble());
}
}
double fl = m_flPercentData.GetValue();
double fr = m_frPercentData.GetValue();
double rl = m_rlPercentData.GetValue();
double rr = m_rrPercentData.GetValue();
m_speedVector =
ImVec2((fl - fr - rl + rr) / 4.0f, -(fl + fr + rl + rr) / 4.0f);
m_rotation = -(-fl + fr - rl + rr) / 4;
}
bool NTMecanumDriveModel::Exists() {
return m_nt.IsConnected() && nt::GetEntryType(m_flPercent) != NT_UNASSIGNED;
}

View File

@@ -4,11 +4,13 @@
#include "glass/networktables/NTCommandScheduler.h"
#include "glass/networktables/NTCommandSelector.h"
#include "glass/networktables/NTDifferentialDrive.h"
#include "glass/networktables/NTDigitalInput.h"
#include "glass/networktables/NTDigitalOutput.h"
#include "glass/networktables/NTFMS.h"
#include "glass/networktables/NTField2D.h"
#include "glass/networktables/NTGyro.h"
#include "glass/networktables/NTMecanumDrive.h"
#include "glass/networktables/NTPIDController.h"
#include "glass/networktables/NTSpeedController.h"
#include "glass/networktables/NTStringChooser.h"
@@ -40,6 +42,17 @@ void glass::AddStandardNetworkTablesViews(NetworkTablesProvider& provider) {
DisplayCommandSelector(static_cast<NTCommandSelectorModel*>(model));
});
});
provider.Register(
NTDifferentialDriveModel::kType,
[](NT_Inst inst, const char* path) {
return std::make_unique<NTDifferentialDriveModel>(inst, path);
},
[](Window* win, Model* model, const char*) {
win->SetDefaultSize(300, 350);
return MakeFunctionView([=] {
DisplayDrive(static_cast<NTDifferentialDriveModel*>(model));
});
});
provider.Register(
NTFMSModel::kType,
[](NT_Inst inst, const char* path) {
@@ -94,6 +107,16 @@ void glass::AddStandardNetworkTablesViews(NetworkTablesProvider& provider) {
return MakeFunctionView(
[=] { DisplayGyro(static_cast<NTGyroModel*>(model)); });
});
provider.Register(
NTMecanumDriveModel::kType,
[](NT_Inst inst, const char* path) {
return std::make_unique<NTMecanumDriveModel>(inst, path);
},
[](Window* win, Model* model, const char*) {
win->SetDefaultSize(300, 350);
return MakeFunctionView(
[=] { DisplayDrive(static_cast<NTMecanumDriveModel*>(model)); });
});
provider.Register(
NTPIDControllerModel::kType,
[](NT_Inst inst, const char* path) {