Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include "glass/networktables/NTDifferentialDrive.h" |
| 6 | |
| 7 | #include <fmt/format.h> |
| 8 | #include <imgui.h> |
| 9 | #include <wpi/MathExtras.h> |
| 10 | #include <wpi/StringExtras.h> |
| 11 | |
| 12 | using namespace glass; |
| 13 | |
| 14 | NTDifferentialDriveModel::NTDifferentialDriveModel(std::string_view path) |
| 15 | : NTDifferentialDriveModel(nt::GetDefaultInstance(), path) {} |
| 16 | |
| 17 | NTDifferentialDriveModel::NTDifferentialDriveModel(NT_Inst instance, |
| 18 | std::string_view path) |
| 19 | : m_nt(instance), |
| 20 | m_name(m_nt.GetEntry(fmt::format("{}/.name", path))), |
| 21 | m_controllable(m_nt.GetEntry(fmt::format("{}/.controllable", path))), |
| 22 | m_lPercent(m_nt.GetEntry(fmt::format("{}/Left Motor Speed", path))), |
| 23 | m_rPercent(m_nt.GetEntry(fmt::format("{}/Right Motor Speed", path))), |
| 24 | m_nameValue(wpi::rsplit(path, '/').second), |
| 25 | m_lPercentData(fmt::format("NTDiffDriveL:{}", path)), |
| 26 | m_rPercentData(fmt::format("NTDiffDriveR:{}", path)) { |
| 27 | m_nt.AddListener(m_name); |
| 28 | m_nt.AddListener(m_controllable); |
| 29 | m_nt.AddListener(m_lPercent); |
| 30 | m_nt.AddListener(m_rPercent); |
| 31 | |
| 32 | m_wheels.emplace_back("L % Output", &m_lPercentData, [this](auto value) { |
| 33 | nt::SetEntryValue(m_lPercent, nt::NetworkTableValue::MakeDouble(value)); |
| 34 | }); |
| 35 | |
| 36 | m_wheels.emplace_back("R % Output", &m_rPercentData, [this](auto value) { |
| 37 | nt::SetEntryValue(m_rPercent, nt::NetworkTableValue::MakeDouble(value)); |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | void NTDifferentialDriveModel::Update() { |
| 42 | for (auto&& event : m_nt.PollListener()) { |
| 43 | if (event.entry == m_name && event.value && event.value->IsString()) { |
| 44 | m_nameValue = event.value->GetString(); |
| 45 | } else if (event.entry == m_lPercent && event.value && |
| 46 | event.value->IsDouble()) { |
| 47 | m_lPercentData.SetValue(event.value->GetDouble()); |
| 48 | } else if (event.entry == m_rPercent && event.value && |
| 49 | event.value->IsDouble()) { |
| 50 | m_rPercentData.SetValue(event.value->GetDouble()); |
| 51 | } else if (event.entry == m_controllable && event.value && |
| 52 | event.value->IsBoolean()) { |
| 53 | m_controllableValue = event.value->GetBoolean(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | double l = m_lPercentData.GetValue(); |
| 58 | double r = m_rPercentData.GetValue(); |
| 59 | |
| 60 | m_speedVector = ImVec2(0.0, -(l + r) / 2.0); |
| 61 | m_rotation = (l - r) / 2.0; |
| 62 | } |
| 63 | |
| 64 | bool NTDifferentialDriveModel::Exists() { |
| 65 | return m_nt.IsConnected() && nt::GetEntryType(m_lPercent) != NT_UNASSIGNED; |
| 66 | } |