How to delay initilize command

We are planning a robot with a claw. However, when it is time to return to the robot, the claw hits the robot, which is damaging the part a little. To solve this, we tried to create a timer inside the command to delay the return so that the claw does not hit. However, when we tried this, the code crashed and gave this error starcompatition:

09:16:22,831
Error at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:387): The startCompetition() method (or methods called by it) should have handled the exception above. See https://wpilib.org/stacktrace for more information. The above stacktrace can help determine where the error occurred. ERROR 1 The startCompetition() method (or methods called by it) should have handled the exception above. edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:387) Warning at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:380): The robot program quit unexpectedly. This is usually due to a code error. at frc.robot.Main.main(Main.java:23) at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:458) at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:366) at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:131) at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(IterativeRobotBase.java:381) at frc.robot.Robot.robotPeriodic(Robot.java:47) at edu.wpi.first.wpilibj2.command.CommandScheduler.run(CommandScheduler.java:285) Warning 1 The robot program quit unexpectedly. This is usually due to a code error.
The above stacktrace can help determine where the error occurred.
See https://wpilib.org/stacktrace for more information. edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:380)

Here is how we initially tried to implement it:

  private Timer tempoParaSubir;


  @Override
  public void initialize() {
    tempoParaSubir.restart();

    if (tempoParaSubir.get() >= 1.4 && tempoParaSubir.get() <= 2) {
      intakeMode = IntakeMode.INSIDE;
    }
  }

And how we finally tried to implement it:

  private Timer tempoParaSubir;

  @Override
  public void initialize() {
    tempoParaSubir.restart();
  }

  @Override
  public void execute() {
    
    if (tempoParaSubir.get() >= 1.4 && tempoParaSubir.get() <= 2) {
      intakeMode = IntakeMode.INSIDE;
    }
}

We are not sure what it was, if anyone can help I would appreciate it.

Use a waitTime command in a sequential group before your command.

4 Likes

looks like you never initialized the timer, so its just null

3 Likes

Your first line should look like this:

private Timer tempoParaSubir = new Timer();
1 Like