├── BPPID.py └── PID.py /BPPID.py: -------------------------------------------------------------------------------- 1 | import PID 2 | import matplotlib.pyplot as plt 3 | 4 | plt.figure(1) # 创建图表1 5 | 6 | def TestPID(): 7 | Process = PID.IncrementalPID() 8 | ProcessXaxis = [0] 9 | ProcessYaxis = [0] 10 | X = [0] 11 | Y = [0] 12 | 13 | for i in range(1, 500): 14 | Process.SetStepSignal(10) 15 | # Process.SetInertiaTime(100, 50) 16 | ProcessXaxis.append(i) 17 | ProcessYaxis.append(Process.y) 18 | X.append(i) 19 | Y.append(Process.e) 20 | 21 | 22 | plt.figure(1) 23 | plt.plot(ProcessXaxis, ProcessYaxis, 'r') 24 | 25 | plt.xlim(0, 1000) 26 | plt.ylim(0, 50) 27 | plt.title("IncrementalPID") 28 | plt.show() 29 | 30 | if __name__ =='__main__': 31 | TestPID() -------------------------------------------------------------------------------- /PID.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | 5 | class IncrementalPID: 6 | def __init__(self): 7 | 8 | self.xite_1 = 0.2 9 | self.alfa = 0.95 10 | self.IN = 4 11 | self.H = 5 12 | self.Out = 3 13 | self.wi = np.mat([[-0.6394, -0.2696, -0.3756, -0.7023], 14 | [-0.8603, -0.2013, -0.5024, -0.2596], 15 | [-1.0000, 0.5543, -1.0000, -0.5437], 16 | [-0.3625, -0.0724, 0.6463, -0.2859], 17 | [0.1425, 0.0279, -0.5406, -0.7660]] 18 | ) 19 | 20 | 21 | self.wi_1 = self.wi 22 | self.wi_2 = self.wi 23 | self.wi_3 = self.wi 24 | self.wo = np.mat([[0.7576, 0.2616, 0.5820, -0.1416, -0.1325], 25 | [-0.1146, 0.2949, 0.8352, 0.2205, 0.4508], 26 | [0.7201, 0.4566, 0.7672, 0.4962, 0.3632]] 27 | ) 28 | 29 | self.wo_1 = self.wo 30 | self.wo_2 = self.wo 31 | self.wo_3 = self.wo 32 | 33 | self.Kp = 0.0 34 | self.Ki = 0.0 35 | self.Kd = 0.0 36 | 37 | # self.ts = 40 # 采样周期取值 38 | self.x = [self.Kp, self.Ki, self.Kd] # 比例、积分、微分初值 39 | 40 | self.y = 0.0 # 系统输出值 41 | self.y_1 = 0.0 # 上次系统输出值 42 | self.y_2 = 0.0 # 上次系统输出值 43 | 44 | self.e = 0.0 # 输出值与输入值的偏差 45 | self.e_1 = 0.0 46 | self.e_2 = 0.0 47 | self.de_1 = 0.0 48 | 49 | self.u = 0.0 50 | self.u_1 = 0.0 51 | self.u_2 = 0.0 52 | self.u_3 = 0.0 53 | self.u_4 = 0.0 54 | self.u_5 = 0.0 55 | 56 | 57 | self.Oh = np.mat(np.zeros((self.H, 1))) # %隐含层的输出 58 | self.I = self.Oh # 隐含层的输入 59 | 60 | 61 | self.Oh_sub = [0.0, 0.0, 0.0, 0.0, 0.0] 62 | self.K_sub = [0.0, 0.0, 0.0] 63 | self.dK_sub = [0.0, 0.0, 0.0] 64 | self.delta3_sub = [0.0, 0.0, 0.0] 65 | self.dO_sub = [0.0, 0.0, 0.0, 0.0, 0.0] 66 | self.delta2_sub = [0.0, 0.0, 0.0, 0.0, 0.0] 67 | 68 | self.den = -0.8251 69 | self.num = 0.2099 70 | 71 | # 设置PID控制器参数 72 | def SetStepSignal(self, SetSignal): 73 | 74 | self.y = -self.den * self.y_1 + self.num * self.u_5 75 | self.e = SetSignal - self.y 76 | self.xi = np.mat([SetSignal, self.y, self.e, 5]) 77 | 78 | self.x[0] = self.e - self.e_1 # 比例输出 79 | self.x[1] = self.e # 积分输出 80 | self.x[2] = self.e - 2 * self.e_1 + self.e_2 # 微分输出 81 | self.epid = np.mat([[self.x[0]], [self.x[1]], [self.x[2]]])#列 82 | 83 | self.I = np.dot(self.xi, (self.wi.T)) 84 | 85 | for i1 in range(5): 86 | self.Oh_sub[i1] = (np.e ** (self.I.tolist()[0][i1]) - np.e ** (-self.I.tolist()[0][i1])) / (np.e ** (self.I.tolist()[0][i1]) + np.e ** (-self.I.tolist()[0][i1])) # %在激活函数作用下隐含层的输出 87 | self.Oh = np.mat([[self.Oh_sub[0]], [self.Oh_sub[1]], [self.Oh_sub[2]], [self.Oh_sub[3]], [self.Oh_sub[4]]]) 88 | 89 | self.K = np.dot(self.wo, self.Oh) # 输出层的输入,即隐含层的输出*权值 90 | 91 | for i2 in range(3): 92 | self.K_sub[i2] = (np.e ** (self.K.tolist()[i2][0])) / (np.e ** (self.K.tolist()[i2][0]) + np.e ** (-self.K.tolist()[i2][0])) # 输出层的输出,即PID三个参数 93 | self.K = np.mat([[self.K_sub[0]], [self.K_sub[1]], [self.K_sub[2]]]) 94 | 95 | self.Kp = self.K_sub[0] 96 | self.Ki = self.K_sub[1] 97 | self.Kd = self.K_sub[2] 98 | self.Kpid = np.mat([self.Kp, self.Ki, self.Kd]) #行 99 | 100 | self.du = np.dot(self.Kpid, self.epid).tolist()[0][0] 101 | self.u = self.u_1 + self.du 102 | # if self.u >= 10: 103 | # self.u = 10 104 | # 105 | # if self.u <= -10: 106 | # self.u = -10 107 | 108 | self.de = self.e - self.e_1 109 | if self.de > (self.de_1 * 1.04): 110 | self.xite = 0.7 * self.xite_1 111 | elif self.de < self.de_1: 112 | self.xite = 1.05 * self.xite_1 113 | else: 114 | self.xite = self.xite_1 115 | 116 | # 权值在线调整 117 | self.dyu = np.sin((self.y - self.y_1) / (self.u - self.u_1 + 0.0000001)) 118 | 119 | for i3 in range(3): 120 | self.dK_sub[i3] = 2 / ((np.e ** (self.K_sub[i3]) + np.e ** (-self.K_sub[i3])) * (np.e ** (self.K_sub[i3]) + np.e ** (-self.K_sub[i3]))) 121 | self.dK = np.mat([self.dK_sub[0], self.dK_sub[1], self.dK_sub[2]]) 122 | 123 | 124 | for i4 in range(3): 125 | self.delta3_sub[i4] = self.e * self.dyu * self.epid.tolist()[i4][0] * self.dK_sub[i4] 126 | self.delta3 = np.mat([self.delta3_sub[0], self.delta3_sub[1], self.delta3_sub[2]]) 127 | 128 | for l in range(3): 129 | for i5 in range(5): 130 | self.d_wo = (1 - self.alfa) * self.xite * self.delta3_sub[l] * self.Oh.tolist()[i5][0] + self.alfa * (self.wo_1 - self.wo_2) 131 | 132 | # self.wo = self.wo_1 + self.d_wo + self.alfa * (self.wo_1 - self.wo_2) 133 | self.wo = self.wo_1 + self.d_wo 134 | 135 | for i6 in range(5): 136 | self.dO_sub[i6] = 4 / ((np.e ** (self.I.tolist()[0][i6]) + np.e ** (-self.I.tolist()[0][i6])) * (np.e ** (self.I.tolist()[0][i6]) + np.e ** (-self.I.tolist()[0][i6]))) 137 | self.dO = np.mat([self.dO_sub[0], self.dO_sub[1], self.dO_sub[2], self.dO_sub[3], self.dO_sub[4]]) 138 | 139 | self.segma = np.dot(self.delta3, self.wo) 140 | 141 | for i7 in range(5): 142 | self.delta2_sub[i7] = self.dO_sub[i7] * self.segma.tolist()[0][i7] 143 | self.delta2 = np.mat([self.delta2_sub[0], self.delta2_sub[1], self.delta2_sub[2], self.delta2_sub[3], self.delta2_sub[4]]) 144 | 145 | self.d_wi = (1 - self.alfa) * self.xite * self.delta2.T * self.xi + self.alfa * (self.wi_1 - self.wi_2) 146 | self.wi = self.wi_1 + self.d_wi 147 | 148 | # 参数更新 149 | self.u_5 = self.u_4 150 | self.u_4 = self.u_3 151 | self.u_3 = self.u_2 152 | self.u_2 = self.u_1 153 | self.u_1 = self.u 154 | 155 | self.y_2 = self.y_1 156 | self.y_1 = self.y 157 | 158 | self.wo_3 = self.wo_2 159 | self.wo_2 = self.wo_1 160 | self.wo_1 = self.wo 161 | 162 | self.wi_3 = self.wi_2 163 | self.wi_2 = self.wi_1 164 | self.wi_1 = self.wi 165 | 166 | self.e_2 = self.e_1 167 | self.e_1 = self.e 168 | self.xite_1 = self.xite 169 | 170 | 171 | # IncrementValue = self.Kp * (self.Error - self.LastError) + self.Ki * self.Error + self.Kd * ( 172 | # self.Error - 2 * self.LastError + self.LastLastError) 173 | # self.PIDOutput += IncrementValue 174 | # self.LastLastError = self.LastError 175 | # self.LastError = self.Error 176 | 177 | # 设置一阶惯性环节系统 其中InertiaTime为惯性时间常数 178 | def SetInertiaTime(self, InertiaTime, SampleTime): 179 | self.y = (InertiaTime * self.y_1 + SampleTime * self.u_5) / ( 180 | SampleTime + InertiaTime) 181 | # self.LastSystemOutput = self.SystemOutput 182 | --------------------------------------------------------------------------------