一、速度控制算法:首先定义速度偏差-50km/h≤e(k)≤50km/h,-20≤ec(i)=e(k)-e(k-1)≤20,阀值eswith=10km/h设计思想:油门控制采用增量式PID控制算法,刹车控制采用模糊控制算法,最后通过选择规则进行选择控制量输入。选择规则:e(k)0①e(k)-eswithandthrottlr_1≠0选择油门控制②否则:先将油门控制量置0,再选择刹车控制0e(k)先选择刹车控制,再选择油门控制e(k)=0直接跳出选择刹车控制:刹车采用模糊控制算法1.确定模糊语言变量e基本论域取[-50,50],ec基本论域取[-20,20],刹车控制量输出u基本论域取[-30,30],这里我将这三个变量按照下面的公式进行离散化:)]2(2[baxabny其中,],[bax,n为离散度。E、ec和u均取离散度n=3,离散化后得到三个量的语言值论域分别为:E=EC=U={-3,-2,-1,0,1,2,3}其对应语言值为{NB,NM,NS,ZO,PS,PM,PB}2.确定隶属度函数E/EC和U取相同的隶属度函数即:EECU(,5,1)(,3,2,0)(,3,1,1)u(,2,0,2)(,1,1,3)(,0,2,3)(,1,5)gxtrigxtrigxtrigxtrigxtrigxgx说明:边界选择钟形隶属度函数,中间选用三角形隶属度函数,图像略实际EC和E输入值若超出论域范围,则取相应的端点值。3.模糊控制规则由隶属度函数可以得到语言值隶属度(通过图像直接可以看出)如下表:表1:E/EC和U语言值隶属度向量表-3-2-10123NB100000P0NM010000P1NS01000P2ZO00100P3PS00010P4PM000010P5PB000001P6设置模糊规则库如下表:表2:模糊规则表UEEC—NBNMNSZOPSPMPBNBPBPBPMPMPSZOZONMPBPMPMPSZOZONSNSPMPMPSPSZONSNSZOPMPSPSZOZONSNMPSPSPSZOZOZONSNMPMPSZOZOZONSNMNBPBZOZOZONS*NMNMNB3.模糊推理由模糊规则表3可以知道输入E与EC和输出U的模糊关系,这里我取两个例子做模糊推理如下:if(EisNB)and(ECisNM)then(UisPB)那么他的模糊关系子矩阵为:1211UECERRRR其中,711)0,,0,5.0,1(0PRE,即表1中NB对应行向量,同理可以得到,712)0,,0,5.0,1,0(1PREC,711)0,,0,5.0,1(0PRU77210000000000005.05.00005.010)0,,0,5.0,1,0()0,,0,5.0,1(TECERR49121)0,,0,5.0,5.0,0,0,0,0,0,5.0,1,0(ECER7491211000000005.05.00005.0100000)0,,0,5.0,1()0,,5.0,1,0(TUECERRRif(EisNVBorNB)and(ECisNVB)then(UisPVB)1112UECERRRR结果略按此法可得到27个关系子矩阵,对所有子矩阵取并集得到模糊关系矩阵如下:)27,,2,1(21iRRRRi由R可以得到模拟量输出为:()UEECR4.去模糊化由上面得到的模拟量输出为1×7的模糊向量,每一行的行元素(u(zij))对应相应的离散变量zj,则可通过加权平均法公式解模糊:)21,,2,1()()(210210jizuzzuuiijijij从而得到实际刹车控制量的精确值u。油门控制:油门控制采用增量式PID控制,即:)2()1()2()()()1()(kekkekkkekkkkukuddpdip只需要设置pk、ik、dk三个参数即可输出油门控制量。二、程序实现及参数调节clearall%************************模糊算法%/*********隶属度向量*****%P0=[1,,0,0,0,0,0];%*********NBP1=[0,1,,0,0,0,0];%*********NMP2=[0,,1,,0,0,0];%*********NSP3=[0,0,,1,,0,0];%*********ZOP4=[0,0,0,,1,,0];%*********PSP5=[0,0,0,0,,1,0];%*********PMP6=[0,0,0,0,0,,1];%*********PB%***********语言值*****%NB=-3;NM=-2;NS=-1;ZO=0;PS=1;PM=2;PB=3;%/*********模糊规则表*****%Pg=[PBPBPMPMPSZOZO;PBPMPMPSZOZONS;PMPMPSPSZONSNS;PMPSPSZOZONSNM;PSPSZOZOZONSNM;PSZOZOZONSNMNB;ZOZOZONSNMNMNB];%/*********根据规则表计算模糊关系矩阵*****%R1_=dikaer(xbing(P0,P1),7,P0,7);R1_=reshape(R1_,1,49);R1=dikaer(R1_,49,P6,7);R2_=dikaer(xbing(P2,P3),7,P0,7);R2_=reshape(R2_,1,49);R2=dikaer(R2_,49,P5,7);R3_=dikaer(P0,7,P1,7);R3_=reshape(R3_,1,49);R3=dikaer(R2_,49,P6,7);R4_=dikaer(xbing(P1,P2),7,P1,7);R4_=reshape(R4_,1,49);R4=dikaer(R4_,49,P5,7);R5_=dikaer(P3,7,P1,7);R5_=reshape(R5_,1,49);R5=dikaer(R5_,49,P4,7);R6_=dikaer(xbing(P0,P1),7,P2,7);R6_=reshape(R6_,1,49);R6=dikaer(R6_,49,P5,7);R7_=dikaer(xbing(P2,P3),7,P2,7);R7_=reshape(R7_,1,49);R7=dikaer(R7_,49,P4,7);R8_=dikaer(P0,7,P3,7);R8_=reshape(R8_,1,49);R8=dikaer(R8_,49,P5,7);R9_=dikaer(xbing(P1,P2),7,P3,7);R9_=reshape(R9_,1,49);R9=dikaer(R9_,49,P4,7);R10_=dikaer(P3,7,P3,7);R10_=reshape(R10_,1,49);R10=dikaer(R10_,49,P3,7);R11_=dikaer(xbing(P0,P1),7,P4,7);R11_=reshape(R11_,1,49);R11=dikaer(R11_,49,P4,7);P45=xbing(P4,P5);R12_=dikaer(xbing(P2,P3),7,P45,7);R12_=reshape(R12_,1,49);R12=dikaer(R12_,49,P3,7);R13_=dikaer(P0,7,P5,7);R13_=reshape(R13_,1,49);R13=dikaer(R13_,49,P4,7);R14_=dikaer(P1,7,P5,7);R14_=reshape(R14_,1,49);R14=dikaer(R14_,49,P3,7);P01=xbing(P0,P1);R15_=dikaer(xbing(P01,P2),7,P6,7);R15_=reshape(R15_,1,49);R15=dikaer(R15_,49,P3,7);R16_=dikaer(P3,7,P6,7);R16_=reshape(R16_,1,49);R16=dikaer(R16_,49,P2,7);R17_=dikaer(P4,7,P0,7);R17_=reshape(R17_,1,49);R17=dikaer(R17_,49,P4,7);R18_=dikaer(xbing(P5,P6),7,P0,7);R18_=reshape(R18_,1,49);R18=dikaer(R18_,49,P3,7);R19_=dikaer(xbing(P4,P5),7,P1,7);R19_=reshape(R19_,1,49);R19=dikaer(R19_,49,P3,7);R20_=dikaer(P6,7,xbing(P1,P2),7);R20_=reshape(R20_,1,49);R20=dikaer(R20_,49,P2,7);P23=xbing(P2,P3);R21_=dikaer(P4,7,xbing(P23,P4),7);R21_=reshape(R21_,1,49);R21=dikaer(R21_,49,P3,7);R22_=dikaer(P5,7,xbing(P23,P4),7);R22_=reshape(R22_,1,49);R22=dikaer(R22_,49,P2,7);R23_=dikaer(P6,7,xbing(P3,P4),7);R23_=reshape(R23_,1,49);R23=dikaer(R23_,49,P1,7);R24_=dikaer(P4,7,P5,7);R24_=reshape(R24_,1,49);R24=dikaer(R24_,49,P2,7);R25_=dikaer(P5,7,P5,7);R25_=reshape(R25_,1,49);R25=dikaer(R25_,49,P1,7);R26_=dikaer(P6,7,xbing(P6,P5),7);R26_=reshape(R26_,1,49);R26=dikaer(R26_,49,P0,7);R27_=dikaer(xbing(P4,P5),7,P6,7);R27_=reshape(R27_,1,49);R27=dikaer(R27_,49,P1,7);m=[R1,R2,R3,R4,R5,R6,R7,R8,R9,R10,R11,R12,R13,R14,R15,R16,R17,R18,R19,R20,R21,R22,R23,R24,R25,R26,R27];R=bingji(m);%*************初始化参量e=0;ec=0;y_1=0;y_2=0;u=0;u_1=0;u_2=0;u_3=0;e_1=0;e_2=0;Eswith=10;throttle_1=0;brake_1=0;x=[000];ts=;sys=tf(1,[1,2,1],'inputdelay',;dsys=c2d(sys,ts,'zoh');[num,den]=tfdata(dsys,'v');fork=1:1:40000%****************控制系统time(k)=k*ts;if(k25000)vd(k)=40;elsevd(k)=0;endy(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_1+num(3)*u_2;e=vd(k)-y(k);ec=e-e_1;u_3=u_2;u_2=u_1;u_1=u;y_2=y_1;y_1=y(k);x(1)=e;x(2)=(e-e_1)/ts;x(3)=x(3)+e*ts;%*******************************油门PID控制kp=;Ti=30;Td=;ki=kp*ts/Ti;kd=kp*Td/ts;dthrottle=kp*x(1)+k