mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
[ntcore] NetworkTables 4 (#3217)
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user