blob: 57d1fa837c10425097cd4d36227a0a4db5892042 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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
12using namespace glass;
13
14NTDifferentialDriveModel::NTDifferentialDriveModel(std::string_view path)
James Kuszmaulcf324122023-01-14 14:07:17 -080015 : NTDifferentialDriveModel(nt::NetworkTableInstance::GetDefault(), path) {}
Austin Schuh812d0d12021-11-04 20:16:48 -070016
James Kuszmaulcf324122023-01-14 14:07:17 -080017NTDifferentialDriveModel::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 Schuh812d0d12021-11-04 20:16:48 -070032
James Kuszmaulcf324122023-01-14 14:07:17 -080033 m_wheels.emplace_back("R % Output", &m_rPercentData,
34 [this](auto value) { m_rPercent.Set(value); });
Austin Schuh812d0d12021-11-04 20:16:48 -070035}
36
37void NTDifferentialDriveModel::Update() {
James Kuszmaulcf324122023-01-14 14:07:17 -080038 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 Schuh812d0d12021-11-04 20:16:48 -070049 }
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
58bool NTDifferentialDriveModel::Exists() {
James Kuszmaulcf324122023-01-14 14:07:17 -080059 return m_lPercent.Exists();
Austin Schuh812d0d12021-11-04 20:16:48 -070060}