2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
package org.wpilib.command2;
|
2019-08-25 17:47:07 -04:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
import org.wpilib.util.sendable.Sendable;
|
|
|
|
|
import org.wpilib.util.sendable.SendableBuilder;
|
|
|
|
|
import org.wpilib.util.sendable.SendableRegistry;
|
2023-09-14 23:56:48 -04:00
|
|
|
|
2019-08-25 17:47:07 -04:00
|
|
|
/**
|
|
|
|
|
* A base for subsystems that handles registration in the constructor, and provides a more intuitive
|
|
|
|
|
* method for setting the default command.
|
2022-01-08 11:11:34 -08:00
|
|
|
*
|
|
|
|
|
* <p>This class is provided by the NewCommands VendorDep
|
2019-08-25 17:47:07 -04:00
|
|
|
*/
|
2023-09-14 23:56:48 -04:00
|
|
|
public abstract class SubsystemBase implements Subsystem, Sendable {
|
2023-12-16 14:05:53 -05:00
|
|
|
/** Constructor. Telemetry/log name defaults to the classname. */
|
2023-12-09 21:45:02 -08:00
|
|
|
@SuppressWarnings("this-escape")
|
2023-09-14 23:56:48 -04:00
|
|
|
public SubsystemBase() {
|
|
|
|
|
String name = this.getClass().getSimpleName();
|
|
|
|
|
name = name.substring(name.lastIndexOf('.') + 1);
|
2025-01-25 10:52:19 -08:00
|
|
|
SendableRegistry.add(this, name, name);
|
2023-09-14 23:56:48 -04:00
|
|
|
CommandScheduler.getInstance().registerSubsystem(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-16 14:05:53 -05:00
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param name Name of the subsystem for telemetry and logging.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("this-escape")
|
|
|
|
|
public SubsystemBase(String name) {
|
2025-01-25 10:52:19 -08:00
|
|
|
SendableRegistry.add(this, name, name);
|
2023-12-16 14:05:53 -05:00
|
|
|
CommandScheduler.getInstance().registerSubsystem(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 23:56:48 -04:00
|
|
|
/**
|
|
|
|
|
* Gets the name of this Subsystem.
|
|
|
|
|
*
|
|
|
|
|
* @return Name
|
|
|
|
|
*/
|
2023-12-01 02:10:53 -05:00
|
|
|
@Override
|
2023-09-14 23:56:48 -04:00
|
|
|
public String getName() {
|
|
|
|
|
return SendableRegistry.getName(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the name of this Subsystem.
|
|
|
|
|
*
|
|
|
|
|
* @param name name
|
|
|
|
|
*/
|
|
|
|
|
public void setName(String name) {
|
|
|
|
|
SendableRegistry.setName(this, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the subsystem name of this Subsystem.
|
|
|
|
|
*
|
|
|
|
|
* @return Subsystem name
|
|
|
|
|
*/
|
|
|
|
|
public String getSubsystem() {
|
|
|
|
|
return SendableRegistry.getSubsystem(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the subsystem name of this Subsystem.
|
|
|
|
|
*
|
|
|
|
|
* @param subsystem subsystem name
|
|
|
|
|
*/
|
|
|
|
|
public void setSubsystem(String subsystem) {
|
|
|
|
|
SendableRegistry.setSubsystem(this, subsystem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Associates a {@link Sendable} with this Subsystem. Also update the child's name.
|
|
|
|
|
*
|
|
|
|
|
* @param name name to give child
|
|
|
|
|
* @param child sendable
|
|
|
|
|
*/
|
|
|
|
|
public void addChild(String name, Sendable child) {
|
2025-01-25 10:52:19 -08:00
|
|
|
SendableRegistry.add(child, getSubsystem(), name);
|
2023-09-14 23:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initSendable(SendableBuilder builder) {
|
|
|
|
|
builder.setSmartDashboardType("Subsystem");
|
|
|
|
|
|
|
|
|
|
builder.addBooleanProperty(".hasDefault", () -> getDefaultCommand() != null, null);
|
|
|
|
|
builder.addStringProperty(
|
|
|
|
|
".default",
|
|
|
|
|
() -> getDefaultCommand() != null ? getDefaultCommand().getName() : "none",
|
|
|
|
|
null);
|
|
|
|
|
builder.addBooleanProperty(".hasCommand", () -> getCurrentCommand() != null, null);
|
|
|
|
|
builder.addStringProperty(
|
|
|
|
|
".command",
|
|
|
|
|
() -> getCurrentCommand() != null ? getCurrentCommand().getName() : "none",
|
|
|
|
|
null);
|
|
|
|
|
}
|
|
|
|
|
}
|