BTW, P is usually referred to as Kp or proportional constant and I as Ki or integral constant.
some code I use on my robot modified for this context. Mine is a PD loop.
{
// Error = difference between desired and measured
error = (desired_Current - current_measured);
_pd = ( (error * k_p) + ( (error - last_error) * k_d) );
last_error = error; // store our last error
pwmDuty += limit_range(pwmDuty + _pd, MINRANGE, MAXRANGE);
}
// limit the range of the value to between low and high. Si
int16_t limit_range(int16_t val, int16_t low, int16_t high)
{
if(val < low) return low;
else if(val > high) return high;
else return val;
}
Jay
|