Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpiutil] Rewrite Sendable in the style of Struct and Protobuf #6453

Draft
wants to merge 60 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
2359599
Rewrite Sendable in the style of Struct and Protobuf
PeterJohnson Mar 19, 2024
01ae9de
Rename SendableCloser to SendableSet
PeterJohnson Mar 20, 2024
c7ea2f2
Start working on C++
PeterJohnson Mar 20, 2024
401c712
Remove isActuator, setSafeState
PeterJohnson Mar 21, 2024
cce9308
Add property functions
PeterJohnson Mar 21, 2024
ebaac11
Use newSetFromMap
PeterJohnson Mar 21, 2024
0e2f9ed
Fix up newSetFromMap
PeterJohnson Mar 22, 2024
4ae90f6
Add C++ SendableSet, SendableSet.forEach
PeterJohnson Mar 22, 2024
2ceca19
C++ WIP
PeterJohnson Mar 26, 2024
476037f
Ongoing C++ work
PeterJohnson Mar 30, 2024
d1b9e70
Add to AnalogTrigger, make buildable
PeterJohnson Mar 31, 2024
9b35a7b
Add SendableOption to Java
PeterJohnson Mar 31, 2024
34eeb1e
Add functions for pub/sub options, remove from other functions
PeterJohnson Mar 31, 2024
fda3d6d
Add protobuf
PeterJohnson Apr 1, 2024
424bb7d
Java: use isClosed() rather than getSendableSet()
PeterJohnson Apr 1, 2024
c75edee
Pass SendableTable to close function
PeterJohnson Apr 1, 2024
9208284
Add AddChild()
PeterJohnson Apr 1, 2024
be8ceb9
Don't make SendableTable final
PeterJohnson Apr 1, 2024
97b8e9b
Commit SendableWrapper
PeterJohnson Apr 1, 2024
9301b05
Start implementing Java struct
PeterJohnson Apr 1, 2024
06f6756
Rename AddChild to GetChild
PeterJohnson Apr 1, 2024
dad7236
Rename erase to remove
PeterJohnson Apr 2, 2024
e99e0a5
Fix clear() comment in Java
PeterJohnson Apr 2, 2024
82507af
Rename raw functions in Java
PeterJohnson Apr 2, 2024
2c71895
Implement protobuf Java
PeterJohnson Apr 2, 2024
76b7450
Remove commented-out GetSendable
PeterJohnson Apr 2, 2024
1d9e9ca
Add array functions to Java, rename int to integer
PeterJohnson Apr 2, 2024
ef710ff
Remove kDefaultSendableOptions
PeterJohnson Apr 2, 2024
311347a
Rename function-returning publishers to addXPublisher
PeterJohnson Apr 3, 2024
a544759
Reorder functions in SendableTable
PeterJohnson Apr 6, 2024
6e319c2
Start adding Telemetry class
PeterJohnson Apr 6, 2024
fc8f113
Add get functions
PeterJohnson Apr 6, 2024
2172c58
Start C++ Telemetry class
PeterJohnson Apr 6, 2024
621b722
Implement C++ Telemetry
PeterJohnson Apr 6, 2024
9109689
Add missing includes
PeterJohnson Apr 6, 2024
f06c8f1
MoveTracker: Don't include Signal, since it's commented out
PeterJohnson Apr 6, 2024
258fa50
[cmake] Add quickbuf to wpilibj java build
PeterJohnson Apr 6, 2024
2dc0628
Don't return old table from setTable
PeterJohnson Apr 6, 2024
bf4fb62
Add temporary build fix
PeterJohnson Apr 6, 2024
b1db73b
Start implementing NetworkSendableTable
PeterJohnson Apr 7, 2024
c4b2b87
Add missing includes
PeterJohnson Apr 7, 2024
123431d
More NetworkSendableTable and DataLogSendableTable work
PeterJohnson Apr 8, 2024
54b011a
Continued work on DataLogSendableTable
PeterJohnson Apr 9, 2024
6c45589
Telemetry: Default-initialize with NetworkTables backend
PeterJohnson Apr 9, 2024
f789ce6
Telemetry: Add missing name parameter
PeterJohnson Apr 10, 2024
3047947
Finish NetworkSendableTable implementation
PeterJohnson Apr 15, 2024
5799f24
DataLogSendableTable: Remove change tracking and getters
PeterJohnson Apr 16, 2024
7838e20
DataLogSendableTable: Thread safety
PeterJohnson Apr 16, 2024
6d8471d
Various Java formatting/bug fixups
PeterJohnson Apr 17, 2024
8d4109b
Add addPeriodic() to SendableTable interface
PeterJohnson Apr 17, 2024
aa620cf
ADXL and ADXRS new Sendable implementations (Java)
PeterJohnson Apr 17, 2024
981358c
WIP
PeterJohnson Aug 1, 2024
42aa448
Update AllAxes struct's to getTypeName
PeterJohnson Aug 3, 2024
fa22e13
[wpiutil] StringLogEntry::Update(): Take string_view
PeterJohnson Aug 3, 2024
5cd3f7b
Get C++ building
PeterJohnson Aug 3, 2024
439475a
Implement DataLog SetPublishOptions
PeterJohnson Aug 3, 2024
0cc209d
Make mutex mutable
PeterJohnson Aug 3, 2024
8ac88b1
Fix missing name for set options
PeterJohnson Aug 3, 2024
835c475
Remove lock from RefreshProperties
PeterJohnson Aug 3, 2024
2da46c4
Continue working on C++
PeterJohnson Aug 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,204 changes: 1,204 additions & 0 deletions ntcore/src/main/java/edu/wpi/first/networktables/NetworkSendableTable.java

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions styleguide/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<Match>
<Bug pattern="EI_EXPOSE_REP2" />
</Match>
<Match>
<Bug pattern="EI_EXPOSE_STATIC_REP2" />
<Class name="edu.wpi.first.wpilibj.telemetry.Telemetry" />
</Match>
<Match>
<Bug pattern="FL_FLOATS_AS_LOOP_COUNTERS" />
</Match>
Expand Down
12 changes: 4 additions & 8 deletions wpilibc/src/main/native/cpp/AnalogInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#include <hal/HALBase.h>
#include <hal/Ports.h>
#include <wpi/StackTrace.h>
#include <wpi/sendable/SendableBuilder.h>
#include <wpi/sendable/SendableRegistry.h>
#include <wpi/sendable2/SendableTable.h>

