/**************************** the so-called PID is P (proportional) +i (integral) +d (differential **************************/* * (1) P Control (proportional control): E (t) = sp-y (t); sp--Set value E (t)--error value (set value and current error) y (t)--Feedback value (current value) * * for small hysteresis system U (t) = e (t) * P; U (t)--output value (required output) p--scale factor * * * (2) Pi control (proportional + integral): U (t) = kp*e (t) + ki∑e (t) + u0 * * can bring system into steady state without steady state error U (t)-output kp--proportional magnification factor ki--integral amplification factor E (t)-Error u0--Control amount reference value (base deviation) * * (3) PD control (differential control): * * Differential term solves response speed problem integral Item solution stability * * (4) PID control (proportional integral differential control): * * Differential term solves response speed problem U (t) = Kp*e (t) + ki∑e (t) + kd[e (t) –e (T-1)] + u0 * * Sampling period (that is, the sampling period of feedback value) * * Control period (that is, how often the PID operation, and the result output) ***************** Summary: P (scale)--instantaneous I (integral)--response speed D (differential)--Stability ************************************ */#include <stdio.h> # include<string.h> #include "StandardPID.h"//#include <stdib.h>//Declaration of variables typedef struct pid{Double Setpoin T Set value double Kp; Proportional coefficient double Ki; Integral coefficient double Kd; Differential coefficient double lasterror;
Last error number er[-1]Double Preverror; Last second error number er[-2] Double sumerror;
Error integral}pid;
/* **pid COMPUTE part */double Pidcalc (PID *pp, double nextpoint) {double derror,//Current differential Error; Deviation error = pp->setpoint-nextpoint; Deviation value = setpoint-Input value (current value) Pp->sumerror + = Error; integral = integral + deviation--accumulation of deviations derror = pp->lasterror-pp->preverror; Current differential = Last error-Previous error pp->preverror = pp->lasterror; Update "before error" Pp->lasterror = error; Update "Last Error" return (PP->KP * ERROR/scale item = Proportional constant * deviation + Pp->ki * pp->sumerror
Integral term = Integral constant * ERROR integral + PP->KD * derror//Differential term = Differential constant * current differential); }//For the PID variable to request memory, the range points to pp pointer void Pidinit (PID *pp) {//memset is a C/S language function in the computer. The first n memset (pp, 0, sizeof (PID)) of a piece of memory pointed to by S; The contents of the byte are allSet to the ASCII value specified by CH, the size of the block is specified by the third parameter,}//This function usually initializes the newly requested memory, and its return value is a pointer to S.
Double sensor (void)//input {double Shuru;
scanf ("%lf", &shuru);
return Shuru;
} void Actuator (double rdelta)//output {printf ("%lf\n", Rdelta);} int main () {PID SPID; PID structural body double rOut; Output variable double rIn; Input variable pidinit (&spid);
Initialize struct SPID.KP = 0.5;
Spid.ki = 0.5;
SPID.KD = 0.5;
Spid.setpoint = 100.0; for (;;) {rIn = sensor (); Take the input value ROut = Pidcalc (&spid, rIn); PID Operation Actuator (ROut); Output}}
#define __STANDARDPID_H double Pidcalc (PID *pp, double nextpoint); void Pidinit (PID *pp); Doubl
e sensor (void);
void Actuator (double rdelta); #endif