My love spiders out in a web of emotions when I think of you.
Home
Go Back   Chief Delphi > Competition > Rules/Strategy
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 09-02-2017, 14:28
microbuns's Avatar
microbuns microbuns is offline
Registered User
AKA: Sam Maier
FRC #4917 (Sir Lancerbot)
Team Role: Mentor
 
Join Date: Jan 2015
Rookie Year: 2014
Location: Elmira, ON
Posts: 87
microbuns is an unknown quantity at this point
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?
Reply With Quote
  #2   Spotlight this post!  
Unread 09-02-2017, 15:18
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 417
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
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.
__________________
Creator of SmartDashboard.js, an extensible nodejs/webkit replacement for SmartDashboard


https://ligerbots.org

Last edited by euhlmann : 09-02-2017 at 15:22.
Reply With Quote
  #3   Spotlight this post!  
Unread 09-02-2017, 15:40
AlexanderTheOK AlexanderTheOK is offline
Guy
no team
 
Join Date: Jan 2014
Rookie Year: 2012
Location: Los Angeles
Posts: 148
AlexanderTheOK is just really niceAlexanderTheOK is just really niceAlexanderTheOK is just really niceAlexanderTheOK is just really nice
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.
Reply With Quote
  #4   Spotlight this post!  
Unread 09-02-2017, 15:50
Skyehawk's Avatar
Skyehawk Skyehawk is offline
Nuts N' Bolts
AKA: Skye Leake
FRC #0876 (Thunder Robotics)
Team Role: Mentor
 
Join Date: Nov 2012
Rookie Year: 2011
Location: Northwood, ND
Posts: 276
Skyehawk is a name known to allSkyehawk is a name known to allSkyehawk is a name known to allSkyehawk is a name known to allSkyehawk is a name known to allSkyehawk is a name known to all
Re: Faster loop scheduling

Why is this thread in Rules/Strategy? I'm just curious.
__________________
My time in FIRST has made me a better person, frankly I don't know where I'd be without it.



2011 Lake Superior Regional Champs (Thanks 2512 & 3747)
2015 Central Illinois Regional Engineering Inspiration Award recipients
2016 Central Illinois Regional Champs (Thanks 2481 & 2220)
Reply With Quote
  #5   Spotlight this post!  
Unread 09-02-2017, 16:14
kylestach1678's Avatar
kylestach1678 kylestach1678 is offline
Registered User
AKA: Kyle Stachowicz
FRC #1678 (Citrus Circuits)
Team Role: Programmer
 
Join Date: Dec 2014
Rookie Year: 2015
Location: Davis, CA
Posts: 25
kylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to behold
Re: Faster loop scheduling

Quote:
Originally Posted by microbuns View Post
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.
__________________

Reply With Quote
  #6   Spotlight this post!  
Unread 09-02-2017, 18:02
microbuns's Avatar
microbuns microbuns is offline
Registered User
AKA: Sam Maier
FRC #4917 (Sir Lancerbot)
Team Role: Mentor
 
Join Date: Jan 2015
Rookie Year: 2014
Location: Elmira, ON
Posts: 87
microbuns is an unknown quantity at this point
Re: Faster loop scheduling

Quote:
Originally Posted by Skyehawk View Post
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!
Reply With Quote
  #7   Spotlight this post!  
Unread 09-02-2017, 18:25
slibert slibert is offline
Software Mentor
AKA: Scott Libert
FRC #2465 (Kauaibots)
Team Role: Mentor
 
Join Date: Oct 2011
Rookie Year: 2005
Location: Kauai, Hawaii
Posts: 374
slibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud ofslibert has much to be proud of
Re: Faster loop scheduling

Quote:
Originally Posted by euhlmann View Post
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 );
}

Last edited by slibert : 09-02-2017 at 18:30.
Reply With Quote
  #8   Spotlight this post!  
Unread 09-02-2017, 21:20
rich2202 rich2202 is offline
Registered User
FRC #2202 (BEAST Robotics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Wisconsin
Posts: 1,301
rich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond reputerich2202 has a reputation beyond repute
Re: Faster loop scheduling

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

Reply With Quote
  #9   Spotlight this post!  
Unread 09-02-2017, 21:40
kylestach1678's Avatar
kylestach1678 kylestach1678 is offline
Registered User
AKA: Kyle Stachowicz
FRC #1678 (Citrus Circuits)
Team Role: Programmer
 
Join Date: Dec 2014
Rookie Year: 2015
Location: Davis, CA
Posts: 25
kylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to beholdkylestach1678 is a splendid one to behold
Re: Faster loop scheduling

Quote:
Originally Posted by rich2202 View Post
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.
__________________

Reply With Quote
  #10   Spotlight this post!  
Unread 09-02-2017, 21:46
beijing_strbow beijing_strbow is offline
Registered User
FRC #5968 (Cyborg Indians)
Team Role: Programmer
 
Join Date: Aug 2016
Rookie Year: 2016
Location: Kansas
Posts: 56
beijing_strbow is an unknown quantity at this point
Re: Faster loop scheduling

Quote:
Originally Posted by rich2202 View Post
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.
Reply With Quote
  #11   Spotlight this post!  
Unread 09-02-2017, 21:50
beijing_strbow beijing_strbow is offline
Registered User
FRC #5968 (Cyborg Indians)
Team Role: Programmer
 
Join Date: Aug 2016
Rookie Year: 2016
Location: Kansas
Posts: 56
beijing_strbow is an unknown quantity at this point
Re: Faster loop scheduling

Quote:
Originally Posted by euhlmann View Post
- 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.
Reply With Quote
  #12   Spotlight this post!  
Unread 10-02-2017, 01:15
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 417
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
Re: Faster loop scheduling

Quote:
Originally Posted by slibert View Post
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
__________________
Creator of SmartDashboard.js, an extensible nodejs/webkit replacement for SmartDashboard


https://ligerbots.org
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


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

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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