Auto Timed command for new 2020 command based framework

I don’t know how I can make a timed autonomous command for new 2020 command based framework. Since the new command doesn’t provided “Timed Command” class.

I tired using the java thread method as you can see but when I pressed the button to trigger the command the robot quite.

public void execute() {
    drive.leftspeed(lspeed);
    drive.rightspeed(rspeed);
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    drive.leftspeed(0);
    drive.rightspeed(0);
  }

Github code here.

Help would be appreciated.

Never use Thread.sleep. It will delay the comms you need to make the code run.
You have at least two options:
Add a timeout variable that you increment every tick
Or
Try the new .withTimeout(). Not experienced with that so can’t help.

1 Like

I have tired withTimeout() before but it didn’t workout well for me. It made robot quite every time a command is called. I notice it’s a part of ParallelRaceGroup.

The word tick I bit confused about do you mean

What was the problem? We use .withTimeout() and it works as expected.

1 Like

By tick I just mean every cycle/iteration/ every time execute() runs.

Like when I code this in ParallelRaceGroup command and called it from class RobotContainer.

super(new TankdriveControl(new DriveTrain(), .5, .5).withTimeout(3));

The error I get is:

NT: server: client CONNECTED: 10.65.93.172 port 54681
Unhandled exception: edu.wpi.first.hal.util.UncleanStatusException: Code: -1029. HAL: Resource already allocated
Error at frc.robot.subsystems.DriveTrain.<init>(DriveTrain.java:25): Unhandled exception: edu.wpi.first.hal.util.UncleanStatusException: Code: -1029. HAL: Resource already allocated
at edu.wpi.first.hal.PWMJNI.initializePWMPort(Native Method)
at edu.wpi.first.wpilibj.PWM.<init>(PWM.java:62)
at edu.wpi.first.wpilibj.PWMSpeedController.<init>(PWMSpeedController.java:25)
at edu.wpi.first.wpilibj.Spark.<init>(Spark.java:52)
at frc.robot.subsystems.DriveTrain.<init>(DriveTrain.java:25)
at frc.robot.commands.DriveStraightAuto.<init>(DriveStraightAuto.java:26)
at frc.robot.RobotContainer.<init>(RobotContainer.java:43)
at frc.robot.Robot.robotInit(Robot.java:40)
at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:64)
at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:276)
at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:348)
at frc.robot.Main.main(Main.java:27)
Robots should not quit, but yours did!
Warning at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:291): Robots should not quit, but yours did!
The startCompetition() method (or methods called by it) should have handled the exception above.
Error at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:293): The startCompetition() method (or methods called by it) should have handled the exception above.
pure virtual method called
terminate called without an active exception

Don’t create a new drivetrain, pass in the one you already have in RobotContainer.

1 Like

What @Amicus1 said. The error you’re getting is Resource already allocated and it’s not related to using .withTimeout().
Because you’re instantiating the drivetrain multiple times, you’re instantiating hardware multiple times, which is not possible.

Is this what you mean or one without it so I have to define the drivetrain in the command.

public DriveStraightAuto(DriveTrain drive) {
    // Add your commands in the super() call, e.g.
    // super(new FooCommand(), new BarCommand());
    super(new TankdriveControl(drive, .5, .5).withTimeout(3));
  }

Like that if you’re using a command group. Good.

Now I get this error after I fix that:

Unhandled exception: java.lang.NullPointerException
Error at java.base/java.util.Objects.requireNonNull(Unknown Source): Unhandled exception: java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Unknown Source)
at java.base/java.util.ImmutableCollections$Set12.<init>(Unknown Source)
at java.base/java.util.Set.of(Unknown Source)
at edu.wpi.first.wpilibj2.command.CommandBase.addRequirements(CommandBase.java:36)
at frc.robot.commands.TankdriveControl.<init>(TankdriveControl.java:26)
at frc.robot.commands.DriveStraightAuto.<init>(DriveStraightAuto.java:26)
at frc.robot.RobotContainer.<init>(RobotContainer.java:43)
at frc.robot.Robot.robotInit(Robot.java:40)
at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:64)
at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:276)
at edu.wpi.first.wpilibj.RobotBase.startRobot(RobotBase.java:348)
at frc.robot.Main.main(Main.java:27)
Robots should not quit, but yours did!
Warning at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:291): Robots should not quit, but yours did!
The startCompetition() method (or methods called by it) should have handled the exception above.
Error at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:293): The startCompetition() method (or methods called by it) should have handled the exception above.
NT: server: client CONNECTED: 10.65.93.11 port 44984

It’s a bit hard to debug the problem without the code. Could you please upload it to GitHub or something like that so we could take a look?

Updated the code:

Ithout even seeing your code I can tell you the drivetrain object you passed in was null.
This happens in line 23 of TankDriveControl when you set the passed in parameter d to the null drive instead of the other way around. Simple error, just swap the two.

Unrelated to your issue, but this line isn’t doing anything and can be deleted. Subsystems should be instantiated inside RobotContainer, not Robot.

As for your issue, looks like @Amicus1 found it. That would definitely cause the error you’re seeing

1 Like

My problem has been resolved thanks to all of you who helped me out.

So the initial problem I had was that I couldn’t come up with a way to set the timeout for command for this year due to changes of command based framework. After I got help from wonderful people I was able to fix this problem. So there is help from wpilib API for timeout it’s called .withTimeout(sec). It work just like timeout from old command framework but with this you have to initialize this within your autonomous command so like super(new TankdriveControl(drive, .6, .7).withTimeout(3));. Just make sure you’re not passing in new object inside the same command (this is one of the problem I was having), constructor would be helpful in that scenario.

Thank you once again for all the help.

2 Likes

“TimedCommand” exists in the new command framework, it’s named “WaitCommand/WaitUntilCommand”.