Faster loop scheduling

I’ve learned that some teams use a special (not in WPILib) way of scheduling their loop callbacks. I’ve seen the number 200Hz thrown around occasionally. I believe (at least for C++) that WPILib tries to schedule at ~50Hz. Obviously, tighter control loops would be nicer, but I have 2 questions about this:

  1. How hard is this to implement? I have tried to look around in WPILib, but I haven’t found an obvious way to do so right now.
  2. How much benefit does it bring? Clearly faster control loops are better than slower ones, but really how much does it add? Are there things you can do with a faster control loop that you can’t with the standard ~50Hz WPILib one?

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.

While putting a thread.sleep in a while loop works, I prefer the more institutional method of using a scheduledExecutorService. It’s good for when you’re not certain how long some code will take exactly.

Why is this thread in Rules/Strategy? I’m just curious.

We run all of the loops on our robot on a separate thread running at 200hz.

The big benefit of this doesn’t really come from the “200hz” part, but the “separate thread”. Using IterativeRobot, the update rate is supposed to be “about” 50hz, but it’s really inconsistent. The loop is synchronized to driver station packets, meaning that it’s waiting on the network in between iterations. Things like writing to the console or sending CAN messages can mess up your timing as well. This can give very strange results, especially if you’re relying on consistent timing in your control loops.

That said, there is still a benefit to running loops at 200hz if you’re relying on really tight controls (faster update rates will allow your code to respond more quickly to disturbances).

I believe that WPILib gets around these timing issues by updating their built-in PID controller class in a separate thread. However, if you’re rolling your own PID code or doing some sort of other logic, you’ll want to manually create a new thread to run your control loops so that you can avoid timing issues. Fair warning - multithreading can be really tricky to get right, especially when communicating between threads.

Good question. I thought I was posting in Programming - guess not. I’m not sure if I can move it. If a mod sees this, I’d appreciate it getting moved to the appropriate section!

Just wanted to let you know that the latest navX-MXP/navX-Micro has been updated this year to support a 200Hz update rate…

There’s also a new callback mechanism in the C++/Java Libraries that allow you to run code (as long as it’s very efficient) off of the navX IO thread:

public interface ITimestampedDataSubscriber {
public void timestampedDataReceived( long system_timestamp, long sensor_timestamp, AHRSUpdateBase sensor_data, Object context );
}

There is Teleop.Continuous if you want to be continuously called (rather than waiting for the next cycle).

As far as I can tell, support for TeleopContinuous has been removed from WPILib. Implementing it will no longer actually override any function, and it will never be called.

According to this thread that function was removed in 2013.

What kind of precision do you guys get with this? We wanted to try something similar this year, but gave it because it was taking longer than expected to get the robot working mechanically.

Ah, that’s useful to know. I looked through the source a while ago and didn’t see such a callback mechanism before. Glad to see you have one now :slight_smile: