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) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 15 | : NTMecanumDriveModel(nt::NetworkTableInstance::GetDefault(), path) {} |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 16 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 17 | NTMecanumDriveModel::NTMecanumDriveModel(nt::NetworkTableInstance inst, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 18 | std::string_view path) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 19 | : m_inst{inst}, |
| 20 | m_name{inst.GetStringTopic(fmt::format("{}/.name", path)).Subscribe("")}, |
| 21 | m_controllable{inst.GetBooleanTopic(fmt::format("{}/.controllable", path)) |
| 22 | .Subscribe(0)}, |
| 23 | m_flPercent{ |
| 24 | inst.GetDoubleTopic(fmt::format("{}/Front Left Motor Speed", path)) |
| 25 | .GetEntry(0)}, |
| 26 | m_frPercent{ |
| 27 | inst.GetDoubleTopic(fmt::format("{}/Front Right Motor Speed", path)) |
| 28 | .GetEntry(0)}, |
| 29 | m_rlPercent{ |
| 30 | inst.GetDoubleTopic(fmt::format("{}/Rear Left Motor Speed", path)) |
| 31 | .GetEntry(0)}, |
| 32 | m_rrPercent{ |
| 33 | inst.GetDoubleTopic(fmt::format("{}/Rear Right Motor Speed", path)) |
| 34 | .GetEntry(0)}, |
| 35 | m_nameValue{wpi::rsplit(path, '/').second}, |
| 36 | m_flPercentData{fmt::format("NTMcnmDriveFL:{}", path)}, |
| 37 | m_frPercentData{fmt::format("NTMcnmDriveFR:{}", path)}, |
| 38 | m_rlPercentData{fmt::format("NTMcnmDriveRL:{}", path)}, |
| 39 | m_rrPercentData{fmt::format("NTMcnmDriveRR:{}", path)} { |
| 40 | m_wheels.emplace_back("FL % Output", &m_flPercentData, |
| 41 | [this](auto value) { m_flPercent.Set(value); }); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 42 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 43 | m_wheels.emplace_back("FR % Output", &m_frPercentData, |
| 44 | [this](auto value) { m_frPercent.Set(value); }); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 45 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 46 | m_wheels.emplace_back("RL % Output", &m_rlPercentData, |
| 47 | [this](auto value) { m_rlPercent.Set(value); }); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 48 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 49 | m_wheels.emplace_back("RR % Output", &m_rrPercentData, |
| 50 | [this](auto value) { m_rrPercent.Set(value); }); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void NTMecanumDriveModel::Update() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 54 | for (auto&& v : m_name.ReadQueue()) { |
| 55 | m_nameValue = std::move(v.value); |
| 56 | } |
| 57 | for (auto&& v : m_flPercent.ReadQueue()) { |
| 58 | m_flPercentData.SetValue(v.value, v.time); |
| 59 | } |
| 60 | for (auto&& v : m_frPercent.ReadQueue()) { |
| 61 | m_frPercentData.SetValue(v.value, v.time); |
| 62 | } |
| 63 | for (auto&& v : m_rlPercent.ReadQueue()) { |
| 64 | m_rlPercentData.SetValue(v.value, v.time); |
| 65 | } |
| 66 | for (auto&& v : m_rrPercent.ReadQueue()) { |
| 67 | m_rrPercentData.SetValue(v.value, v.time); |
| 68 | } |
| 69 | for (auto&& v : m_controllable.ReadQueue()) { |
| 70 | m_controllableValue = v.value; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | double fl = m_flPercentData.GetValue(); |
| 74 | double fr = m_frPercentData.GetValue(); |
| 75 | double rl = m_rlPercentData.GetValue(); |
| 76 | double rr = m_rrPercentData.GetValue(); |
| 77 | |
| 78 | m_speedVector = |
| 79 | ImVec2((fl - fr - rl + rr) / 4.0f, -(fl + fr + rl + rr) / 4.0f); |
| 80 | m_rotation = -(-fl + fr - rl + rr) / 4; |
| 81 | } |
| 82 | |
| 83 | bool NTMecanumDriveModel::Exists() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 84 | return m_flPercent.Exists(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 85 | } |