[ntcore] NetworkTables 4 (#3217)

This commit is contained in:
Peter Johnson
2022-10-08 10:01:31 -07:00
committed by GitHub
parent 90cfa00115
commit 77301b126c
380 changed files with 34573 additions and 22095 deletions

View File

@@ -195,7 +195,7 @@ class IterativeRobotBase : public RobotBase {
/**
* Enables or disables flushing NetworkTables every loop iteration.
* By default, this is disabled.
* By default, this is enabled.
*
* @param enabled True to enable, false to disable
*/
@@ -227,7 +227,7 @@ class IterativeRobotBase : public RobotBase {
Mode m_lastMode = Mode::kNone;
units::second_t m_period;
Watchdog m_watchdog;
bool m_ntFlushEnabled = false;
bool m_ntFlushEnabled = true;
void PrintLoopOverrunMessage();
};

View File

@@ -0,0 +1,79 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <memory>
#include <string_view>
#include "BooleanEvent.h"
namespace nt {
class BooleanSubscriber;
class BooleanTopic;
class NetworkTable;
class NetworkTableInstance;
} // namespace nt
namespace frc {
/**
* A Button that uses a NetworkTable boolean field.
*
* This class is provided by the NewCommands VendorDep
*/
class NetworkBooleanEvent : public BooleanEvent {
public:
/**
* Creates a new event with the given boolean topic determining whether it is
* active.
*
* @param loop the loop that polls this event
* @param topic The boolean topic that contains the value
*/
NetworkBooleanEvent(EventLoop* loop, nt::BooleanTopic topic);
/**
* Creates a new event with the given boolean subscriber determining whether
* it is active.
*
* @param loop the loop that polls this event
* @param sub The boolean subscriber that provides the value
*/
NetworkBooleanEvent(EventLoop* loop, nt::BooleanSubscriber sub);
/**
* Creates a new event with the given boolean topic determining whether it is
* active.
*
* @param loop the loop that polls this event
* @param table The NetworkTable that contains the topic
* @param topicName The topic name within the table that contains the value
*/
NetworkBooleanEvent(EventLoop* loop, std::shared_ptr<nt::NetworkTable> table,
std::string_view topicName);
/**
* Creates a new event with the given boolean topic determining whether it is
* active.
*
* @param loop the loop that polls this event
* @param tableName The NetworkTable name that contains the topic
* @param topicName The topic name within the table that contains the value
*/
NetworkBooleanEvent(EventLoop* loop, std::string_view tableName,
std::string_view topicName);
/**
* Creates a new event with the given boolean topic determining whether it is
* active.
*
* @param loop the loop that polls this event
* @param inst The NetworkTable instance to use
* @param tableName The NetworkTable that contains the topic
* @param topicName The topic name within the table that contains the value
*/
NetworkBooleanEvent(EventLoop* loop, nt::NetworkTableInstance inst,
std::string_view tableName, std::string_view topicName);
};
} // namespace frc

View File

@@ -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;
};

View File

@@ -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

View File

@@ -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

View File

@@ -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.
*

View File

@@ -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();
};

View File

@@ -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

View File

@@ -10,7 +10,7 @@
#include <utility>
#include <vector>
#include <networktables/NetworkTableEntry.h>
#include <networktables/DoubleArrayTopic.h>
#include <units/length.h>
#include <wpi/SmallVector.h>
#include <wpi/mutex.h>
@@ -105,7 +105,7 @@ class FieldObject2d {
mutable wpi::mutex m_mutex;
std::string m_name;
nt::NetworkTableEntry m_entry;
nt::DoubleArrayEntry m_entry;
mutable wpi::SmallVector<Pose2d, 1> m_poses;
};

View File

@@ -7,8 +7,10 @@
#include <memory>
#include <string>
#include <networktables/DoubleArrayTopic.h>
#include <networktables/NTSendable.h>
#include <networktables/NetworkTableEntry.h>
#include <networktables/NetworkTable.h>
#include <networktables/StringTopic.h>
#include <wpi/StringMap.h>
#include <wpi/mutex.h>
#include <wpi/sendable/SendableHelper.h>
@@ -83,5 +85,7 @@ class Mechanism2d : public nt::NTSendable,
mutable wpi::mutex m_mutex;
std::shared_ptr<nt::NetworkTable> m_table;
wpi::StringMap<std::unique_ptr<MechanismRoot2d>> m_roots;
nt::DoubleArrayPublisher m_dimsPub;
nt::StringPublisher m_colorPub;
};
} // namespace frc

View File

