Custom Mechanism Actions
Write actions that cooperate with drive paths.
Road Runner 1.0 Actions and AutonomousRoad Runner
Write actions that cooperate with drive paths.
In this lesson, you will:
Custom actions let mechanisms participate in autonomous without threads or sleeps. They run in small repeated steps and finish by returning false.
A good mechanism action is polite: it does a little work, reports useful packet telemetry, and gives other actions time to run.
Write an action that initializes once, commands a target, checks sensor or encoder error, and returns false when finished.
CustomAction.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); // Also stop the mechanism when the OpMode is interrupted.
}Actions that sleep or loop internally starve parallel actions. Actions with bad finish conditions stall or skip steps.
Create a lift action and run it in parallel with a short drive action.
Check your understanding before moving on.
0 of 2 answered