11#include <wpi/datalog/DataLog.hpp>
12#include <wpi/nt/NetworkTable.hpp>
13#include <wpi/nt/StructTopic.hpp>
14#include <wpi/system/DataLogManager.hpp>
15#include <wpi/util/struct/Struct.hpp>
30template <
typename T,
typename F>
43 m_key{std::move(key)},
45 m_defaultValue{std::move(defaultVal)},
46 m_cachedValue{m_defaultValue} {}
53 void SetDefaultValue(T value) { m_cachedValue = m_defaultValue = std::move(value); }
62 std::shared_ptr<wpi::nt::NetworkTable> tuningTable) {
63 m_dataTable = dataTable;
64 m_tuningTable = tuningTable;
65 if (!m_enabled)
return;
67 if (tuningTable && m_tunable) {
68 auto topic = tuningTable->template GetStructTopic<T>(m_key);
69 m_subPublisher = topic.Publish();
70 m_subPublisher->SetDefault(m_defaultValue);
71 m_subscriber = topic.Subscribe(m_defaultValue);
72 }
else if (dataTable) {
73 auto topic = dataTable->template GetStructTopic<T>(m_key);
74 m_publisher = topic.Publish();
75 m_publisher->SetDefault(m_defaultValue);
94 if (m_tunable)
return;
95 std::string path = prefix;
96 if (!path.empty() && path.back() !=
'/') path +=
'/';
98 m_dataLogEntry = wpi::log::StructLogEntry<T>{wpi::DataLogManager::GetLog(), path};
107 bool Set(
const T& value) {
108 if (!m_enabled)
return false;
109 if (m_dataLogEntry) {
110 m_dataLogEntry->Append(value);
113 T tuningValue = m_subscriber->Get(m_defaultValue);
114 if (!(tuningValue == value))
return false;
117 m_publisher->Set(value);
128 if (!m_enabled)
return m_defaultValue;
130 return m_subscriber->Get(m_defaultValue);
132 throw std::runtime_error(
"Tuning table not configured for " + m_key +
"!");
141 if (m_subscriber && m_tunable && m_enabled) {
142 T current = m_subscriber->Get(m_defaultValue);
143 if (!(current == m_cachedValue)) {
144 m_cachedValue = current;
160 void Display(
bool state) { m_enabled = state; }
168 m_subscriber.reset();
169 m_subPublisher.reset();
172 m_dataTable->GetEntry(m_key).Unpublish();
175 m_tuningTable->GetEntry(m_key).Unpublish();
183 bool m_enabled{
false};
187 std::optional<wpi::nt::StructPublisher<T>> m_publisher;
188 std::optional<wpi::nt::StructSubscriber<T>> m_subscriber;
189 std::optional<wpi::nt::StructPublisher<T>> m_subPublisher;
190 std::optional<wpi::log::StructLogEntry<T>> m_dataLogEntry;
192 std::shared_ptr<wpi::nt::NetworkTable> m_tuningTable;
193 std::shared_ptr<wpi::nt::NetworkTable> m_dataTable;
Definition StructTelemetry.hpp:31
bool Set(const T &value)
Definition StructTelemetry.hpp:107
void Disable()
Definition StructTelemetry.hpp:154
void Enable()
Definition StructTelemetry.hpp:152
void SetDefaultValue(T value)
Definition StructTelemetry.hpp:53
StructTelemetry(std::string key, T defaultVal, F field, bool tunable)
Definition StructTelemetry.hpp:41
void Display(bool state)
Definition StructTelemetry.hpp:160
bool IsEnabled() const
Definition StructTelemetry.hpp:164
T Get() const
Definition StructTelemetry.hpp:127
void SetupDataLog(const std::string &prefix)
Definition StructTelemetry.hpp:93
void SetupNetworkTable(std::shared_ptr< wpi::nt::NetworkTable > dataTable)
Definition StructTelemetry.hpp:84
F GetField() const
Definition StructTelemetry.hpp:162
void Close()
Definition StructTelemetry.hpp:167
void SetupNetworkTables(std::shared_ptr< wpi::nt::NetworkTable > dataTable, std::shared_ptr< wpi::nt::NetworkTable > tuningTable)
Definition StructTelemetry.hpp:61
bool IsTunable()
Definition StructTelemetry.hpp:140
Definition SwerveDrive.hpp:864