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. */ 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 * Gets the name of this Subsystem. 029 * 030 * @return Name 031 */ 032 @Override 033 public String getName() { 034 return SendableRegistry.getName(this); 035 } 036 037 /** 038 * Sets the name of this Subsystem. 039 * 040 * @param name name 041 */ 042 public void setName(String name) { 043 SendableRegistry.setName(this, name); 044 } 045 046 /** 047 * Gets the subsystem name of this Subsystem. 048 * 049 * @return Subsystem name 050 */ 051 public String getSubsystem() { 052 return SendableRegistry.getSubsystem(this); 053 } 054 055 /** 056 * Sets the subsystem name of this Subsystem. 057 * 058 * @param subsystem subsystem name 059 */ 060 public void setSubsystem(String subsystem) { 061 SendableRegistry.setSubsystem(this, subsystem); 062 } 063 064 /** 065 * Associates a {@link Sendable} with this Subsystem. Also update the child's name. 066 * 067 * @param name name to give child 068 * @param child sendable 069 */ 070 public void addChild(String name, Sendable child) { 071 SendableRegistry.addLW(child, getSubsystem(), name); 072 } 073 074 @Override 075 public void initSendable(SendableBuilder builder) { 076 builder.setSmartDashboardType("Subsystem"); 077 078 builder.addBooleanProperty(".hasDefault", () -> getDefaultCommand() != null, null); 079 builder.addStringProperty( 080 ".default", 081 () -> getDefaultCommand() != null ? getDefaultCommand().getName() : "none", 082 null); 083 builder.addBooleanProperty(".hasCommand", () -> getCurrentCommand() != null, null); 084 builder.addStringProperty( 085 ".command", 086 () -> getCurrentCommand() != null ? getCurrentCommand().getName() : "none", 087 null); 088 } 089}