001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.wpilibj2.command; 006 007import edu.wpi.first.util.sendable.Sendable; 008import edu.wpi.first.util.sendable.SendableBuilder; 009import edu.wpi.first.util.sendable.SendableRegistry; 010 011/** 012 * A base for subsystems that handles registration in the constructor, and provides a more intuitive 013 * method for setting the default command. 014 * 015 * <p>This class is provided by the NewCommands VendorDep 016 */ 017public abstract class SubsystemBase implements Subsystem, Sendable { 018 /** Constructor. Telemetry/log name defaults to the classname. */ 019 @SuppressWarnings("this-escape") 020 public SubsystemBase() { 021 String name = this.getClass().getSimpleName(); 022 name = name.substring(name.lastIndexOf('.') + 1); 023 SendableRegistry.addLW(this, name, name); 024 CommandScheduler.getInstance().registerSubsystem(this); 025 } 026 027 /** 028 * Constructor. 029 * 030 * @param name Name of the subsystem for telemetry and logging. 031 */ 032 @SuppressWarnings("this-escape") 033 public SubsystemBase(String name) { 034 SendableRegistry.addLW(this, name, name); 035 CommandScheduler.getInstance().registerSubsystem(this); 036 } 037 038 /** 039 * Gets the name of this Subsystem. 040 * 041 * @return Name 042 */ 043 @Override 044 public String getName() { 045 return SendableRegistry.getName(this); 046 } 047 048 /** 049 * Sets the name of this Subsystem. 050 * 051 * @param name name 052 */ 053 public void setName(String name) { 054 SendableRegistry.setName(this, name); 055 } 056 057 /** 058 * Gets the subsystem name of this Subsystem. 059 * 060 * @return Subsystem name 061 */ 062 public String getSubsystem() { 063 return SendableRegistry.getSubsystem(this); 064 } 065 066 /** 067 * Sets the subsystem name of this Subsystem. 068 * 069 * @param subsystem subsystem name 070 */ 071 public void setSubsystem(String subsystem) { 072 SendableRegistry.setSubsystem(this, subsystem); 073 } 074 075 /** 076 * Associates a {@link Sendable} with this Subsystem. Also update the child's name. 077 * 078 * @param name name to give child 079 * @param child sendable 080 */ 081 public void addChild(String name, Sendable child) { 082 SendableRegistry.addLW(child, getSubsystem(), name); 083 } 084 085 @Override 086 public void initSendable(SendableBuilder builder) { 087 builder.setSmartDashboardType("Subsystem"); 088 089 builder.addBooleanProperty(".hasDefault", () -> getDefaultCommand() != null, null); 090 builder.addStringProperty( 091 ".default", 092 () -> getDefaultCommand() != null ? getDefaultCommand().getName() : "none", 093 null); 094 builder.addBooleanProperty(".hasCommand", () -> getCurrentCommand() != null, null); 095 builder.addStringProperty( 096 ".command", 097 () -> getCurrentCommand() != null ? getCurrentCommand().getName() : "none", 098 null); 099 } 100}