From 6a5448322bcfbdb7727fe940af524a91aa4201bc Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Tue, 2 Jul 2024 16:31:50 -0400 Subject: [PATCH] [wpilib] Add metadata to all dashboard ".type" entries (#6799) --- wpilibc/src/main/native/cpp/DriverStation.cpp | 16 +++++++++++++--- wpilibc/src/main/native/cpp/Preferences.cpp | 8 +++++--- .../main/native/cpp/livewindow/LiveWindow.cpp | 9 +++++++-- .../cpp/shuffleboard/ShuffleboardLayout.cpp | 8 +++++++- .../native/cpp/shuffleboard/ShuffleboardTab.cpp | 8 +++++++- .../cpp/smartdashboard/MechanismLigament2d.cpp | 8 ++++++-- .../edu/wpi/first/wpilibj/DriverStation.java | 9 ++++++++- .../java/edu/wpi/first/wpilibj/Preferences.java | 15 +++++++++++---- .../wpi/first/wpilibj/livewindow/LiveWindow.java | 10 ++++++++-- .../wpilibj/shuffleboard/ShuffleboardLayout.java | 8 +++++++- .../wpilibj/shuffleboard/ShuffleboardTab.java | 8 +++++++- .../smartdashboard/MechanismLigament2d.java | 11 +++++++++-- 12 files changed, 95 insertions(+), 23 deletions(-) diff --git a/wpilibc/src/main/native/cpp/DriverStation.cpp b/wpilibc/src/main/native/cpp/DriverStation.cpp index 8f80427d22..04ea4b9d9f 100644 --- a/wpilibc/src/main/native/cpp/DriverStation.cpp +++ b/wpilibc/src/main/native/cpp/DriverStation.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -45,8 +46,11 @@ class MatchDataSenderEntry { public: MatchDataSenderEntry(const std::shared_ptr& table, std::string_view key, - typename Topic::ParamType initialVal) - : publisher{Topic{table->GetTopic(key)}.Publish()}, prevVal{initialVal} { + typename Topic::ParamType initialVal, + wpi::json topicProperties = {{}}) + : publisher{Topic{table->GetTopic(key)}.PublishEx(Topic::kTypeString, + topicProperties)}, + prevVal{initialVal} { publisher.Set(initialVal); } @@ -62,10 +66,16 @@ class MatchDataSenderEntry { typename Topic::ValueType prevVal; }; +static constexpr std::string_view kSmartDashboardType = "FMSInfo"; + struct MatchDataSender { std::shared_ptr table = nt::NetworkTableInstance::GetDefault().GetTable("FMSInfo"); - MatchDataSenderEntry typeMetaData{table, ".type", "FMSInfo"}; + MatchDataSenderEntry typeMetaData{ + table, + ".type", + kSmartDashboardType, + {{"SmartDashboard", kSmartDashboardType}}}; MatchDataSenderEntry gameSpecificMessage{ table, "GameSpecificMessage", ""}; MatchDataSenderEntry eventName{table, "EventName", ""}; diff --git a/wpilibc/src/main/native/cpp/Preferences.cpp b/wpilibc/src/main/native/cpp/Preferences.cpp index cfb5964516..07d79cb57c 100644 --- a/wpilibc/src/main/native/cpp/Preferences.cpp +++ b/wpilibc/src/main/native/cpp/Preferences.cpp @@ -13,19 +13,21 @@ #include #include #include +#include using namespace frc; // The Preferences table name static constexpr std::string_view kTableName{"Preferences"}; - +static constexpr std::string_view kSmartDashboardType = "RobotPreferences"; namespace { struct Instance { Instance(); std::shared_ptr table{ nt::NetworkTableInstance::GetDefault().GetTable(kTableName)}; - nt::StringPublisher typePublisher{table->GetStringTopic(".type").Publish()}; + nt::StringPublisher typePublisher{table->GetStringTopic(".type").PublishEx( + nt::StringTopic::kTypeString, {{"SmartDashboard", kSmartDashboardType}})}; nt::MultiSubscriber tableSubscriber{nt::NetworkTableInstance::GetDefault(), {{fmt::format("{}/", table->GetPath())}}}; nt::NetworkTableListener listener; @@ -165,7 +167,7 @@ void Preferences::RemoveAll() { } Instance::Instance() { - typePublisher.Set("RobotPreferences"); + typePublisher.Set(kSmartDashboardType); listener = nt::NetworkTableListener::CreateListener( tableSubscriber, NT_EVENT_PUBLISH | NT_EVENT_IMMEDIATE, [typeTopic = typePublisher.GetTopic().GetHandle()](auto& event) { diff --git a/wpilibc/src/main/native/cpp/livewindow/LiveWindow.cpp b/wpilibc/src/main/native/cpp/livewindow/LiveWindow.cpp index 0a4e1b8fad..aff42a4efc 100644 --- a/wpilibc/src/main/native/cpp/livewindow/LiveWindow.cpp +++ b/wpilibc/src/main/native/cpp/livewindow/LiveWindow.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,8 @@ using namespace frc; +static constexpr std::string_view kSmartDashboardType = "LW Subsystem"; + namespace { struct Component { bool firstTime = true; @@ -207,8 +210,10 @@ void LiveWindow::UpdateValuesUnsafe() { comp.namePub.Set(cbdata.name); static_cast(cbdata.builder).SetTable(table); cbdata.sendable->InitSendable(cbdata.builder); - comp.typePub = nt::StringTopic{ssTable->GetTopic(".type")}.Publish(); - comp.typePub.Set("LW Subsystem"); + comp.typePub = nt::StringTopic{ssTable->GetTopic(".type")}.PublishEx( + nt::StringTopic::kTypeString, + {{"SmartDashboard", kSmartDashboardType}}); + comp.typePub.Set(kSmartDashboardType); comp.firstTime = false; } diff --git a/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardLayout.cpp b/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardLayout.cpp index c4c933ec6f..fb7d35bfe4 100644 --- a/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardLayout.cpp +++ b/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardLayout.cpp @@ -4,8 +4,12 @@ #include "frc/shuffleboard/ShuffleboardLayout.h" +#include + using namespace frc; +static constexpr std::string_view kSmartDashboardType = "ShuffleboardLayout"; + ShuffleboardLayout::ShuffleboardLayout(ShuffleboardContainer& parent, std::string_view title, std::string_view type) @@ -20,7 +24,9 @@ void ShuffleboardLayout::BuildInto( std::shared_ptr metaTable) { BuildMetadata(metaTable); auto table = parentTable->GetSubTable(GetTitle()); - table->GetEntry(".type").SetString("ShuffleboardLayout"); + table->GetEntry(".type").SetString(kSmartDashboardType); + table->GetEntry(".type").GetTopic().SetProperty("SmartDashboard", + kSmartDashboardType); for (auto& component : GetComponents()) { component->BuildInto(table, metaTable->GetSubTable(component->GetTitle())); } diff --git a/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardTab.cpp b/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardTab.cpp index 61e0e4541a..6cd1c3878b 100644 --- a/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardTab.cpp +++ b/wpilibc/src/main/native/cpp/shuffleboard/ShuffleboardTab.cpp @@ -4,8 +4,12 @@ #include "frc/shuffleboard/ShuffleboardTab.h" +#include + using namespace frc; +static constexpr std::string_view kSmartDashboardType = "ShuffleboardLayout"; + ShuffleboardTab::ShuffleboardTab(ShuffleboardRoot& root, std::string_view title) : ShuffleboardValue(title), ShuffleboardContainer(title), m_root(root) {} @@ -16,7 +20,9 @@ ShuffleboardRoot& ShuffleboardTab::GetRoot() { void ShuffleboardTab::BuildInto(std::shared_ptr parentTable, std::shared_ptr metaTable) { auto tabTable = parentTable->GetSubTable(GetTitle()); - tabTable->GetEntry(".type").SetString("ShuffleboardTab"); + tabTable->GetEntry(".type").SetString(kSmartDashboardType); + tabTable->GetEntry(".type").GetTopic().SetProperty("SmartDashboard", + kSmartDashboardType); for (auto& component : GetComponents()) { component->BuildInto(tabTable, metaTable->GetSubTable(component->GetTitle())); diff --git a/wpilibc/src/main/native/cpp/smartdashboard/MechanismLigament2d.cpp b/wpilibc/src/main/native/cpp/smartdashboard/MechanismLigament2d.cpp index 75ec44efe0..85f112d86a 100644 --- a/wpilibc/src/main/native/cpp/smartdashboard/MechanismLigament2d.cpp +++ b/wpilibc/src/main/native/cpp/smartdashboard/MechanismLigament2d.cpp @@ -5,9 +5,12 @@ #include "frc/smartdashboard/MechanismLigament2d.h" #include +#include using namespace frc; +static constexpr std::string_view kSmartDashboardType = "line"; + MechanismLigament2d::MechanismLigament2d(std::string_view name, double length, units::degree_t angle, double lineWeight, @@ -21,8 +24,9 @@ MechanismLigament2d::MechanismLigament2d(std::string_view name, double length, void MechanismLigament2d::UpdateEntries( std::shared_ptr table) { - m_typePub = table->GetStringTopic(".type").Publish(); - m_typePub.Set("line"); + m_typePub = table->GetStringTopic(".type").PublishEx( + nt::StringTopic::kTypeString, {{"SmartDashboard", kSmartDashboardType}}); + m_typePub.Set(kSmartDashboardType); m_colorEntry = table->GetStringTopic("color").GetEntry(""); m_colorEntry.Set(m_color); diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java index c5d720589a..1e51b5b4c9 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java @@ -13,6 +13,7 @@ import edu.wpi.first.networktables.BooleanPublisher; import edu.wpi.first.networktables.IntegerPublisher; import edu.wpi.first.networktables.NetworkTableInstance; import edu.wpi.first.networktables.StringPublisher; +import edu.wpi.first.networktables.StringTopic; import edu.wpi.first.util.EventVector; import edu.wpi.first.util.WPIUtilJNI; import edu.wpi.first.util.datalog.BooleanArrayLogEntry; @@ -93,6 +94,8 @@ public final class DriverStation { @SuppressWarnings("MemberName") private static class MatchDataSender { + private static final String kSmartDashboardType = "FMSInfo"; + final StringPublisher gameSpecificMessage; final StringPublisher eventName; final IntegerPublisher matchNumber; @@ -112,7 +115,11 @@ public final class DriverStation { MatchDataSender() { var table = NetworkTableInstance.getDefault().getTable("FMSInfo"); - table.getStringTopic(".type").publish().set("FMSInfo"); + table + .getStringTopic(".type") + .publishEx( + StringTopic.kTypeString, "{\"SmartDashboard\":\"" + kSmartDashboardType + "\"}") + .set(kSmartDashboardType); gameSpecificMessage = table.getStringTopic("GameSpecificMessage").publish(); gameSpecificMessage.set(""); eventName = table.getStringTopic("EventName").publish(); diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java index 69d0fb32e7..b0ab57224e 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java @@ -15,6 +15,7 @@ import edu.wpi.first.networktables.NetworkTableEvent; import edu.wpi.first.networktables.NetworkTableInstance; import edu.wpi.first.networktables.NetworkTableListener; import edu.wpi.first.networktables.StringPublisher; +import edu.wpi.first.networktables.StringTopic; import edu.wpi.first.networktables.Topic; import java.util.Collection; import java.util.EnumSet; @@ -34,7 +35,9 @@ import java.util.EnumSet; */ public final class Preferences { /** The Preferences table name. */ - private static final String TABLE_NAME = "Preferences"; + private static final String kTableName = "Preferences"; + + private static final String kSmartDashboardType = "RobotPreferences"; /** The network table. */ private static NetworkTable m_table; @@ -57,12 +60,16 @@ public final class Preferences { * @param inst NetworkTable instance */ public static synchronized void setNetworkTableInstance(NetworkTableInstance inst) { - m_table = inst.getTable(TABLE_NAME); + m_table = inst.getTable(kTableName); if (m_typePublisher != null) { m_typePublisher.close(); } - m_typePublisher = m_table.getStringTopic(".type").publish(); - m_typePublisher.set("RobotPreferences"); + m_typePublisher = + m_table + .getStringTopic(".type") + .publishEx( + StringTopic.kTypeString, "{\"SmartDashboard\":\"" + kSmartDashboardType + "\"}"); + m_typePublisher.set(kSmartDashboardType); // Subscribe to all Preferences; this ensures we get the latest values // ahead of a getter call. diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java index ecb0e89f22..f30ac59d17 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java @@ -36,6 +36,8 @@ public final class LiveWindow { StringPublisher m_typePub; } + private static final String kSmartDashboardType = "LW Subsystem"; + private static final int dataHandle = SendableRegistry.getDataHandle(); private static final NetworkTable liveWindowTable = NetworkTableInstance.getDefault().getTable("LiveWindow"); @@ -224,8 +226,12 @@ public final class LiveWindow { component.m_namePub.set(cbdata.name); ((SendableBuilderImpl) cbdata.builder).setTable(table); cbdata.sendable.initSendable(cbdata.builder); - component.m_typePub = new StringTopic(ssTable.getTopic(".type")).publish(); - component.m_typePub.set("LW Subsystem"); + component.m_typePub = + new StringTopic(ssTable.getTopic(".type")) + .publishEx( + StringTopic.kTypeString, + "{\"SmartDashboard\":\"" + kSmartDashboardType + "\"}"); + component.m_typePub.set(kSmartDashboardType); component.m_firstTime = false; } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/ShuffleboardLayout.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/ShuffleboardLayout.java index 661c06e928..564f5e4261 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/ShuffleboardLayout.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/ShuffleboardLayout.java @@ -18,6 +18,8 @@ import java.util.function.Supplier; /** A layout in a Shuffleboard tab. Layouts can contain widgets and other layouts. */ public final class ShuffleboardLayout extends ShuffleboardComponent implements ShuffleboardContainer { + private static final String kSmartDashboardType = "ShuffleboardLayout"; + private final ContainerHelper m_helper = new ContainerHelper(this); ShuffleboardLayout(ShuffleboardContainer parent, String title, String type) { @@ -127,7 +129,11 @@ public final class ShuffleboardLayout extends ShuffleboardComponent component : getComponents()) { component.buildInto(table, metaTable.getSubTable(component.getTitle())); } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/ShuffleboardTab.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/ShuffleboardTab.java index f1feabb9b7..b71255de76 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/ShuffleboardTab.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/shuffleboard/ShuffleboardTab.java @@ -20,6 +20,8 @@ import java.util.function.Supplier; * arbitrarily deep (note that too many levels may make deeper components unusable). */ public final class ShuffleboardTab implements ShuffleboardContainer { + private static final String kSmartDashboardType = "ShuffleboardTab"; + private final ContainerHelper m_helper = new ContainerHelper(this); private final ShuffleboardRoot m_root; private final String m_title; @@ -140,7 +142,11 @@ public final class ShuffleboardTab implements ShuffleboardContainer { @Override public void buildInto(NetworkTable parentTable, NetworkTable metaTable) { NetworkTable tabTable = parentTable.getSubTable(m_title); - tabTable.getEntry(".type").setString("ShuffleboardTab"); + tabTable.getEntry(".type").setString(kSmartDashboardType); + tabTable + .getEntry(".type") + .getTopic() + .setProperty("SmartDashboard", "\"" + kSmartDashboardType + "\""); for (ShuffleboardComponent component : m_helper.getComponents()) { component.buildInto(tabTable, metaTable.getSubTable(component.getTitle())); } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/MechanismLigament2d.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/MechanismLigament2d.java index ea7fda38bf..7d8671faa1 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/MechanismLigament2d.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/MechanismLigament2d.java @@ -9,6 +9,7 @@ import edu.wpi.first.networktables.DoubleEntry; import edu.wpi.first.networktables.NetworkTable; import edu.wpi.first.networktables.StringEntry; import edu.wpi.first.networktables.StringPublisher; +import edu.wpi.first.networktables.StringTopic; import edu.wpi.first.wpilibj.util.Color8Bit; /** @@ -28,6 +29,8 @@ public class MechanismLigament2d extends MechanismObject2d { private double m_weight; private DoubleEntry m_weightEntry; + private static String kSmartDashboardType = "line"; + /** * Create a new ligament. * @@ -201,8 +204,12 @@ public class MechanismLigament2d extends MechanismObject2d { if (m_typePub != null) { m_typePub.close(); } - m_typePub = table.getStringTopic(".type").publish(); - m_typePub.set("line"); + m_typePub = + table + .getStringTopic(".type") + .publishEx( + StringTopic.kTypeString, "{\"SmartDashboard\":\"" + kSmartDashboardType + "\"}"); + m_typePub.set(kSmartDashboardType); if (m_angleEntry != null) { m_angleEntry.close();