Actions and Sequencing
Prepare autonomous code for Road Runner action composition.
Prepare autonomous code for Road Runner action composition.
In this lesson, you will:
Actions are reusable autonomous behaviors that continue until they report completion. This model aligns with Road Runner 1.0 and command-based robot thinking.
An action is a cooperative task. It must do a small amount of work each time run is called, report telemetry, and finish when its condition is satisfied.
Create one instant servo action and one encoder-wait action. Then compose them sequentially. Keep run calls short; do not hide sleeps inside custom actions.
MechanismAction.javaJava
public Action liftTo(int target) {
return new Action() {
private boolean initialized;
private final ElapsedTime timer = new ElapsedTime();
@Override
public boolean run(@NonNull TelemetryPacket packet) {
boolean stopRequested = !opModeIsActive();
if (stopRequested) {
lift.setPower(0.0);
packet.put("liftStopRequested", true);
packet.put("liftExit", "stop");
return false;
}
if (!initialized) {
lift.setTargetPosition(target);
lift.setMode(DcMotor.RunMode.RUN_TO_POSITION);
lift.setPower(0.4);
timer.reset();
initialized = true;
}
int error = target - lift.getCurrentPosition();
boolean timedOut = timer.seconds() >= 2.0;
boolean running = Math.abs(error) > 25 && !timedOut;
packet.put("liftError", error);
packet.put("liftTimedOut", timedOut);
packet.put("liftStopRequested", stopRequested);
if (!running) {
lift.setPower(0.0);
packet.put("liftExit", timedOut ? "timeout" : "target");
}
return running;
}
};
}
try {
Actions.runBlocking(liftTo(targetTicks));
} finally {
lift.setPower(0.0); // Cover OpMode interruption as well as normal completion.
}Returning false too soon starts the next action early; returning true forever stalls auto. Long blocking calls starve parallel actions.
Write an action that moves a lift until error is small, then use it in a sequence with a claw action.
Check your understanding before moving on.
0 of 2 answered