@@ -7,7 +7,8 @@
#include <memory>
#include <string_view>
#include <networktables/NetworkTableEntry.h>
#include <networktables/DoubleTopic.h>
#include <networktables/StringTopic.h>
#include <units/angle.h>
#include "frc/smartdashboard/MechanismObject2d.h"
@@ -89,14 +90,14 @@ class MechanismLigament2d : public MechanismObject2d {
void UpdateEntries(std::shared_ptr<nt::NetworkTable> table) override;
private:
void Flush();
nt::StringPublisher m_typePub;
double m_length;
nt::NetworkTableEntry m_lengthEntry;
nt::DoubleEntry m_lengthEntry;
double m_angle;
nt::NetworkTableEntry m_angleEntry;
nt::DoubleEntry m_angleEntry;
double m_weight;
nt::NetworkTableEntry m_weightEntry;
nt::DoubleEntry m_weightEntry;
char m_color[10];
nt::NetworkTableEntry m_colorEntry;
nt::StringEntry m_colorEntry;
};
} // namespace frc

View File

@@ -7,7 +7,7 @@
#include <memory>
#include <string_view>
#include <networktables/NetworkTableEntry.h>
#include <networktables/DoubleTopic.h>
#include "MechanismObject2d.h"
@@ -48,7 +48,7 @@ class MechanismRoot2d : private MechanismObject2d {
inline void Flush();
double m_x;
double m_y;
nt::NetworkTableEntry m_xEntry;
nt::NetworkTableEntry m_yEntry;
nt::DoublePublisher m_xPub;
nt::DoublePublisher m_yPub;
};
} // namespace frc

View File

