View Single Post
Old 05-22-2009, 02:56 PM   #1352 (permalink)
JayC
EcoModding Lurker
 
Join Date: Jul 2008
Location: Memphis, TN
Posts: 58

2010 Prius - '10 Toyota Prius IV
Last 3: 51.4 mpg (US)
Thanks: 0
Thanked 1 Time in 1 Post
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
  Reply With Quote