[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

@@ -19,9 +19,11 @@
#include <hal/DriverStationTypes.h>
#include <hal/HALBase.h>
#include <hal/Power.h>
#include <networktables/BooleanTopic.h>
#include <networktables/IntegerTopic.h>
#include <networktables/NetworkTable.h>
#include <networktables/NetworkTableEntry.h>
#include <networktables/NetworkTableInstance.h>
#include <networktables/StringTopic.h>
#include <wpi/DataLog.h>
#include <wpi/condition_variable.h>
#include <wpi/mutex.h>
@@ -36,56 +38,42 @@ using namespace frc;
namespace {
// A simple class which caches the previous value written to an NT entry
// Used to prevent redundant, repeated writes of the same value
template <class T>
template <typename Topic>
class MatchDataSenderEntry {
public:
MatchDataSenderEntry(const std::shared_ptr<nt::NetworkTable>& table,
std::string_view key, const T& initialVal) {
static_assert(std::is_same_v<T, bool> || std::is_same_v<T, double> ||
std::is_same_v<T, std::string>,
"Invalid type for MatchDataSenderEntry - must be "
"to bool, double or std::string");
ntEntry = table->GetEntry(key);
if constexpr (std::is_same_v<T, bool>) {
ntEntry.ForceSetBoolean(initialVal);
} else if constexpr (std::is_same_v<T, double>) {
ntEntry.ForceSetDouble(initialVal);
} else if constexpr (std::is_same_v<T, std::string>) {
ntEntry.ForceSetString(initialVal);
}
prevVal = initialVal;
std::string_view key,
typename Topic::ParamType initialVal)
: publisher{Topic{table->GetTopic(key)}.Publish()}, prevVal{initialVal} {
publisher.Set(initialVal);
}
void Set(const T& val) {
void Set(typename Topic::ParamType val) {
if (val != prevVal) {
SetValue(val);
publisher.Set(val);
prevVal = val;
}
}
private:
nt::NetworkTableEntry ntEntry;
T prevVal;
void SetValue(bool val) { ntEntry.SetBoolean(val); }
void SetValue(double val) { ntEntry.SetDouble(val); }
void SetValue(std::string_view val) { ntEntry.SetString(val); }
typename Topic::PublisherType publisher;
typename Topic::ValueType prevVal;
};
struct MatchDataSender {
std::shared_ptr<nt::NetworkTable> table =
nt::NetworkTableInstance::GetDefault().GetTable("FMSInfo");
MatchDataSenderEntry<std::string> typeMetaData{table, ".type", "FMSInfo"};
MatchDataSenderEntry<std::string> gameSpecificMessage{
MatchDataSenderEntry<nt::StringTopic> typeMetaData{table, ".type", "FMSInfo"};
MatchDataSenderEntry<nt::StringTopic> gameSpecificMessage{
table, "GameSpecificMessage", ""};
MatchDataSenderEntry<std::string> eventName{table, "EventName", ""};
MatchDataSenderEntry<double> matchNumber{table, "MatchNumber", 0.0};
MatchDataSenderEntry<double> replayNumber{table, "ReplayNumber", 0.0};
MatchDataSenderEntry<double> matchType{table, "MatchType", 0.0};
MatchDataSenderEntry<bool> alliance{table, "IsRedAlliance", true};
MatchDataSenderEntry<double> station{table, "StationNumber", 1.0};
MatchDataSenderEntry<double> controlWord{table, "FMSControlData", 0.0};
MatchDataSenderEntry<nt::StringTopic> eventName{table, "EventName", ""};
MatchDataSenderEntry<nt::IntegerTopic> matchNumber{table, "MatchNumber", 0};
MatchDataSenderEntry<nt::IntegerTopic> replayNumber{table, "ReplayNumber", 0};
MatchDataSenderEntry<nt::IntegerTopic> matchType{table, "MatchType", 0};
MatchDataSenderEntry<nt::BooleanTopic> alliance{table, "IsRedAlliance", true};
MatchDataSenderEntry<nt::IntegerTopic> station{table, "StationNumber", 1};
MatchDataSenderEntry<nt::IntegerTopic> controlWord{table, "FMSControlData",
0};
};
class JoystickLogSender {