Skip to content

Commit

Permalink
Remove poorly implemented Scheduler interop
Browse files Browse the repository at this point in the history
  • Loading branch information
bubner committed Dec 3, 2024
1 parent cfb19f1 commit 52cf23b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ class Scheduler : BunyipsComponent() {
private val subsystems = ArrayList<BunyipsSubsystem>()
private val allocatedTasks = ArrayList<ScheduledTask>()

/**
* Create a new scheduler and reset static fields.
*/
init {
isMuted = false
reports.clear()
}

/**
Expand Down Expand Up @@ -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",
Expand All @@ -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(
"<small><b>%</b>% <font color='gray'>|</font> <b>%</b> -> %",
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 += "</small>"
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
) {
Expand All @@ -143,7 +150,6 @@ class Scheduler : BunyipsComponent() {
)
}
}
reports.clear()
}

for (task in allocatedTasks) {
Expand Down Expand Up @@ -708,37 +714,6 @@ class Scheduler : BunyipsComponent() {
}

companion object {
private val reports = ArrayList<String>()
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(
"<small><b>%</b>% <font color='gray'>|</font> <b>%</b> -> %",
className,
if (isDefaultTask) " (d.)" else "",
taskName.replace("$className:", ""),
deltaTimeSec
)
report += if (timeoutSec == 0.0) "s" else "/" + timeoutSec + "s"
reports.add("$report</small>")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 52cf23b

Please sign in to comment.