Java: default command

I have an intake sub system. When the arm of my robot is in the home position, I want the intake to go to the stow position, assuming I’m not pursuing an intake specific command like “intake note”.

I thought this would be an appropriate place to use a default command. However, the Java docs are fairly unclear as to how I set that up. Can someone explain it a little more clearly?

Call setDefaultCommand on the subsystem. That’s it.

Where do I call it from? What do I need in the subsystem? Where do I place the code I want run as default?

In this case, you might be better suited to have your “arm to home position” command also invoke the “intake to stow position” command.

A Default Command is the thing that runs when there are no other active commands. It’s great for things like drivetrains, where you typically want joystick/controller commands running any time you are not doing a special task. However, for something like an arm, you may not want the intake to always return to a “stow” position after every attempt to run a command that sets it to a specific angle.

However, if that is actually what you want, then Oblarg is right, you just call setDefaultCommand in your initialization code

If you want a default command after all, you call it like: subsystemName.setDefaultCommand(CommandName);

That should go in your RobotContainer() constructor,.

1 Like

To provide some extra clarification, you pass setDefaultCommand any command, be that a command class, a command factory method (in the subsystem or not), etc. You do not really need anything in the subsystem aside from whatever that command uses/needs (and the factory method if you are using one). setDefaultCommand can be called either from an outside class like RobotContainer or from inside the subsystem, depending on preference.

1 Like

Thanks guys! That makes perfect sense.

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.