YAMS
Yet Another Mechanism System
Loading...
Searching...
No Matches
MechanismTelemetry.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 <functional>
7#include <memory>
8#include <optional>
9#include <span>
10#include <string>
11#include <wpi/datalog/DataLog.hpp>
12#include <wpi/nt/DoubleTopic.hpp>
13#include <wpi/nt/NetworkTable.hpp>
14#include <wpi/nt/StructArrayTopic.hpp>
15#include <wpi/nt/StructTopic.hpp>
16#include <wpi/system/DataLogManager.hpp>
17#include <wpi/util/json.hpp>
18
19namespace yams::motorcontrollers {
20class SmartMotorController;
21}
22
23namespace yams::telemetry {
24
35 public:
42 void SetupTelemetry(const std::string& mechanismName,
44
50 void SetupTelemetry(const std::string& mechanismName);
51
60 void SetupTelemetry(const std::string& mechanismName, const std::string& dataLogName);
61
77 void AddMotorController(const std::string& subTableName,
79
91 std::function<void(double)> PublishDouble(const std::string& key, const std::string& unit = "");
92
102 template <typename T>
103 std::function<void(const T&)> PublishStruct(const std::string& key) {
104 auto publisher = std::make_shared<wpi::nt::StructPublisher<T>>(
105 m_networkTable->template GetStructTopic<T>(key).Publish());
106 std::shared_ptr<wpi::log::StructLogEntry<T>> logEntry;
107 if (m_dataLogName) {
108 logEntry = std::make_shared<wpi::log::StructLogEntry<T>>(wpi::DataLogManager::GetLog(),
109 *m_dataLogName + "/" + key);
110 }
111 return [publisher, logEntry](const T& value) {
112 publisher->Set(value);
113 if (logEntry) logEntry->Append(value);
114 };
115 }
116
126 template <typename T>
127 std::function<void(std::span<const T>)> PublishStructArray(const std::string& key) {
128 auto publisher = std::make_shared<wpi::nt::StructArrayPublisher<T>>(
129 m_networkTable->template GetStructArrayTopic<T>(key).Publish());
130 std::shared_ptr<wpi::log::StructArrayLogEntry<T>> logEntry;
131 if (m_dataLogName) {
132 logEntry = std::make_shared<wpi::log::StructArrayLogEntry<T>>(wpi::DataLogManager::GetLog(),
133 *m_dataLogName + "/" + key);
134 }
135 return [publisher, logEntry](std::span<const T> value) {
136 publisher->Set(value);
137 if (logEntry) logEntry->Append(value);
138 };
139 }
140
143
145 std::shared_ptr<wpi::nt::NetworkTable> GetDataTable() const;
146
148 std::shared_ptr<wpi::nt::NetworkTable> GetTuningTable() const;
149
150 private:
151 void SetupLoopTime();
152
153 std::shared_ptr<wpi::nt::NetworkTable> m_networkTable;
154 std::shared_ptr<wpi::nt::NetworkTable> m_tuningNetworkTable;
155 std::optional<wpi::nt::DoublePublisher> m_loopTimePublisher;
156 double m_prevTimestamp{0.0};
157 std::optional<std::string> m_dataLogName;
158};
159
160} // namespace yams::telemetry
Definition SmartMotorController.hpp:48
Definition MechanismTelemetry.hpp:34
std::function< void(std::span< const T >)> PublishStructArray(const std::string &key)
Definition MechanismTelemetry.hpp:127
std::shared_ptr< wpi::nt::NetworkTable > GetDataTable() const
void SetupTelemetry(const std::string &mechanismName, motorcontrollers::SmartMotorController &motorController)
std::function< void(double)> PublishDouble(const std::string &key, const std::string &unit="")
std::function< void(const T &)> PublishStruct(const std::string &key)
Definition MechanismTelemetry.hpp:103
void SetupTelemetry(const std::string &mechanismName, const std::string &dataLogName)
void SetupTelemetry(const std::string &mechanismName)
std::shared_ptr< wpi::nt::NetworkTable > GetTuningTable() const
void AddMotorController(const std::string &subTableName, motorcontrollers::SmartMotorController &motorController)
Definition SimSensorConfig.hpp:15
Definition SwerveDrive.hpp:864