Code:
int rate = 200; // Hz
private void mySuperFastLoop() {
while (true) {
// do some fast stuff
try {
Thread.sleep(1000 / rate);
} catch (InterruptedException ex) {}
}
}
protected void robotInit() {
Thread superFastLoopThread = new Thread(this::mySuperFastLoop);
superFastLoopThread.setDaemon(true);
superFastLoopThread.setName("Custom Super Fast Loop");
superFastLoopThread.start();
}
What we use fast off-thread loops for:
- Dead reckoning. Integrating gyro angle with encoder distance for approximate field position. We run it at 60 Hz, the update rate of the NavX, for best accuracy
- 775pro monitoring. If close to stall current is detected, it immediately shuts off the motors. This is an important safety control so we don't want it potentially hung up by main thread stuff.