Tasks

class Tasks

Utility class for running Task instances from an environment that does not have a task scheduler. This is useful for OpModes that are not using a command-based structure, such as the base BunyipsOpMode, but still needs support to run tasks on subsystems that only return Tasks. This class simplifies the task run process to a more intermediate "register and run" system. Do note it is recommended to use a more integrated system such as the Scheduler, which is integrated into CommandBasedBunyipsOpMode.

This class will allow you to simply pass a task instance that you wish to run (with run conditions being managed at your own discretion), where continuous calls to runRepeatedly() will execute the task continually, resetting it if it is done. This behaviour is similar to how the run() call works in the Scheduler, however, this comes with the guarantee that a task will only execute any time it is being actively told to do so.

If you simply wish to run tasks with limited instance management, instantiating them directly in register and running them can be accomplished with an index, as done in the run/runRepeatedly overloads. This limits your ability to manage/reset tasks, but is useful for simpler control loops.

This class is used maximally where you will manage the instances of the tasks you retrieve from your subsystems/methods, to allow you finer-grain control over your tasks and their reset/runtime methods. Do note however it may be more worthwhile migrating to a command-based structure such as the Scheduler or CommandBasedBunyipsOpMode. This pattern is useful for the standard run method that will execute one start-to-finish cycle of a task, with the added benefit that a task will only iterate when being told to do so by methods in this utility.

Example usage:


    // init-phase, index-based registration
    Tasks.register(arm.tasks.home(), claw.tasks.open(), ...);
    // active phase
    if (condition) {
      Tasks.run(0); // runs arm.tasks.home() one iteration, if it is finished it will no-op forever
      Tasks.runRepeatedly(1); // runs claw.tasks.open(), if it is finished it will be auto-reset and can run again
    }

    // init-phase, instance-based registration
    Task homeTask = arm.tasks.home();
    Task openTask = claw.tasks.open();
    Tasks.register(homeTask, openTask);
    // active phase
    if (condition) {
        Tasks.run(homeTask); // runs arm.tasks.home() one iteration, if it is finished it will no-op until a reset
        Tasks.run(1); // index-based from above still works, the only difference with this approach is that
                      // you have additional control over your tasks.
    }
    // reset can be accomplished as with any task, allowing run calls to work again
    if (... && homeTask.isFinished()) homeTask.reset();

Author

Lucas Bubner, 2024

Since

4.0.0

See also

Functions

Link copied to clipboard
open fun register(@NonNull tasks: Array<Task>)
Register a series of tasks that can be executed at any time within the run methods.
Link copied to clipboard
open fun run(@NonNull task: Task)
open fun run(registeredIndex: Int)
Run a single robot task iteration, where further calls will no-op if the task is finished.
Link copied to clipboard
open fun runRepeatedly(@NonNull task: Task)
open fun runRepeatedly(registeredIndex: Int)
Run a single robot task iteration, auto-resetting the task if it finishes.