Placing Commands to SmartDashboard in new Command Based Java?

This took me way too long to figure out, so I’m going to put down what I found, what I figured out, with the hope that someone with a couple more brain cells than I have left today can figure out a good way to address this in docs.wpilib.org

The Error

I’m getting stack overflow errors setting up a new Command for the first time in a program where I’ve been using inline commands so far, in the New Command Based framework.
The stack overflow occurs when I deploy code with the command added to RobotContainer, tied to a button.

Error at java.base/java.util.HashMap.newNode(HashMap.java:1797): Unhandled exception: java.lang.StackOverflowError 
 	at frc.robot.commands.centerToTarget.<init>(centerToTarget.java:33) 
 	at frc.robot.commands.centerToTarget.<init>(centerToTarget.java:33) 
 	at frc.robot.commands.centerToTarget.<init>(centerToTarget.java:33) 
 	at frc.robot.commands.centerToTarget.<init>(centerToTarget.java:33) 
  	at frc.robot.commands.centerToTarget.<init>(centerToTarget.java:33) 
 	etc x 100+
  1. Just in case… Do I need a constructor of some kind in robotContainer before I assign the command to the button? The Hatchbot uses “new Command” directly in the button map, so I think I don’t.

  2. Much more likely from the error message based on that I ****'d my constructor.
    [Editor’s note I have a subsystem class called “X” and I regret this choice]

    public class centerToTarget extends CommandBase {
    private final X m_sub;
    //Creates a new centerToTarget
    public centerToTarget(X sub) {
    m_sub = sub;
    //use addRequirements
    addRequirements(sub);
    SmartDashboard.putData(“CenterToTarget”, new centerToTarget(m_sub));
    }

The culprit is the SmartDashboard line here, that I’m placing in the constructor with a new command… of itself! Cue loop to infinity and beyond.

How I got here

What annoys me about currentstate Docs is that the only guidance I found when trying to add the command button to smartdashboard was “Check the box in RobotBuilder”, which is… not helpful when I’m not using RobotBuilder. So all I had was the codesnippet, which I inserted in the command constructor because shrug.

Maybe we could revise Hatchbot Traditional to have a smart dashboard Command button included?

Related, I have no idea what a good convention is in robotContainer for this.
I’m assuming I should probably only make one copy of the command? So in the RobotContainer, I’d do something like

public class RobotContainer {
private final Command m_command = new Command(); 

public RobotContainer{
final JoystickButton button = new JoystickButton(args); 
button.whenPressed(m_command);  
SmartDashboard.putData("Command", m_command); 
}

This is compiling and working, so I guess I’ll keep it.

You definitely only want to do one copy of the command. You probably also want to consolidate your smart dashboard stuff to one portion of code. This should have a do once section and a do every cycle( for updating things)

2 Likes

The hour of code video and code reveal from WPI accomplishes this. You can see the blog post here(scroll down a bit), and the code here.

1 Like