blob: 84c1ce78cc4a68b44bb28472cc70e7ecf407d9a5 [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/NTFMS.h"
6
7#include <stdint.h>
8
9#include <fmt/format.h>
10#include <wpi/SmallVector.h>
11#include <wpi/timestamp.h>
12
13using namespace glass;
14
15NTFMSModel::NTFMSModel(std::string_view path)
16 : NTFMSModel{nt::GetDefaultInstance(), path} {}
17
18NTFMSModel::NTFMSModel(NT_Inst inst, std::string_view path)
19 : m_nt{inst},
20 m_gameSpecificMessage{
21 m_nt.GetEntry(fmt::format("{}/GameSpecificMessage", path))},
22 m_alliance{m_nt.GetEntry(fmt::format("{}/IsRedAlliance", path))},
23 m_station{m_nt.GetEntry(fmt::format("{}/StationNumber", path))},
24 m_controlWord{m_nt.GetEntry(fmt::format("{}/FMSControlData", path))},
25 m_fmsAttached{fmt::format("NT_FMS:FMSAttached:{}", path)},
26 m_dsAttached{fmt::format("NT_FMS:DSAttached:{}", path)},
27 m_allianceStationId{fmt::format("NT_FMS:AllianceStationID:{}", path)},
28 m_estop{fmt::format("NT_FMS:EStop:{}", path)},
29 m_enabled{fmt::format("NT_FMS:RobotEnabled:{}", path)},
30 m_test{fmt::format("NT_FMS:TestMode:{}", path)},
31 m_autonomous{fmt::format("NT_FMS:AutonomousMode:{}", path)} {
32 m_nt.AddListener(m_alliance);
33 m_nt.AddListener(m_station);
34 m_nt.AddListener(m_controlWord);
35
36 m_fmsAttached.SetDigital(true);
37 m_dsAttached.SetDigital(true);
38 m_estop.SetDigital(true);
39 m_enabled.SetDigital(true);
40 m_test.SetDigital(true);
41 m_autonomous.SetDigital(true);
42}
43
44std::string_view NTFMSModel::GetGameSpecificMessage(
45 wpi::SmallVectorImpl<char>& buf) {
46 buf.clear();
47 auto value = nt::GetEntryValue(m_gameSpecificMessage);
48 if (value && value->IsString()) {
49 auto str = value->GetString();
50 buf.append(str.begin(), str.end());
51 }
52 return std::string_view{buf.data(), buf.size()};
53}
54
55void NTFMSModel::Update() {
56 for (auto&& event : m_nt.PollListener()) {
57 if (event.entry == m_alliance) {
58 if (event.value && event.value->IsBoolean()) {
59 int allianceStationId = m_allianceStationId.GetValue();
60 allianceStationId %= 3;
61 // true if red
62 allianceStationId += 3 * (event.value->GetBoolean() ? 0 : 1);
63 m_allianceStationId.SetValue(allianceStationId);
64 }
65 } else if (event.entry == m_station) {
66 if (event.value && event.value->IsDouble()) {
67 int allianceStationId = m_allianceStationId.GetValue();
68 bool isRed = (allianceStationId < 3);
69 // the NT value is 1-indexed
70 m_allianceStationId.SetValue(event.value->GetDouble() - 1 +
71 3 * (isRed ? 0 : 1));
72 }
73 } else if (event.entry == m_controlWord) {
74 if (event.value && event.value->IsDouble()) {
75 uint32_t controlWord = event.value->GetDouble();
76 // See HAL_ControlWord definition
77 auto time = wpi::Now();
78 m_enabled.SetValue(((controlWord & 0x01) != 0) ? 1 : 0, time);
79 m_autonomous.SetValue(((controlWord & 0x02) != 0) ? 1 : 0, time);
80 m_test.SetValue(((controlWord & 0x04) != 0) ? 1 : 0, time);
81 m_estop.SetValue(((controlWord & 0x08) != 0) ? 1 : 0, time);
82 m_fmsAttached.SetValue(((controlWord & 0x10) != 0) ? 1 : 0, time);
83 m_dsAttached.SetValue(((controlWord & 0x20) != 0) ? 1 : 0, time);
84 }
85 }
86 }
87}
88
89bool NTFMSModel::Exists() {
90 return m_nt.IsConnected() && nt::GetEntryType(m_controlWord) != NT_UNASSIGNED;
91}