PID控制算法的C语言实现

PID控制框图

PID算法的一般形式
PID控制算法的C语言实现_第1张图片

位置式PID

PID控制算法的C语言实现_第2张图片

增量式PID

PID控制算法的C语言实现_第3张图片

PID控制的自动控制系统框图

PID控制规律结合到控制理论中,根据下图进行编程实现。
PID控制算法的C语言实现_第4张图片

代码实现

#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;

struct PID{
	double e;						//e(t)
	double e1;						//e(t)的前一时刻
	double e2;						//e1的前一个时刻
	double u;						//u(t)
	double u1;						//u(t)的前一时刻
	double Kp, Ki, Kd;				//比例、积分、微分系数
	double integral;				//积分值
};

//0.323, 0.357, 0.0553;
void InitPID(PID& pid,double Kp, double Ki, double Kd)
{
	pid.Kp = Kp;
	pid.Ki = Ki;
	pid.Kd = Kd;
	pid.e = 0.0;						
	pid.e1 = 0.0;
	pid.e2 = 0.0;
	pid.u = 0.0;
	pid.u1 = 0.0;
	pid.integral = 0.0;
}
double PidPosition(PID& pid,double R,double y1)//位置式pid
{
	pid.e = R - y1;//e=R-y
	pid.integral += pid.e;
	//u(k)=Kp*e+Ki*pid.integral+Kd*(e-e1)
	pid.u = pid.Kp*pid.e + pid.Ki*pid.integral + pid.Kd*(pid.e - pid.e1);
	pid.e1 = pid.e;
	pid.u1 = pid.u;
	return pid.u;
}

double increment;
double PidIncrement(PID& pid,double R,double y1)//增量式PID
{
	pid.e = R - y1;
	increment = pid.Kp*(pid.e - pid.e1) + pid.Ki*pid.e + pid.Kd*(pid.e - 2 * pid.e1 + pid.e2);
	pid.u += increment;
	pid.e2 = pid.e1;
	pid.e1 = pid.e;
	return pid.u;
}

double Transfer(PID& pid)
{
	return 0.5*pid.u1/(1-0.5*pid.u1);
}

int main()
{
	double Kp = 0.0, Ki = 0.0, Kd = 0.0;
	PID pid;
	double R = 0.0, y = 0.0, y1 = y;
	printf("请输入Kp,Ki,Kd:>");
	scanf("%lf%lf%lf", &Kp, &Ki, &Kd);
	InitPID(pid,Kp,Ki,Kd);
	cout << "PID begin>\n";
	for (int i = 0; i < 1000; i++)//单位阶跃信号
	{
		if (i < 50)
			R = 0;
		else
			R = 1;
	}

	double et = 0.0;//传递函数的输入值
	for (int i = 0; i < 1000; i++)
	{
		et = PidPosition(pid, 1,y1);//PID的结果u(t)是传递函数的输入
		y1 = y;//y1是上一个系统输出,负反馈到PID部分,PID的e(t)=R-y1,R是系统输入
		y = Transfer(pid);
		printf("%12f", y);
	}
	//system("pause");vs软件需要使用它让程序停下来
	return 0;
}

请输入Kp,Ki,Kd:>0.323 0.357 0.0553
PID begin>
0.867650 1.018500 0.444183 0.360913 0.860088 1.015671 0.694336 0.598122 0.878432 1.007589 0.833452 0.750840 0.904146 1.000738 0.909913 0.847454 0.928797 0.996559 0.951484 0.907649 0.949246 0.994694 0.973874 0.944652 0.964919 0.994362 0.985849 0.967125 0.976332 0.994868 0.992230 0.980627 0.984344 0.995726 0.995638 0.988660 0.989814 0.996645 0.997474 0.993398 0.993466 0.997476 0.998483 0.996172 0.995859 0.998164 0.999054 0.997785 0.997404 0.998699 0.999389 0.998719 0.998388 0.999097 0.999595 0.999258 0.999007 0.999385 0.999726 0.999568 0.999393 0.999588 0.999813 0.999747 0.999631 0.999728 0.999872 0.999851 0.999777 0.999822 0.999912 0.999911 0.999865 0.999885 0.999940 0.999946 0.999919 0.999926 0.999959 0.999967 0.999952 0.999953 0.999973 0.999979 0.999971 0.999970 0.999982 0.999987 0.999983 0.999982 0.999988 0.999992 0.999990 0.999988 0.999992 0.999995 0.999994 0.999993 0.999995 0.999997 0.999996 0.999996 0.999997 0.999998 0.999998 0.999997 0.999998 0.999999 0.999999 0.999998 0.999999 0.999999 0.999999 0.999999 0.999999 0.999999 0.999999 0.999999 0.999999 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 请按任意键继续. . .
PID控制算法的C语言实现_第5张图片

你可能感兴趣的:(编程工具)