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.
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/glass/networktables/NTDifferentialDrive.hpp"
|
2020-12-30 11:51:55 -05:00
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2020-12-30 11:51:55 -05:00
|
|
|
#include <imgui.h>
|
2025-11-07 19:57:55 -05:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/util/MathExtras.hpp"
|
|
|
|
|
#include "wpi/util/StringExtras.hpp"
|
2020-12-30 11:51:55 -05:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi::glass;
|
2020-12-30 11:51:55 -05:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
NTDifferentialDriveModel::NTDifferentialDriveModel(std::string_view path)
|
2025-11-07 20:00:05 -05:00
|
|
|
: NTDifferentialDriveModel(wpi::nt::NetworkTableInstance::GetDefault(), path) {}
|
2022-10-08 10:01:31 -07:00
|
|
|
|
|
|
|
|
NTDifferentialDriveModel::NTDifferentialDriveModel(
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::nt::NetworkTableInstance inst, std::string_view path)
|
2022-10-08 10:01:31 -07:00
|
|
|
: m_inst{inst},
|
|
|
|
|
m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")},
|
|
|
|
|
m_controllable{inst.GetBooleanTopic(fmt::format("{}/.controllable", path))
|
|
|
|
|
.Subscribe(false)},
|
|
|
|
|
m_lPercent{inst.GetDoubleTopic(fmt::format("{}/Left Motor Speed", path))
|
|
|
|
|
.GetEntry(0)},
|
|
|
|
|
m_rPercent{inst.GetDoubleTopic(fmt::format("{}/Right Motor Speed", path))
|
|
|
|
|
.GetEntry(0)},
|
2025-11-07 20:00:05 -05:00
|
|
|
m_nameValue{wpi::util::rsplit(path, '/').second},
|
2022-10-08 10:01:31 -07:00
|
|
|
m_lPercentData{fmt::format("NTDiffDriveL:{}", path)},
|
|
|
|
|
m_rPercentData{fmt::format("NTDiffDriveR:{}", path)} {
|
|
|
|
|
m_wheels.emplace_back("L % Output", &m_lPercentData,
|
|
|
|
|
[this](auto value) { m_lPercent.Set(value); });
|
|
|
|
|
|
|
|
|
|
m_wheels.emplace_back("R % Output", &m_rPercentData,
|
|
|
|
|
[this](auto value) { m_rPercent.Set(value); });
|
2020-12-30 11:51:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NTDifferentialDriveModel::Update() {
|
2022-10-08 10:01:31 -07:00
|
|
|
for (auto&& v : m_name.ReadQueue()) {
|
|
|
|
|
m_nameValue = std::move(v.value);
|
|
|
|
|
}
|
|
|
|
|
for (auto&& v : m_lPercent.ReadQueue()) {
|
|
|
|
|
m_lPercentData.SetValue(v.value, v.time);
|
|
|
|
|
}
|
|
|
|
|
for (auto&& v : m_rPercent.ReadQueue()) {
|
|
|
|
|
m_rPercentData.SetValue(v.value, v.time);
|
|
|
|
|
}
|
|
|
|
|
for (auto&& v : m_controllable.ReadQueue()) {
|
|
|
|
|
m_controllableValue = v.value;
|
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() {
|
2022-11-20 17:27:28 -08:00
|
|
|
return m_lPercent.Exists();
|
2020-12-30 11:51:55 -05:00
|
|
|
}
|