@@ -11,10 +11,11 @@
#include <utility>
#include <vector>
#include <networktables/BooleanTopic.h>
#include <networktables/NTSendableBuilder.h>
#include <networktables/NetworkTable.h>
#include <networktables/NetworkTableEntry.h>
#include <networktables/NetworkTableValue.h>
#include <networktables/StringTopic.h>
#include <wpi/FunctionExtras.h>
#include <wpi/SmallVector.h>
#include <wpi/span.h>
@@ -54,7 +55,8 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
bool IsActuator() const;
/**
* Update the network table values by calling the getters for all properties.
* Synchronize with network table values by calling the getters for all
* properties and setters when the network table value has changed.
*/
void Update() override;
@@ -88,12 +90,18 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
void SetSmartDashboardType(std::string_view type) override;
void SetActuator(bool value) override;
void SetSafeState(std::function<void()> func) override;
void SetUpdateTable(std::function<void()> func) override;
nt::NetworkTableEntry GetEntry(std::string_view key) override;
void SetUpdateTable(wpi::unique_function<void()> func) override;
nt::Topic GetTopic(std::string_view key) override;
void AddBooleanProperty(std::string_view key, std::function<bool()> getter,
std::function<void(bool)> setter) override;
void AddIntegerProperty(std::string_view key, std::function<int64_t()> getter,
std::function<void(int64_t)> setter) override;
void AddFloatProperty(std::string_view key, std::function<float()> getter,
std::function<void(float)> setter) override;
void AddDoubleProperty(std::string_view key, std::function<double()> getter,
std::function<void(double)> setter) override;
@@ -105,6 +113,14 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
std::string_view key, std::function<std::vector<int>()> getter,
std::function<void(wpi::span<const int>)> setter) override;
void AddIntegerArrayProperty(
std::string_view key, std::function<std::vector<int64_t>()> getter,
std::function<void(wpi::span<const int64_t>)> setter) override;
void AddFloatArrayProperty(
std::string_view key, std::function<std::vector<float>()> getter,
std::function<void(wpi::span<const float>)> setter) override;
void AddDoubleArrayProperty(
std::string_view key, std::function<std::vector<double>()> getter,
std::function<void(wpi::span<const double>)> setter) override;
@@ -113,12 +129,10 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
std::string_view key, std::function<std::vector<std::string>()> getter,
std::function<void(wpi::span<const std::string>)> setter) override;
void AddRawProperty(std::string_view key, std::function<std::string()> getter,
std::function<void(std::string_view)> setter) override;
void AddValueProperty(
std::string_view key, std::function<std::shared_ptr<nt::Value>()> getter,
std::function<void(std::shared_ptr<nt::Value>)> setter) override;
void AddRawProperty(
std::string_view key, std::string_view typeString,
std::function<std::vector<uint8_t>()> getter,
std::function<void(wpi::span<const uint8_t>)> setter) override;
void AddSmallStringProperty(
std::string_view key,
@@ -131,6 +145,19 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
getter,
std::function<void(wpi::span<const int>)> setter) override;
void AddSmallIntegerArrayProperty(
std::string_view key,
std::function<
wpi::span<const int64_t>(wpi::SmallVectorImpl<int64_t>& buf)>
getter,
std::function<void(wpi::span<const int64_t>)> setter) override;
void AddSmallFloatArrayProperty(
std::string_view key,
std::function<wpi::span<const float>(wpi::SmallVectorImpl<float>& buf)>
getter,
std::function<void(wpi::span<const float>)> setter) override;
void AddSmallDoubleArrayProperty(
std::string_view key,
std::function<wpi::span<const double>(wpi::SmallVectorImpl<double>& buf)>
@@ -145,64 +172,46 @@ class SendableBuilderImpl : public nt::NTSendableBuilder {
std::function<void(wpi::span<const std::string>)> setter) override;
void AddSmallRawProperty(
std::string_view key,
std::function<std::string_view(wpi::SmallVectorImpl<char>& buf)> getter,
std::function<void(std::string_view)> setter) override;
std::string_view key, std::string_view typeString,
std::function<wpi::span<uint8_t>(wpi::SmallVectorImpl<uint8_t>& buf)>
getter,
std::function<void(wpi::span<const uint8_t>)> setter) override;
private:
struct Property {
Property(nt::NetworkTable& table, std::string_view key)
: entry(table.GetEntry(key)) {}
Property(const Property&) = delete;
Property& operator=(const Property&) = delete;
Property(Property&& other) noexcept
: entry(other.entry),
listener(other.listener),
update(std::move(other.update)),
createListener(std::move(other.createListener)) {
other.entry = nt::NetworkTableEntry();
other.listener = 0;
}
Property& operator=(Property&& other) noexcept {
entry = other.entry;
listener = other.listener;
other.entry = nt::NetworkTableEntry();
other.listener = 0;
update = std::move(other.update);
createListener = std::move(other.createListener);
return *this;
}
~Property() { StopListener(); }
void StartListener() {
if (entry && listener == 0 && createListener) {
listener = createListener(entry);
}
}
void StopListener() {
if (entry && listener != 0) {
entry.RemoveListener(listener);
listener = 0;
}
}
nt::NetworkTableEntry entry;
NT_EntryListener listener = 0;
std::function<void(nt::NetworkTableEntry entry, uint64_t time)> update;
std::function<NT_EntryListener(nt::NetworkTableEntry entry)> createListener;
virtual ~Property() = default;
virtual void Update(bool controllable, int64_t time) = 0;
};
std::vector<Property> m_properties;
template <typename Topic>
struct PropertyImpl : public Property {
void Update(bool controllable, int64_t time) override;
using Publisher = typename Topic::PublisherType;
using Subscriber = typename Topic::SubscriberType;
Publisher pub;
Subscriber sub;
std::function<void(Publisher& pub, int64_t time)> updateNetwork;
std::function<void(Subscriber& sub)> updateLocal;
};
template <typename Topic, typename Getter, typename Setter>
void AddPropertyImpl(Topic topic, Getter getter, Setter setter);
template <typename T, size_t Size, typename Topic, typename Getter,
typename Setter>
void AddSmallPropertyImpl(Topic topic, Getter getter, Setter setter);
std::vector<std::unique_ptr<Property>> m_properties;
std::function<void()> m_safeState;
std::vector<std::function<void()>> m_updateTables;
std::vector<wpi::unique_function<void()>> m_updateTables;
std::shared_ptr<nt::NetworkTable> m_table;
nt::NetworkTableEntry m_controllableEntry;
bool m_controllable = false;
bool m_actuator = false;
nt::BooleanPublisher m_controllablePublisher;
nt::StringPublisher m_typePublisher;
nt::BooleanPublisher m_actuatorPublisher;
};
} // namespace frc

View File

