YAMS
Yet Another Mechanism System
Loading...
Searching...
No Matches
StringTelemetry.hpp
Go to the documentation of this file.
1// Copyright (c) 2026 Yet Another Software Suite
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#pragma once
5
6#include <memory>
7#include <optional>
8#include <stdexcept>
9#include <string>
10#include <string_view>
11#include <utility>
12#include <wpi/datalog/DataLog.hpp>
13#include <wpi/nt/NetworkTable.hpp>
14#include <wpi/nt/StringTopic.hpp>
15#include <wpi/system/DataLogManager.hpp>
16
17namespace yams::telemetry {
18
25template <typename F>
27 public:
36 StringTelemetry(std::string key, std::string defaultVal, F field, bool tunable)
37 : m_field{field},
38 m_key{std::move(key)},
39 m_tunable{tunable},
40 m_defaultValue{defaultVal},
41 m_cachedValue{std::move(defaultVal)} {}
42
48 void SetDefaultValue(std::string value) {
49 m_defaultValue = value;
50 m_cachedValue = std::move(value);
51 }
52
59 void SetupNetworkTables(std::shared_ptr<wpi::nt::NetworkTable> dataTable,
60 std::shared_ptr<wpi::nt::NetworkTable> tuningTable) {
61 m_dataTable = dataTable;
62 m_tuningTable = tuningTable;
63 if (!m_enabled) return;
64
65 if (tuningTable && m_tunable) {
66 auto topic = tuningTable->GetStringTopic(m_key);
67 m_pubSub = topic.Publish();
68 m_pubSub->SetDefault(m_defaultValue);
69 m_subscriber = topic.Subscribe(m_defaultValue);
70 } else if (dataTable) {
71 auto topic = dataTable->GetStringTopic(m_key);
72 m_publisher = topic.Publish();
73 m_publisher->SetDefault(m_defaultValue);
74 }
75 }
76
82 void SetupNetworkTable(std::shared_ptr<wpi::nt::NetworkTable> dataTable) {
83 SetupNetworkTables(dataTable, nullptr);
84 }
85
91 void SetupDataLog(const std::string& prefix) {
92 if (m_tunable) return;
93 std::string path = prefix;
94 if (!path.empty() && path.back() != '/') path += '/';
95 path += m_key;
96 m_dataLogEntry = wpi::log::StringLogEntry{wpi::DataLogManager::GetLog(), path};
97 }
98
105 bool Set(std::string_view value) {
106 if (!m_enabled) return false;
107 if (m_dataLogEntry) {
108 m_dataLogEntry->Append(value);
109 }
110 if (m_subscriber) {
111 if (m_subscriber->Get(m_defaultValue) != value) return false;
112 }
113 if (m_publisher) {
114 m_publisher->Set(value);
115 }
116 return true;
117 }
118
124 std::string Get() const {
125 if (!m_enabled) return m_defaultValue;
126 if (m_subscriber) {
127 return m_subscriber->Get(m_defaultValue);
128 }
129 throw std::runtime_error("Tuning table not configured for " + m_key + "!");
130 }
131
138 void ForceSet(std::string_view value) {
139 if (m_pubSub) {
140 m_pubSub->Set(value);
141 } else if (m_publisher) {
142 m_publisher->Set(value);
143 }
144 }
145
151 bool IsTunable() {
152 if (m_subscriber && m_tunable && m_enabled) {
153 std::string current = m_subscriber->Get(m_defaultValue);
154 if (current != m_cachedValue) {
155 m_cachedValue = std::move(current);
156 return true;
157 }
158 }
159 return false;
160 }
161
163 void Enable() { m_enabled = true; }
165 void Disable() { m_enabled = false; }
171 void Display(bool state) { m_enabled = state; }
173 F GetField() const { return m_field; }
175 bool IsEnabled() const { return m_enabled; }
176
178 void Close() {
179 m_subscriber.reset();
180 m_pubSub.reset();
181 m_publisher.reset();
182 if (m_dataTable) {
183 m_dataTable->GetEntry(m_key).Unpublish();
184 }
185 if (m_tuningTable) {
186 m_tuningTable->GetEntry(m_key).Unpublish();
187 }
188 }
189
190 private:
191 F m_field;
192 std::string m_key;
193 bool m_tunable;
194 bool m_enabled{false};
195 std::string m_defaultValue;
196 std::string m_cachedValue;
197
198 std::optional<wpi::nt::StringPublisher> m_publisher;
199 std::optional<wpi::nt::StringSubscriber> m_subscriber;
200 std::optional<wpi::nt::StringPublisher> m_pubSub; // tunable: publish + subscribe
201 std::optional<wpi::log::StringLogEntry> m_dataLogEntry;
202
203 std::shared_ptr<wpi::nt::NetworkTable> m_tuningTable;
204 std::shared_ptr<wpi::nt::NetworkTable> m_dataTable;
205};
206
207} // namespace yams::telemetry
void ForceSet(std::string_view value)
Definition StringTelemetry.hpp:138
void Enable()
Definition StringTelemetry.hpp:163
void SetupNetworkTable(std::shared_ptr< wpi::nt::NetworkTable > dataTable)
Definition StringTelemetry.hpp:82
void SetDefaultValue(std::string value)
Definition StringTelemetry.hpp:48
bool Set(std::string_view value)
Definition StringTelemetry.hpp:105
bool IsTunable()
Definition StringTelemetry.hpp:151
void SetupDataLog(const std::string &prefix)
Definition StringTelemetry.hpp:91
void SetupNetworkTables(std::shared_ptr< wpi::nt::NetworkTable > dataTable, std::shared_ptr< wpi::nt::NetworkTable > tuningTable)
Definition StringTelemetry.hpp:59
void Close()
Definition StringTelemetry.hpp:178
StringTelemetry(std::string key, std::string defaultVal, F field, bool tunable)
Definition StringTelemetry.hpp:36
void Display(bool state)
Definition StringTelemetry.hpp:171
std::string Get() const
Definition StringTelemetry.hpp:124
void Disable()
Definition StringTelemetry.hpp:165
F GetField() const
Definition StringTelemetry.hpp:173
bool IsEnabled() const
Definition StringTelemetry.hpp:175
Definition SwerveDrive.hpp:872