Ending a command?

This is my first year programming and at this point the only way I know how to end a command is to use a while statement and a timer with a boolean as shown:

@Override
public void initialize() {

m_indexerSubsystem.indexBallSimple();
m_intakeSubsystem.intakeForward();
m_intakeSubsystem.intakeDown();
time.reset();
time.start();
while (time.get() < 2){
  isFinished = false;
}
isFinished = true;

}

The issue here is that when I run this command, It doesn’t allow me to run anything else in those 2 seconds. Is there any other way for me to end the command without doing this? this is in the AutoIntakeStart.java file

this is the Github if you want to look at the code:

What do you want to use as a determination of how the command should end?

For just a time based command the .withTimeout decorator is probably easiest.

So you would change this line to: autoIntakeStartButton.whenPressed(new AutoIntakeStart(m_intakeSubsystem, m_indexerSubsystem).withTimeout(2));

2 Likes

that line was mostly there for me to test out that command. It is supposed to just intake, index, and put down the intake during auto and then put it back up after 2 seconds or so. Is that how you do that there? Before, every time I tried the function in a parallel command group(auto) or just try to drive around(teleop) it just stops everything else until that command finishes.

Do you have an example of where you tried it? You mention a parallel command group, what things were you running in parallel? Generally an Auto starts out at the top level as a sequential command group.

if you check the github, I pushed the auto that I tried before. It would stall after shooting. Im trying to run a swervecontrollercommand and intake at the same time

The reason why this is stalling everything is because this blocks the thread. No other code is getting ran while this while loop is active. You could change the command so the isFinished() returns return time.get() > 2. However, there are better ways of doing it, such as using :

1 Like

If you really wanted to do it like that (I wouldn’t) you could put a time.get in the execute part that when true it changes the is finished but while loops are generally a bad idea in FRC programming…the vast majority of the time.

okay. Like I said, it is my first year and all I had for resources was youtube so I tried my best. the .withTimeout() makes a lot of sense. thanks!

1 Like

Or you could do something like this

ExampleDriveCommand.racewith(ExampleIntakeCommand)

In this case ExampleIntakeCommand would not need to “finish”, it would get interrupted when ExampleDriveCommand ends.

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