2020-12-30 11:51:55 -05: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 "glass/networktables/NTDifferentialDrive.h"
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2020-12-30 11:51:55 -05:00
|
|
|
#include <imgui.h>
|
|
|
|
|
#include <wpi/MathExtras.h>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <wpi/StringExtras.h>
|
2020-12-30 11:51:55 -05:00
|
|
|
|
|
|
|
|
using namespace glass;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
NTDifferentialDriveModel::NTDifferentialDriveModel(std::string_view path)
|
2020-12-30 11:51:55 -05:00
|
|
|
: NTDifferentialDriveModel(nt::GetDefaultInstance(), path) {}
|
|
|
|
|
|
|
|
|
|
NTDifferentialDriveModel::NTDifferentialDriveModel(NT_Inst instance,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view path)
|
2020-12-30 11:51:55 -05:00
|
|
|
: m_nt(instance),
|
2021-06-06 16:13:58 -07:00
|
|
|
m_name(m_nt.GetEntry(fmt::format("{}/.name", path))),
|
|
|
|
|
m_controllable(m_nt.GetEntry(fmt::format("{}/.controllable", path))),
|
|
|
|
|
m_lPercent(m_nt.GetEntry(fmt::format("{}/Left Motor Speed", path))),
|
|
|
|
|
m_rPercent(m_nt.GetEntry(fmt::format("{}/Right Motor Speed", path))),
|
|
|
|
|
m_nameValue(wpi::rsplit(path, '/').second),
|
|
|
|
|
m_lPercentData(fmt::format("NTDiffDriveL:{}", path)),
|
|
|
|
|
m_rPercentData(fmt::format("NTDiffDriveR:{}", path)) {
|
2020-12-30 11:51:55 -05:00
|
|
|
m_nt.AddListener(m_name);
|
2021-01-05 21:33:05 -05:00
|
|
|
m_nt.AddListener(m_controllable);
|
2020-12-30 11:51:55 -05:00
|
|
|
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());
|
2021-01-05 21:33:05 -05:00
|
|
|
} else if (event.entry == m_controllable && event.value &&
|
|
|
|
|
event.value->IsBoolean()) {
|
|
|
|
|
m_controllableValue = event.value->GetBoolean();
|
2020-12-30 11:51:55 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|