|
|
|
| My love is autonomous when you enter the room. |
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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:
|
|
#2
|
||||
|
||||
|
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();
}
- 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. Last edited by euhlmann : 09-02-2017 at 15:22. |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
||||
|
||||
|
Re: Faster loop scheduling
Why is this thread in Rules/Strategy? I'm just curious.
|
|
#5
|
||||
|
||||
|
Re: Faster loop scheduling
Quote:
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. |
|
#6
|
||||
|
||||
|
Re: Faster loop scheduling
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!
|
|
#7
|
|||
|
|||
|
Re: Faster loop scheduling
Quote:
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. |
|
#8
|
|||
|
|||
|
Re: Faster loop scheduling
There is Teleop.Continuous if you want to be continuously called (rather than waiting for the next cycle).
|
|
#9
|
||||
|
||||
|
Re: Faster loop scheduling
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.
|
|
#10
|
|||
|
|||
|
Re: Faster loop scheduling
Quote:
|
|
#11
|
|||
|
|||
|
Re: Faster loop scheduling
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.
|
|
#12
|
||||
|
||||
|
Re: Faster loop scheduling
Quote:
![]() |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|