How to delay a part of code in robot without delaying everything?

For things like the ballshooter, we want it to delay for a bit of time before it retracts. But I’m afraid that delay(); from timer will delay all of the periodics in the robot. Is there any way to just delay one part?

de

Are you using a command based robot or an iterative one?

1 Like

A typical pattern goes something like this (pseudocode):

if (shouldStartDelayedActivity) { //e.g. from a button press, a command's initialize(), etc...
    startTime = Timer.getFPGATimestamp();
}
if (Timer.getFPGATimestamp() - 2 > startTime) { // if it's been 2 seconds since the start time
    DoTheActivity();
}
1 Like

This is perfect for delay-based code.

If you need a way to cancel the delayed activity, it is easy to force the entire action to be a sequence only while a button is held down:

if( buttonIsPressed != lastCycleButtonIsPressed && buttonIsPressed) { // only start this if the button goes from not pressed to pressed
 <code above>
} else {
  StopTheActivity(); // reset a talon, reset a solenoid, etc
}
lastCycleButtonIsPressed = buttonIsPressed;

We’re using command based

You could also use a CommandGroup, using (pseudocode again):

public class DelayCommandGroup extends CommandGroup {

    public DelayCommandGroup() {
        DoNothingCommand doNothing = new DoNothingCommand();
        DoStuffCommand doStuff = new DoStuffCommand ();
        
        doNothing.setTimeout(2.0);

        this.addSequential(doNothing);
        this.addSequential(doStuff);
    }
}

If you use a CommandGroup, there is already a built-in WaitCommand that does exactly what you are talking about.

[quote]
public class WaitCommand extends Command

A WaitCommand will wait for a certain amount of time before finishing. It is useful if you want a CommandGroup to pause for a moment.[/quote]

2 Likes

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