Control PID para un planta RC (control de la carga del capacitor), los parámetros se ingresan por medio de 4 pulsadores, teniendo en cuenta una librería antirebote y el resultado del control se visualiza en una pantalla LCD 16x2.
Fork of DebouncedIn_HelloWorld by
main.cpp
00001 #include "mbed.h" 00002 #include "DebouncedIn.h" 00003 #include "TextLCD.h" 00004 00005 AnalogIn Vin(PTC2); 00006 AnalogOut Vout(PTE30); 00007 00008 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); //Puertos LCD rs, e, d4, d5, d6, d7 00009 00010 DigitalOut led1(LED1); //led de cambio de posición 00011 DigitalOut led2(LED2); //led incremento de parámetros 00012 DigitalOut led3(LED3); //led decremento de parámetros 00013 DebouncedIn bot1(PTC12); //cambiar la posición 00014 DebouncedIn bot2(PTC13); //incrementar variable 00015 DebouncedIn bot3(PTC16); //decrementar variable 00016 DebouncedIn bot4(PTC17); //salida de bucle 00017 00018 //Códigos LCD 00019 int C1=0x0E; // Muestra el cursor 00020 // 0x18; // desplazamiento izquierda 00021 // 0x1A; // desplazamiento derecha 00022 int C4=0x0C; // Quita el cursor 00023 00024 00025 int a, sp, kp, ki, kd, ciclo; // indice de la variable 00026 float med, sp0, ap, err, ai, ad, pid, err_v; 00027 00028 00029 int main() { 00030 00031 led1=led2=led3=1; 00032 lcd.printf("Control PID"); 00033 wait(1.5); 00034 lcd.cls(); 00035 00036 lcd.writeCommand(C1); //Comando para mostrar el cursor en el LCD 00037 lcd.locate(0,0); //Ubica e imprime nombre de las variables en la LCD 00038 lcd.printf("Sp=%d",sp); 00039 lcd.locate(8,0); 00040 lcd.printf("Kp=%d",kp); 00041 lcd.locate(0,1); 00042 lcd.printf("Ki=%d",ki); 00043 lcd.locate(8,1); 00044 lcd.printf("Kd=%d",kd); 00045 00046 00047 //Inicio del ciclo 00048 00049 while(1) { 00050 if (bot1.falling()) { //Aumenta posición el cursor 00051 a++; 00052 led1=0; 00053 wait(.15); //enciende el led azul cada vez que se oprime este botón 00054 led1=1; 00055 if (a>3){ 00056 a = 0; 00057 } 00058 switch (a) { 00059 case 0: 00060 00061 lcd.locate(2,0); 00062 lcd.printf("="); 00063 break; 00064 00065 case 1: 00066 lcd.locate(10,0); 00067 lcd.printf("="); 00068 break; 00069 00070 case 2: 00071 lcd.locate(2,1); 00072 lcd.printf("="); 00073 break; 00074 00075 case 3: 00076 lcd.locate(10,1); 00077 lcd.printf("="); 00078 break; 00079 } 00080 } 00081 00082 if (bot2.falling()) { //Incrementa la variable 00083 led2=0; 00084 wait(.15); //enciende el led verde cada vez que se oprime el botón de incremento 00085 led2=1; 00086 00087 switch (a) { 00088 case 0: 00089 00090 lcd.locate(2,0); //Ubica el parámetro Set-point 00091 lcd.printf("= "); 00092 lcd.locate(3,0); 00093 lcd.printf("%d", ++sp); 00094 break; 00095 00096 case 1: 00097 lcd.locate(10,0); //Ubica el parámetro kp 00098 lcd.printf("= "); 00099 lcd.locate(11,0); 00100 lcd.printf("%d", ++kp); 00101 break; 00102 00103 case 2: 00104 lcd.locate(2,1); //Ubica el parámetro ki 00105 lcd.printf("= "); 00106 lcd.locate(3,1); 00107 lcd.printf("%d", ++ki); 00108 break; 00109 00110 case 3: 00111 lcd.locate(10,1); //Ubica el parámetro kd 00112 lcd.printf("= "); 00113 lcd.locate(11,1); 00114 lcd.printf("%d", ++kd); 00115 break; 00116 } 00117 } 00118 if (bot3.falling()) { //Decrementa la variable 00119 wait(0.1); 00120 led3=0; 00121 wait(.15); //enciende el led roja cada vez que se oprime este botón 00122 led3=1; 00123 00124 switch (a) { 00125 case 0: 00126 if (sp<0) { //No se admite valores negativos 00127 sp=0; 00128 } 00129 lcd.locate(2,0); //Ubica el parámetro Set-point 00130 lcd.printf("= "); 00131 lcd.locate(3,0); 00132 lcd.printf("%d",sp--); 00133 break; 00134 00135 case 1: 00136 if (kp<0) { //No se admite valores negativos 00137 kp=0; 00138 } 00139 lcd.locate(10,0); //Ubica el parámetro kp 00140 lcd.printf("= "); 00141 lcd.locate(11,0); 00142 lcd.printf("%d",kp--); 00143 break; 00144 00145 case 2: 00146 if (ki<0) { //No se admite valores negativos 00147 ki=0; 00148 } 00149 lcd.locate(2,1); //Ubica el parámetro ki 00150 lcd.printf("= "); 00151 lcd.locate(3,1); 00152 lcd.printf("%d",ki--); 00153 break; 00154 00155 case 3: 00156 if (kd<0) { //No se admite valores negativos 00157 kd=0; 00158 } 00159 lcd.locate(10,1); //Ubica el parámetro kd 00160 lcd.printf("= "); 00161 lcd.locate(11,1); 00162 lcd.printf("%d",kd--); 00163 break; 00164 } 00165 } 00166 if (bot4.falling()){ //sale del bucle de la pantalla 00167 led1=led2=led3=0; //Flash para salir del bucle 00168 wait(0.25); 00169 led1=led2=led3=1; 00170 break; 00171 } 00172 } 00173 lcd.writeCommand(C4); //Quita el cursor bajo del LCD 00174 lcd.cls(); //borra la pantalla 00175 lcd.printf("GUARDADOS!"); 00176 wait(2); 00177 lcd.cls(); 00178 lcd.printf("INICIA EL PID"); 00179 wait(2); 00180 // se imprimen los parches del control ***************************************** 00181 lcd.cls(); 00182 00183 lcd.printf("Er%d",err); 00184 lcd.locate(8,0); 00185 lcd.printf("Me%d",med); 00186 lcd.locate(0,1); 00187 lcd.printf("Sp%d",sp0); 00188 lcd.locate(8,1); 00189 lcd.printf("Pid%d",pid); 00190 00191 // CICLO PRINCIPAL CONTROLADOR PID 00192 00193 while(1) { 00194 00195 wait(0.001); 00196 //leer puerto analogo y asignar a med 00197 00198 00199 med = (Vin*3.27); 00200 sp0 = sp*0.0327; 00201 err = (sp0-med); 00202 float kp0; 00203 kp0 = kp*0.001; 00204 ap = kp0*err; 00205 float ki0; 00206 ki0 = ki*0.001; 00207 ai = (ki0*err)+ai; //calculo de la integral del error 00208 float kd0; 00209 kd0 = kd*0.0001; 00210 ad = kd0*(err-err_v); //calculo de la accion derivativa 00211 pid = ap+ai+ad; 00212 00213 00214 // se verifica que pid sea menor o igual la valor maximo ***************** 00215 if (pid > 0.999){ 00216 pid=1; 00217 } 00218 00219 // se verifica que pid sea positivo ************************************** 00220 if (pid < 0){ 00221 pid=0; 00222 } 00223 00224 // se verifica que la accion integral no sea muy grande 00225 if (ai > 999){ 00226 ai=1000; 00227 } 00228 00229 Vout=(pid); 00230 //Mostrar resultados PID 00231 if(ciclo>700) { 00232 lcd.locate(2,0); 00233 lcd.printf(" "); 00234 lcd.locate(0,0); 00235 lcd.printf("Er%2.1f",err); 00236 lcd.locate(10,0); 00237 lcd.printf(" "); 00238 lcd.locate(8,0); 00239 lcd.printf("Me%4.2f",med); 00240 lcd.locate(2,1); 00241 lcd.printf(" "); 00242 lcd.locate(0,1); 00243 lcd.printf("Sp%4.2f",sp0); 00244 lcd.locate(10,1); 00245 lcd.printf(" "); 00246 lcd.locate(8,1); 00247 lcd.printf("Pid%4.3f",pid); 00248 ciclo=0; 00249 } 00250 else 00251 ciclo++; 00252 err_v = err; //guarda el error 00253 } // Envía parámetro pid al puerto analogico de salida (D/A) y se repite el ciclo 00254 } 00255
Generated on Fri Jul 29 2022 10:57:41 by 1.7.2