Custom Mechanism Actions
Write actions that cooperate with drive paths.
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) {
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);
if (!running) lift.setPower(0.0);
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.
Check your understanding before moving on.
Which result best demonstrates completion of “Custom Mechanism Actions”?
Why record the test setup, prediction, and observed result?
0 of 2 answered
Mark this lesson complete — “MeepMeep Preview” is up next.