Go to Post The most important part of successfully solving the problem is to understand what the problem is. Elegance in engineering comes from "optimizing" your solution, not solving problems that don't really exist. Fulfill your requirements, meet your goals... - JVN [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
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 19-01-2017, 11:53
Oblarg Oblarg is offline
Registered User
AKA: Eli Barnett
FRC #0449 (The Blair Robot Project)
Team Role: Mentor
 
Join Date: Mar 2009
Rookie Year: 2008
Location: Philadelphia, PA
Posts: 1,113
Oblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond repute
WPILibJ PIDController bufTotal Floating Point Error Accumulation

Last year, I posted about a bug due to WPILibJ's use of a running buffer sum in PIDController.onTarget().

Said bug has, indeed, been fixed - but this year, we've found another problem with this particular implementation: it is prone to accumulating floating-point errors, and this can cause a PID-based command to never exit.

For those who haven't looked at the WPILib code, onTarget() is a function that is supposed to return TRUE when the average error over the past n loop iterations is below a set tolerance. This is implemented as follows:

The PIDController contains a length n buffer of doubles, and a double "bufTotal." Each iteration through the loop, the current error value is placed in the front of the buffer, and its value is added to bufTotal. If the buffer is full, the last value is kicked out of the end of the buffer, and its value is subtracted from bufTotal. Thus, bufTotal can be used to calculate the desired average error.

The idea behind this, as far as I can tell, is to reduce the number of calculations needing to be done at each loop iteration from n (the length of the buffer) to 2. This is a rather marginal benefit.

A drawback, however, is that due to the nature of floating-point arithmetic, the value added to bufTotal when the error hits the front of the buffer is not necessarily the same as the value subtracted off again when it comes out the other end.

Our turn-to-angle loop uses a sizeable buffer length to ensure that we do not accidentally exit the loop if we "swing past" the setpoint in an overshoot. It also uses a "minimum output" to account for the minor nonlinearity introduced by stiction - but this also requires that, to avoid "dancing" or "jittering" around the setpoint, we have a small deadband (slightly smaller than our set PIDController tolerance).

A resulting behavior of this confluence of factors is that our loop can turn our robot to within the deadband (which is also within tolerance), but due to accumulated roundoff errors the calculated average error is greater than tolerance, and onTarget() returns FALSE. The robot will thus stay in this position forever, not adjusting its angle but not exiting the loop. We have since implemented a timeout to deal with this contingency, but that is less-than-ideal.

I think it would be best to simply sum the buffer every loop iteration. The performance gains from keeping a running total are not worth the problems introduced.
__________________
"Mmmmm, chain grease and aluminum shavings..."
"The breakfast of champions!"

Member, FRC Team 449: 2007-2010
Drive Mechanics Lead, FRC Team 449: 2009-2010
Alumnus/Technical Mentor, FRC Team 449: 2010-Present
Lead Technical Mentor, FRC Team 4464: 2012-2015
Technical Mentor, FRC Team 5830: 2015-2016
Reply With Quote
  #2   Spotlight this post!  
Unread 19-01-2017, 13:13
otherguy's Avatar
otherguy otherguy is offline
sparkE
AKA: James
FRC #2168 (The Aluminum Falcons)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: CT
Posts: 434
otherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to behold
It is probably worth creating an issue in the github repo for wpilib so this gets visibility by the dev.s and can be tracked to completion.

https://github.com/wpilibsuite/allwpilib

Sent from my 6045I using Tapatalk
__________________
http://team2168.org
Reply With Quote
  #3   Spotlight this post!  
Unread 19-01-2017, 20:49
Oblarg Oblarg is offline
Registered User
AKA: Eli Barnett
FRC #0449 (The Blair Robot Project)
Team Role: Mentor
 
Join Date: Mar 2009
Rookie Year: 2008
Location: Philadelphia, PA
Posts: 1,113
Oblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond repute
Re: WPILibJ PIDController bufTotal Floating Point Error Accumulation

Quote:
Originally Posted by otherguy View Post
It is probably worth creating an issue in the github repo for wpilib so this gets visibility by the dev.s and can be tracked to completion.

https://github.com/wpilibsuite/allwpilib

Sent from my 6045I using Tapatalk
Done.
__________________
"Mmmmm, chain grease and aluminum shavings..."
"The breakfast of champions!"

Member, FRC Team 449: 2007-2010
Drive Mechanics Lead, FRC Team 449: 2009-2010
Alumnus/Technical Mentor, FRC Team 449: 2010-Present
Lead Technical Mentor, FRC Team 4464: 2012-2015
Technical Mentor, FRC Team 5830: 2015-2016
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 13:15.

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