diff --git a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/TaskOpMode.java b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/TaskOpMode.java index 0aed31b53f31..b154f72bb809 100644 --- a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/TaskOpMode.java +++ b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/TaskOpMode.java @@ -59,6 +59,16 @@ public int registerTask(Task task) { return registerTask(task, -1); } + /** + * Register a task in the task map. All the tasks in the map will be iterated in + * the main loop when it's the right time to do. + * + * @param task the task that need registering. + * @param linkedTaskId the taskId of the linked task. The task being registered will + * be run after the linked task is finished. If nothing provided, + * the registered task will start to run immediately. + * @return the taskId of the registered task. + */ public int registerTask(Task task, int linkedTaskId) { task.init(); int taskId = findMinFreeTaskId(); @@ -67,6 +77,11 @@ public int registerTask(Task task, int linkedTaskId) { return taskId; } + /** + * Cancel the task and all the linked-after tasks if necessary. + * + * @param taskId the taskId of the task needs canceling. + */ public void cancelTask(int taskId) { Task task = tasks.get(taskId); if (task == null) return; diff --git a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/TimedServo.java b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/TimedServo.java index 1449a6e5260c..f5f6a641ecc7 100644 --- a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/TimedServo.java +++ b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/hardware/TimedServo.java @@ -89,6 +89,15 @@ public void scaleRange(double min, double max) { servo.scaleRange(min, max); } + /** + * Sets the current position of the servo in a fixed time, expressed as a fraction of + * its available range. If PWM power is enabled for the servo, the servo will attempt + * to move to the indicated position. + * + * @param position the position to which the servo should move, a value in the range [0.0, 1.0] + * @param timeMs the time that the servo will take to move. + * @return a Task object + */ public Task setPosition(double position, long timeMs) { final double delta = position - servo.getPosition(); final long iterationCount = timeMs / Task.TICK_MS; diff --git a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/task/Task.java b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/task/Task.java index efe228aa534e..2ced8b70b134 100644 --- a/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/task/Task.java +++ b/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/task/Task.java @@ -3,16 +3,33 @@ public interface Task { int TICK_MS = 50; + /** + * This method is called in the main loop of OpMode + * It should contain the code of EACH TICK, instead of a loop in it! + */ + void iterate(); + + /** + * To judge whether the task needs to continue. + * @return Return true when it need to continue, otherwise false. + */ + boolean hasNext(); + + /** + * This method is called when the task is registered in the TaskOpMode. + */ default void init() { } - void iterate(); - + /** + * The method is called when the task is finished. + */ default void finish() { } - boolean hasNext(); - + /** + * The method is called when the task is canceled. + */ default void cancel() { } }