pid y telado matricial en una plante de primer orden
Dependencies: FPointer TextLCD keypad mbed
main.cpp
00001 #include "mbed.h" 00002 #include "keypad.h" 00003 #include "TextLCD.h" 00004 00005 DigitalOut Led(LED2); 00006 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 00007 AnalogIn Vin(PTB3); 00008 AnalogOut Vout(PTE30); 00009 //Serial pc(USBTX, USBRX); 00010 int C1=0x0E; // solo muestra el curzor 00011 int C3=0x1A; // desplaza derecha 00012 int a,num=0, i=0 , bt,f=0; 00013 int j; 00014 int Kp, Ki, Kd, Sp, yr, cycle, med2 , c; 00015 float med,pid,ap, err, ai, ad,err_v; 00016 // Define your own keypad values 00017 char Keytable[] = { '1', '2', '3', 'A', 00018 '4', '5', '6', 'B', 00019 '7', '8', '9', 'C', 00020 '*', '0', '#', 'D' 00021 }; 00022 00023 int Keytablei[] = { 1, 2, 3, 10, 00024 4, 5, 6, 11, 00025 7, 8, 9, 12, 00026 13,0,14, 15 00027 }; 00028 00029 uint32_t cbAfterInput(uint32_t key) 00030 { 00031 if(bt==0) { 00032 if(a<2) { 00033 if(Keytable[key]=='A') { 00034 bt=3; 00035 a=3; 00036 f=1; 00037 } 00038 00039 00040 switch(a) { 00041 case 0: 00042 num=num+Keytablei[key]*10; 00043 break; 00044 case 1: 00045 num=num+Keytablei[key]*1; 00046 break; 00047 } 00048 00049 00050 00051 a++; 00052 } 00053 00054 } 00055 00056 switch(i) { 00057 case 0: 00058 //a=0; 00059 lcd.locate(2,0); 00060 lcd.printf("= "); 00061 lcd.locate(3,0); 00062 lcd.printf("%d",num); 00063 Sp=num; 00064 //num=0; 00065 //lcd.printf("%d",num); 00066 00067 break; 00068 case 1: 00069 lcd.locate(10,0); 00070 lcd.printf("= "); 00071 lcd.locate(11,0); 00072 lcd.printf("%d",num); 00073 Kp=num; 00074 //num=0; 00075 //lcd.printf("%d",num); 00076 break; 00077 case 2: 00078 00079 lcd.locate(2,1); 00080 lcd.printf("= "); 00081 lcd.locate(3,1); 00082 lcd.printf("%d",num); 00083 Kd=num; 00084 //num=0; 00085 //lcd.printf("%d",num); 00086 break; 00087 case 3: 00088 lcd.locate(10,1); 00089 lcd.printf("= "); 00090 lcd.locate(11,1); 00091 lcd.printf("%d",num); 00092 Ki=num; 00093 // num=0; 00094 //lcd.printf("%d",++Kd); 00095 break; 00096 } 00097 00098 if(f==1) { 00099 a=0; 00100 f=0; 00101 } 00102 if(Keytable[key]=='A') { 00103 bt=3; 00104 a=0; 00105 num=0; 00106 } 00107 if(Keytable[key]=='D') 00108 bt=4; 00109 return 0; 00110 } 00111 00112 int main() { 00113 Keypad keypad(PTA2,PTD4,PTD3,PTD1,PTD2,PTD0,PTD5,PTA13); 00114 keypad.CallAfterInput(&cbAfterInput); 00115 keypad.Start(); 00116 00117 lcd.writeCommand(C1); 00118 lcd.printf("Sp= Kp="); 00119 lcd.locate(0,1); 00120 lcd.printf("Ki= Kd="); 00121 lcd.locate(2,0); 00122 lcd.printf("="); 00123 00124 00125 while (1) { 00126 00127 00128 if(bt==3) { 00129 i++; 00130 if(i>3) { 00131 i=0; 00132 } 00133 00134 switch (i) { 00135 case 0: 00136 lcd.locate(2,0); 00137 lcd.printf("="); 00138 break; 00139 case 1: 00140 lcd.locate(10,0); 00141 lcd.printf("="); 00142 break; 00143 case 2: 00144 lcd.locate(2,1); 00145 lcd.printf("="); 00146 break; 00147 case 3: 00148 lcd.locate(10,1); 00149 lcd.printf("="); 00150 break; 00151 } 00152 //wait(.1); 00153 00154 bt=0; 00155 } 00156 00157 00158 if (bt==4){ 00159 bt=0; 00160 break; //sale del bucle si pisan suiche4 00161 } 00162 00163 } 00164 lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo 00165 lcd.cls(); //borra la pantalla 00166 lcd.printf(" GUARDADOS!"); 00167 wait(2); 00168 lcd.cls(); 00169 lcd.printf(" INICIA EL PID"); 00170 wait(2); 00171 // se imprimen los parches del control ***************************************** 00172 lcd.cls(); 00173 00174 00175 lcd.printf("Er%d",err); 00176 lcd.locate(8,0); 00177 lcd.printf("Me%d",med2); 00178 lcd.locate(0,1); 00179 lcd.printf("Sp%d",Sp); 00180 lcd.locate(8,1); 00181 lcd.printf("Pid%d",pid); 00182 //wait(5); 00183 00184 // CICLO PRINCIPAL CONTROLADOR PID 00185 00186 while(1) { 00187 //wait(0.01); //leer puerto analogo y asignar a med 00188 med=Vin.read(); 00189 med2=med*100; 00190 err = (Sp-med2); 00191 float kp2; 00192 kp2=Kp*0.001; 00193 ap = kp2*err; 00194 float ki2; 00195 ki2=Ki*0.01; 00196 ai =(ki2*err)+ai; //calculo de la integral del error 00197 float kd2; 00198 kd2=Kd*0.0001; 00199 ad = kd2*(err-err_v); //calculo de la accion derivativa 00200 err_v=err; //guarda el error 00201 pid = (ap+ai+ad); 00202 00203 // se verifica que pid sea menor o igual la valor maximo ***************** 00204 if (pid > .99999){ 00205 pid=1; 00206 } 00207 00208 // se verifica que pid sea positivo ************************************** 00209 if (pid <0){ 00210 pid=0; 00211 } 00212 00213 //wait(.5); 00214 00215 00216 00217 // se verifica que la accion integral no sea muy grande 00218 if (ai > 999){ 00219 ai=1000; 00220 } 00221 00222 00223 Vout=pid; 00224 00225 00226 //****se muestran las variables****************************************** 00227 if(c>600){ 00228 lcd.locate(2,0); 00229 lcd.printf(" "); 00230 lcd.locate(0,0); 00231 lcd.printf("Er%2.2f",err); 00232 lcd.locate(10,0); 00233 lcd.printf(" "); 00234 lcd.locate(8,0); 00235 lcd.printf("Me%d",med2); 00236 lcd.locate(2,1); 00237 lcd.printf(" "); 00238 lcd.locate(0,1); 00239 lcd.printf("Sp%d",Sp); 00240 lcd.locate(10,1); 00241 lcd.printf(" "); 00242 lcd.locate(8,1); 00243 lcd.printf("Pid%4.3f",pid); 00244 c=0; 00245 } 00246 else 00247 c++; 00248 00249 00250 } 00251 }
Generated on Wed Jul 20 2022 23:18:52 by 1.7.2