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/NTMecanumDrive.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 | NTMecanumDriveModel::NTMecanumDriveModel(std::string_view path) |
| 15 | : NTMecanumDriveModel(nt::GetDefaultInstance(), path) {} |
| 16 | |
| 17 | NTMecanumDriveModel::NTMecanumDriveModel(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_flPercent( |
| 23 | m_nt.GetEntry(fmt::format("{}/Front Left Motor Speed", path))), |
| 24 | m_frPercent( |
| 25 | m_nt.GetEntry(fmt::format("{}/Front Right Motor Speed", path))), |
| 26 | m_rlPercent(m_nt.GetEntry(fmt::format("{}/Rear Left Motor Speed", path))), |
| 27 | m_rrPercent( |
| 28 | m_nt.GetEntry(fmt::format("{}/Rear Right Motor Speed", path))), |
| 29 | m_nameValue(wpi::rsplit(path, '/').second), |
| 30 | m_flPercentData(fmt::format("NTMcnmDriveFL:{}", path)), |
| 31 | m_frPercentData(fmt::format("NTMcnmDriveFR:{}", path)), |
| 32 | m_rlPercentData(fmt::format("NTMcnmDriveRL:{}", path)), |
| 33 | m_rrPercentData(fmt::format("NTMcnmDriveRR:{}", path)) { |
| 34 | m_nt.AddListener(m_name); |
| 35 | m_nt.AddListener(m_controllable); |
| 36 | m_nt.AddListener(m_flPercent); |
| 37 | m_nt.AddListener(m_frPercent); |
| 38 | m_nt.AddListener(m_rlPercent); |
| 39 | m_nt.AddListener(m_rrPercent); |
| 40 | |
| 41 | m_wheels.emplace_back("FL % Output", &m_flPercentData, [this](auto value) { |
| 42 | nt::SetEntryValue(m_flPercent, nt::NetworkTableValue::MakeDouble(value)); |
| 43 | }); |
| 44 | |
| 45 | m_wheels.emplace_back("FR % Output", &m_frPercentData, [this](auto value) { |
| 46 | nt::SetEntryValue(m_frPercent, nt::NetworkTableValue::MakeDouble(value)); |
| 47 | }); |
| 48 | |
| 49 | m_wheels.emplace_back("RL % Output", &m_rlPercentData, [this](auto value) { |
| 50 | nt::SetEntryValue(m_rlPercent, nt::NetworkTableValue::MakeDouble(value)); |
| 51 | }); |
| 52 | |
| 53 | m_wheels.emplace_back("RR % Output", &m_rrPercentData, [this](auto value) { |
| 54 | nt::SetEntryValue(m_rrPercent, nt::NetworkTableValue::MakeDouble(value)); |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | void NTMecanumDriveModel::Update() { |
| 59 | for (auto&& event : m_nt.PollListener()) { |
| 60 | if (event.entry == m_name && event.value && event.value->IsString()) { |
| 61 | m_nameValue = event.value->GetString(); |
| 62 | } else if (event.entry == m_flPercent && event.value && |
| 63 | event.value->IsDouble()) { |
| 64 | m_flPercentData.SetValue(event.value->GetDouble()); |
| 65 | } else if (event.entry == m_frPercent && event.value && |
| 66 | event.value->IsDouble()) { |
| 67 | m_frPercentData.SetValue(event.value->GetDouble()); |
| 68 | } else if (event.entry == m_rlPercent && event.value && |
| 69 | event.value->IsDouble()) { |
| 70 | m_rlPercentData.SetValue(event.value->GetDouble()); |
| 71 | } else if (event.entry == m_rrPercent && event.value && |
| 72 | event.value->IsDouble()) { |
| 73 | m_rrPercentData.SetValue(event.value->GetDouble()); |
| 74 | } else if (event.entry == m_controllable && event.value && |
| 75 | event.value->IsBoolean()) { |
| 76 | m_controllableValue = event.value->GetBoolean(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | double fl = m_flPercentData.GetValue(); |
| 81 | double fr = m_frPercentData.GetValue(); |
| 82 | double rl = m_rlPercentData.GetValue(); |
| 83 | double rr = m_rrPercentData.GetValue(); |
| 84 | |
| 85 | m_speedVector = |
| 86 | ImVec2((fl - fr - rl + rr) / 4.0f, -(fl + fr + rl + rr) / 4.0f); |
| 87 | m_rotation = -(-fl + fr - rl + rr) / 4; |
| 88 | } |
| 89 | |
| 90 | bool NTMecanumDriveModel::Exists() { |
| 91 | return m_nt.IsConnected() && nt::GetEntryType(m_flPercent) != NT_UNASSIGNED; |
| 92 | } |