View Single Post
  #1   Spotlight this post!  
Unread 25-03-2012, 16:23
Clint Clint is offline
Registered User
FRC #0308 (Monsters)
Team Role: Programmer
 
Join Date: Feb 2012
Rookie Year: 2011
Location: Michigan
Posts: 2
Clint is an unknown quantity at this point
Bug in WPILib PIDController?

The following code excerpt is found in PIDController.cpp of revision 3111 of WPILib, on lines 128 through 141.
Code:
double potentialIGain = (m_totalError + m_error) * m_I;
if (potentialIGain < m_maximumOutput)
{
	if (potentialIGain > m_minimumOutput)
		m_totalError += m_error;
	else
		m_totalError = m_minimumOutput / m_I;
}
else
{
	m_totalError = m_maximumOutput / m_I;
}

m_result = m_P * m_error + m_I * m_totalError + m_D * (m_error - m_prevError);
In the lines of code above, a subtle problem exists, under a certain few specific conditions:
  • Output is restricted with a minimum or maximum of 0
  • m_I is 0

Under these instances, the first if statement fails (or, it continues through to the second inner statement which also fails). In both cases, the total error is calculated by dividing by m_I. When m_I is zero--a perfectly logical use case--m_totalError becomes NaN.
As a result, m_result becomes NaN as well. Subsequently, when PIDController::Calculate writes this value to the output, errors rapidly accumulate in the console.


Clint
Reply With Quote