freetech

HCS08下的1段PID程序

0
阅读(3189)

#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */

//long int
unsigned long data,time;
typedef struct PID
{
  int SetPoint; //设定目标 Desired Value
  //long SumError; //误差累计
  double Proportion; //比例常数 Proportional Const
  double Integral; //积分常数 Integral Const
  double Derivative; //微分常数 Derivative Const
  int LastError; //Error[-1]
  int PrevError; //Error[-2]
} PID;
PID sPID;
PID *sptr = &sPID;
void IncPIDInit(void)
{
  //sptr->SumError = 0;
  sptr->LastError = 0; //Error[-1]
  sptr->PrevError = 0; //Error[-2]
  sptr->Proportion = 0; //比例常数 Proportional Const
  sptr->Integral = 0; //积分常数Integral Const
  sptr->Derivative = 0; //微分常数 Derivative Const
  sptr->SetPoint = 0;
}
int IncPIDCalc(int NextPoint)
{
  int iError, iIncpid; //当前误差
  iError = sptr->SetPoint - NextPoint; //增量计算
  iIncpid = sptr->Proportion * iError //E[k]项
  - sptr->Integral * sptr->LastError //E[k-1]项
  + sptr->Derivative * sptr->PrevError; //E[k-2]项
  //存储误差,用于下次计算
  sptr->PrevError = sptr->LastError;
  sptr->LastError = iError;
  //返回增量值
  return(iIncpid);
}
void main(void) {
  int nextpoint = 0;
  int buf[20],i;
  EnableInterrupts; /* enable interrupts */
  /* include your code here */
  i = 0;
  IncPIDInit();
  sptr->SetPoint = 200;
  sptr->Proportion = 0.5;
  sptr->Integral = 0.3;
  sptr->Derivative = 0.1;
  PTEDD_PTEDD0 = 1;
  for(;;) {
    nextpoint += IncPIDCalc(nextpoint);
    buf[i++] = nextpoint;
    if( i >= 20 )
    {
      i = 0;
    }
    __RESET_WATCHDOG(); /* feeds the dog */
  } /* loop forever */
  /* please make sure that you never leave main */
}

image

可以看到第15次已经达到了198,但是永远达不到希望值200——这个还没弄清怎么回事。