diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java index c868539ff4..c164b08695 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java @@ -261,7 +261,7 @@ public final class CommandScheduler implements Sendable, AutoCloseable { if (RobotBase.isSimulation()) { subsystem.simulationPeriodic(); } - m_watchdog.addEpoch(subsystem.getClass().getSimpleName() + ".periodic()"); + m_watchdog.addEpoch(subsystem.getName() + ".periodic()"); } // Cache the active instance to avoid concurrency problems if setActiveLoop() is called from diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java index ac89dbdc21..ed9454362a 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Subsystem.java @@ -40,6 +40,15 @@ public interface Subsystem { */ default void simulationPeriodic() {} + /** + * Gets the subsystem name of this Subsystem. + * + * @return Subsystem name + */ + default String getName() { + return this.getClass().getSimpleName(); + } + /** * Sets the default {@link Command} of the subsystem. The default command will be automatically * scheduled when no other commands are scheduled that require the subsystem. Default commands diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/SubsystemBase.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/SubsystemBase.java index 6cf926ee23..ea7a1ecaf7 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/SubsystemBase.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/SubsystemBase.java @@ -28,6 +28,7 @@ public abstract class SubsystemBase implements Subsystem, Sendable { * * @return Name */ + @Override public String getName() { return SendableRegistry.getName(this); } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp index d61ffbda3e..b424533106 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandScheduler.cpp @@ -183,7 +183,7 @@ void CommandScheduler::Run() { if constexpr (frc::RobotBase::IsSimulation()) { subsystem.getFirst()->SimulationPeriodic(); } - m_watchdog.AddEpoch("Subsystem Periodic()"); + m_watchdog.AddEpoch(subsystem.getFirst()->GetName() + ".Periodic()"); } // Cache the active instance to avoid concurrency problems if SetActiveLoop() diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp index 4c06f1f4ea..a0241bdd02 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/Subsystem.cpp @@ -4,6 +4,8 @@ #include "frc2/command/Subsystem.h" +#include + #include "frc2/command/CommandPtr.h" #include "frc2/command/Commands.h" @@ -16,6 +18,10 @@ void Subsystem::Periodic() {} void Subsystem::SimulationPeriodic() {} +std::string Subsystem::GetName() const { + return wpi::GetTypeName(*this); +} + void Subsystem::SetDefaultCommand(CommandPtr&& defaultCommand) { CommandScheduler::GetInstance().SetDefaultCommand(this, std::move(defaultCommand)); diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/Command.h b/wpilibNewCommands/src/main/native/include/frc2/command/Command.h index 9a48b2ecc4..a1b5dc170f 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/Command.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/Command.h @@ -18,11 +18,6 @@ namespace frc2 { -template -std::string GetTypeName(const T& type) { - return wpi::Demangle(typeid(type).name()); -} - /** * A state machine representing a complete action to be performed by the robot. * Commands are run by the CommandScheduler, and can be composed into diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h b/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h index cdac0c0466..b95b837c5f 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/Subsystem.h @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -59,6 +60,13 @@ class Subsystem { */ virtual void SimulationPeriodic(); + /** + * Gets the name of this Subsystem. + * + * @return Name + */ + virtual std::string GetName() const; + /** * Sets the default Command of the subsystem. The default command will be * automatically scheduled when no other commands are scheduled that require diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/SubsystemBase.h b/wpilibNewCommands/src/main/native/include/frc2/command/SubsystemBase.h index 86fb026fc1..cc9eeb35a5 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/SubsystemBase.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/SubsystemBase.h @@ -30,7 +30,7 @@ class SubsystemBase : public Subsystem, * * @return Name */ - std::string GetName() const; + std::string GetName() const override; /** * Sets the name of this Subsystem. diff --git a/wpiutil/src/main/native/include/wpi/Demangle.h b/wpiutil/src/main/native/include/wpi/Demangle.h index 03a7d3f254..8514be3fe2 100644 --- a/wpiutil/src/main/native/include/wpi/Demangle.h +++ b/wpiutil/src/main/native/include/wpi/Demangle.h @@ -7,6 +7,7 @@ #include #include +#include namespace wpi { @@ -18,6 +19,15 @@ namespace wpi { */ std::string Demangle(std::string_view mangledSymbol); +/** + * Returns the type name of an object + * @param type The object + */ +template +std::string GetTypeName(const T& type) { + return Demangle(typeid(type).name()); +} + } // namespace wpi #endif // WPIUTIL_WPI_DEMANGLE_H_