@@ -48,7 +48,14 @@ auto SendableChooser<T>::GetSelected()
template <class T>
void SendableChooser<T>::InitSendable(nt::NTSendableBuilder& builder) {
builder.SetSmartDashboardType("String Chooser");
builder.GetEntry(kInstance).SetDouble(m_instance);
{
std::scoped_lock lock(m_mutex);
m_instancePubs.emplace_back(
nt::IntegerTopic{builder.GetTopic(kInstance)}.Publish());
m_instancePubs.back().Set(m_instance);
m_activePubs.emplace_back(
nt::StringTopic{builder.GetTopic(kActive)}.Publish());
}
builder.AddStringArrayProperty(
kOptions,
[=] {
@@ -82,16 +89,12 @@ void SendableChooser<T>::InitSendable(nt::NTSendableBuilder& builder) {
}
},
nullptr);
{
std::scoped_lock lock(m_mutex);
m_activeEntries.emplace_back(builder.GetEntry(kActive));
}
builder.AddStringProperty(kSelected, nullptr, [=](std::string_view val) {
std::scoped_lock lock(m_mutex);
m_haveSelected = true;
m_selected = val;
for (auto& entry : m_activeEntries) {
entry.SetString(val);
for (auto& pub : m_activePubs) {
pub.Set(val);
}
});
}

View File

@@ -7,8 +7,9 @@
#include <atomic>
#include <string>
#include <networktables/IntegerTopic.h>
#include <networktables/NTSendable.h>
#include <networktables/NetworkTableEntry.h>
#include <networktables/StringTopic.h>
#include <wpi/SmallVector.h>
#include <wpi/mutex.h>
#include <wpi/sendable/SendableHelper.h>
@@ -40,7 +41,8 @@ class SendableChooserBase : public nt::NTSendable,
std::string m_defaultChoice;
std::string m_selected;
bool m_haveSelected = false;
wpi::SmallVector<nt::NetworkTableEntry, 2> m_activeEntries;
wpi::SmallVector<nt::IntegerPublisher, 2> m_instancePubs;
wpi::SmallVector<nt::StringPublisher, 2> m_activePubs;
wpi::mutex m_mutex;
int m_instance;
static std::atomic_int s_instances;

View File

@@ -62,39 +62,6 @@ class SmartDashboard {
*/
static bool IsPersistent(std::string_view key);
/**
* Sets flags on the specified key in this table. The key can
* not be null.
*
* @param key the key name
* @param flags the flags to set (bitmask)
*/
static void SetFlags(std::string_view key, unsigned int flags);
/**
* Clears flags on the specified key in this table. The key can
* not be null.
*
* @param key the key name
* @param flags the flags to clear (bitmask)
*/
static void ClearFlags(std::string_view key, unsigned int flags);
/**
* Returns the flags for the specified key.
*
* @param key the key name
* @return the flags, or 0 if the key is not defined
*/
static unsigned int GetFlags(std::string_view key);
/**
* Deletes the specified key in this table.
*
* @param key the key name
*/
static void Delete(std::string_view key);
/**
* Returns an NT Entry mapping to the specified key
*
@@ -363,7 +330,7 @@ class SmartDashboard {
* @param value The value that will be assigned.
* @return False if the table key already exists with a different type
*/
static bool PutRaw(std::string_view key, std::string_view value);
static bool PutRaw(std::string_view key, wpi::span<const uint8_t> value);
/**
* Gets the current value in the table, setting it if it does not exist.
@@ -373,7 +340,7 @@ class SmartDashboard {
* @returns False if the table key exists with a different type
*/
static bool SetDefaultRaw(std::string_view key,
std::string_view defaultValue);
wpi::span<const uint8_t> defaultValue);
/**
* Returns the raw value (byte array) the key maps to.
@@ -389,8 +356,8 @@ class SmartDashboard {
* @note This makes a copy of the raw contents. If the overhead of this is a
* concern, use GetValue() instead.
*/
static std::string GetRaw(std::string_view key,
std::string_view defaultValue);
static std::vector<uint8_t> GetRaw(std::string_view key,
wpi::span<const uint8_t> defaultValue);
/**
* Maps the specified key to the specified complex value (such as an array) in
@@ -403,8 +370,7 @@ class SmartDashboard {
* @param value the value
* @return False if the table key already exists with a different type
*/
static bool PutValue(std::string_view keyName,
std::shared_ptr<nt::Value> value);
static bool PutValue(std::string_view keyName, const nt::Value& value);
/**
* Gets the current value in the table, setting it if it does not exist.
@@ -414,7 +380,7 @@ class SmartDashboard {
* @returns False if the table key exists with a different type
*/
static bool SetDefaultValue(std::string_view key,
std::shared_ptr<nt::Value> defaultValue);
const nt::Value& defaultValue);
/**
* Retrieves the complex value (such as an array) in this table into the
@@ -422,7 +388,7 @@ class SmartDashboard {
*
* @param keyName the key
*/
static std::shared_ptr<nt::Value> GetValue(std::string_view keyName);
static nt::Value GetValue(std::string_view keyName);
/**
* Posts a task from a listener to the ListenerExecutor, so that it can be run