#include "frc/Errors.h"
#include "frc/SensorUtil.h"
Expand All @@ -33,8 +32,6 @@ AnalogInput::AnalogInput(int channel) {
FRC_CheckErrorStatus(status, "Channel {}", channel);

HAL_Report(HALUsageReporting::kResourceType_AnalogChannel, channel + 1);

wpi::SendableRegistry::AddLW(this, "AnalogInput", channel);
}

AnalogInput::~AnalogInput() {
Expand Down Expand Up @@ -194,8 +191,7 @@ void AnalogInput::SetSimDevice(HAL_SimDeviceHandle device) {
HAL_SetAnalogInputSimDevice(m_port, device);
}

void AnalogInput::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("Analog Input");
builder.AddDoubleProperty(
"Value", [=, this] { return GetAverageVoltage(); }, nullptr);
void wpi2::Sendable<AnalogInput>::Init(frc::AnalogInput* obj,
SendableTable& table) {
table.PublishDouble("Value", [obj] { return obj->GetAverageVoltage(); });
}
18 changes: 8 additions & 10 deletions wpilibc/src/main/native/cpp/AnalogTrigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <hal/AnalogTrigger.h>
#include <hal/FRCUsageReporting.h>
#include <wpi/NullDeleter.h>
#include <wpi/sendable/SendableRegistry.h>
#include <wpi/sendable2/SendableTable.h>

#include "frc/AnalogInput.h"
#include "frc/DutyCycle.h"
Expand All @@ -20,7 +20,6 @@ using namespace frc;
AnalogTrigger::AnalogTrigger(int channel)
: AnalogTrigger(new AnalogInput(channel)) {
m_ownsAnalog = true;
wpi::SendableRegistry::AddChild(this, m_analogInput);
}

AnalogTrigger::AnalogTrigger(AnalogInput* input) {
Expand All @@ -31,7 +30,6 @@ AnalogTrigger::AnalogTrigger(AnalogInput* input) {
int index = GetIndex();

HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, index + 1);
wpi::SendableRegistry::AddLW(this, "AnalogTrigger", index);
}

AnalogTrigger::AnalogTrigger(DutyCycle* input) {
Expand All @@ -42,7 +40,6 @@ AnalogTrigger::AnalogTrigger(DutyCycle* input) {
int index = GetIndex();

HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, index + 1);
wpi::SendableRegistry::AddLW(this, "AnalogTrigger", index);
}

AnalogTrigger::~AnalogTrigger() {
Expand Down Expand Up @@ -112,12 +109,6 @@ std::shared_ptr<AnalogTriggerOutput> AnalogTrigger::CreateOutput(
new AnalogTriggerOutput(*this, type));
}

void AnalogTrigger::InitSendable(wpi::SendableBuilder& builder) {
if (m_ownsAnalog) {
m_analogInput->InitSendable(builder);
}
}

int AnalogTrigger::GetSourceChannel() const {
if (m_analogInput) {
return m_analogInput->GetChannel();
Expand All @@ -127,3 +118,10 @@ int AnalogTrigger::GetSourceChannel() const {
return -1;
}
}

void wpi2::Sendable<AnalogTrigger>::Init(frc::AnalogTrigger* obj,
SendableTable& table) {
if (obj->m_ownsAnalog) {
Sendable<AnalogInput>::Init(obj->m_analogInput, table);
}
}
191 changes: 191 additions & 0 deletions wpilibc/src/main/native/cpp/telemetry/Telemetry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// 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.

#include "frc/telemetry/Telemetry.h"

#include <wpi/json.h>
#include <wpi/sendable2/SendableTable.h>

using namespace frc;

wpi2::SendableTable& Telemetry::GetTableHolder() {
static wpi2::SendableTable instance{nullptr}; // FIXME
return instance;
}

void Telemetry::SetTable(wpi2::SendableTable table) {
GetTableHolder() = std::move(table);
}

wpi2::SendableTable Telemetry::GetTable() {
return GetTableHolder();
}

void Telemetry::SetBoolean(std::string_view name, bool value) {
GetTableHolder().SetBoolean(name, value);
}

void Telemetry::SetInteger(std::string_view name, int64_t value) {
GetTableHolder().SetInteger(name, value);
}

void Telemetry::SetFloat(std::string_view name, float value) {
GetTableHolder().SetFloat(name, value);
}

void Telemetry::SetDouble(std::string_view name, double value) {
GetTableHolder().SetDouble(name, value);
}

void Telemetry::SetString(std::string_view name, std::string_view value) {
GetTableHolder().SetString(name, value);
}

void Telemetry::SetRaw(std::string_view name, std::string_view typeString,
std::span<const uint8_t> value) {
GetTableHolder().SetRaw(name, typeString, value);
}

void Telemetry::PublishBoolean(std::string_view name,
std::function<bool()> supplier) {
GetTableHolder().PublishBoolean(name, std::move(supplier));
}

void Telemetry::PublishInteger(std::string_view name,
std::function<int64_t()> supplier) {
GetTableHolder().PublishInteger(name, std::move(supplier));
}

void Telemetry::PublishFloat(std::string_view name,
std::function<float()> supplier) {
GetTableHolder().PublishFloat(name, std::move(supplier));
}

void Telemetry::PublishDouble(std::string_view name,
std::function<double()> supplier) {
GetTableHolder().PublishDouble(name, std::move(supplier));
}

void Telemetry::PublishString(std::string_view name,
std::function<std::string()> supplier) {
GetTableHolder().PublishString(name, std::move(supplier));
}

void Telemetry::PublishRaw(std::string_view name, std::string_view typeString,
std::function<std::vector<uint8_t>()> supplier) {
GetTableHolder().PublishRaw(name, typeString, std::move(supplier));
}

void Telemetry::PublishRawSmall(
std::string_view name, std::string_view typeString,
std::function<std::span<uint8_t>(wpi::SmallVectorImpl<uint8_t>& buf)>
supplier) {
GetTableHolder().PublishRawSmall(name, typeString, std::move(supplier));
}

std::function<void(bool)> Telemetry::AddBooleanPublisher(
std::string_view name) {
return GetTableHolder().AddBooleanPublisher(name);
}

std::function<void(int64_t)> Telemetry::AddIntegerPublisher(
std::string_view name) {
return GetTableHolder().AddIntegerPublisher(name);
}

std::function<void(float)> Telemetry::AddFloatPublisher(std::string_view name) {
return GetTableHolder().AddFloatPublisher(name);
}

std::function<void(double)> Telemetry::AddDoublePublisher(
std::string_view name) {
return GetTableHolder().AddDoublePublisher(name);
}

std::function<void(std::string_view)> Telemetry::AddStringPublisher(
std::string_view name) {
return GetTableHolder().AddStringPublisher(name);
}

std::function<void(std::span<const uint8_t>)> Telemetry::AddRawPublisher(
std::string_view name, std::string_view typeString) {
return GetTableHolder().AddRawPublisher(name, typeString);
}

void Telemetry::SubscribeBoolean(std::string_view name,
std::function<void(bool)> consumer) {
GetTableHolder().SubscribeBoolean(name, std::move(consumer));
}

void Telemetry::SubscribeInteger(std::string_view name,
std::function<void(int64_t)> consumer) {
GetTableHolder().SubscribeInteger(name, std::move(consumer));
}

void Telemetry::SubscribeFloat(std::string_view name,
std::function<void(float)> consumer) {
GetTableHolder().SubscribeFloat(name, std::move(consumer));
}

void Telemetry::SubscribeDouble(std::string_view name,
std::function<void(double)> consumer) {
GetTableHolder().SubscribeDouble(name, std::move(consumer));
}

void Telemetry::SubscribeString(
std::string_view name, std::function<void(std::string_view)> consumer) {
GetTableHolder().SubscribeString(name, std::move(consumer));
}

void Telemetry::SubscribeRaw(
std::string_view name, std::string_view typeString,
std::function<void(std::span<const uint8_t>)> consumer) {
GetTableHolder().SubscribeRaw(name, typeString, std::move(consumer));
}

wpi2::SendableTable Telemetry::GetChild(std::string_view name) {
return GetTableHolder().GetChild(name);
}

void Telemetry::SetPublishOptions(std::string_view name,
const wpi2::SendableOptions& options) {
GetTableHolder().SetPublishOptions(name, options);
}

void Telemetry::SetSubscribeOptions(std::string_view name,
const wpi2::SendableOptions& options) {
GetTableHolder().SetSubscribeOptions(name, options);
}

wpi::json Telemetry::GetProperty(std::string_view name,
std::string_view propName) {
return GetTableHolder().GetProperty(name, propName);
}

void Telemetry::SetProperty(std::string_view name, std::string_view propName,
const wpi::json& value) {
GetTableHolder().SetProperty(name, propName, value);
}

void Telemetry::DeleteProperty(std::string_view name,
std::string_view propName) {
GetTableHolder().DeleteProperty(name, propName);
}

wpi::json Telemetry::GetProperties(std::string_view name) {
return GetTableHolder().GetProperties(name);
}

bool Telemetry::SetProperties(std::string_view name,
const wpi::json& properties) {
return GetTableHolder().SetProperties(name, properties);
}

void Telemetry::Remove(std::string_view name) {
GetTableHolder().Remove(name);
}

void Telemetry::Clear() {
GetTableHolder().Clear();
}
22 changes: 16 additions & 6 deletions wpilibc/src/main/native/include/frc/AnalogInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <stdint.h>

#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
#include <wpi/sendable2/Sendable.h>
#include <wpi/MoveTracker.h>

namespace frc {

Expand All @@ -27,8 +27,7 @@ class DMASample;
* are divided by the number of samples to retain the resolution, but get more
* stable values.
*/
class AnalogInput : public wpi::Sendable,
public wpi::SendableHelper<AnalogInput> {
class AnalogInput : public wpi::MoveTrackerBase {
friend class AnalogTrigger;
friend class AnalogGyro;
friend class DMA;
Expand Down Expand Up @@ -280,12 +279,23 @@ class AnalogInput : public wpi::Sendable,
*/
void SetSimDevice(HAL_SimDeviceHandle device);

void InitSendable(wpi::SendableBuilder& builder) override;

private:
int m_channel;
hal::Handle<HAL_AnalogInputHandle> m_port;
int64_t m_accumulatorOffset;
};

} // namespace frc

namespace wpi2 {

template<>
struct Sendable<frc::AnalogInput> {
Starlight220 marked this conversation as resolved.
Show resolved Hide resolved
static constexpr std::string_view GetTypeString() { return "Analog Input"; }
static void Init(frc::AnalogInput* obj, SendableTable& table);
static void Close(frc::AnalogInput* obj, SendableTable& table) {}
};

static_assert(SendableSerializable<frc::AnalogInput>);

} // namespace wpi2
23 changes: 17 additions & 6 deletions wpilibc/src/main/native/include/frc/AnalogTrigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <memory>

#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
#include <wpi/sendable2/Sendable.h>
#include <wpi/MoveTracker.h>

#include "frc/AnalogTriggerOutput.h"

Expand All @@ -17,9 +17,9 @@ namespace frc {
class AnalogInput;
class DutyCycle;

class AnalogTrigger : public wpi::Sendable,
public wpi::SendableHelper<AnalogTrigger> {
class AnalogTrigger : public wpi::MoveTrackerBase {
friend class AnalogTriggerOutput;
friend struct wpi2::Sendable<frc::AnalogTrigger>;

public:
/**
Expand Down Expand Up @@ -148,8 +148,6 @@ class AnalogTrigger : public wpi::Sendable,
std::shared_ptr<AnalogTriggerOutput> CreateOutput(
AnalogTriggerType type) const;

void InitSendable(wpi::SendableBuilder& builder) override;

private:
int GetSourceChannel() const;

Expand All @@ -160,3 +158,16 @@ class AnalogTrigger : public wpi::Sendable,
};

} // namespace frc

namespace wpi2 {

template <>
struct Sendable<frc::AnalogTrigger> {
static constexpr std::string_view GetTypeString() { return "Analog Input"; }
static void Init(frc::AnalogTrigger* obj, SendableTable& table);
static void Close(frc::AnalogTrigger* obj, SendableTable& table) {}
};

static_assert(SendableSerializable<frc::AnalogTrigger>);

} // namespace wpi2
Loading