14#include <wpi/datalog/DataLog.hpp>
15#include <wpi/nt/NetworkTable.hpp>
16#include <wpi/nt/StructArrayTopic.hpp>
17#include <wpi/system/DataLogManager.hpp>
18#include <wpi/util/struct/Struct.hpp>
31template <
typename T,
typename F>
44 m_key{std::move(key)},
46 m_defaultValue{std::move(defaultVal)},
47 m_cachedValue{m_defaultValue} {}
54 void SetDefaultValue(std::vector<T> value) { m_cachedValue = m_defaultValue = std::move(value); }
63 std::shared_ptr<wpi::nt::NetworkTable> tuningTable) {
64 m_dataTable = dataTable;
65 m_tuningTable = tuningTable;
66 if (!m_enabled)
return;
68 if (tuningTable && m_tunable) {
69 auto topic = tuningTable->template GetStructArrayTopic<T>(m_key);
70 m_subPublisher = topic.Publish();
71 m_subPublisher->SetDefault(m_defaultValue);
72 m_subscriber = topic.Subscribe(m_defaultValue);
73 }
else if (dataTable) {
74 auto topic = dataTable->template GetStructArrayTopic<T>(m_key);
75 m_publisher = topic.Publish();
76 m_publisher->SetDefault(m_defaultValue);
95 if (m_tunable)
return;
96 std::string path = prefix;
97 if (!path.empty() && path.back() !=
'/') path +=
'/';
99 m_dataLogEntry = wpi::log::StructArrayLogEntry<T>{wpi::DataLogManager::GetLog(), path};
108 bool Set(std::span<const T> value) {
109 if (!m_enabled)
return false;
110 if (m_dataLogEntry) {
111 m_dataLogEntry->Append(value);
114 std::vector<T> tuningValue = m_subscriber->Get(m_defaultValue);
115 if (!(tuningValue.size() == value.size() &&
116 std::equal(tuningValue.begin(), tuningValue.end(), value.begin()))) {
121 m_publisher->Set(value);
131 std::vector<T>
Get()
const {
132 if (!m_enabled)
return m_defaultValue;
134 return m_subscriber->Get(m_defaultValue);
136 throw std::runtime_error(
"Tuning table not configured for " + m_key +
"!");
145 if (m_subscriber && m_tunable && m_enabled) {
146 std::vector<T> current = m_subscriber->Get(m_defaultValue);
147 if (current != m_cachedValue) {
148 m_cachedValue = current;
164 void Display(
bool state) { m_enabled = state; }
172 m_subscriber.reset();
173 m_subPublisher.reset();
176 m_dataTable->GetEntry(m_key).Unpublish();
179 m_tuningTable->GetEntry(m_key).Unpublish();
187 bool m_enabled{
false};
188 std::vector<T> m_defaultValue;
189 std::vector<T> m_cachedValue;
191 std::optional<wpi::nt::StructArrayPublisher<T>> m_publisher;
192 std::optional<wpi::nt::StructArraySubscriber<T>> m_subscriber;
193 std::optional<wpi::nt::StructArrayPublisher<T>> m_subPublisher;
194 std::optional<wpi::log::StructArrayLogEntry<T>> m_dataLogEntry;
196 std::shared_ptr<wpi::nt::NetworkTable> m_tuningTable;
197 std::shared_ptr<wpi::nt::NetworkTable> m_dataTable;
Definition StructArrayTelemetry.hpp:32
void Disable()
Definition StructArrayTelemetry.hpp:158
bool IsEnabled() const
Definition StructArrayTelemetry.hpp:168
std::vector< T > Get() const
Definition StructArrayTelemetry.hpp:131
void SetDefaultValue(std::vector< T > value)
Definition StructArrayTelemetry.hpp:54
bool IsTunable()
Definition StructArrayTelemetry.hpp:144
void Close()
Definition StructArrayTelemetry.hpp:171
StructArrayTelemetry(std::string key, std::vector< T > defaultVal, F field, bool tunable)
Definition StructArrayTelemetry.hpp:42
F GetField() const
Definition StructArrayTelemetry.hpp:166
void SetupNetworkTable(std::shared_ptr< wpi::nt::NetworkTable > dataTable)
Definition StructArrayTelemetry.hpp:85
void SetupDataLog(const std::string &prefix)
Definition StructArrayTelemetry.hpp:94
bool Set(std::span< const T > value)
Definition StructArrayTelemetry.hpp:108
void Enable()
Definition StructArrayTelemetry.hpp:156
void Display(bool state)
Definition StructArrayTelemetry.hpp:164
void SetupNetworkTables(std::shared_ptr< wpi::nt::NetworkTable > dataTable, std::shared_ptr< wpi::nt::NetworkTable > tuningTable)
Definition StructArrayTelemetry.hpp:62
Definition SwerveDrive.hpp:864