mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
[ntcore] NetworkTables 4 (#3217)
This commit is contained in:
@@ -8,8 +8,10 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <networktables/BooleanTopic.h>
|
||||
#include <networktables/NetworkTable.h>
|
||||
#include <networktables/NetworkTableInstance.h>
|
||||
#include <networktables/StringTopic.h>
|
||||
#include <wpi/SmallVector.h>
|
||||
|
||||
#include "frc/shuffleboard/ShuffleboardEventImportance.h"
|
||||
@@ -30,8 +32,8 @@ class RecordingController final {
|
||||
ShuffleboardEventImportance importance);
|
||||
|
||||
private:
|
||||
nt::NetworkTableEntry m_recordingControlEntry;
|
||||
nt::NetworkTableEntry m_recordingFileNameFormatEntry;
|
||||
nt::BooleanPublisher m_recordingControlEntry;
|
||||
nt::StringPublisher m_recordingFileNameFormatEntry;
|
||||
std::shared_ptr<nt::NetworkTable> m_eventsTable;
|
||||
};
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ using CS_Handle = int; // NOLINT
|
||||
using CS_Source = CS_Handle; // NOLINT
|
||||
#endif
|
||||
|
||||
#include <networktables/StringArrayTopic.h>
|
||||
#include <wpi/sendable/Sendable.h>
|
||||
#include <wpi/sendable/SendableHelper.h>
|
||||
#include <wpi/span.h>
|
||||
@@ -55,6 +56,17 @@ class SendableCameraWrapper
|
||||
*/
|
||||
SendableCameraWrapper(std::string_view cameraName, const private_init&);
|
||||
|
||||
/**
|
||||
* Creates a new sendable wrapper. Private constructor to avoid direct
|
||||
* instantiation with multiple wrappers floating around for the same camera.
|
||||
*
|
||||
* @param cameraName the name of the camera to wrap
|
||||
* @param cameraUrls camera URLs
|
||||
*/
|
||||
SendableCameraWrapper(std::string_view cameraName,
|
||||
wpi::span<const std::string> cameraUrls,
|
||||
const private_init&);
|
||||
|
||||
/**
|
||||
* Gets a sendable wrapper object for the given video source, creating the
|
||||
* wrapper if one does not already exist for the source.
|
||||
@@ -73,6 +85,7 @@ class SendableCameraWrapper
|
||||
|
||||
private:
|
||||
std::string m_uri;
|
||||
nt::StringArrayPublisher m_streams;
|
||||
};
|
||||
|
||||
#ifndef DYNAMIC_CAMERA_SERVER
|
||||
@@ -83,6 +96,17 @@ inline SendableCameraWrapper::SendableCameraWrapper(std::string_view name,
|
||||
m_uri += name;
|
||||
}
|
||||
|
||||
inline SendableCameraWrapper::SendableCameraWrapper(
|
||||
std::string_view cameraName, wpi::span<const std::string> cameraUrls,
|
||||
const private_init&)
|
||||
: SendableCameraWrapper(cameraName, private_init{}) {
|
||||
m_streams = nt::NetworkTableInstance::GetDefault()
|
||||
.GetStringArrayTopic(
|
||||
fmt::format("/CameraPublisher/{}/streams", cameraName))
|
||||
.Publish();
|
||||
m_streams.Set(cameraUrls);
|
||||
}
|
||||
|
||||
inline SendableCameraWrapper& SendableCameraWrapper::Wrap(
|
||||
const cs::VideoSource& source) {
|
||||
return Wrap(source.GetHandle());
|
||||
@@ -102,12 +126,9 @@ inline SendableCameraWrapper& SendableCameraWrapper::Wrap(
|
||||
std::string_view cameraName, wpi::span<const std::string> cameraUrls) {
|
||||
auto& wrapper = detail::GetSendableCameraWrapper(cameraName);
|
||||
if (!wrapper) {
|
||||
wrapper =
|
||||
std::make_shared<SendableCameraWrapper>(cameraName, private_init{});
|
||||
wrapper = std::make_shared<SendableCameraWrapper>(cameraName, cameraUrls,
|
||||
private_init{});
|
||||
}
|
||||
auto streams = fmt::format("/CameraPublisher/{}/streams", cameraName);
|
||||
nt::NetworkTableInstance::GetDefault().GetEntry(streams).SetStringArray(
|
||||
cameraUrls);
|
||||
return *wrapper;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -35,7 +35,7 @@ class ShuffleboardComponentBase : public virtual ShuffleboardValue {
|
||||
const std::string& GetType() const;
|
||||
|
||||
protected:
|
||||
wpi::StringMap<std::shared_ptr<nt::Value>> m_properties;
|
||||
wpi::StringMap<nt::Value> m_properties;
|
||||
bool m_metadataDirty = true;
|
||||
int m_column = -1;
|
||||
int m_row = -1;
|
||||
@@ -49,7 +49,7 @@ class ShuffleboardComponentBase : public virtual ShuffleboardValue {
|
||||
/**
|
||||
* Gets the custom properties for this component. May be null.
|
||||
*/
|
||||
const wpi::StringMap<std::shared_ptr<nt::Value>>& GetProperties() const;
|
||||
const wpi::StringMap<nt::Value>& GetProperties() const;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -171,8 +171,7 @@ class ShuffleboardContainer : public virtual ShuffleboardValue {
|
||||
* @see AddPersistent(std::string_view, std::shared_ptr<nt::Value>)
|
||||
* Add(std::string_view title, std::shared_ptr<nt::Value> defaultValue)
|
||||
*/
|
||||
SimpleWidget& Add(std::string_view title,
|
||||
std::shared_ptr<nt::Value> defaultValue);
|
||||
SimpleWidget& Add(std::string_view title, const nt::Value& defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display the given data.
|
||||
@@ -200,6 +199,19 @@ class ShuffleboardContainer : public virtual ShuffleboardValue {
|
||||
*/
|
||||
SimpleWidget& Add(std::string_view title, double defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display the given data.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param defaultValue the default value of the widget
|
||||
* @return a widget to display the sendable data
|
||||
* @throws IllegalArgumentException if a widget already exists in this
|
||||
* container with the given title
|
||||
* @see AddPersistent(std::string_view, double)
|
||||
* Add(std::string_view title, double defaultValue)
|
||||
*/
|
||||
SimpleWidget& Add(std::string_view title, float defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display the given data.
|
||||
*
|
||||
@@ -266,6 +278,34 @@ class ShuffleboardContainer : public virtual ShuffleboardValue {
|
||||
SimpleWidget& Add(std::string_view title,
|
||||
wpi::span<const double> defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display the given data.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param defaultValue the default value of the widget
|
||||
* @return a widget to display the sendable data
|
||||
* @throws IllegalArgumentException if a widget already exists in this
|
||||
* container with the given title
|
||||
* @see AddPersistent(std::string_view, wpi::span<const double>)
|
||||
* Add(std::string_view title, wpi::span<const double> defaultValue)
|
||||
*/
|
||||
SimpleWidget& Add(std::string_view title,
|
||||
wpi::span<const float> defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display the given data.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param defaultValue the default value of the widget
|
||||
* @return a widget to display the sendable data
|
||||
* @throws IllegalArgumentException if a widget already exists in this
|
||||
* container with the given title
|
||||
* @see AddPersistent(std::string_view, wpi::span<const double>)
|
||||
* Add(std::string_view title, wpi::span<const double> defaultValue)
|
||||
*/
|
||||
SimpleWidget& Add(std::string_view title,
|
||||
wpi::span<const int64_t> defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display the given data.
|
||||
*
|
||||
@@ -306,6 +346,45 @@ class ShuffleboardContainer : public virtual ShuffleboardValue {
|
||||
SuppliedValueWidget<double>& AddNumber(std::string_view title,
|
||||
std::function<double()> supplier);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container. The widget will display the data provided
|
||||
* by the value supplier. Changes made on the dashboard will not propagate to
|
||||
* the widget object, and will be overridden by values from the value
|
||||
* supplier.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param supplier the supplier for values
|
||||
* @return a widget to display data
|
||||
*/
|
||||
SuppliedValueWidget<double>& AddDouble(std::string_view title,
|
||||
std::function<double()> supplier);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container. The widget will display the data provided
|
||||
* by the value supplier. Changes made on the dashboard will not propagate to
|
||||
* the widget object, and will be overridden by values from the value
|
||||
* supplier.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param supplier the supplier for values
|
||||
* @return a widget to display data
|
||||
*/
|
||||
SuppliedValueWidget<float>& AddFloat(std::string_view title,
|
||||
std::function<float()> supplier);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container. The widget will display the data provided
|
||||
* by the value supplier. Changes made on the dashboard will not propagate to
|
||||
* the widget object, and will be overridden by values from the value
|
||||
* supplier.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param supplier the supplier for values
|
||||
* @return a widget to display data
|
||||
*/
|
||||
SuppliedValueWidget<int64_t>& AddInteger(std::string_view title,
|
||||
std::function<int64_t()> supplier);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container. The widget will display the data provided
|
||||
* by the value supplier. Changes made on the dashboard will not propagate to
|
||||
@@ -346,6 +425,45 @@ class ShuffleboardContainer : public virtual ShuffleboardValue {
|
||||
SuppliedValueWidget<std::vector<double>>& AddNumberArray(
|
||||
std::string_view title, std::function<std::vector<double>()> supplier);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container. The widget will display the data provided
|
||||
* by the value supplier. Changes made on the dashboard will not propagate to
|
||||
* the widget object, and will be overridden by values from the value
|
||||
* supplier.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param supplier the supplier for values
|
||||
* @return a widget to display data
|
||||
*/
|
||||
SuppliedValueWidget<std::vector<double>>& AddDoubleArray(
|
||||
std::string_view title, std::function<std::vector<double>()> supplier);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container. The widget will display the data provided
|
||||
* by the value supplier. Changes made on the dashboard will not propagate to
|
||||
* the widget object, and will be overridden by values from the value
|
||||
* supplier.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param supplier the supplier for values
|
||||
* @return a widget to display data
|
||||
*/
|
||||
SuppliedValueWidget<std::vector<float>>& AddFloatArray(
|
||||
std::string_view title, std::function<std::vector<float>()> supplier);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container. The widget will display the data provided
|
||||
* by the value supplier. Changes made on the dashboard will not propagate to
|
||||
* the widget object, and will be overridden by values from the value
|
||||
* supplier.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param supplier the supplier for values
|
||||
* @return a widget to display data
|
||||
*/
|
||||
SuppliedValueWidget<std::vector<int64_t>>& AddIntegerArray(
|
||||
std::string_view title, std::function<std::vector<int64_t>()> supplier);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container. The widget will display the data provided
|
||||
* by the value supplier. Changes made on the dashboard will not propagate to
|
||||
@@ -369,8 +487,23 @@ class ShuffleboardContainer : public virtual ShuffleboardValue {
|
||||
* @param supplier the supplier for values
|
||||
* @return a widget to display data
|
||||
*/
|
||||
SuppliedValueWidget<std::string_view>& AddRaw(
|
||||
std::string_view title, std::function<std::string_view()> supplier);
|
||||
SuppliedValueWidget<std::vector<uint8_t>>& AddRaw(
|
||||
std::string_view title, std::function<std::vector<uint8_t>()> supplier);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container. The widget will display the data provided
|
||||
* by the value supplier. Changes made on the dashboard will not propagate to
|
||||
* the widget object, and will be overridden by values from the value
|
||||
* supplier.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param typeString the NT type string
|
||||
* @param supplier the supplier for values
|
||||
* @return a widget to display data
|
||||
*/
|
||||
SuppliedValueWidget<std::vector<uint8_t>>& AddRaw(
|
||||
std::string_view title, std::string_view typeString,
|
||||
std::function<std::vector<uint8_t>()> supplier);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display a simple piece of data.
|
||||
@@ -386,7 +519,7 @@ class ShuffleboardContainer : public virtual ShuffleboardValue {
|
||||
* Add(std::string_view title, std::shared_ptr<nt::Value> defaultValue)
|
||||
*/
|
||||
SimpleWidget& AddPersistent(std::string_view title,
|
||||
std::shared_ptr<nt::Value> defaultValue);
|
||||
const nt::Value& defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display a simple piece of data.
|
||||
@@ -421,15 +554,30 @@ class ShuffleboardContainer : public virtual ShuffleboardValue {
|
||||
/**
|
||||
* Adds a widget to this container to display a simple piece of data.
|
||||
*
|
||||
* Unlike Add(std::string_view, int), the value in the widget will be saved on
|
||||
* the robot and will be used when the robot program next starts rather than
|
||||
* {@code defaultValue}.
|
||||
* Unlike Add(std::string_view, float), the value in the widget will be saved
|
||||
* on the robot and will be used when the robot program next starts rather
|
||||
* than {@code defaultValue}.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param defaultValue the default value of the widget
|
||||
* @return a widget to display the sendable data
|
||||
* @see Add(std:string_view, int)
|
||||
* Add(std::string_view title, int defaultValue)
|
||||
* @see Add(std::string_view, float)
|
||||
* Add(std::string_view title, float defaultValue)
|
||||
*/
|
||||
SimpleWidget& AddPersistent(std::string_view title, float defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display a simple piece of data.
|
||||
*
|
||||
* Unlike Add(std::string_view, int64_t), the value in the widget will be
|
||||
* saved on the robot and will be used when the robot program next starts
|
||||
* rather than {@code defaultValue}.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param defaultValue the default value of the widget
|
||||
* @return a widget to display the sendable data
|
||||
* @see Add(std:string_view, int64_t)
|
||||
* Add(std::string_view title, int64_t defaultValue)
|
||||
*/
|
||||
SimpleWidget& AddPersistent(std::string_view title, int defaultValue);
|
||||
|
||||
@@ -481,6 +629,38 @@ class ShuffleboardContainer : public virtual ShuffleboardValue {
|
||||
SimpleWidget& AddPersistent(std::string_view title,
|
||||
wpi::span<const double> defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display a simple piece of data.
|
||||
*
|
||||
* Unlike Add(std::string_view, wpi::span<const float>), the value in the
|
||||
* widget will be saved on the robot and will be used when the robot program
|
||||
* next starts rather than {@code defaultValue}.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param defaultValue the default value of the widget
|
||||
* @return a widget to display the sendable data
|
||||
* @see Add(std::string_view, wpi::span<const float>)
|
||||
* Add(std::string_view title, wpi::span<const float> defaultValue)
|
||||
*/
|
||||
SimpleWidget& AddPersistent(std::string_view title,
|
||||
wpi::span<const float> defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display a simple piece of data.
|
||||
*
|
||||
* Unlike Add(std::string_view, wpi::span<const int64_t>), the value in the
|
||||
* widget will be saved on the robot and will be used when the robot program
|
||||
* next starts rather than {@code defaultValue}.
|
||||
*
|
||||
* @param title the title of the widget
|
||||
* @param defaultValue the default value of the widget
|
||||
* @return a widget to display the sendable data
|
||||
* @see Add(std::string_view, wpi::span<const int64_t>)
|
||||
* Add(std::string_view title, wpi::span<const int64_t> defaultValue)
|
||||
*/
|
||||
SimpleWidget& AddPersistent(std::string_view title,
|
||||
wpi::span<const int64_t> defaultValue);
|
||||
|
||||
/**
|
||||
* Adds a widget to this container to display a simple piece of data.
|
||||
*
|
||||
|
||||
@@ -5,10 +5,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <networktables/GenericEntry.h>
|
||||
#include <networktables/NetworkTable.h>
|
||||
#include <networktables/NetworkTableEntry.h>
|
||||
|
||||
#include "frc/shuffleboard/ShuffleboardWidget.h"
|
||||
|
||||
@@ -26,14 +27,24 @@ class SimpleWidget final : public ShuffleboardWidget<SimpleWidget> {
|
||||
|
||||
/**
|
||||
* Gets the NetworkTable entry that contains the data for this widget.
|
||||
* The widget owns the entry.
|
||||
*/
|
||||
nt::NetworkTableEntry GetEntry();
|
||||
nt::GenericEntry& GetEntry();
|
||||
|
||||
/**
|
||||
* Gets the NetworkTable entry that contains the data for this widget.
|
||||
* The widget owns the entry.
|
||||
*
|
||||
* @param typeString NT type string
|
||||
*/
|
||||
nt::GenericEntry& GetEntry(std::string_view typeString);
|
||||
|
||||
void BuildInto(std::shared_ptr<nt::NetworkTable> parentTable,
|
||||
std::shared_ptr<nt::NetworkTable> metaTable) override;
|
||||
|
||||
private:
|
||||
nt::NetworkTableEntry m_entry;
|
||||
nt::GenericEntry m_entry;
|
||||
std::string m_typeString;
|
||||
|
||||
void ForceGenerate();
|
||||
};
|
||||
|
||||
@@ -6,10 +6,12 @@
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <networktables/BooleanTopic.h>
|
||||
#include <networktables/GenericEntry.h>
|
||||
#include <networktables/NetworkTable.h>
|
||||
#include <networktables/NetworkTableEntry.h>
|
||||
|
||||
#include "frc/shuffleboard/ShuffleboardComponent.h"
|
||||
#include "frc/shuffleboard/ShuffleboardComponent.inc"
|
||||
@@ -23,24 +25,31 @@ template <typename T>
|
||||
class SuppliedValueWidget : public ShuffleboardWidget<SuppliedValueWidget<T>> {
|
||||
public:
|
||||
SuppliedValueWidget(ShuffleboardContainer& parent, std::string_view title,
|
||||
std::function<T()> supplier,
|
||||
std::function<void(nt::NetworkTableEntry, T)> setter)
|
||||
std::string_view typeString, std::function<T()> supplier,
|
||||
std::function<void(nt::GenericPublisher&, T)> setter)
|
||||
: ShuffleboardValue(title),
|
||||
ShuffleboardWidget<SuppliedValueWidget<T>>(parent, title),
|
||||
m_typeString(typeString),
|
||||
m_supplier(supplier),
|
||||
m_setter(setter) {}
|
||||
|
||||
void BuildInto(std::shared_ptr<nt::NetworkTable> parentTable,
|
||||
std::shared_ptr<nt::NetworkTable> metaTable) override {
|
||||
this->BuildMetadata(metaTable);
|
||||
metaTable->GetEntry("Controllable").SetBoolean(false);
|
||||
m_controllablePub =
|
||||
nt::BooleanTopic{metaTable->GetTopic("Controllable")}.Publish();
|
||||
m_controllablePub.Set(false);
|
||||
|
||||
auto entry = parentTable->GetEntry(this->GetTitle());
|
||||
m_setter(entry, m_supplier());
|
||||
m_entry =
|
||||
parentTable->GetTopic(this->GetTitle()).GenericPublish(m_typeString);
|
||||
m_setter(m_entry, m_supplier());
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_typeString;
|
||||
std::function<T()> m_supplier;
|
||||
std::function<void(nt::NetworkTableEntry, T)> m_setter;
|
||||
std::function<void(nt::GenericPublisher&, T)> m_setter;
|
||||
nt::BooleanPublisher m_controllablePub;
|
||||
nt::GenericPublisher m_entry;
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
Reference in New Issue
Block a user