blob: cb564e81689a5767200069bed278c5aed67baf28 [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/NTMecanumDrive.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
14NTMecanumDriveModel::NTMecanumDriveModel(std::string_view path)
James Kuszmaulcf324122023-01-14 14:07:17 -080015 : NTMecanumDriveModel(nt::NetworkTableInstance::GetDefault(), path) {}
Austin Schuh812d0d12021-11-04 20:16:48 -070016
James Kuszmaulcf324122023-01-14 14:07:17 -080017NTMecanumDriveModel::NTMecanumDriveModel(nt::NetworkTableInstance inst,
Austin Schuh812d0d12021-11-04 20:16:48 -070018 std::string_view path)
James Kuszmaulcf324122023-01-14 14:07:17 -080019 : 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 Schuh812d0d12021-11-04 20:16:48 -070042
James Kuszmaulcf324122023-01-14 14:07:17 -080043 m_wheels.emplace_back("FR % Output", &m_frPercentData,
44 [this](auto value) { m_frPercent.Set(value); });
Austin Schuh812d0d12021-11-04 20:16:48 -070045
James Kuszmaulcf324122023-01-14 14:07:17 -080046 m_wheels.emplace_back("RL % Output", &m_rlPercentData,
47 [this](auto value) { m_rlPercent.Set(value); });
Austin Schuh812d0d12021-11-04 20:16:48 -070048
James Kuszmaulcf324122023-01-14 14:07:17 -080049 m_wheels.emplace_back("RR % Output", &m_rrPercentData,
50 [this](auto value) { m_rrPercent.Set(value); });
Austin Schuh812d0d12021-11-04 20:16:48 -070051}
52
53void NTMecanumDriveModel::Update() {
James Kuszmaulcf324122023-01-14 14:07:17 -080054 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 Schuh812d0d12021-11-04 20:16:48 -070071 }
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
83bool NTMecanumDriveModel::Exists() {
James Kuszmaulcf324122023-01-14 14:07:17 -080084 return m_flPercent.Exists();
Austin Schuh812d0d12021-11-04 20:16:48 -070085}