Task

abstract class Task(var timeout: Measure<Time>) : BunyipsComponent, Runnable, Action

A task, or command is an action that can be performed by a robot. This has been designed to reflect closely the command-based programming style used in FRC, while still being reflective of the past nature of how the Task system was implemented in BunyipsLib.

The task system in BunyipsLib has been constructed from the ground-up with a more lightweight ecosystem where Tasks are at their core stripped into running some code for some time, somewhere. The original behaviour of tasks running outright used to be the legacy roots of how Tasks worked, and since has adopted some command-based structures that work alongside the lightweight premise of a Task being "a Runnable with a timeout" with a run implementation left to the user.

Some different implementations on how Tasks are interpreted are demonstrated in these classes:

  • Scheduler

  • BunyipsOpMode

  • AutonomousBunyipsOpMode

  • BunyipsSubsystem

  • Tasks

Task extends BunyipsComponent to allow for simpler integration with accessing the OpMode, and was a legacy feature that was kept for the sake of simplicity, more pedantic exception handling, and ease of use.

As of 6.0.0, the Task system now implements the Action interface for seamless compatibility with RoadRunner v1.0.0. The ActionTask may be used to convert a pure Action into a task, however, writing a task directly using a Task is encouraged for more control and flexibility, due to there being no downsides to doing so and the exposure of the same accessors.

Author

Lucas Bubner, 2024

Since

1.0.0-pre

Inheritors

Constructors

Link copied to clipboard
constructor()

A task that does not have an integrated timeout, and will rely on manual intervention and isTaskFinished.

constructor(timeout: Measure<Time>)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Time in seconds since the task was started.

Link copied to clipboard
protected lateinit var fieldOverlay: Canvas

Convenience field to get a reference to FtcDashboard's field overlay for drawing on the field. Available as soon as init() has been called for this task.

Link copied to clipboard
Link copied to clipboard
protected lateinit var p: TelemetryPacket

Convenience field to get a reference to a TelemetryPacket for sending telemetry to the dashboard. Available as soon as init() has been called for this task.

Link copied to clipboard

Whether the task is finished or not via timeout or custom condition. Will be true regardless of the finisher being fired or not, as some tasks will handle this via finishNow().

Link copied to clipboard

Maximum timeout of the task. If set to 0 magnitude (or a timeout-less constructor) this will serve as an indefinite task, and will only finish when isTaskFinished() returns true, or this task is manually interrupted via finish.

Inherited properties

Link copied to clipboard
protected val opMode: BunyipsOpMode?

Get a reference to the currently running BunyipsOpMode.

Functions

Link copied to clipboard
infix fun after(waitTime: Measure<Time>): SequentialTaskGroup
fun after(waitDuration: Double, unit: Time): SequentialTaskGroup

Composes a WaitTask to run before this task.

infix fun after(otherTask: Task): SequentialTaskGroup

Compose this task into a SequentialTaskGroup with the supplied task to run before this one.

infix fun after(runnable: Runnable): SequentialTaskGroup

Implicitly run a SequentialTaskGroup with this supplied Runnable, queued to run before this task starts.

fun after(vararg otherTasks: Task): SequentialTaskGroup

Compose this task into a SequentialTaskGroup with the supplied tasks to run before this one.

Link copied to clipboard
infix fun during(otherTask: Task): DeadlineTaskGroup

Compose this task into a DeadlineTaskGroup with the supplied task to run alongside this one until this task is done.

fun during(vararg otherTasks: Task): DeadlineTaskGroup

Compose this task into a DeadlineTaskGroup with the supplied tasks to run these extra tasks until this task is done.

Link copied to clipboard
fun finish()

Tell a task to finish on the next iteration.

Link copied to clipboard
fun finishNow()

Force a task to finish immediately, and fire the onFinish() method without waiting for the next polling loop. This method is useful when your task needs to die and needs to finish up immediately. If your finisher has already been fired, this method will do nothing but ensure that the task is marked as finished.

Link copied to clipboard
infix fun forAtLeast(waitTime: Measure<Time>): ParallelTaskGroup
fun forAtLeast(waitDuration: Double, unit: Time): ParallelTaskGroup

Composes a ParallelTaskGroup with a WaitTask to run before this task. This will ensure the task runs for at least the specified time, and no-ops until the duration if it finishes early.

Link copied to clipboard

Get the subsystem reference that this task has elected a dependency on. Will return an Optional where if it is not present, this task is not dependent on any subsystem.

Link copied to clipboard

Return whether this task has elected a dependency on a subsystem or not.

Link copied to clipboard
protected open fun init()

Define code to run once, when the task is started. Override to implement.

Link copied to clipboard

Query (but not update) the finished state of the task. This will return true if the task is finished and the finisher has been fired.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
protected abstract fun isTaskFinished(): Boolean

