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