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

@@ -298,6 +298,11 @@ public interface Command {
return false;
}
/**
* Gets the name of this Command.
*
* @return Name
*/
default String getName() {
return this.getClass().getSimpleName();
}

View File

@@ -12,6 +12,7 @@ import java.util.Set;
import edu.wpi.first.wpilibj.Sendable;
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* A {@link Sendable} base class for {@link Command}s.
@@ -19,10 +20,13 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
@SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod")
public abstract class CommandBase implements Sendable, Command {
protected String m_name = this.getClass().getSimpleName();
protected String m_subsystem = "Ungrouped";
protected Set<Subsystem> m_requirements = new HashSet<>();
protected CommandBase() {
String name = getClass().getName();
SendableRegistry.add(this, name.substring(name.lastIndexOf('.') + 1));
}
/**
* Adds the specified requirements to the command.
*
@@ -39,22 +43,37 @@ public abstract class CommandBase implements Sendable, Command {
@Override
public String getName() {
return this.getClass().getSimpleName();
return SendableRegistry.getName(this);
}
/**
* Sets the name of this Command.
*
* @param name name
*/
@Override
public void setName(String name) {
m_name = name;
SendableRegistry.setName(this, name);
}
/**
* Gets the subsystem name of this Command.
*
* @return Subsystem name
*/
@Override
public String getSubsystem() {
return m_subsystem;
return SendableRegistry.getSubsystem(this);
}
/**
* Sets the subsystem name of this Command.
*
* @param subsystem subsystem name
*/
@Override
public void setSubsystem(String subsystem) {
m_subsystem = subsystem;
SendableRegistry.setSubsystem(this, subsystem);
}
/**

View File

@@ -23,8 +23,9 @@ import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.wpilibj.RobotState;
import edu.wpi.first.wpilibj.SendableBase;
import edu.wpi.first.wpilibj.Sendable;
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* The scheduler responsible for running {@link Command}s. A Command-based robot should call {@link
@@ -34,7 +35,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
* Subsystem#periodic()} methods to be called and for their default commands to be scheduled.
*/
@SuppressWarnings({"PMD.GodClass", "PMD.TooManyMethods", "PMD.TooManyFields"})
public final class CommandScheduler extends SendableBase {
public final class CommandScheduler implements Sendable {
/**
* The Singleton Instance.
*/
@@ -82,7 +83,7 @@ public final class CommandScheduler extends SendableBase {
CommandScheduler() {
HAL.report(tResourceType.kResourceType_Command, tInstances.kCommand_Scheduler);
setName("Scheduler");
SendableRegistry.addLW(this, "Scheduler");
}
/**

View File

@@ -8,8 +8,8 @@
package edu.wpi.first.wpilibj2.command;
import edu.wpi.first.wpilibj.Sendable;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* A base for subsystems that handles registration in the constructor, and provides a more intuitive
@@ -17,30 +17,54 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
*/
public abstract class SubsystemBase implements Subsystem, Sendable {
protected String m_name = this.getClass().getSimpleName();
/**
* Constructor.
*/
public SubsystemBase() {
String name = this.getClass().getSimpleName();
name = name.substring(name.lastIndexOf('.') + 1);
SendableRegistry.addLW(this, name, name);
CommandScheduler.getInstance().registerSubsystem(this);
}
/**
* Gets the name of this Subsystem.
*
* @return Name
*/
@Override
public String getName() {
return m_name;
return SendableRegistry.getName(this);
}
/**
* Sets the name of this Subsystem.
*
* @param name name
*/
@Override
public void setName(String name) {
m_name = name;
SendableRegistry.setName(this, name);
}
/**
* Gets the subsystem name of this Subsystem.
*
* @return Subsystem name
*/
@Override
public String getSubsystem() {
return getName();
return SendableRegistry.getSubsystem(this);
}
/**
* Sets the subsystem name of this Subsystem.
*
* @param subsystem subsystem name
*/
@Override
public void setSubsystem(String subsystem) {
setName(subsystem);
SendableRegistry.setSubsystem(this, subsystem);
}
/**
@@ -51,8 +75,8 @@ public abstract class SubsystemBase implements Subsystem, Sendable {
* @param child sendable
*/
public void addChild(String name, Sendable child) {
child.setName(getSubsystem(), name);
LiveWindow.add(child);
SendableRegistry.addLW(child, getSubsystem(), name);
SendableRegistry.addChild(this, child);
}
@Override

View File

@@ -8,6 +8,7 @@
package edu.wpi.first.wpilibj2.command;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* A command that does nothing but takes a specified amount of time to finish. Useful for
@@ -24,7 +25,7 @@ public class WaitCommand extends CommandBase {
*/
public WaitCommand(double seconds) {
m_duration = seconds;
setName(m_name + ": " + seconds + " seconds");
SendableRegistry.setName(this, getName() + ": " + seconds + " seconds");
}
@Override