PID算法(c語言)(來自老外)
#include <stdio.h>
#include<math.h>
//定義PID的結(jié)構(gòu)體
struct _pid
{
int pv; //integer that contains the process value 過程量
int sp; //*integer that contains the set point 設(shè)定值
float integral; // 積分值 -- 偏差累計值
float pgain;
float igain;
float dgain;
int deadband; //死區(qū)
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"
本函數(shù)使用位置式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);