Simplify Sendable interface (#1864)

This removes the name and subsystem from individual objects, and instead
puts this data into a new singleton class, SendableRegistry.  Much of
LiveWindow has been refactored into SendableRegistry.

In C++, a new CRTP helper class, SendableHelper, has been added to provide
move and destruction functionality.

Shims for GetName, SetName, GetSubsystem, and SetSubsystem have been added
to Command and Subsystem (both old and new), and also to SendableHelper to
prevent code breakage.

This deprecates SendableBase in preparation for future removal.
This commit is contained in:
Peter Johnson
2019-09-14 15:22:54 -05:00
committed by GitHub
parent 1d8c4d016f
commit 471f375a38
216 changed files with 2448 additions and 1433 deletions

View File

@@ -24,6 +24,14 @@ using namespace frc2;
Command::~Command() { CommandScheduler::GetInstance().Cancel(this); }
Command::Command(const Command& rhs) : ErrorBase(rhs) {}
Command& Command::operator=(const Command& rhs) {
ErrorBase::operator=(rhs);
m_isGrouped = false;
return *this;
}
void Command::Initialize() {}
void Command::Execute() {}
void Command::End(bool interrupted) {}

View File

@@ -8,20 +8,14 @@
#include "frc2/command/CommandBase.h"
#include <frc/smartdashboard/SendableBuilder.h>
#include <frc/smartdashboard/SendableRegistry.h>
#include <frc2/command/CommandScheduler.h>
#include <frc2/command/SetUtilities.h>
using namespace frc2;
CommandBase::CommandBase() {
m_name = Command::GetName();
m_subsystem = "Unknown";
}
CommandBase::CommandBase(const CommandBase& other) : Sendable{}, Command{} {
m_name = other.m_name;
m_subsystem = other.m_subsystem;
m_requirements = other.m_requirements;
frc::SendableRegistry::GetInstance().AddLW(this, GetTypeName(*this));
}
void CommandBase::AddRequirements(
@@ -37,14 +31,20 @@ wpi::SmallSet<Subsystem*, 4> CommandBase::GetRequirements() const {
return m_requirements;
}
void CommandBase::SetName(const wpi::Twine& name) { m_name = name.str(); }
void CommandBase::SetName(const wpi::Twine& name) {
frc::SendableRegistry::GetInstance().SetName(this, name);
}
std::string CommandBase::GetName() const { return m_name; }
std::string CommandBase::GetName() const {
return frc::SendableRegistry::GetInstance().GetName(this);
}
std::string CommandBase::GetSubsystem() const { return m_subsystem; }
std::string CommandBase::GetSubsystem() const {
return frc::SendableRegistry::GetInstance().GetSubsystem(this);
}
void CommandBase::SetSubsystem(const wpi::Twine& subsystem) {
m_subsystem = subsystem.str();
frc::SendableRegistry::GetInstance().SetSubsystem(this, subsystem);
}
void CommandBase::InitSendable(frc::SendableBuilder& builder) {

View File

@@ -11,6 +11,7 @@
#include <frc/WPIErrors.h>
#include <frc/commands/Scheduler.h>
#include <frc/smartdashboard/SendableBuilder.h>
#include <frc/smartdashboard/SendableRegistry.h>
#include <frc2/command/CommandGroupBase.h>
#include <frc2/command/Subsystem.h>
@@ -22,7 +23,9 @@ static bool ContainsKey(const TMap& map, TKey keyToCheck) {
return map.find(keyToCheck) != map.end();
}
CommandScheduler::CommandScheduler() { SetName("Scheduler"); }
CommandScheduler::CommandScheduler() {
frc::SendableRegistry::GetInstance().AddLW(this, "Scheduler");
}
CommandScheduler& CommandScheduler::GetInstance() {
static CommandScheduler scheduler;

View File

@@ -7,15 +7,15 @@
#include "frc2/command/SubsystemBase.h"
#include <frc/livewindow/LiveWindow.h>
#include <frc/smartdashboard/SendableBuilder.h>
#include <frc/smartdashboard/SendableRegistry.h>
#include <frc2/command/Command.h>
#include <frc2/command/CommandScheduler.h>
using namespace frc2;
SubsystemBase::SubsystemBase() {
m_name = GetTypeName(*this);
frc::SendableRegistry::GetInstance().AddLW(this, GetTypeName(*this));
CommandScheduler::GetInstance().RegisterSubsystem({this});
}
@@ -43,15 +43,24 @@ void SubsystemBase::InitSendable(frc::SendableBuilder& builder) {
nullptr);
}
std::string SubsystemBase::GetName() const { return m_name; }
std::string SubsystemBase::GetName() const {
return frc::SendableRegistry::GetInstance().GetName(this);
}
void SubsystemBase::SetName(const wpi::Twine& name) { m_name = name.str(); }
void SubsystemBase::SetName(const wpi::Twine& name) {
frc::SendableRegistry::GetInstance().SetName(this, name);
}
std::string SubsystemBase::GetSubsystem() const { return GetName(); }
std::string SubsystemBase::GetSubsystem() const {
return frc::SendableRegistry::GetInstance().GetSubsystem(this);
}
void SubsystemBase::SetSubsystem(const wpi::Twine& name) { SetName(name); }
void SubsystemBase::SetSubsystem(const wpi::Twine& name) {
frc::SendableRegistry::GetInstance().SetSubsystem(this, name);
}
void SubsystemBase::AddChild(std::string name, frc::Sendable* child) {
child->SetName(name);
frc::LiveWindow::GetInstance()->Add(child);
auto& registry = frc::SendableRegistry::GetInstance();
registry.AddLW(child, GetSubsystem(), name);
registry.AddChild(this, child);
}

View File

@@ -11,7 +11,7 @@ using namespace frc2;
WaitCommand::WaitCommand(units::second_t duration) : m_duration{duration} {
auto durationStr = std::to_string(duration.to<double>());
SetName(wpi::Twine(m_name) + ": " + wpi::Twine(durationStr) + " seconds");
SetName(wpi::Twine(GetName()) + ": " + wpi::Twine(durationStr) + " seconds");
}
void WaitCommand::Initialize() {