|
| 1 | +package com.acmerobotics.roadrunner.actions |
| 2 | + |
| 3 | +import com.acmerobotics.dashboard.canvas.Canvas |
| 4 | +import com.acmerobotics.dashboard.telemetry.TelemetryPacket |
| 5 | +import kotlin.time.ComparableTimeMark |
| 6 | +import kotlin.time.Duration |
| 7 | +import kotlin.time.Duration.Companion.nanoseconds |
| 8 | +import kotlin.time.Duration.Companion.seconds |
| 9 | +import kotlin.time.DurationUnit |
| 10 | +import kotlin.time.TimeSource |
| 11 | +import kotlin.time.TimeSource.Monotonic.markNow |
| 12 | +import kotlin.time.toJavaDuration |
| 13 | +import kotlin.time.toKotlinDuration |
| 14 | + |
| 15 | +/** |
| 16 | + * Concurrent task for cooperative multitasking with some FTC dashboard hooks. Actions may have a mutable state. |
| 17 | + */ |
| 18 | +@JvmDefaultWithoutCompatibility |
| 19 | +fun interface Action { |
| 20 | + /** |
| 21 | + * Runs a single uninterruptible block. Returns true if the action should run again and false if it has completed. |
| 22 | + * A telemetry packet [p] is provided to record any information on the action's progress. |
| 23 | + */ |
| 24 | + fun run(p: TelemetryPacket): Boolean |
| 25 | + |
| 26 | + /** |
| 27 | + * Draws a preview of the action on canvas [fieldOverlay]. |
| 28 | + */ |
| 29 | + fun preview(fieldOverlay: Canvas) {} |
| 30 | + |
| 31 | + /** |
| 32 | + * The action's requirements (optional). |
| 33 | + * It is up to the action queue to resolve requirements. |
| 34 | + */ |
| 35 | + val requirements: Set<Any> get() = emptySet() |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns a new action that executes this action followed by [a]. |
| 39 | + */ |
| 40 | + fun then(a: Action) = SequentialAction(this, a) |
| 41 | + fun then(f: InstantFunction) = SequentialAction(this, InstantAction(f)) |
| 42 | + fun then(a: () -> Action) = SequentialAction(this, a()) |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns a new action that executes this action in parallel with [a]. |
| 46 | + */ |
| 47 | + fun with(a: Action) = ParallelAction(this, a) |
| 48 | + fun with(f: InstantFunction) = ParallelAction(this, InstantAction(f)) |
| 49 | + fun with(a: () -> Action) = ParallelAction(this, a()) |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns a new action that executes this action in parallel with [a]. |
| 53 | + */ |
| 54 | + fun race(a: Action) = RaceAction(this, a) |
| 55 | + fun race(f: InstantFunction) = RaceAction(this, InstantAction(f)) |
| 56 | + fun race(a: () -> Action) = RaceAction(this, a()) |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns a new action that waits [dt] seconds before executing this action. |
| 60 | + */ |
| 61 | + fun delay(dt: Double) = SleepAction(dt).then(this) |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns an interruptible copy of this action, with [onInterruption] occurring on interrupt. |
| 65 | + */ |
| 66 | + fun interruptible(onInterruption: Action) = object : Interruptible { |
| 67 | + override fun onInterrupt(): Action = onInterruption |
| 68 | + |
| 69 | + override fun run(p: TelemetryPacket): Boolean = this@Action.run(p) |
| 70 | + |
| 71 | + override fun preview(fieldOverlay: Canvas) = this@Action.preview(fieldOverlay) |
| 72 | + } |
| 73 | + fun interruptible(onInterruption: InstantFunction) = interruptible(InstantAction(onInterruption)) |
| 74 | + fun interruptible(onInterruption: () -> Action) = interruptible(onInterruption()) |
| 75 | +} |
| 76 | + |
| 77 | +open class ActionEx @JvmOverloads constructor( |
| 78 | + private val initBlock: (TelemetryPacket) -> Unit = { }, |
| 79 | + private val loopBlock: (TelemetryPacket) -> Boolean = { false }, |
| 80 | + private val endBlock: (TelemetryPacket) -> Unit = { } |
| 81 | +) : Action { |
| 82 | + |
| 83 | + open fun init(packet: TelemetryPacket) = initBlock(packet) |
| 84 | + open fun loop(packet: TelemetryPacket) = loopBlock(packet) |
| 85 | + open fun end(packet: TelemetryPacket) = endBlock(packet) |
| 86 | + |
| 87 | + private val sequential = SequentialAction( |
| 88 | + { init(it).let { false } }, |
| 89 | + { loop(it) }, |
| 90 | + { end(it).let { false } } |
| 91 | + ) |
| 92 | + |
| 93 | + final override fun run(packet: TelemetryPacket) = sequential.run(packet) |
| 94 | + |
| 95 | + fun withInit(initBlock: (TelemetryPacket) -> Unit) = ActionEx(initBlock, loopBlock, endBlock) |
| 96 | + fun withLoop(loopBlock: (TelemetryPacket) -> Boolean) = ActionEx(initBlock, loopBlock, endBlock) |
| 97 | + fun withEnd(endBlock: (TelemetryPacket) -> Unit) = ActionEx(initBlock, loopBlock, endBlock) |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Utility object for action-related functionality. |
| 102 | + */ |
| 103 | +object Actions { |
| 104 | + /** |
| 105 | + * Returns the current time in seconds. |
| 106 | + */ |
| 107 | + @JvmStatic fun now() = System.nanoTime().nanoseconds.toDouble(DurationUnit.SECONDS) |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Primitive sleep action that stalls for [dt]. |
| 112 | + */ |
| 113 | +data class SleepAction(val dt: Duration) : ActionEx() { |
| 114 | + constructor(dt: java.time.Duration) : this(dt.toKotlinDuration()) |
| 115 | + constructor(dt: Double) : this(dt.seconds) |
| 116 | + |
| 117 | + private lateinit var start: ComparableTimeMark |
| 118 | + |
| 119 | + override fun init(packet: TelemetryPacket) { |
| 120 | + start = markNow() |
| 121 | + } |
| 122 | + |
| 123 | + override fun loop(packet: TelemetryPacket) = (start.elapsedNow() - dt).isPositive() |
| 124 | +} |
| 125 | +fun interface InstantFunction { |
| 126 | + fun run() |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Instant action that executes [f] immediately. |
| 131 | + */ |
| 132 | +class InstantAction(val f: InstantFunction) : Action { |
| 133 | + override fun run(p: TelemetryPacket): Boolean { |
| 134 | + f.run() |
| 135 | + return false |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Null action that does nothing. |
| 141 | + */ |
| 142 | +class NullAction : Action { |
| 143 | + override fun run(p: TelemetryPacket) = false |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * An action that can be interrupted, providing a specific action to execute upon interruption. |
| 148 | + */ |
| 149 | +interface Interruptible : Action { |
| 150 | + |
| 151 | + /** |
| 152 | + * Returns the action to execute upon interruption. |
| 153 | + */ |
| 154 | + fun onInterrupt(): Action |
| 155 | +} |
0 commit comments