#include <string.h>
#include<stdio.h>
typedef struct PID{
double Command; //输入指令
double Proportion; //比例系数
double Integral; //积分系数
double Derivative; //微分系数
double preErr; //前一拍误差
double sumErr; //误差累积
}PID;
double PIDCale(PID *p,double feedback)
{
double dErr,Err;
Err=p->Command-feedback; //当前误差
p->sumErr+=Err; //误差累加
dErr=Err-p->preErr; //误差微分
p->preErr=Err;
return(p->Proportion*Err //比例项
+p->Derivative*dErr //微分项
+p->Integral*p->sumErr); //积分项
}
void PIDInit(PID *p)
{
memset(p,0,sizeof(PID)); //初始化
}
typedef struct motor{
double lastY;
double preY;
double lastU;
double preU;
}motor;
void motorInit(motor *m)
{
memset(m,0,sizeof(motor));
}
double motorCal(motor *m,double u)
{
double y=1.9753*m->lastY-0.9753*m->preY+0.00003284*u+0.00006568*m->lastU+0.00003284*m->preU;//二阶系统
m->preY=m->lastY;
m->lastY=y;
m->preU=m->lastU;
m->lastU=u;
return y;
}
void main()
{
FILE *fp=fopen("data.txt","w+");
PID sPID;
double u;
double y=0;
PIDInit(&sPID);
sPID.Proportion=2;
sPID.Derivative=10;
sPID.Integral=0.00001;
sPID.Command=10;
motor m_motor;
motorInit(&m_motor);
int k=0;
while(k<=1000)
{
fprintf(fp,"%f %fn",y,sPID.Command);
u=PIDCale(&sPID,y);
y=motorCal(&m_motor,u);
k++;
}
printf("%fn",y);
fclose(fp);
}
#include<stdio.h>
typedef struct PID{
double Command; //输入指令
double Proportion; //比例系数
double Integral; //积分系数
double Derivative; //微分系数
double preErr; //前一拍误差
double sumErr; //误差累积
}PID;
double PIDCale(PID *p,double feedback)
{
double dErr,Err;
Err=p->Command-feedback; //当前误差
p->sumErr+=Err; //误差累加
dErr=Err-p->preErr; //误差微分
p->preErr=Err;
return(p->Proportion*Err //比例项
+p->Derivative*dErr //微分项
+p->Integral*p->sumErr); //积分项
}
void PIDInit(PID *p)
{
memset(p,0,sizeof(PID)); //初始化
}
typedef struct motor{
double lastY;
double preY;
double lastU;
double preU;
}motor;
void motorInit(motor *m)
{
memset(m,0,sizeof(motor));
}
double motorCal(motor *m,double u)
{
double y=1.9753*m->lastY-0.9753*m->preY+0.00003284*u+0.00006568*m->lastU+0.00003284*m->preU;//二阶系统
m->preY=m->lastY;
m->lastY=y;
m->preU=m->lastU;
m->lastU=u;
return y;
}
void main()
{
FILE *fp=fopen("data.txt","w+");
PID sPID;
double u;
double y=0;
PIDInit(&sPID);
sPID.Proportion=2;
sPID.Derivative=10;
sPID.Integral=0.00001;
sPID.Command=10;
motor m_motor;
motorInit(&m_motor);
int k=0;
while(k<=1000)
{
fprintf(fp,"%f %fn",y,sPID.Command);
u=PIDCale(&sPID,y);
y=motorCal(&m_motor,u);
k++;
}
printf("%fn",y);
fclose(fp);
}
界面设计:
http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=3617085&bbs_page_no=1&search_mode=4&search_text=rlplc&bbs_id=9999
务必参考:
http://blog.niwota.com/a/591138.htm
http://www.dzsc.com/data/html/2008-11-24/73559.html
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/3252/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2010-6-26 16:05
评论列表