[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,55 @@
/*----------------------------------------------------------------------------*/
/* 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/networktables/NTCommandScheduler.h"
#include <iostream>
using namespace glass;
NTCommandSchedulerModel::NTCommandSchedulerModel(wpi::StringRef path)
: NTCommandSchedulerModel(nt::GetDefaultInstance(), path) {}
NTCommandSchedulerModel::NTCommandSchedulerModel(NT_Inst instance,
wpi::StringRef path)
: m_nt(instance),
m_name(m_nt.GetEntry(path + "/.name")),
m_commands(m_nt.GetEntry(path + "/Names")),
m_ids(m_nt.GetEntry(path + "/Ids")),
m_cancel(m_nt.GetEntry(path + "/Cancel")),
m_nameValue(path.rsplit('/').second) {
m_nt.AddListener(m_name);
m_nt.AddListener(m_commands);
m_nt.AddListener(m_ids);
m_nt.AddListener(m_cancel);
Update();
}
void NTCommandSchedulerModel::CancelCommand(size_t index) {
if (index < m_idsValue.size())
nt::SetEntryValue(
m_cancel, nt::NetworkTableValue::MakeDoubleArray({m_idsValue[index]}));
}
void NTCommandSchedulerModel::Update() {
for (auto&& event : m_nt.PollListener()) {
if (event.entry == m_name) {
if (event.value && event.value->IsString())
m_nameValue = event.value->GetString();
} else if (event.entry == m_commands) {
if (event.value && event.value->IsStringArray())
m_commandsValue = event.value->GetStringArray();
} else if (event.entry == m_ids) {
if (event.value && event.value->IsDoubleArray())
m_idsValue = event.value->GetDoubleArray();
}
}
}
bool NTCommandSchedulerModel::Exists() {
return m_nt.IsConnected() && nt::GetEntryType(m_commands) != NT_UNASSIGNED;
}

View File

@@ -0,0 +1,46 @@
/*----------------------------------------------------------------------------*/
/* 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/networktables/NTCommandSelector.h"
using namespace glass;
NTCommandSelectorModel::NTCommandSelectorModel(wpi::StringRef path)
: NTCommandSelectorModel(nt::GetDefaultInstance(), path) {}
NTCommandSelectorModel::NTCommandSelectorModel(NT_Inst instance,
wpi::StringRef path)
: m_nt(instance),
m_running(m_nt.GetEntry(path + "/running")),
m_name(m_nt.GetEntry(path + "/.name")),
m_runningData("NTCmd:" + path),
m_nameValue(path.rsplit('/').second) {
m_runningData.SetDigital(true);
m_nt.AddListener(m_running);
m_nt.AddListener(m_name);
Update();
}
void NTCommandSelectorModel::SetRunning(bool run) {
nt::SetEntryValue(m_running, nt::NetworkTableValue::MakeBoolean(run));
}
void NTCommandSelectorModel::Update() {
for (auto&& event : m_nt.PollListener()) {
if (event.entry == m_running) {
if (event.value && event.value->IsBoolean())
m_runningData.SetValue(event.value->GetBoolean());
} else if (event.entry == m_name) {
if (event.value && event.value->IsString())
m_nameValue = event.value->GetString();
}
}
}
bool NTCommandSelectorModel::Exists() {
return m_nt.IsConnected() && nt::GetEntryType(m_running) != NT_UNASSIGNED;
}

View File

@@ -0,0 +1,40 @@
/*----------------------------------------------------------------------------*/
/* 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/networktables/NTGyro.h"
using namespace glass;
NTGyroModel::NTGyroModel(wpi::StringRef path)
: NTGyroModel(nt::GetDefaultInstance(), path) {}
NTGyroModel::NTGyroModel(NT_Inst instance, wpi::StringRef path)
: m_nt(instance),
m_angle(m_nt.GetEntry(path + "/Value")),
m_name(m_nt.GetEntry(path + "/.name")),
m_angleData("NT_Gyro:" + path),
m_nameValue(path.rsplit('/').second) {
m_nt.AddListener(m_angle);
m_nt.AddListener(m_name);
Update();
}
void NTGyroModel::Update() {
for (auto&& event : m_nt.PollListener()) {
if (event.entry == m_angle) {
if (event.value && event.value->IsDouble())
m_angleData.SetValue(event.value->GetDouble());
} else if (event.entry == m_name) {
if (event.value && event.value->IsString())
m_nameValue = event.value->GetString();
}
}
}
bool NTGyroModel::Exists() {
return m_nt.IsConnected() && nt::GetEntryType(m_angle) != NT_UNASSIGNED;
}

View File

@@ -0,0 +1,75 @@
/*----------------------------------------------------------------------------*/
/* 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/networktables/NTPIDController.h"
using namespace glass;
NTPIDControllerModel::NTPIDControllerModel(wpi::StringRef path)
: NTPIDControllerModel(nt::GetDefaultInstance(), path) {}
NTPIDControllerModel::NTPIDControllerModel(NT_Inst instance,
wpi::StringRef path)
: m_nt(instance),
m_name(m_nt.GetEntry(path + "/.name")),
m_p(m_nt.GetEntry(path + "/p")),
m_i(m_nt.GetEntry(path + "/i")),
m_d(m_nt.GetEntry(path + "/d")),
m_setpoint(m_nt.GetEntry(path + "/setpoint")),
m_pData("NTPIDCtrlP:" + path),
m_iData("NTPIDCtrlI:" + path),
m_dData("NTPIDCtrlD:" + path),
m_setpointData("NTPIDCtrlStpt:" + path),
m_nameValue(path.rsplit('/').second) {
m_nt.AddListener(m_name);
m_nt.AddListener(m_p);
m_nt.AddListener(m_i);
m_nt.AddListener(m_d);
m_nt.AddListener(m_setpoint);
Update();
}
void NTPIDControllerModel::SetP(double value) {
nt::SetEntryValue(m_p, nt::NetworkTableValue::MakeDouble(value));
}
void NTPIDControllerModel::SetI(double value) {
nt::SetEntryValue(m_i, nt::NetworkTableValue::MakeDouble(value));
}
void NTPIDControllerModel::SetD(double value) {
nt::SetEntryValue(m_d, nt::NetworkTableValue::MakeDouble(value));
}
void NTPIDControllerModel::SetSetpoint(double value) {
nt::SetEntryValue(m_setpoint, nt::NetworkTableValue::MakeDouble(value));
}
void NTPIDControllerModel::Update() {
for (auto&& event : m_nt.PollListener()) {
if (event.entry == m_name) {
if (event.value && event.value->IsString())
m_nameValue = event.value->GetString();
} else if (event.entry == m_p) {
if (event.value && event.value->IsDouble())
m_pData.SetValue(event.value->GetDouble());
} else if (event.entry == m_i) {
if (event.value && event.value->IsDouble())
m_iData.SetValue(event.value->GetDouble());
} else if (event.entry == m_d) {
if (event.value && event.value->IsDouble())
m_dData.SetValue(event.value->GetDouble());
} else if (event.entry == m_setpoint) {
if (event.value && event.value->IsDouble())
m_setpointData.SetValue(event.value->GetDouble());
}
}
}
bool NTPIDControllerModel::Exists() {
return m_nt.IsConnected() && nt::GetEntryType(m_setpoint) != NT_UNASSIGNED;
}

View File

@@ -0,0 +1,45 @@
/*----------------------------------------------------------------------------*/
/* 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/networktables/NTSpeedController.h"
using namespace glass;
NTSpeedControllerModel::NTSpeedControllerModel(wpi::StringRef path)
: NTSpeedControllerModel(nt::GetDefaultInstance(), path) {}
NTSpeedControllerModel::NTSpeedControllerModel(NT_Inst instance,
wpi::StringRef path)
: m_nt(instance),
m_value(m_nt.GetEntry(path + "/Value")),
m_name(m_nt.GetEntry(path + "/.name")),
m_valueData("NT_SpdCtrl:" + path),
m_nameValue(path.rsplit('/').second) {
m_nt.AddListener(m_value);
m_nt.AddListener(m_name);
Update();
}
void NTSpeedControllerModel::SetPercent(double value) {
nt::SetEntryValue(m_value, nt::NetworkTableValue::MakeDouble(value));
}
void NTSpeedControllerModel::Update() {
for (auto&& event : m_nt.PollListener()) {
if (event.entry == m_value) {
if (event.value && event.value->IsDouble())
m_valueData.SetValue(event.value->GetDouble());
} else if (event.entry == m_name) {
if (event.value && event.value->IsString())
m_nameValue = event.value->GetString();
}
}
}
bool NTSpeedControllerModel::Exists() {
return m_nt.IsConnected() && nt::GetEntryType(m_value) != NT_UNASSIGNED;
}

View File

@@ -0,0 +1,45 @@
/*----------------------------------------------------------------------------*/
/* 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/networktables/NTSubsystem.h"
using namespace glass;
NTSubsystemModel::NTSubsystemModel(wpi::StringRef path)
: NTSubsystemModel(nt::GetDefaultInstance(), path) {}
NTSubsystemModel::NTSubsystemModel(NT_Inst instance, wpi::StringRef path)
: m_nt(instance),
m_name(m_nt.GetEntry(path + "/.name")),
m_defaultCommand(m_nt.GetEntry(path + "/.default")),
m_currentCommand(m_nt.GetEntry(path + "/.command")) {
m_nt.AddListener(m_name);
m_nt.AddListener(m_defaultCommand);
m_nt.AddListener(m_currentCommand);
Update();
}
void NTSubsystemModel::Update() {
for (auto&& event : m_nt.PollListener()) {
if (event.entry == m_name) {
if (event.value && event.value->IsString())
m_nameValue = event.value->GetString();
} else if (event.entry == m_defaultCommand) {
if (event.value && event.value->IsString())
m_defaultCommandValue = event.value->GetString();
} else if (event.entry == m_currentCommand) {
if (event.value && event.value->IsString()) {
m_currentCommandValue = event.value->GetString();
}
}
}
}
bool NTSubsystemModel::Exists() {
return m_nt.IsConnected() &&
nt::GetEntryType(m_defaultCommand) != NT_UNASSIGNED;
}

View File

@@ -5,16 +5,44 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include "glass/networktables/NTCommandScheduler.h"
#include "glass/networktables/NTCommandSelector.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/NTPIDController.h"
#include "glass/networktables/NTSpeedController.h"
#include "glass/networktables/NTStringChooser.h"
#include "glass/networktables/NTSubsystem.h"
#include "glass/networktables/NetworkTablesProvider.h"
using namespace glass;
void glass::AddStandardNetworkTablesViews(NetworkTablesProvider& provider) {
provider.Register(
NTCommandSchedulerModel::kType,
[](NT_Inst inst, const char* path) {
return std::make_unique<NTCommandSchedulerModel>(inst, path);
},
[](Window* win, Model* model, const char*) {
win->SetDefaultSize(400, 200);
return MakeFunctionView([=] {
DisplayCommandScheduler(static_cast<NTCommandSchedulerModel*>(model));
});
});
provider.Register(
NTCommandSelectorModel::kType,
[](NT_Inst inst, const char* path) {
return std::make_unique<NTCommandSelectorModel>(inst, path);
},
[](Window* win, Model* model, const char*) {
win->SetFlags(ImGuiWindowFlags_AlwaysAutoResize);
return MakeFunctionView([=] {
DisplayCommandSelector(static_cast<NTCommandSelectorModel*>(model));
});
});
provider.Register(
NTFMSModel::kType,
[](NT_Inst inst, const char* path) {
@@ -59,6 +87,38 @@ void glass::AddStandardNetworkTablesViews(NetworkTablesProvider& provider) {
return std::make_unique<Field2DView>(
static_cast<NTField2DModel*>(model));
});
provider.Register(
NTGyroModel::kType,
[](NT_Inst inst, const char* path) {
return std::make_unique<NTGyroModel>(inst, path);
},
[](Window* win, Model* model, const char* path) {
win->SetDefaultSize(320, 380);
return MakeFunctionView(
[=] { DisplayGyro(static_cast<NTGyroModel*>(model)); });
});
provider.Register(
NTPIDControllerModel::kType,
[](NT_Inst inst, const char* path) {
return std::make_unique<NTPIDControllerModel>(inst, path);
},
[](Window* win, Model* model, const char* path) {
win->SetFlags(ImGuiWindowFlags_AlwaysAutoResize);
return MakeFunctionView([=] {
DisplayPIDController(static_cast<NTPIDControllerModel*>(model));
});
});
provider.Register(
NTSpeedControllerModel::kType,
[](NT_Inst inst, const char* path) {
return std::make_unique<NTSpeedControllerModel>(inst, path);
},
[](Window* win, Model* model, const char* path) {
win->SetFlags(ImGuiWindowFlags_AlwaysAutoResize);
return MakeFunctionView([=] {
DisplaySpeedController(static_cast<NTSpeedControllerModel*>(model));
});
});
provider.Register(
NTStringChooserModel::kType,
[](NT_Inst inst, const char* path) {
@@ -70,4 +130,14 @@ void glass::AddStandardNetworkTablesViews(NetworkTablesProvider& provider) {
DisplayStringChooser(static_cast<NTStringChooserModel*>(model));
});
});
provider.Register(
NTSubsystemModel::kType,
[](NT_Inst inst, const char* path) {
return std::make_unique<NTSubsystemModel>(inst, path);
},
[](Window* win, Model* model, const char*) {
win->SetFlags(ImGuiWindowFlags_AlwaysAutoResize);
return MakeFunctionView(
[=] { DisplaySubsystem(static_cast<NTSubsystemModel*>(model)); });
});
}