Class RealtimeTask
The work a RealtimeThread runs every period.
class MotorLoop extends RealtimeTask {
private PwmChannel pwm;
private final byte[] reading = new byte[2];
private I2cDevice encoder;
public void setup() {
// runs once on the real-time thread; may allocate
...
}
public void cycle() {
// every period; must not allocate
encoder.read(reading, 0, 2);
pwm.setDutyCycle(controlLaw(reading));
}
}
cycle() and periodsMissed(int) must not allocate: no new, no boxing, no
string concatenation, no iterator from a for-each over a collection. Keep buffers
and objects in fields, made in the constructor or in setup(). The build checks
every method they reach through calls it can resolve and fails with the call chain;
at run time an allocation the build could not see ends the steady state with a
RealtimeAllocationError, unless the thread has an allocation budget.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidcycle()One period's work.voidCalled on the real-time thread when the task ends abnormally:setup()orcycle()threw, or the thread could not be made real-time as its parameters require.voidperiodsMissed(int count) Called beforecycle()when releases were missed because the previous cycle ran past them.voidsetup()Runs once on the real-time thread, after its priority is applied and before the first period.voidstopped()Called on the real-time thread after the last cycle, whenRealtimeThread.stop()was called or afterfailed(Throwable).
-
Constructor Details
-
RealtimeTask
public RealtimeTask()
-
-
Method Details
-
setup
public void setup()Runs once on the real-time thread, after its priority is applied and before the first period. It may allocate: create here what the cycles will use, and touch every class they use so its static initializer has run. -
cycle
public abstract void cycle()One period's work. Runs on the real-time thread at every release and must not allocate. -
periodsMissed
public void periodsMissed(int count) Called before
cycle()when releases were missed because the previous cycle ran past them. Missed releases are skipped, not run late: the cycle that follows is the one due now. Must not allocate. The default does nothing.Parameters
count: how many releases were skipped
-
failed
Called on the real-time thread when the task ends abnormally:
setup()orcycle()threw, or the thread could not be made real-time as its parameters require. The steady state is over when this runs, so it may allocate. The thread stops afterwards. The default prints the stack trace.Parameters
failure: what ended the task
-
stopped
public void stopped()Called on the real-time thread after the last cycle, whenRealtimeThread.stop()was called or afterfailed(Throwable). May allocate. The default does nothing.
-