This is a Proportional, Integral, Derivative (PID) controller I am writing for the MSP430. Does this look correct?
/*This is a PID or Proportional, Intergral, Derivative controller.
It is used by passing the current feedback (FDB) and reference (REF) number.
The function computes the correction (COR) and returns it as an int.
For proper functionality, this must be called at regular intervals as it does not
account for time between samples. Adding this into a interupt routine is ideal.
*/
/*These are the PID constants. They will need to be tweaked to get the controller to operate correctly.*/
#define Kp /*to be filled in*/
#define Ki /*to be filled in*/
#define Kd /*to be filled in*/
/*These varibles need to stick around after the function exists*/
static int Ui, Up_old;
/*Additional functions*/
int COR, ERR, Up, Ud;
/*Start PID controller sequence*/
int PID(int FDB, int REF)
{
ERR = REF-FDB; /*Calculate the ERR.*/
Up = Kp*ERR; /*Proportion*/
Ui = Ui + Ki*Up; /*Intergral*/
Ud = Kd*(Up-Up_old); /*Derivative*/
COR = Up + Ui + Ud; //Correction value*/
Up_old = Up; /*Update the derivative value*/
return COR; /*return the correction*/
}