Command run another command

I want a command to run another command in the execute function. This is so we can hit one button to run multiple commands. We will also use this multi command

you might want to look into this Command Compositions — FIRST Robotics Competition documentation

3 Likes

You can call .schedule() on a command to run it.

1 Like

You can but you generally should not. Make compositions or overload your bindings instead.

3 Likes

Yeah I agree, it is not a very clean way to do it. If you want more control over a command in the execute than command compositions, you could also have one command extend another command.

Inheritance generally isn’t a great way to handle it either

1 Like

why not

It’s extremely rigid. Composition is much more flexible.

1 Like

What do you exactly mean by a command running an another command? Is it an action that can’t be accomplished by building another command that works as a multi command and run multiple tasks at once?

So, we had to do just that because of the way how we do Auto commands with PathPlanner.

In our code it’s here:

So, we have AutonomousTrajectory2PosesDynamic command scheduling AutonomousTrajectory2Poses command.

Note that it’s more complicated if you need command sequences that need to track the completion of the command scheduled that way. In our case it’s the last command in the sequence, so independent schedule works fine.

But I believe this code does exactly what you’re asking about.

That does not always work depending on the situation.

Example - I want to drive to the target when the button is pressed if LL can determine the robot pose based on the current pose and the final known pose.

Because it’s a sequence, I want to do so inside SequentialCommandGroup.

Also all my commands including auto-trajectory ones are extending the classes (vs running AutoBuilder) because that way I have much more control for troubleshooting of the trajectories.

The issue is that commands in SequentialCommandGroup are done with early binding, which means you cannot pass dynamic parameters there even if you use Suppliers. So, you really need a command (e.g. instant command) that will have a logic determining which commands needs to run and under what condition (and yes, conditional command does early binding as well; I tested that, so it’s not an option), and then schedules the command dynamically from its “initialize”.

Because the new command is scheduled separately, it’s not part of the sequence. Hence, the sequences need to be handled with care if this mechanism is used.

We went that direction first (inheritance with overloading bindings), but it seems to result in a bloated code for something that can be accomplished in much simpler way. Also makes it much easier for the students to understand.
I thought about doing it through an interface instead, may be with a default method. But that way still has its limitations.

Ok thanks.