From 52cf23bfa5fd3448b243d0220386f0608e38e8be Mon Sep 17 00:00:00 2001 From: Lucas Bubner Date: Tue, 3 Dec 2024 14:30:32 +1030 Subject: [PATCH] Remove poorly implemented Scheduler interop --- .../bunyipslib/BunyipsSubsystem.java | 20 +++---- .../studentrobotics/bunyipslib/Scheduler.kt | 57 ++++++------------- .../bunyipslib/tasks/IdleTask.java | 7 +-- .../bunyipslib/tasks/bases/Task.kt | 2 +- 4 files changed, 27 insertions(+), 59 deletions(-) diff --git a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/BunyipsSubsystem.java b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/BunyipsSubsystem.java index 4e9f1325f..177144bc4 100644 --- a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/BunyipsSubsystem.java +++ b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/BunyipsSubsystem.java @@ -12,7 +12,6 @@ import java.util.Objects; import java.util.function.Consumer; -import au.edu.sa.mbhs.studentrobotics.bunyipslib.external.Mathf; import au.edu.sa.mbhs.studentrobotics.bunyipslib.external.units.Measure; import au.edu.sa.mbhs.studentrobotics.bunyipslib.external.units.Time; import au.edu.sa.mbhs.studentrobotics.bunyipslib.tasks.IdleTask; @@ -278,13 +277,21 @@ public final void setDefaultTask(@NonNull Task defaultTask) { } /** - * Determine if the subsystem is idle, meaning an IdleTask is running. + * Determine if the subsystem is idle, meaning an IdleTask (or no task) is running. */ public final boolean isIdle() { Task current = getCurrentTask(); return current == null || current.toString().equals("IdleTask"); } + /** + * Determine if the subsystem is running the default task. + */ + public final boolean isRunningDefaultTask() { + Task current = getCurrentTask(); + return current != null && current.equals(defaultTask); + } + /** * Set the current task to the given task. * @@ -378,15 +385,6 @@ private void internalUpdate() { task.run(); // Update the state of isFinished() after running the task as it may have changed task.poll(); - if (!task.isFinished()) { // TODO: was mute check - Scheduler.addTaskReport( - toString(), - task == defaultTask, - task.toString(), - Mathf.round(task.getDeltaTime().in(Seconds), 1), - task.getTimeout().in(Seconds) - ); - } } // This should be the only place where periodic() is called for this subsystem Exceptions.runUserMethod(opMode, this::periodic); diff --git a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/Scheduler.kt b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/Scheduler.kt index fd719f19d..de4c22b56 100644 --- a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/Scheduler.kt +++ b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/Scheduler.kt @@ -28,12 +28,8 @@ class Scheduler : BunyipsComponent() { private val subsystems = ArrayList() private val allocatedTasks = ArrayList() - /** - * Create a new scheduler and reset static fields. - */ init { isMuted = false - reports.clear() } /** @@ -114,7 +110,7 @@ class Scheduler : BunyipsComponent() { // Task count will account for tasks on subsystems that are not IdleTasks, and also subsystem tasks val taskCount = (allocatedTasks.size - allocatedTasks.stream() .filter { task -> task.taskToRun.dependency.isPresent }.count() - + subsystems.size - subsystems.stream().filter { obj: BunyipsSubsystem -> obj.isIdle }.count()) + + subsystems.size - subsystems.stream().filter { s -> s.isIdle }.count()) it.telemetry.add("\nManaging % task% (%s, %c) on % subsystem%", taskCount, if (taskCount == 1L) "" else "s", @@ -124,12 +120,23 @@ class Scheduler : BunyipsComponent() { subsystems.size, if (subsystems.size == 1) "" else "s" ) - for (item in reports) { - if (item.contains("IdleTask")) continue - it.telemetry.add(item) + for (subsystem in subsystems) { + val task = subsystem.currentTask + if (task == null || task is IdleTask) continue + var report = Text.format( + "%% | % -> %", + subsystem.javaClass.simpleName, + if (subsystem.isRunningDefaultTask) " (d.)" else "", + task.toString().replace("${subsystem.javaClass.simpleName}:", ""), + task.deltaTime to Seconds round 1 + ) + val timeoutSec = task.timeout to Seconds + report += if (timeoutSec == 0.0) "s" else "/" + timeoutSec + "s" + report += "" + it.telemetry.add(report) } for (task in allocatedTasks) { - if (task.taskToRun.dependency.isPresent // Whether the task is never run from the Scheduler (and task reports will come from the reports array) + if (task.taskToRun.dependency.isPresent // Whether the task is never run from the Scheduler (and task reports were handled above) || !task.taskToRun.isRunning // Whether this task is actually running || task.muted // Whether the task has declared itself as muted ) { @@ -143,7 +150,6 @@ class Scheduler : BunyipsComponent() { ) } } - reports.clear() } for (task in allocatedTasks) { @@ -708,37 +714,6 @@ class Scheduler : BunyipsComponent() { } companion object { - private val reports = ArrayList() private var isMuted = false - - /** - * Used internally by subsystems and tasks to report their running status statically. - * This method is not intended for use by the user. - * - * @param className The class name of the subsystem or context. - * @param isDefaultTask Whether this task is a default task. - * @param taskName The name of the task. - * @param deltaTimeSec The time this task has been running in seconds - * @param timeoutSec The time this task is allowed to run in seconds, 0.0 if indefinite - */ - @JvmStatic - fun addTaskReport( - className: String, - isDefaultTask: Boolean, - taskName: String, - deltaTimeSec: Double, - timeoutSec: Double - ) { - if (isMuted) return - var report = Text.format( - "%% | % -> %", - className, - if (isDefaultTask) " (d.)" else "", - taskName.replace("$className:", ""), - deltaTimeSec - ) - report += if (timeoutSec == 0.0) "s" else "/" + timeoutSec + "s" - reports.add("$report") - } } } diff --git a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/tasks/IdleTask.java b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/tasks/IdleTask.java index f76d112a5..289eeb12d 100644 --- a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/tasks/IdleTask.java +++ b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/tasks/IdleTask.java @@ -5,15 +5,10 @@ /** * Task that runs forever but does nothing. * This is used as a default task on subsystems that don't have a default task. - * IdleTask is ignored from subsystem report telemetry via a string match to "IdleTask". * * @author Lucas Bubner, 2024 * @since 1.0.0-pre */ public class IdleTask extends Task { - @Override - protected void init() { - // Ensure the name remains the same for telemetry ignoring - named("IdleTask"); - } + // no-op } diff --git a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/tasks/bases/Task.kt b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/tasks/bases/Task.kt index cfd4685c2..370738b30 100644 --- a/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/tasks/bases/Task.kt +++ b/src/main/java/au/edu/sa/mbhs/studentrobotics/bunyipslib/tasks/bases/Task.kt @@ -479,6 +479,6 @@ abstract class Task( /** * Default task setter extension for [BunyipsSubsystem] to set the default task of a subsystem. */ - infix fun BunyipsSubsystem.default(defaultTask: Task) = this.setDefaultTask(defaultTask) + infix fun BunyipsSubsystem.default(defaultTask: Task) = setDefaultTask(defaultTask) } } \ No newline at end of file