Bug in WPILib PIDController?

The following code excerpt is found in PIDController.cpp of revision 3111 of WPILib, on lines 128 through 141.


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

You should file a bug at the WPILib tracker.

http://firstforge.wpi.edu/sf/tracker/do/listArtifacts/projects.wpilib/tracker.wpilib_c_bugs

Thanks. If anyone else is interested in following this issue, the artifact can be found here.