Ending a Command Sequence Group

Is there a way to end a command sequence group from within the group? I tried using isFinished, but got a runtime error at the isFinished call. Or do I need to do an isFinished check in each of the individual commands and let each finish separately.

Here’s what I tried:

package frc.robot.commands.IntakeShooter;
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import frc.robot.subsystems.IntakeConveyor;
import frc.robot.subsystems.Stick;

public class IntakeBallToLower extends SequentialCommandGroup {
    Stick m_stick;

      public IntakeBallToLower(IntakeConveyor intakeConveyor, Stick stick) {
        addCommands(
            new StartIntakeLowerConveyor(intakeConveyor),
            new stopIntakeLowerConveyor(intakeConveyor)
        );
    }

    @Override
    public boolean isFinished() {
      return (!m_stick.getStick().getRawButton(14));
    }
}

Have you tried cancel()ing the command?

Also, I recommend binding commands rather than passing buttons in.

1 Like

What was the runtime error? I’d expect a NullPointerException given the provided code since you’re not actually assigning anything to m_stick in the constructor.

2 Likes

Ahh - I forgot to assign m_stick. Thank you ! I’ll see if that works.
I do have a button binding to initially trigger the command, but I’ll try using that for the finishing part.

That’s not the problem. If you override methods of built-in command classes, you need to call the super definition of the method, otherwise you’ll get unexpected behavior–likely ending with an exception that’ll crash your code, possibly unexpectedly.

1 Like

One thing we’re considering is eventually moving to a model in which explicit subclassing of commands is discouraged and the library command implementations are hidden entirely. It’s not clear that continuing support for this pattern is worth it.

Can you explain this more? Is the problem that the OP didn’t call super(…) in the constructor or super.isFinished() in the method?

The latter. The built in command classes have functionality in those methods that is counted upon. For an example I’ve helped with as a CSA, SequentialCommandGroup without initialize crashes because the index doesn’t get initialized from -1.
I’ve never tried SequentialCommandGroup without isFinished, but I speculate that --for starters-- it won’t end when all commands are run. This may lead to an exception later, I don’t remember the exact data/control flow there.


Side note: I think that calling super() in the constructor is cleaner than addCommands, but that’s mostly personal preference.

OK - that makes sense, thank you

We’ll try it out as soon as our mechanical students finish disassembling and reassembling our Falcon 500’s. This is our first year using command based programming, and after the learning curve I can definitely say it’s worth it.

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