Error in code in AVR App note on PID

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

App note 221. Last line on page 7.
They use the process value for the derivative term. Sure, why not. But they show (everywhere shows in fact) that the derivative is calculated by y[n]-y[n-1].
But if you look at their code pid.c, they do the error correct, but the derivative incorrect... they do it backwards: y[n-1] - y[n]

int16_t pid_Controller(int16_t setPoint, int16_t processValue, struct PID_DATA *pid_st)
{
  int16_t error, p_term, d_term;
  int32_t i_term, ret, temp;

  error = setPoint - processValue;

...

// Calculate Dterm
  d_term = pid_st->D_Factor * (pid_st->lastProcessValue - processValue);

  pid_st->lastProcessValue = processValue;

  ret = (p_term + i_term + d_term) / SCALING_FACTOR;
  return((int16_t)ret);
}

Am I incorrect or am I missing something?

Cheers,
Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Seems like they're just using a shortcut to arrive at -d_term. It makes sense that if the derivative is going up, the correction factor should push the output down, and vice versa.