Click here to load reader
Upload
bree
View
99
Download
1
Embed Size (px)
DESCRIPTION
第 7 章 单片机常用算法设计. 7.1 单片机滤波算法的设计 7.2 信号处理的 FFT 变换 7.3 SPWM 正弦逆变算法的设计 7.4 PID 控制算法 7.5 51 单片机 PID 算法程序 7.6 模糊控制算法. 7.1 单片机滤波算法的设计. 电路的滤波分为 模拟滤波 与 数字滤波 。其中数字滤波器具有 精度高、高可靠性和高稳定性 的特点 , 因此被广泛应用。用数字滤波算法克服随机误差主要有如下 优点 : 数字滤波由软件程序实现 , 不需要硬件 , 因此 不存在阻抗匹配的问题 ; - PowerPoint PPT Presentation
Citation preview
7 7.1 7.2 FFT7.3 SPWM7.4 PID7.5 51PID7.6
7.1 ,: , , ,, ,
A. , n n - 1y(n) y(n 1) , ,y(n) , ,y(n) ,y(n 1) y(n) ,
( A value ,new_value )#define A 10char value;char filter(){ char new_value; new_value = get_ad(); if ( ( new_value - value > A ) || ( value - new_value > A )) return value; return new_value;}
B. , n( n ) , ,
C. , N , Y , Y
D. , N , , , , N , N , ,
E.,.a.1/2#define a 50char value;char filter(){ char new_value; new_value = get_ad(); return (100-a)*value + a*new_value;}
7.2 FFT(Fast Fourier TransfonnFFT) (Discrete Fourier TransformDFT)
FFTWNNN=2k,kN/2N/2DFTN/2^2NN/2DFTNDFTN+2N/22=N+N^2/2
7.3 SPWMPWMPulse Width ModulationSPWMPWMSPWM,
SPWMA.,,,PWM,,,,
B., ,PWM.,,SPWM,
C.,,SPWM,,
D.,,SPWM,,,
E.,,,PWM5,7
7.4 PIDPIDPIDPID;PID;PIDPIPD
PIDPID
e(t)TI()PID
PIDr(t)c(t)(P)(I)(D)PID PID
PIDPID
PID
P PI PD PID
PID
PIDPID
PID
/
7.5 51PID PIDPID
::u(n)k KP Ki Kd
e(k)(k=0,1,n) u(n)u(n)
#include #include typedef struct PID { double SetPoint; // Desired value double Proportion; // Proportional Const double Integral; // Integral Const double Derivative; // Derivative Const double LastError; // Error[-1] double PrevError; // Error[-2] double SumError; // Sums of Errors } PID;
PIDdouble PIDCalc( PID *pp, double NextPoint ) { double dError, Error; Error = pp->SetPoint - NextPoint; // pp->SumError += Error; // dError = Error - pp->LastError; // pp->PrevError = pp->LastError; pp->LastError = Error; return (pp->Proportion * Error // + pp->Integral * pp->SumError // + pp->Derivative * dError ); } // void PIDInit (PID *pp) { memset ( pp,0,sizeof(PID)); }
double sensor (void) { return 100.0; } void actuator(double rDelta) {} void main(void) { PID sPID; double rOut; double rIn; PIDInit ( &sPID ); sPID.Proportion = 0.5 sPID.Derivative = 0.0; sPID.SetPoint = 100.0; for (;;) { rIn = sensor (); rOut = PIDCalc ( &sPID,rIn ); actuator ( rOut ); }}
PID
PID
typedef struct PID { int SetPoint; //long SumError; // double Proportion; //double Integral; //double Derivative; //int LastError; //Error[-1] int PrevError; //Error[-2] } PID;static PID sPID; static PID *sptr = &sPID;
/*PID*/ void IncPIDInit(void) { sptr->SumError = 0; sptr->LastError = 0; //Error[-1] sptr->PrevError = 0; //Error[-2] sptr->Proportion = 0; //sptr->Integral = 0; //sptr->Derivative = 0; //sptr->SetPoint = 0; }
/*PID*/ int IncPIDCalc(int NextPoint) { register int iError, iIncpid; // iError = sptr->SetPoint - NextPoint; // iIncpid = sptr->Proportion * iError //E[k] - sptr->Integral * sptr->LastError //E[k1] + sptr->Derivative * sptr->PrevError; //E[k2] sptr->PrevError = sptr->LastError; sptr->LastError = iError; // return(iIncpid); }
7.6
void main() { int a=0,e,ec; /*int nowpoint,p1,p2=1; FILE *in,*out; in=fopen("in.txt","r"); out=fopen("out.txt","w");*/ //while(!feof(in)) while(1) { //fscanf(in,"%d",&nowpoint); //p1=nowpoint; //e=0-nowpoint; //ec= p1-p2; printf("e:"); scanf("%d",&e); printf("ec:"); scanf("%d",&ec); a=Fuzzy(e,ec); //fprintf(out,"%d",a); //printf("%d:",p1); printf("e:%dec:%d",e,ec); printf("a: %d \n",a); //p2=p1; } //fclose(in); //fclose(out); }