![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
desarrollo de un controlador PID
Dependencies: Debounced TextLCD mbed
Fork of PID by
main.cpp@1:e7782774b135, 2015-10-14 (annotated)
- Committer:
- squinteroz
- Date:
- Wed Oct 14 20:44:27 2015 +0000
- Revision:
- 1:e7782774b135
- Parent:
- 0:f0eb5248ae8f
PID
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
caarbelaezg | 0:f0eb5248ae8f | 1 | #include "mbed.h" |
squinteroz | 1:e7782774b135 | 2 | #include "TextLCD.h" // nos permite usar el lcd en nuestro comando |
squinteroz | 1:e7782774b135 | 3 | #include "DebouncedIn.h" // esta libreria es útil para podeer declarar todas las teclas o botones que se posean |
caarbelaezg | 0:f0eb5248ae8f | 4 | TextLCD lcd(PTE0, PTE1, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 Teclado |
caarbelaezg | 0:f0eb5248ae8f | 5 | |
squinteroz | 1:e7782774b135 | 6 | //asignamos el puerto a cada interruptor |
squinteroz | 1:e7782774b135 | 7 | |
squinteroz | 1:e7782774b135 | 8 | DebouncedIn button1(PTC12); //Aumentar |
squinteroz | 1:e7782774b135 | 9 | DebouncedIn button2(PTC13); //Decrementar |
squinteroz | 1:e7782774b135 | 10 | DebouncedIn button3(PTC16); //Pasar |
squinteroz | 1:e7782774b135 | 11 | DebouncedIn button4(PTC17); //inicio |
squinteroz | 1:e7782774b135 | 12 | |
squinteroz | 1:e7782774b135 | 13 | // asignamos los pines a la salida y a la entrada (analógicas) |
squinteroz | 1:e7782774b135 | 14 | |
caarbelaezg | 0:f0eb5248ae8f | 15 | AnalogIn y(PTB1); // Salida de la planta |
caarbelaezg | 0:f0eb5248ae8f | 16 | AnalogOut u(PTE30); // Señal de control |
caarbelaezg | 0:f0eb5248ae8f | 17 | |
squinteroz | 1:e7782774b135 | 18 | //Salidas digitales utilziadas para la comprobación del funcionamiento |
squinteroz | 1:e7782774b135 | 19 | |
squinteroz | 1:e7782774b135 | 20 | DigitalOut led1(LED1); |
caarbelaezg | 0:f0eb5248ae8f | 21 | DigitalOut led2(LED2); |
caarbelaezg | 0:f0eb5248ae8f | 22 | DigitalOut led3(LED3); |
caarbelaezg | 0:f0eb5248ae8f | 23 | |
squinteroz | 1:e7782774b135 | 24 | Timer t;// nos permite saber cuanto tiempo permanecen presionados los botones asociados al aumento y al decremento |
caarbelaezg | 0:f0eb5248ae8f | 25 | |
squinteroz | 1:e7782774b135 | 26 | int C1=0x0F; // inicializamos el lcd y el tipo de formato que este ultizará |
squinteroz | 1:e7782774b135 | 27 | int sp=0,kp=0,kd=0,ki=0,p=1; // condiciones iniciales de los parametros de controlador. NOTA: la variable p indica en que se parametro se desea intervenir (kp,ki,kd,sp, pasar e iniciar) |
caarbelaezg | 0:f0eb5248ae8f | 28 | float ap, ai, ad, err, med, err_v, pid=0; // |
caarbelaezg | 0:f0eb5248ae8f | 29 | |
caarbelaezg | 0:f0eb5248ae8f | 30 | int main() { |
caarbelaezg | 0:f0eb5248ae8f | 31 | lcd.writeCommand(C1); |
squinteroz | 1:e7782774b135 | 32 | lcd.printf(" Controlador PID\n"); |
caarbelaezg | 0:f0eb5248ae8f | 33 | wait(2); |
caarbelaezg | 0:f0eb5248ae8f | 34 | lcd.cls(); |
squinteroz | 1:e7782774b135 | 35 | lcd.printf(" Equipo_5:\nEliana Mejia E."); |
caarbelaezg | 0:f0eb5248ae8f | 36 | wait(2); |
caarbelaezg | 0:f0eb5248ae8f | 37 | lcd.cls(); |
squinteroz | 1:e7782774b135 | 38 | lcd.printf("Sebastian Quintero Z. Sebastian Vergara P"); |
caarbelaezg | 0:f0eb5248ae8f | 39 | wait(2); |
caarbelaezg | 0:f0eb5248ae8f | 40 | lcd.cls(); |
squinteroz | 1:e7782774b135 | 41 | lcd.printf(" Ingresar Parametros "); |
caarbelaezg | 0:f0eb5248ae8f | 42 | wait(2); |
caarbelaezg | 0:f0eb5248ae8f | 43 | |
caarbelaezg | 0:f0eb5248ae8f | 44 | retorno: //Cuando se presiona por segunda vez el botón 4 se retorna a la interfaz de parametros para modificarlos |
caarbelaezg | 0:f0eb5248ae8f | 45 | p=1; |
caarbelaezg | 0:f0eb5248ae8f | 46 | lcd.cls(); |
caarbelaezg | 0:f0eb5248ae8f | 47 | lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD |
caarbelaezg | 0:f0eb5248ae8f | 48 | lcd.locate(8,0); |
caarbelaezg | 0:f0eb5248ae8f | 49 | lcd.printf("Kp=%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 50 | lcd.locate(0,1); |
caarbelaezg | 0:f0eb5248ae8f | 51 | lcd.printf("Ki=%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 52 | lcd.locate(8,1); |
caarbelaezg | 0:f0eb5248ae8f | 53 | lcd.printf("Kd=%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 54 | lcd.locate(0,0); |
caarbelaezg | 0:f0eb5248ae8f | 55 | lcd.printf("Sp=%d", sp); |
caarbelaezg | 0:f0eb5248ae8f | 56 | |
caarbelaezg | 0:f0eb5248ae8f | 57 | while(1) { |
squinteroz | 1:e7782774b135 | 58 | if (button1.falling()) // se presiona el botón de aumento |
caarbelaezg | 0:f0eb5248ae8f | 59 | { |
caarbelaezg | 0:f0eb5248ae8f | 60 | |
squinteroz | 1:e7782774b135 | 61 | led1 =!led1; // se utiliza para prender el led |
squinteroz | 1:e7782774b135 | 62 | |
squinteroz | 1:e7782774b135 | 63 | // sub menú para la constante sp |
caarbelaezg | 0:f0eb5248ae8f | 64 | if (p==1) |
caarbelaezg | 0:f0eb5248ae8f | 65 | { |
caarbelaezg | 0:f0eb5248ae8f | 66 | |
caarbelaezg | 0:f0eb5248ae8f | 67 | ++sp; |
caarbelaezg | 0:f0eb5248ae8f | 68 | lcd.locate(3,0); |
caarbelaezg | 0:f0eb5248ae8f | 69 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 70 | lcd.locate(3,0); |
caarbelaezg | 0:f0eb5248ae8f | 71 | lcd.printf("%d", sp); |
caarbelaezg | 0:f0eb5248ae8f | 72 | if(sp>=5){ |
squinteroz | 1:e7782774b135 | 73 | sp=5; //El límite de la señal de saida se va a encontrar entre [0V;5V] |
caarbelaezg | 0:f0eb5248ae8f | 74 | lcd.locate(3,0); |
caarbelaezg | 0:f0eb5248ae8f | 75 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 76 | lcd.locate(3,0); |
caarbelaezg | 0:f0eb5248ae8f | 77 | lcd.printf("%d", sp); |
caarbelaezg | 0:f0eb5248ae8f | 78 | } |
caarbelaezg | 0:f0eb5248ae8f | 79 | //A continuación se explica para Kp como funciona el aumento de los parametros, para el resto de parametros |
caarbelaezg | 0:f0eb5248ae8f | 80 | //funciona de la misma manera así que se omiten los comentarios, para el decremento tambien funciona igual |
caarbelaezg | 0:f0eb5248ae8f | 81 | //En este caso se exhibe cuando el tiempo pasa los parametros aumentan de 10 o 100 veces, tambien se puede realizar |
caarbelaezg | 0:f0eb5248ae8f | 82 | // con aumento unitario y más velocidad en la subida del parametro a medida que pasa el tiempo así: |
caarbelaezg | 0:f0eb5248ae8f | 83 | /* |
caarbelaezg | 0:f0eb5248ae8f | 84 | if((t<7) && (t>=1)){ //Primera velocidad de incremento en el intervalo |
caarbelaezg | 0:f0eb5248ae8f | 85 | |
caarbelaezg | 0:f0eb5248ae8f | 86 | wait(0.3); //Cada cierto intervalo de tiempo se cambia el wait por un valor mas pequeño |
caarbelaezg | 0:f0eb5248ae8f | 87 | ++kp; //perceptible al ojo, de modo que aumente mas rapido unitariamente |
caarbelaezg | 0:f0eb5248ae8f | 88 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 89 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 90 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 91 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 92 | } |
caarbelaezg | 0:f0eb5248ae8f | 93 | */ |
caarbelaezg | 0:f0eb5248ae8f | 94 | |
caarbelaezg | 0:f0eb5248ae8f | 95 | |
caarbelaezg | 0:f0eb5248ae8f | 96 | } |
squinteroz | 1:e7782774b135 | 97 | |
squinteroz | 1:e7782774b135 | 98 | // sub menú para la constante kp |
caarbelaezg | 0:f0eb5248ae8f | 99 | else if (p==2) |
caarbelaezg | 0:f0eb5248ae8f | 100 | { |
squinteroz | 1:e7782774b135 | 101 | ++kp; // representa un aumento unitario de la variable |
caarbelaezg | 0:f0eb5248ae8f | 102 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 103 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 104 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 105 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 106 | |
caarbelaezg | 0:f0eb5248ae8f | 107 | |
squinteroz | 1:e7782774b135 | 108 | while(button1==0){ //se ejecuta esta instrucción, mientras el botón se encuentra presionado |
squinteroz | 1:e7782774b135 | 109 | if(t==0){t.start();} //se contabiliza el tiempo que se deja presionado |
caarbelaezg | 0:f0eb5248ae8f | 110 | |
caarbelaezg | 0:f0eb5248ae8f | 111 | if((t<7) && (t>=1)){ //Primera velocidad de incremento en el intervalo |
caarbelaezg | 0:f0eb5248ae8f | 112 | |
squinteroz | 1:e7782774b135 | 113 | wait(0.3); //Espera 0.3 segundos para volver a imprimir en el lcd |
caarbelaezg | 0:f0eb5248ae8f | 114 | ++kp; |
caarbelaezg | 0:f0eb5248ae8f | 115 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 116 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 117 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 118 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 119 | } |
caarbelaezg | 0:f0eb5248ae8f | 120 | |
caarbelaezg | 0:f0eb5248ae8f | 121 | if((t>=7)&&(t<13)){ //Segunda velocidad de incremento |
squinteroz | 1:e7782774b135 | 122 | //Aumento en un factor de 10 |
caarbelaezg | 0:f0eb5248ae8f | 123 | wait(0.3); |
squinteroz | 1:e7782774b135 | 124 | kp=10+kp; |
caarbelaezg | 0:f0eb5248ae8f | 125 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 126 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 127 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 128 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 129 | } |
caarbelaezg | 0:f0eb5248ae8f | 130 | if((t>=13) && (t<19)){ //Tercera velocidad de incremento 100 |
squinteroz | 1:e7782774b135 | 131 | //Aumento en un factor de 100 |
caarbelaezg | 0:f0eb5248ae8f | 132 | wait(0.3); |
squinteroz | 1:e7782774b135 | 133 | kp=100+kp; |
caarbelaezg | 0:f0eb5248ae8f | 134 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 135 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 136 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 137 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 138 | } |
caarbelaezg | 0:f0eb5248ae8f | 139 | |
squinteroz | 1:e7782774b135 | 140 | if(kp>999){ //condición limite que poseen los parámetros |
caarbelaezg | 0:f0eb5248ae8f | 141 | kp=999; |
caarbelaezg | 0:f0eb5248ae8f | 142 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 143 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 144 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 145 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 146 | } |
caarbelaezg | 0:f0eb5248ae8f | 147 | } |
caarbelaezg | 0:f0eb5248ae8f | 148 | t.reset(); //Se reinicia el tiempo y vuelve a incrementar unitariamente |
caarbelaezg | 0:f0eb5248ae8f | 149 | } |
squinteroz | 1:e7782774b135 | 150 | |
squinteroz | 1:e7782774b135 | 151 | // sub menú para la constante ki |
caarbelaezg | 0:f0eb5248ae8f | 152 | else if (p==3) |
caarbelaezg | 0:f0eb5248ae8f | 153 | { |
caarbelaezg | 0:f0eb5248ae8f | 154 | ++ki; |
caarbelaezg | 0:f0eb5248ae8f | 155 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 156 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 157 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 158 | lcd.printf("%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 159 | |
caarbelaezg | 0:f0eb5248ae8f | 160 | |
caarbelaezg | 0:f0eb5248ae8f | 161 | while(button1==0){ |
caarbelaezg | 0:f0eb5248ae8f | 162 | if(t==0){t.start();} |
caarbelaezg | 0:f0eb5248ae8f | 163 | |
caarbelaezg | 0:f0eb5248ae8f | 164 | if((t<7) && (t>=1)){ |
caarbelaezg | 0:f0eb5248ae8f | 165 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 166 | ++ki; |
caarbelaezg | 0:f0eb5248ae8f | 167 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 168 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 169 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 170 | lcd.printf("%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 171 | } |
caarbelaezg | 0:f0eb5248ae8f | 172 | |
caarbelaezg | 0:f0eb5248ae8f | 173 | if((t>=7)&&(t<13)){ |
caarbelaezg | 0:f0eb5248ae8f | 174 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 175 | ki=10+ki; |
caarbelaezg | 0:f0eb5248ae8f | 176 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 177 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 178 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 179 | lcd.printf("%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 180 | } |
caarbelaezg | 0:f0eb5248ae8f | 181 | if((t>=13) && (t<19)){ |
caarbelaezg | 0:f0eb5248ae8f | 182 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 183 | ki=100+ki; |
caarbelaezg | 0:f0eb5248ae8f | 184 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 185 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 186 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 187 | lcd.printf("%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 188 | } |
caarbelaezg | 0:f0eb5248ae8f | 189 | |
caarbelaezg | 0:f0eb5248ae8f | 190 | if(ki>999){ |
caarbelaezg | 0:f0eb5248ae8f | 191 | ki=999; |
caarbelaezg | 0:f0eb5248ae8f | 192 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 193 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 194 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 195 | lcd.printf("%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 196 | } |
caarbelaezg | 0:f0eb5248ae8f | 197 | |
caarbelaezg | 0:f0eb5248ae8f | 198 | } |
caarbelaezg | 0:f0eb5248ae8f | 199 | t.reset(); |
caarbelaezg | 0:f0eb5248ae8f | 200 | |
caarbelaezg | 0:f0eb5248ae8f | 201 | } |
caarbelaezg | 0:f0eb5248ae8f | 202 | |
squinteroz | 1:e7782774b135 | 203 | |
squinteroz | 1:e7782774b135 | 204 | // sub menú para la constante kd |
caarbelaezg | 0:f0eb5248ae8f | 205 | else if (p==4) |
caarbelaezg | 0:f0eb5248ae8f | 206 | { |
caarbelaezg | 0:f0eb5248ae8f | 207 | ++kd; |
caarbelaezg | 0:f0eb5248ae8f | 208 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 209 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 210 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 211 | lcd.printf("%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 212 | |
caarbelaezg | 0:f0eb5248ae8f | 213 | while(button1==0){ |
caarbelaezg | 0:f0eb5248ae8f | 214 | if(t==0){t.start();} |
caarbelaezg | 0:f0eb5248ae8f | 215 | |
caarbelaezg | 0:f0eb5248ae8f | 216 | if((t<7) && (t>=1)){ |
caarbelaezg | 0:f0eb5248ae8f | 217 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 218 | ++kd; |
caarbelaezg | 0:f0eb5248ae8f | 219 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 220 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 221 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 222 | lcd.printf("%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 223 | } |
caarbelaezg | 0:f0eb5248ae8f | 224 | if((t>=7)&&(t<13)){ |
caarbelaezg | 0:f0eb5248ae8f | 225 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 226 | kd=10+kd; |
caarbelaezg | 0:f0eb5248ae8f | 227 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 228 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 229 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 230 | lcd.printf("%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 231 | } |
caarbelaezg | 0:f0eb5248ae8f | 232 | |
caarbelaezg | 0:f0eb5248ae8f | 233 | if((t>=13) && (t<19)){ |
caarbelaezg | 0:f0eb5248ae8f | 234 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 235 | kd=100+kd; |
caarbelaezg | 0:f0eb5248ae8f | 236 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 237 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 238 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 239 | lcd.printf("%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 240 | } |
caarbelaezg | 0:f0eb5248ae8f | 241 | |
caarbelaezg | 0:f0eb5248ae8f | 242 | if(kd>=999){ |
caarbelaezg | 0:f0eb5248ae8f | 243 | |
caarbelaezg | 0:f0eb5248ae8f | 244 | kd=999; |
caarbelaezg | 0:f0eb5248ae8f | 245 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 246 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 247 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 248 | lcd.printf("%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 249 | |
caarbelaezg | 0:f0eb5248ae8f | 250 | } |
caarbelaezg | 0:f0eb5248ae8f | 251 | } |
caarbelaezg | 0:f0eb5248ae8f | 252 | t.reset(); |
caarbelaezg | 0:f0eb5248ae8f | 253 | |
caarbelaezg | 0:f0eb5248ae8f | 254 | } |
caarbelaezg | 0:f0eb5248ae8f | 255 | } |
squinteroz | 1:e7782774b135 | 256 | // accionamiento del botón asociado al decrecimeinto de las constantes |
caarbelaezg | 0:f0eb5248ae8f | 257 | if (button2.falling()) |
caarbelaezg | 0:f0eb5248ae8f | 258 | { |
caarbelaezg | 0:f0eb5248ae8f | 259 | led2 =!led2; |
caarbelaezg | 0:f0eb5248ae8f | 260 | if (p==1) |
caarbelaezg | 0:f0eb5248ae8f | 261 | { |
caarbelaezg | 0:f0eb5248ae8f | 262 | if (sp==0) // no mostrar nada |
caarbelaezg | 0:f0eb5248ae8f | 263 | { |
caarbelaezg | 0:f0eb5248ae8f | 264 | } |
caarbelaezg | 0:f0eb5248ae8f | 265 | else |
caarbelaezg | 0:f0eb5248ae8f | 266 | { |
caarbelaezg | 0:f0eb5248ae8f | 267 | --sp; |
caarbelaezg | 0:f0eb5248ae8f | 268 | lcd.locate(3,0); |
caarbelaezg | 0:f0eb5248ae8f | 269 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 270 | lcd.locate(3,0); |
caarbelaezg | 0:f0eb5248ae8f | 271 | lcd.printf("%d", sp); |
caarbelaezg | 0:f0eb5248ae8f | 272 | |
caarbelaezg | 0:f0eb5248ae8f | 273 | |
caarbelaezg | 0:f0eb5248ae8f | 274 | |
caarbelaezg | 0:f0eb5248ae8f | 275 | } |
caarbelaezg | 0:f0eb5248ae8f | 276 | } |
caarbelaezg | 0:f0eb5248ae8f | 277 | if (p==2) |
caarbelaezg | 0:f0eb5248ae8f | 278 | { |
caarbelaezg | 0:f0eb5248ae8f | 279 | if (kp==0) // no mostrar nada |
caarbelaezg | 0:f0eb5248ae8f | 280 | { |
caarbelaezg | 0:f0eb5248ae8f | 281 | } |
caarbelaezg | 0:f0eb5248ae8f | 282 | else |
caarbelaezg | 0:f0eb5248ae8f | 283 | { |
caarbelaezg | 0:f0eb5248ae8f | 284 | --kp; |
caarbelaezg | 0:f0eb5248ae8f | 285 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 286 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 287 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 288 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 289 | |
caarbelaezg | 0:f0eb5248ae8f | 290 | while(button2==0){ |
caarbelaezg | 0:f0eb5248ae8f | 291 | if(t==0){t.start();} |
caarbelaezg | 0:f0eb5248ae8f | 292 | |
caarbelaezg | 0:f0eb5248ae8f | 293 | if((t<7) && (t>=1)){ |
caarbelaezg | 0:f0eb5248ae8f | 294 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 295 | --kp; |
caarbelaezg | 0:f0eb5248ae8f | 296 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 297 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 298 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 299 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 300 | } |
caarbelaezg | 0:f0eb5248ae8f | 301 | |
caarbelaezg | 0:f0eb5248ae8f | 302 | if((t>=7)&&(t<13)){ |
caarbelaezg | 0:f0eb5248ae8f | 303 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 304 | kp=kp-10; |
caarbelaezg | 0:f0eb5248ae8f | 305 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 306 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 307 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 308 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 309 | } |
caarbelaezg | 0:f0eb5248ae8f | 310 | if((t>=13) && (t<19)){ |
caarbelaezg | 0:f0eb5248ae8f | 311 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 312 | kp=kp-100; |
caarbelaezg | 0:f0eb5248ae8f | 313 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 314 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 315 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 316 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 317 | } |
caarbelaezg | 0:f0eb5248ae8f | 318 | |
caarbelaezg | 0:f0eb5248ae8f | 319 | } |
caarbelaezg | 0:f0eb5248ae8f | 320 | t.reset(); |
caarbelaezg | 0:f0eb5248ae8f | 321 | |
caarbelaezg | 0:f0eb5248ae8f | 322 | } |
caarbelaezg | 0:f0eb5248ae8f | 323 | } |
caarbelaezg | 0:f0eb5248ae8f | 324 | if (p==3) |
caarbelaezg | 0:f0eb5248ae8f | 325 | { |
caarbelaezg | 0:f0eb5248ae8f | 326 | if (ki==0) // no mostrar nada |
caarbelaezg | 0:f0eb5248ae8f | 327 | { |
caarbelaezg | 0:f0eb5248ae8f | 328 | } |
caarbelaezg | 0:f0eb5248ae8f | 329 | else |
caarbelaezg | 0:f0eb5248ae8f | 330 | { |
caarbelaezg | 0:f0eb5248ae8f | 331 | --ki; |
caarbelaezg | 0:f0eb5248ae8f | 332 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 333 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 334 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 335 | lcd.printf("%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 336 | |
caarbelaezg | 0:f0eb5248ae8f | 337 | while(button2==0){ |
caarbelaezg | 0:f0eb5248ae8f | 338 | if(t==0){t.start();} |
caarbelaezg | 0:f0eb5248ae8f | 339 | |
caarbelaezg | 0:f0eb5248ae8f | 340 | if((t<7) && (t>=1)){ |
caarbelaezg | 0:f0eb5248ae8f | 341 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 342 | --ki; |
caarbelaezg | 0:f0eb5248ae8f | 343 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 344 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 345 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 346 | lcd.printf("%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 347 | } |
caarbelaezg | 0:f0eb5248ae8f | 348 | |
caarbelaezg | 0:f0eb5248ae8f | 349 | if((t>=7)&&(t<13)){ |
caarbelaezg | 0:f0eb5248ae8f | 350 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 351 | ki=ki-10; |
caarbelaezg | 0:f0eb5248ae8f | 352 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 353 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 354 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 355 | lcd.printf("%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 356 | } |
caarbelaezg | 0:f0eb5248ae8f | 357 | if((t>=13) && (t<19)){ |
caarbelaezg | 0:f0eb5248ae8f | 358 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 359 | ki=ki-100; |
caarbelaezg | 0:f0eb5248ae8f | 360 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 361 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 362 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 363 | lcd.printf("%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 364 | } |
caarbelaezg | 0:f0eb5248ae8f | 365 | } |
caarbelaezg | 0:f0eb5248ae8f | 366 | t.reset(); |
caarbelaezg | 0:f0eb5248ae8f | 367 | |
caarbelaezg | 0:f0eb5248ae8f | 368 | } |
caarbelaezg | 0:f0eb5248ae8f | 369 | } |
caarbelaezg | 0:f0eb5248ae8f | 370 | if (p==4) |
caarbelaezg | 0:f0eb5248ae8f | 371 | { |
squinteroz | 1:e7782774b135 | 372 | if (kd==0) // no se visualiza nada en el display |
caarbelaezg | 0:f0eb5248ae8f | 373 | { |
caarbelaezg | 0:f0eb5248ae8f | 374 | } |
caarbelaezg | 0:f0eb5248ae8f | 375 | else |
caarbelaezg | 0:f0eb5248ae8f | 376 | { |
caarbelaezg | 0:f0eb5248ae8f | 377 | --kd; |
caarbelaezg | 0:f0eb5248ae8f | 378 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 379 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 380 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 381 | lcd.printf("%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 382 | |
caarbelaezg | 0:f0eb5248ae8f | 383 | while(button2==0){ |
caarbelaezg | 0:f0eb5248ae8f | 384 | if(t==0){t.start();} |
caarbelaezg | 0:f0eb5248ae8f | 385 | |
caarbelaezg | 0:f0eb5248ae8f | 386 | if((t<7) && (t>=1)){ |
caarbelaezg | 0:f0eb5248ae8f | 387 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 388 | --kd; |
caarbelaezg | 0:f0eb5248ae8f | 389 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 390 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 391 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 392 | lcd.printf("%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 393 | } |
caarbelaezg | 0:f0eb5248ae8f | 394 | |
caarbelaezg | 0:f0eb5248ae8f | 395 | if((t>=7)&&(t<13)){ |
caarbelaezg | 0:f0eb5248ae8f | 396 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 397 | kd=kd-10; |
caarbelaezg | 0:f0eb5248ae8f | 398 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 399 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 400 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 401 | lcd.printf("%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 402 | } |
caarbelaezg | 0:f0eb5248ae8f | 403 | if((t>=13) && (t<19)){ |
caarbelaezg | 0:f0eb5248ae8f | 404 | wait(0.3); |
caarbelaezg | 0:f0eb5248ae8f | 405 | kd=kd-100; |
caarbelaezg | 0:f0eb5248ae8f | 406 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 407 | lcd.printf(" "); |
caarbelaezg | 0:f0eb5248ae8f | 408 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 409 | lcd.printf("%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 410 | } |
caarbelaezg | 0:f0eb5248ae8f | 411 | } |
caarbelaezg | 0:f0eb5248ae8f | 412 | t.reset(); |
caarbelaezg | 0:f0eb5248ae8f | 413 | |
caarbelaezg | 0:f0eb5248ae8f | 414 | } |
caarbelaezg | 0:f0eb5248ae8f | 415 | } |
caarbelaezg | 0:f0eb5248ae8f | 416 | } |
squinteroz | 1:e7782774b135 | 417 | // accionamiento del botón pasar |
caarbelaezg | 0:f0eb5248ae8f | 418 | if (button3.falling()) |
caarbelaezg | 0:f0eb5248ae8f | 419 | { |
caarbelaezg | 0:f0eb5248ae8f | 420 | led3 =!led3; |
caarbelaezg | 0:f0eb5248ae8f | 421 | if (p==1) |
caarbelaezg | 0:f0eb5248ae8f | 422 | { |
caarbelaezg | 0:f0eb5248ae8f | 423 | ++p; |
caarbelaezg | 0:f0eb5248ae8f | 424 | lcd.locate(11,0); |
caarbelaezg | 0:f0eb5248ae8f | 425 | lcd.printf("%d", kp); |
caarbelaezg | 0:f0eb5248ae8f | 426 | |
caarbelaezg | 0:f0eb5248ae8f | 427 | |
caarbelaezg | 0:f0eb5248ae8f | 428 | } |
caarbelaezg | 0:f0eb5248ae8f | 429 | else if (p==2) |
caarbelaezg | 0:f0eb5248ae8f | 430 | { |
caarbelaezg | 0:f0eb5248ae8f | 431 | ++p; |
caarbelaezg | 0:f0eb5248ae8f | 432 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 433 | lcd.printf("%d", ki); |
caarbelaezg | 0:f0eb5248ae8f | 434 | lcd.locate(3,1); |
caarbelaezg | 0:f0eb5248ae8f | 435 | |
caarbelaezg | 0:f0eb5248ae8f | 436 | } |
caarbelaezg | 0:f0eb5248ae8f | 437 | else if (p==3) |
caarbelaezg | 0:f0eb5248ae8f | 438 | { |
caarbelaezg | 0:f0eb5248ae8f | 439 | ++p; |
caarbelaezg | 0:f0eb5248ae8f | 440 | lcd.locate(11,1); |
caarbelaezg | 0:f0eb5248ae8f | 441 | lcd.printf("%d", kd); |
caarbelaezg | 0:f0eb5248ae8f | 442 | |
caarbelaezg | 0:f0eb5248ae8f | 443 | |
caarbelaezg | 0:f0eb5248ae8f | 444 | } |
caarbelaezg | 0:f0eb5248ae8f | 445 | else if (p==4) |
caarbelaezg | 0:f0eb5248ae8f | 446 | { |
caarbelaezg | 0:f0eb5248ae8f | 447 | p=1; |
caarbelaezg | 0:f0eb5248ae8f | 448 | lcd.locate(3,0); |
caarbelaezg | 0:f0eb5248ae8f | 449 | lcd.printf("%d", sp); |
caarbelaezg | 0:f0eb5248ae8f | 450 | |
caarbelaezg | 0:f0eb5248ae8f | 451 | |
caarbelaezg | 0:f0eb5248ae8f | 452 | } |
caarbelaezg | 0:f0eb5248ae8f | 453 | } |
squinteroz | 1:e7782774b135 | 454 | // accionamiento del botón asociado al inicio del programa realizado |
squinteroz | 1:e7782774b135 | 455 | |
caarbelaezg | 0:f0eb5248ae8f | 456 | if(button4.falling()){ |
caarbelaezg | 0:f0eb5248ae8f | 457 | p=1; |
squinteroz | 1:e7782774b135 | 458 | lcd.cls();// limpia la pantalla y la localiza en (0,0) |
caarbelaezg | 0:f0eb5248ae8f | 459 | lcd.printf("Iniciando PID\n"); |
caarbelaezg | 0:f0eb5248ae8f | 460 | wait(1); |
squinteroz | 1:e7782774b135 | 461 | while(p==1){ //si se presiona por segunda vez es para modificar los parámetros |
caarbelaezg | 0:f0eb5248ae8f | 462 | if(button4.falling()){ |
caarbelaezg | 0:f0eb5248ae8f | 463 | p=0; |
squinteroz | 1:e7782774b135 | 464 | goto retorno; // se hace un llamado a la función retorno |
caarbelaezg | 0:f0eb5248ae8f | 465 | } |
caarbelaezg | 0:f0eb5248ae8f | 466 | |
squinteroz | 1:e7782774b135 | 467 | med=5*y.read(); // las constantes por la cuál se amplifica la señal dependen del sistema que se desee controlar y en donde lo deseamos limitar |
squinteroz | 1:e7782774b135 | 468 | err=sp-med; // error del sistema |
caarbelaezg | 0:f0eb5248ae8f | 469 | ap = kp*err; |
squinteroz | 1:e7782774b135 | 470 | |
caarbelaezg | 0:f0eb5248ae8f | 471 | // se verifica que la accion integral no sea muy grande |
squinteroz | 1:e7782774b135 | 472 | |
caarbelaezg | 0:f0eb5248ae8f | 473 | if(ai<5) |
caarbelaezg | 0:f0eb5248ae8f | 474 | { |
squinteroz | 1:e7782774b135 | 475 | ai =(ki*err)+ai; //calculo de la integral del error |
caarbelaezg | 0:f0eb5248ae8f | 476 | } |
squinteroz | 1:e7782774b135 | 477 | ad = kd*(err-err_v); //calculo de la acción derivativa |
caarbelaezg | 0:f0eb5248ae8f | 478 | pid = (ap+ai+ad); |
squinteroz | 1:e7782774b135 | 479 | |
caarbelaezg | 0:f0eb5248ae8f | 480 | // se verifica que pid sea positivo ************************************** |
caarbelaezg | 0:f0eb5248ae8f | 481 | if(pid<=0){pid=0;} |
squinteroz | 1:e7782774b135 | 482 | |
squinteroz | 1:e7782774b135 | 483 | // se verifica que pid sea menor o igual l valor maximo ***************** |
squinteroz | 1:e7782774b135 | 484 | |
caarbelaezg | 0:f0eb5248ae8f | 485 | if(pid>=5){pid=5;} |
squinteroz | 1:e7782774b135 | 486 | u.write(pid); // enviamos el valor de PID al puerto analógico de salida |
caarbelaezg | 0:f0eb5248ae8f | 487 | t.start(); |
squinteroz | 1:e7782774b135 | 488 | |
caarbelaezg | 0:f0eb5248ae8f | 489 | if(t>=1){ |
caarbelaezg | 0:f0eb5248ae8f | 490 | lcd.cls(); |
squinteroz | 1:e7782774b135 | 491 | lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD en este caso modulo 1 |
caarbelaezg | 0:f0eb5248ae8f | 492 | lcd.locate(8,0); |
caarbelaezg | 0:f0eb5248ae8f | 493 | lcd.printf("Err=%0.1f", err); |
caarbelaezg | 0:f0eb5248ae8f | 494 | lcd.locate(0,1); |
caarbelaezg | 0:f0eb5248ae8f | 495 | lcd.printf("Med=%0.1f", med); |
caarbelaezg | 0:f0eb5248ae8f | 496 | lcd.locate(8,1); |
caarbelaezg | 0:f0eb5248ae8f | 497 | lcd.printf("PID=%0.1f", pid); |
caarbelaezg | 0:f0eb5248ae8f | 498 | lcd.locate(0,0); |
caarbelaezg | 0:f0eb5248ae8f | 499 | lcd.printf("Sp=%d", sp); |
caarbelaezg | 0:f0eb5248ae8f | 500 | |
caarbelaezg | 0:f0eb5248ae8f | 501 | t.reset(); |
caarbelaezg | 0:f0eb5248ae8f | 502 | |
caarbelaezg | 0:f0eb5248ae8f | 503 | } |
caarbelaezg | 0:f0eb5248ae8f | 504 | } |
caarbelaezg | 0:f0eb5248ae8f | 505 | |
caarbelaezg | 0:f0eb5248ae8f | 506 | |
caarbelaezg | 0:f0eb5248ae8f | 507 | } |
caarbelaezg | 0:f0eb5248ae8f | 508 | } |
caarbelaezg | 0:f0eb5248ae8f | 509 | |
caarbelaezg | 0:f0eb5248ae8f | 510 | |
caarbelaezg | 0:f0eb5248ae8f | 511 | } |