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) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 15 | : NTDifferentialDriveModel(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 | NTDifferentialDriveModel::NTDifferentialDriveModel( |
| 18 | nt::NetworkTableInstance inst, std::string_view path) |
| 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(false)}, |
| 23 | m_lPercent{inst.GetDoubleTopic(fmt::format("{}/Left Motor Speed", path)) |
| 24 | .GetEntry(0)}, |
| 25 | m_rPercent{inst.GetDoubleTopic(fmt::format("{}/Right Motor Speed", path)) |
| 26 | .GetEntry(0)}, |
| 27 | m_nameValue{wpi::rsplit(path, '/').second}, |
| 28 | m_lPercentData{fmt::format("NTDiffDriveL:{}", path)}, |
| 29 | m_rPercentData{fmt::format("NTDiffDriveR:{}", path)} { |
| 30 | m_wheels.emplace_back("L % Output", &m_lPercentData, |
| 31 | [this](auto value) { m_lPercent.Set(value); }); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 32 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 33 | m_wheels.emplace_back("R % Output", &m_rPercentData, |
| 34 | [this](auto value) { m_rPercent.Set(value); }); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void NTDifferentialDriveModel::Update() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 38 | for (auto&& v : m_name.ReadQueue()) { |
| 39 | m_nameValue = std::move(v.value); |
| 40 | } |
| 41 | for (auto&& v : m_lPercent.ReadQueue()) { |
| 42 | m_lPercentData.SetValue(v.value, v.time); |
| 43 | } |
| 44 | for (auto&& v : m_rPercent.ReadQueue()) { |
| 45 | m_rPercentData.SetValue(v.value, v.time); |
| 46 | } |
| 47 | for (auto&& v : m_controllable.ReadQueue()) { |
| 48 | m_controllableValue = v.value; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | double l = m_lPercentData.GetValue(); |
| 52 | double r = m_rPercentData.GetValue(); |
| 53 | |
| 54 | m_speedVector = ImVec2(0.0, -(l + r) / 2.0); |
| 55 | m_rotation = (l - r) / 2.0; |
| 56 | } |
| 57 | |
| 58 | bool NTDifferentialDriveModel::Exists() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 59 | return m_lPercent.Exists(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 60 | } |