Return a boolean to this method to add custom criteria if a task should be considered finished.

Link copied to clipboard

Mute task reports from the Scheduler.

Link copied to clipboard
infix fun named(name: String): Task

Set the name of this task to be displayed in the OpMode.

Link copied to clipboard
infix fun on(subsystem: BunyipsSubsystem): Task

Set the subsystem you want to elect this task to run on, notifying the runner that this task should run there. This task is scheduled with default override behaviour.

Link copied to clipboard
protected open fun onFinish()

Finalising function to run once the task is finished. This will always run regardless of whether the task was ended because of an interrupt or the task naturally finishing. Override to add your own callback.

Link copied to clipboard
protected open fun onInterrupt()

Finalising function that will be called after onFinish in the event this task is finished via a call to finish or finishNow. Override to add your own callback.

Link copied to clipboard
protected open fun onReset()

Called when the task is resetting now. Override this method to add custom reset behaviour, such as resetting any internal state variables such as iterators or lists.

Link copied to clipboard
open fun onSubsystem(subsystem: BunyipsSubsystem, override: Boolean = false): Task

Set the subsystem you want to elect this task to run on, notifying the runner that this task should run there.

Link copied to clipboard
protected abstract fun periodic()

To run as an active loop during this task's duration.

Link copied to clipboard

Update and query the state of the task if it is finished. This will return true if the task is finished and the finisher has been fired.

Link copied to clipboard
override fun preview(fieldOverlay: Canvas)

RoadRunner Action implementation to preview the action on the field overlay. This method no-ops as previews are done via DualTelemetry and the Drawing util.

Link copied to clipboard
infix fun race(otherTask: Task): RaceTaskGroup

Compose this task into a RaceTaskGroup with the supplied task to run alongside this one until one finishes.

fun race(vararg otherTasks: Task): RaceTaskGroup

Compose this task into a RaceTaskGroup with the supplied tasks to run all of these tasks until one finishes.

Link copied to clipboard

Wrap this task in a RepeatTask where finish conditions are reset immediately.

Link copied to clipboard
fun reset()

Reset a task to an uninitialised and unfinished state. Will no-op if the task is already fully reset.

Link copied to clipboard
override fun run()

Should be called by your polling loop to run the task and manage all state properly.

override fun run(p: TelemetryPacket): Boolean

RoadRunner Action implementation to run this Task.

Link copied to clipboard
infix fun then(otherTask: Task): SequentialTaskGroup

Compose this task into a SequentialTaskGroup with the supplied task to follow after this one.

infix fun then(runnable: Runnable): SequentialTaskGroup

Implicitly run a SequentialTaskGroup with this supplied Runnable, queued to run when this task finishes.

fun then(vararg otherTasks: Task): SequentialTaskGroup

Compose this task into a SequentialTaskGroup with the supplied tasks to follow after this one.

Link copied to clipboard
infix fun timeout(timeout: Measure<Time>): Task

Set the timeout of this task dynamically and return the task.

Link copied to clipboard
override fun toString(): String

Get the name of this task. By default, it will be the class simple name, but you can call withName to set a custom name.

Link copied to clipboard

Get a verbose string representation of this task, including all of its properties.

Link copied to clipboard
infix fun until(condition: BooleanSupplier): RaceTaskGroup

Compose this task into a RaceTaskGroup with a WaitUntilTask based on this condition.

Link copied to clipboard
infix fun with(otherTask: Task): ParallelTaskGroup

Compose this task into a ParallelTaskGroup with the supplied task to run alongside this one.

fun with(vararg otherTasks: Task): ParallelTaskGroup

Compose this task into a ParallelTaskGroup with the supplied tasks to run all of these tasks at once.

Link copied to clipboard
open fun withName(name: String?): Task

Set the name of this task to be displayed in the OpMode. You may override this method if required to enforce a naming convention/prefix.

Link copied to clipboard
fun withTimeout(timeout: Measure<Time>): Task

Set the timeout of this task dynamically and return the task.

Inherited functions

Link copied to clipboard
protected fun opMode(@NonNull ifRunning: Consumer<BunyipsOpMode>)

Null check consumer for the opMode field which will no-op the given consumer if an active BunyipsOpMode is not present (i.e. the opMode field is null). This method is the same to the BunyipsOpMode.ifRunning method, and is supplied here for convenience.

Link copied to clipboard
protected fun require(@Nullable nullableOpMode: BunyipsOpMode?): BunyipsOpMode

Null assertion for the opMode field which throws a NullPointerException if an active BunyipsOpMode is not present (i.e. the supplied field is null). This method replicates Objects.requireNonNull but has a built-in message to alert the user of a non-active OpMode.