Repeat the process every 10 seconds

In Java, I want to repeat the process every 10 seconds only when Enable. What should I do?

Welcome to Chief Delphi!!!

One way to do this is to wire up a button to the roboRIO. Very briefly tap the button every ten seconds, exactly. Then, write some code like this:

DigitalInput freshmanButton = new DigitalInput(0);


public void robotPeriodic(){
    boolean runTheTask = freshmanButton.get();

    if(runTheTask){
        //Do the task once
    }

    // Else, we skip doing the task.

}

As long as you don’t skip pressing the button, you’ll do your task once, every 10 seconds.

However, this is will get boring and annoying very VERY quickly. I do not recommend actually doing this.

Thankfully, there’s a function called Timer.getFPGATimestamp(). This returns the number of seconds (as a floating point number) since the robot turned on. You can use its return values to calculate that same runTheTask boolean above.

double lastRunTime = Timer.getFPGATimestamp();


public void robotPeriodic(){
    boolean runTheTask;

    double curTime = Timer.getFPGATimestamp();
    double timeSinceLastRun = curTime - lastRunTime;

    if(timeSinceLastRun >= 10.0){
        runTheTask = true;
    }else{
        runTheTask = false;
    }

    if(runTheTask){
        //Do the task once

        lastRunTime = curTime;
    }

    // Else, we skip doing the task.

}

Food for thought:

  1. Why does the condition timeSinceLastRun >= 10.0 Have to have >=? Why not just == ?
  2. What would happen if we forgot to putlastRunTime = curTime; after running our task?

Note that this method will run a task once every ten seconds, synchronously and in the same thread as the main robot program. It’s just one of the tasks the robot does during robotPeriodic, sometimes. There’s an alternate methodology using Java Multithreading, which causes the task to execute in parallel to robot code, asynchronously from other things happening. It’s more complex, and I wouldn’t pursue it unless you’re sure the above solution won’t work.

6 Likes

How many seconds is robotPeriodic or teleopPeriodic called?

Depends on your RobotBase, but generally every 20ms, or 50Hz. For TimedRobot base it uses a timer so the period is pretty accurate, it can be less so with other bases.

1 Like

If you are using the Command-Based Framework. You could do this a few ways. The two that come to mind first for me are a continuously running Command that more or less implements the code @gerthworm showed you in the robotPeriodic() function in the Command’s execute() function.

The other one, and probably the one I personally would use, is creating a custom “TimedButton” Class that extends the Button class and implements code similar to @gerthworm’s from the robotPeriodic() in the Button’s get() function. Then you can easily have the button trigger a command from the OI the same way you do with a Joystick, Digital, Analog, or any other WPILIB Button. The advantage to me of this is the ability to set it up to take in the period it would execute at as a parameter to the constructor to easily re-use for other purposes in the future.

2 Likes

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