Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Rules/Strategy (http://www.chiefdelphi.com/forums/forumdisplay.php?f=6)
-   -   Faster loop scheduling (http://www.chiefdelphi.com/forums/showthread.php?t=155007)

microbuns 09-02-2017 14:28

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?

euhlmann 09-02-2017 15:18

Re: Faster loop scheduling
 
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.

AlexanderTheOK 09-02-2017 15:40

Re: Faster loop scheduling
 
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.

Skyehawk 09-02-2017 15:50

Re: Faster loop scheduling
 
Why is this thread in Rules/Strategy? I'm just curious.

kylestach1678 09-02-2017 16:14

Re: Faster loop scheduling
 
Quote:

Originally Posted by microbuns (Post 1642580)
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?

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.

microbuns 09-02-2017 18:02

Re: Faster loop scheduling
 
Quote:

Originally Posted by Skyehawk (Post 1642645)
Why is this thread in Rules/Strategy? I'm just curious.

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!

slibert 09-02-2017 18:25

Re: Faster loop scheduling
 
Quote:

Originally Posted by euhlmann (Post 1642614)
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

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 );
}

rich2202 09-02-2017 21:20

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

kylestach1678 09-02-2017 21:40

Re: Faster loop scheduling
 
Quote:

Originally Posted by rich2202 (Post 1642778)
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.

beijing_strbow 09-02-2017 21:46

Re: Faster loop scheduling
 
Quote:

Originally Posted by rich2202 (Post 1642778)
There is Teleop.Continuous if you want to be continuously called (rather than waiting for the next cycle).

According to this thread that function was removed in 2013.

beijing_strbow 09-02-2017 21:50

Re: Faster loop scheduling
 
Quote:

Originally Posted by euhlmann (Post 1642614)
- 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

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.

euhlmann 10-02-2017 01:15

Re: Faster loop scheduling
 
Quote:

Originally Posted by slibert (Post 1642723)
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 );
}

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 :)


All times are GMT -5. The time now is 20:17.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi