PID算法(c语言)(来自老外)
#include <stdio.h>
#include<math.h>
//定义PID的结构体
struct _pid
{
int pv; //integer that contains the process value 过程量
int sp; //*integer that contains the set point 设定值
float integral; // 积分值 -- 偏差累计值
float pgain;
float igain;
float dgain;
int deadband; //死区
int last_error;
};
//----------------------------------------------
pid_init DESCRIPTION This function initializes the pointers in the _pid structure to the process variable and the setpoint. *pv and *sp are integer pointers.
//----------------------------------------------
//----------------------------------------------pid_tune DESCRIPTION Sets the proportional gain (p_gain), integral gain (i_gain),
derivitive gain (d_gain), and the dead band (dead_band) of a pid control structure _pid.
//----------------------------------------------
pid_setinteg DESCRIPTION Set a new value for the integral term of the pid equation.
This is useful for setting the initial output of the pid controller at start up.
//----------------------------------------------
pid_bumpless DESCRIPTION Bumpless transfer algorithim.
When suddenly changing setpoints, or when restarting the PID equation after an extended pause,
the derivative of the equation can cause a bump in the controller output. This function will help smooth out that bump.
The process value in *pv should be the updated just before this function is used.
//----------------------------------------------
pid_calc DESCRIPTION Performs PID calculations for the _pid structure *a.
This function uses the positional form of the pid equation, and incorporates an integral windup prevention algorithim.
Rectangular integration is used, so this function must be repeated on a consistent time basis for accurate control.
RETURN VALUE The new output value for the pid loop. USAGE #include "control.h"
本函数使用位置式PID计算方式,并且采取了积分饱和限制运算
PID计算
//----------------------------------------------
// printf("Enter the values of Process point, Set point, P gain, I gain, D gain \n");
// scanf("%d%d%f%f%f", &process_point, &set_point, &p_gain, &i_gain, &d_gain);
printf("The values of Process point, Set point, P gain, I gain, D gain \n");
printf(" %6d %6d %4f %4f %4f\n", process_point, set_point, p_gain, i_gain, d_gain);
printf("Enter the values of Process point\n");
while(count<=20)
{
scanf("%d",&process_point);