blob: 65ffa6a435f30d9c75c5a39afa34f2fd6ee2aea1 [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)
James Kuszmaulcf324122023-01-14 14:07:17 -080016 : NTFMSModel{nt::NetworkTableInstance::GetDefault(), path} {}
Austin Schuh812d0d12021-11-04 20:16:48 -070017
James Kuszmaulcf324122023-01-14 14:07:17 -080018NTFMSModel::NTFMSModel(nt::NetworkTableInstance inst, std::string_view path)
19 : m_inst{inst},
Austin Schuh812d0d12021-11-04 20:16:48 -070020 m_gameSpecificMessage{
James Kuszmaulcf324122023-01-14 14:07:17 -080021 inst.GetStringTopic(fmt::format("{}/GameSpecificMessage", path))
22 .Subscribe("")},
23 m_alliance{inst.GetBooleanTopic(fmt::format("{}/IsRedAlliance", path))
24 .Subscribe(false)},
25 m_station{inst.GetIntegerTopic(fmt::format("{}/StationNumber", path))
26 .Subscribe(0)},
27 m_controlWord{inst.GetIntegerTopic(fmt::format("{}/FMSControlData", path))
28 .Subscribe(0)},
Austin Schuh812d0d12021-11-04 20:16:48 -070029 m_fmsAttached{fmt::format("NT_FMS:FMSAttached:{}", path)},
30 m_dsAttached{fmt::format("NT_FMS:DSAttached:{}", path)},
31 m_allianceStationId{fmt::format("NT_FMS:AllianceStationID:{}", path)},
32 m_estop{fmt::format("NT_FMS:EStop:{}", path)},
33 m_enabled{fmt::format("NT_FMS:RobotEnabled:{}", path)},
34 m_test{fmt::format("NT_FMS:TestMode:{}", path)},
35 m_autonomous{fmt::format("NT_FMS:AutonomousMode:{}", path)} {
Austin Schuh812d0d12021-11-04 20:16:48 -070036 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) {
James Kuszmaulcf324122023-01-14 14:07:17 -080046 return m_gameSpecificMessage.Get(buf, "");
Austin Schuh812d0d12021-11-04 20:16:48 -070047}
48
49void NTFMSModel::Update() {
James Kuszmaulcf324122023-01-14 14:07:17 -080050 for (auto&& v : m_alliance.ReadQueue()) {
51 int allianceStationId = m_allianceStationId.GetValue();
52 allianceStationId %= 3;
53 // true if red
54 allianceStationId += 3 * (v.value ? 0 : 1);
55 m_allianceStationId.SetValue(allianceStationId, v.time);
56 }
57 for (auto&& v : m_station.ReadQueue()) {
58 int allianceStationId = m_allianceStationId.GetValue();
59 bool isRed = (allianceStationId < 3);
60 // the NT value is 1-indexed
61 m_allianceStationId.SetValue(v.value - 1 + 3 * (isRed ? 0 : 1), v.time);
62 }
63 for (auto&& v : m_controlWord.ReadQueue()) {
64 uint32_t controlWord = v.value;
65 // See HAL_ControlWord definition
66 m_enabled.SetValue(((controlWord & 0x01) != 0) ? 1 : 0, v.time);
67 m_autonomous.SetValue(((controlWord & 0x02) != 0) ? 1 : 0, v.time);
68 m_test.SetValue(((controlWord & 0x04) != 0) ? 1 : 0, v.time);
69 m_estop.SetValue(((controlWord & 0x08) != 0) ? 1 : 0, v.time);
70 m_fmsAttached.SetValue(((controlWord & 0x10) != 0) ? 1 : 0, v.time);
71 m_dsAttached.SetValue(((controlWord & 0x20) != 0) ? 1 : 0, v.time);
Austin Schuh812d0d12021-11-04 20:16:48 -070072 }
73}
74
75bool NTFMSModel::Exists() {
James Kuszmaulcf324122023-01-14 14:07:17 -080076 return m_controlWord.Exists();
Austin Schuh812d0d12021-11-04 20:16:48 -070077}