El siguiente programa presenta un PID al cual se le pueden modificar los parámetros, para ello se han usado cuatro pulsadores.

Dependencies:   Debounced TextLCD mbed

Committer:
caarbelaezg
Date:
Wed Apr 08 23:46:50 2015 +0000
Revision:
0:f0eb5248ae8f
PID_BOTONES

Who changed what in which revision?

UserRevisionLine numberNew contents of line
caarbelaezg 0:f0eb5248ae8f 1 #include "mbed.h"
caarbelaezg 0:f0eb5248ae8f 2 #include "TextLCD.h"
caarbelaezg 0:f0eb5248ae8f 3 #include "DebouncedIn.h"
caarbelaezg 0:f0eb5248ae8f 4
caarbelaezg 0:f0eb5248ae8f 5 TextLCD lcd(PTE0, PTE1, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 Teclado
caarbelaezg 0:f0eb5248ae8f 6
caarbelaezg 0:f0eb5248ae8f 7 DebouncedIn button1(PTC12); //Botón Aumento
caarbelaezg 0:f0eb5248ae8f 8 DebouncedIn button2(PTC13); //Botón Decremento
caarbelaezg 0:f0eb5248ae8f 9 DebouncedIn button3(PTC16); //Botón de pasar
caarbelaezg 0:f0eb5248ae8f 10 DebouncedIn button4(PTC17); //Botón para iniciar el PID
caarbelaezg 0:f0eb5248ae8f 11
caarbelaezg 0:f0eb5248ae8f 12 AnalogIn y(PTB1); // Salida de la planta
caarbelaezg 0:f0eb5248ae8f 13 AnalogOut u(PTE30); // Señal de control
caarbelaezg 0:f0eb5248ae8f 14
caarbelaezg 0:f0eb5248ae8f 15 DigitalOut led1(LED1); // Salidas para comprobar el funcionamiento del programa
caarbelaezg 0:f0eb5248ae8f 16 DigitalOut led2(LED2);
caarbelaezg 0:f0eb5248ae8f 17 DigitalOut led3(LED3);
caarbelaezg 0:f0eb5248ae8f 18
caarbelaezg 0:f0eb5248ae8f 19 Timer t; //Para cuantizar el tiempoque se deja undido un botón de aumento o decremento
caarbelaezg 0:f0eb5248ae8f 20
caarbelaezg 0:f0eb5248ae8f 21 int C1=0x0F; // Codigo para escribir y matener el el LCD
caarbelaezg 0:f0eb5248ae8f 22 int sp=0,kp=0,kd=0,ki=0,p=1; // Inicializacion de las variables para el controlador
caarbelaezg 0:f0eb5248ae8f 23 float ap, ai, ad, err, med, err_v, pid=0; //
caarbelaezg 0:f0eb5248ae8f 24
caarbelaezg 0:f0eb5248ae8f 25 int main() {
caarbelaezg 0:f0eb5248ae8f 26 lcd.writeCommand(C1);
caarbelaezg 0:f0eb5248ae8f 27 lcd.printf(" CONTROL PID\n");
caarbelaezg 0:f0eb5248ae8f 28 wait(2);
caarbelaezg 0:f0eb5248ae8f 29 lcd.cls();
caarbelaezg 0:f0eb5248ae8f 30 lcd.printf(" EQUIPO1:\nJuan Jose Munoz");
caarbelaezg 0:f0eb5248ae8f 31 wait(2);
caarbelaezg 0:f0eb5248ae8f 32 lcd.cls();
caarbelaezg 0:f0eb5248ae8f 33 lcd.printf("Carlos Arbelaez Rafael Barriento");
caarbelaezg 0:f0eb5248ae8f 34 wait(2);
caarbelaezg 0:f0eb5248ae8f 35 lcd.cls();
caarbelaezg 0:f0eb5248ae8f 36 lcd.printf(" INGRESO DE\n PARAMETROS ");
caarbelaezg 0:f0eb5248ae8f 37 wait(2);
caarbelaezg 0:f0eb5248ae8f 38
caarbelaezg 0:f0eb5248ae8f 39 retorno: //Cuando se presiona por segunda vez el botón 4 se retorna a la interfaz de parametros para modificarlos
caarbelaezg 0:f0eb5248ae8f 40 p=1;
caarbelaezg 0:f0eb5248ae8f 41 lcd.cls();
caarbelaezg 0:f0eb5248ae8f 42 lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
caarbelaezg 0:f0eb5248ae8f 43 lcd.locate(8,0);
caarbelaezg 0:f0eb5248ae8f 44 lcd.printf("Kp=%d", kp);
caarbelaezg 0:f0eb5248ae8f 45 lcd.locate(0,1);
caarbelaezg 0:f0eb5248ae8f 46 lcd.printf("Ki=%d", ki);
caarbelaezg 0:f0eb5248ae8f 47 lcd.locate(8,1);
caarbelaezg 0:f0eb5248ae8f 48 lcd.printf("Kd=%d", kd);
caarbelaezg 0:f0eb5248ae8f 49 lcd.locate(0,0);
caarbelaezg 0:f0eb5248ae8f 50 lcd.printf("Sp=%d", sp);
caarbelaezg 0:f0eb5248ae8f 51
caarbelaezg 0:f0eb5248ae8f 52 while(1) {
caarbelaezg 0:f0eb5248ae8f 53 if (button1.falling())
caarbelaezg 0:f0eb5248ae8f 54 {
caarbelaezg 0:f0eb5248ae8f 55
caarbelaezg 0:f0eb5248ae8f 56 led1 =!led1;
caarbelaezg 0:f0eb5248ae8f 57 if (p==1)
caarbelaezg 0:f0eb5248ae8f 58 {
caarbelaezg 0:f0eb5248ae8f 59
caarbelaezg 0:f0eb5248ae8f 60 ++sp;
caarbelaezg 0:f0eb5248ae8f 61 lcd.locate(3,0);
caarbelaezg 0:f0eb5248ae8f 62 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 63 lcd.locate(3,0);
caarbelaezg 0:f0eb5248ae8f 64 lcd.printf("%d", sp);
caarbelaezg 0:f0eb5248ae8f 65 if(sp>=5){
caarbelaezg 0:f0eb5248ae8f 66 sp=5; //saturamos el setpoint hasta 5 segun la escala medida ya que trabajaremos entre 0V y 5V
caarbelaezg 0:f0eb5248ae8f 67 lcd.locate(3,0);
caarbelaezg 0:f0eb5248ae8f 68 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 69 lcd.locate(3,0);
caarbelaezg 0:f0eb5248ae8f 70 lcd.printf("%d", sp);
caarbelaezg 0:f0eb5248ae8f 71 }
caarbelaezg 0:f0eb5248ae8f 72 //A continuación se explica para Kp como funciona el aumento de los parametros, para el resto de parametros
caarbelaezg 0:f0eb5248ae8f 73 //funciona de la misma manera así que se omiten los comentarios, para el decremento tambien funciona igual
caarbelaezg 0:f0eb5248ae8f 74 //En este caso se exhibe cuando el tiempo pasa los parametros aumentan de 10 o 100 veces, tambien se puede realizar
caarbelaezg 0:f0eb5248ae8f 75 // con aumento unitario y más velocidad en la subida del parametro a medida que pasa el tiempo así:
caarbelaezg 0:f0eb5248ae8f 76 /*
caarbelaezg 0:f0eb5248ae8f 77 if((t<7) && (t>=1)){ //Primera velocidad de incremento en el intervalo
caarbelaezg 0:f0eb5248ae8f 78
caarbelaezg 0:f0eb5248ae8f 79 wait(0.3); //Cada cierto intervalo de tiempo se cambia el wait por un valor mas pequeño
caarbelaezg 0:f0eb5248ae8f 80 ++kp; //perceptible al ojo, de modo que aumente mas rapido unitariamente
caarbelaezg 0:f0eb5248ae8f 81 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 82 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 83 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 84 lcd.printf("%d", kp);
caarbelaezg 0:f0eb5248ae8f 85 }
caarbelaezg 0:f0eb5248ae8f 86 */
caarbelaezg 0:f0eb5248ae8f 87
caarbelaezg 0:f0eb5248ae8f 88
caarbelaezg 0:f0eb5248ae8f 89 }
caarbelaezg 0:f0eb5248ae8f 90 else if (p==2)
caarbelaezg 0:f0eb5248ae8f 91 {
caarbelaezg 0:f0eb5248ae8f 92 ++kp; //Aumento unitario de la constante
caarbelaezg 0:f0eb5248ae8f 93 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 94 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 95 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 96 lcd.printf("%d", kp);
caarbelaezg 0:f0eb5248ae8f 97
caarbelaezg 0:f0eb5248ae8f 98
caarbelaezg 0:f0eb5248ae8f 99 while(button1==0){ //Verifica si el botón 1 se deja presionado
caarbelaezg 0:f0eb5248ae8f 100 if(t==0){t.start();} //Inicia a contar el tiempo que se deja presionado
caarbelaezg 0:f0eb5248ae8f 101
caarbelaezg 0:f0eb5248ae8f 102 if((t<7) && (t>=1)){ //Primera velocidad de incremento en el intervalo
caarbelaezg 0:f0eb5248ae8f 103
caarbelaezg 0:f0eb5248ae8f 104 wait(0.3); //Espera 0.3 segundos para volver a imprimir
caarbelaezg 0:f0eb5248ae8f 105 ++kp;
caarbelaezg 0:f0eb5248ae8f 106 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 107 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 108 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 109 lcd.printf("%d", kp);
caarbelaezg 0:f0eb5248ae8f 110 }
caarbelaezg 0:f0eb5248ae8f 111
caarbelaezg 0:f0eb5248ae8f 112 if((t>=7)&&(t<13)){ //Segunda velocidad de incremento
caarbelaezg 0:f0eb5248ae8f 113
caarbelaezg 0:f0eb5248ae8f 114 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 115 kp=10+kp; //ahora aumenta de 10 en 10
caarbelaezg 0:f0eb5248ae8f 116 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 117 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 118 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 119 lcd.printf("%d", kp);
caarbelaezg 0:f0eb5248ae8f 120 }
caarbelaezg 0:f0eb5248ae8f 121 if((t>=13) && (t<19)){ //Tercera velocidad de incremento 100
caarbelaezg 0:f0eb5248ae8f 122
caarbelaezg 0:f0eb5248ae8f 123 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 124 kp=100+kp; //Aumenta de 100 en 100
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
caarbelaezg 0:f0eb5248ae8f 131 if(kp>999){ //Saturación máxima de los parametos
caarbelaezg 0:f0eb5248ae8f 132 kp=999;
caarbelaezg 0:f0eb5248ae8f 133 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 134 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 135 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 136 lcd.printf("%d", kp);
caarbelaezg 0:f0eb5248ae8f 137 }
caarbelaezg 0:f0eb5248ae8f 138 }
caarbelaezg 0:f0eb5248ae8f 139 t.reset(); //Se reinicia el tiempo y vuelve a incrementar unitariamente
caarbelaezg 0:f0eb5248ae8f 140 }
caarbelaezg 0:f0eb5248ae8f 141 else if (p==3)
caarbelaezg 0:f0eb5248ae8f 142 {
caarbelaezg 0:f0eb5248ae8f 143 ++ki;
caarbelaezg 0:f0eb5248ae8f 144 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 145 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 146 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 147 lcd.printf("%d", ki);
caarbelaezg 0:f0eb5248ae8f 148
caarbelaezg 0:f0eb5248ae8f 149
caarbelaezg 0:f0eb5248ae8f 150 while(button1==0){
caarbelaezg 0:f0eb5248ae8f 151 if(t==0){t.start();}
caarbelaezg 0:f0eb5248ae8f 152
caarbelaezg 0:f0eb5248ae8f 153 if((t<7) && (t>=1)){
caarbelaezg 0:f0eb5248ae8f 154 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 155 ++ki;
caarbelaezg 0:f0eb5248ae8f 156 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 157 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 158 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 159 lcd.printf("%d", ki);
caarbelaezg 0:f0eb5248ae8f 160 }
caarbelaezg 0:f0eb5248ae8f 161
caarbelaezg 0:f0eb5248ae8f 162 if((t>=7)&&(t<13)){
caarbelaezg 0:f0eb5248ae8f 163 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 164 ki=10+ki;
caarbelaezg 0:f0eb5248ae8f 165 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 166 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 167 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 168 lcd.printf("%d", ki);
caarbelaezg 0:f0eb5248ae8f 169 }
caarbelaezg 0:f0eb5248ae8f 170 if((t>=13) && (t<19)){
caarbelaezg 0:f0eb5248ae8f 171 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 172 ki=100+ki;
caarbelaezg 0:f0eb5248ae8f 173 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 174 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 175 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 176 lcd.printf("%d", ki);
caarbelaezg 0:f0eb5248ae8f 177 }
caarbelaezg 0:f0eb5248ae8f 178
caarbelaezg 0:f0eb5248ae8f 179 if(ki>999){
caarbelaezg 0:f0eb5248ae8f 180 ki=999;
caarbelaezg 0:f0eb5248ae8f 181 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 182 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 183 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 184 lcd.printf("%d", ki);
caarbelaezg 0:f0eb5248ae8f 185 }
caarbelaezg 0:f0eb5248ae8f 186
caarbelaezg 0:f0eb5248ae8f 187 }
caarbelaezg 0:f0eb5248ae8f 188 t.reset();
caarbelaezg 0:f0eb5248ae8f 189
caarbelaezg 0:f0eb5248ae8f 190 }
caarbelaezg 0:f0eb5248ae8f 191
caarbelaezg 0:f0eb5248ae8f 192
caarbelaezg 0:f0eb5248ae8f 193 else if (p==4)
caarbelaezg 0:f0eb5248ae8f 194 {
caarbelaezg 0:f0eb5248ae8f 195 ++kd;
caarbelaezg 0:f0eb5248ae8f 196 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 197 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 198 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 199 lcd.printf("%d", kd);
caarbelaezg 0:f0eb5248ae8f 200
caarbelaezg 0:f0eb5248ae8f 201 while(button1==0){
caarbelaezg 0:f0eb5248ae8f 202 if(t==0){t.start();}
caarbelaezg 0:f0eb5248ae8f 203
caarbelaezg 0:f0eb5248ae8f 204 if((t<7) && (t>=1)){
caarbelaezg 0:f0eb5248ae8f 205 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 206 ++kd;
caarbelaezg 0:f0eb5248ae8f 207 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 208 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 209 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 210 lcd.printf("%d", kd);
caarbelaezg 0:f0eb5248ae8f 211 }
caarbelaezg 0:f0eb5248ae8f 212 if((t>=7)&&(t<13)){
caarbelaezg 0:f0eb5248ae8f 213 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 214 kd=10+kd;
caarbelaezg 0:f0eb5248ae8f 215 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 216 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 217 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 218 lcd.printf("%d", kd);
caarbelaezg 0:f0eb5248ae8f 219 }
caarbelaezg 0:f0eb5248ae8f 220
caarbelaezg 0:f0eb5248ae8f 221 if((t>=13) && (t<19)){
caarbelaezg 0:f0eb5248ae8f 222 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 223 kd=100+kd;
caarbelaezg 0:f0eb5248ae8f 224 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 225 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 226 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 227 lcd.printf("%d", kd);
caarbelaezg 0:f0eb5248ae8f 228 }
caarbelaezg 0:f0eb5248ae8f 229
caarbelaezg 0:f0eb5248ae8f 230 if(kd>=999){
caarbelaezg 0:f0eb5248ae8f 231
caarbelaezg 0:f0eb5248ae8f 232 kd=999;
caarbelaezg 0:f0eb5248ae8f 233 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 234 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 235 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 236 lcd.printf("%d", kd);
caarbelaezg 0:f0eb5248ae8f 237
caarbelaezg 0:f0eb5248ae8f 238 }
caarbelaezg 0:f0eb5248ae8f 239 }
caarbelaezg 0:f0eb5248ae8f 240 t.reset();
caarbelaezg 0:f0eb5248ae8f 241
caarbelaezg 0:f0eb5248ae8f 242 }
caarbelaezg 0:f0eb5248ae8f 243 }
caarbelaezg 0:f0eb5248ae8f 244
caarbelaezg 0:f0eb5248ae8f 245 if (button2.falling())
caarbelaezg 0:f0eb5248ae8f 246 {
caarbelaezg 0:f0eb5248ae8f 247 led2 =!led2;
caarbelaezg 0:f0eb5248ae8f 248 if (p==1)
caarbelaezg 0:f0eb5248ae8f 249 {
caarbelaezg 0:f0eb5248ae8f 250 if (sp==0) // no mostrar nada
caarbelaezg 0:f0eb5248ae8f 251 {
caarbelaezg 0:f0eb5248ae8f 252 }
caarbelaezg 0:f0eb5248ae8f 253 else
caarbelaezg 0:f0eb5248ae8f 254 {
caarbelaezg 0:f0eb5248ae8f 255 --sp;
caarbelaezg 0:f0eb5248ae8f 256 lcd.locate(3,0);
caarbelaezg 0:f0eb5248ae8f 257 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 258 lcd.locate(3,0);
caarbelaezg 0:f0eb5248ae8f 259 lcd.printf("%d", sp);
caarbelaezg 0:f0eb5248ae8f 260
caarbelaezg 0:f0eb5248ae8f 261
caarbelaezg 0:f0eb5248ae8f 262
caarbelaezg 0:f0eb5248ae8f 263 }
caarbelaezg 0:f0eb5248ae8f 264 }
caarbelaezg 0:f0eb5248ae8f 265 if (p==2)
caarbelaezg 0:f0eb5248ae8f 266 {
caarbelaezg 0:f0eb5248ae8f 267 if (kp==0) // no mostrar nada
caarbelaezg 0:f0eb5248ae8f 268 {
caarbelaezg 0:f0eb5248ae8f 269 }
caarbelaezg 0:f0eb5248ae8f 270 else
caarbelaezg 0:f0eb5248ae8f 271 {
caarbelaezg 0:f0eb5248ae8f 272 --kp;
caarbelaezg 0:f0eb5248ae8f 273 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 274 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 275 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 276 lcd.printf("%d", kp);
caarbelaezg 0:f0eb5248ae8f 277
caarbelaezg 0:f0eb5248ae8f 278 while(button2==0){
caarbelaezg 0:f0eb5248ae8f 279 if(t==0){t.start();}
caarbelaezg 0:f0eb5248ae8f 280
caarbelaezg 0:f0eb5248ae8f 281 if((t<7) && (t>=1)){
caarbelaezg 0:f0eb5248ae8f 282 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 283 --kp;
caarbelaezg 0:f0eb5248ae8f 284 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 285 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 286 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 287 lcd.printf("%d", kp);
caarbelaezg 0:f0eb5248ae8f 288 }
caarbelaezg 0:f0eb5248ae8f 289
caarbelaezg 0:f0eb5248ae8f 290 if((t>=7)&&(t<13)){
caarbelaezg 0:f0eb5248ae8f 291 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 292 kp=kp-10;
caarbelaezg 0:f0eb5248ae8f 293 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 294 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 295 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 296 lcd.printf("%d", kp);
caarbelaezg 0:f0eb5248ae8f 297 }
caarbelaezg 0:f0eb5248ae8f 298 if((t>=13) && (t<19)){
caarbelaezg 0:f0eb5248ae8f 299 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 300 kp=kp-100;
caarbelaezg 0:f0eb5248ae8f 301 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 302 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 303 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 304 lcd.printf("%d", kp);
caarbelaezg 0:f0eb5248ae8f 305 }
caarbelaezg 0:f0eb5248ae8f 306
caarbelaezg 0:f0eb5248ae8f 307 }
caarbelaezg 0:f0eb5248ae8f 308 t.reset();
caarbelaezg 0:f0eb5248ae8f 309
caarbelaezg 0:f0eb5248ae8f 310 }
caarbelaezg 0:f0eb5248ae8f 311 }
caarbelaezg 0:f0eb5248ae8f 312 if (p==3)
caarbelaezg 0:f0eb5248ae8f 313 {
caarbelaezg 0:f0eb5248ae8f 314 if (ki==0) // no mostrar nada
caarbelaezg 0:f0eb5248ae8f 315 {
caarbelaezg 0:f0eb5248ae8f 316 }
caarbelaezg 0:f0eb5248ae8f 317 else
caarbelaezg 0:f0eb5248ae8f 318 {
caarbelaezg 0:f0eb5248ae8f 319 --ki;
caarbelaezg 0:f0eb5248ae8f 320 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 321 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 322 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 323 lcd.printf("%d", ki);
caarbelaezg 0:f0eb5248ae8f 324
caarbelaezg 0:f0eb5248ae8f 325 while(button2==0){
caarbelaezg 0:f0eb5248ae8f 326 if(t==0){t.start();}
caarbelaezg 0:f0eb5248ae8f 327
caarbelaezg 0:f0eb5248ae8f 328 if((t<7) && (t>=1)){
caarbelaezg 0:f0eb5248ae8f 329 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 330 --ki;
caarbelaezg 0:f0eb5248ae8f 331 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 332 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 333 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 334 lcd.printf("%d", ki);
caarbelaezg 0:f0eb5248ae8f 335 }
caarbelaezg 0:f0eb5248ae8f 336
caarbelaezg 0:f0eb5248ae8f 337 if((t>=7)&&(t<13)){
caarbelaezg 0:f0eb5248ae8f 338 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 339 ki=ki-10;
caarbelaezg 0:f0eb5248ae8f 340 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 341 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 342 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 343 lcd.printf("%d", ki);
caarbelaezg 0:f0eb5248ae8f 344 }
caarbelaezg 0:f0eb5248ae8f 345 if((t>=13) && (t<19)){
caarbelaezg 0:f0eb5248ae8f 346 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 347 ki=ki-100;
caarbelaezg 0:f0eb5248ae8f 348 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 349 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 350 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 351 lcd.printf("%d", ki);
caarbelaezg 0:f0eb5248ae8f 352 }
caarbelaezg 0:f0eb5248ae8f 353 }
caarbelaezg 0:f0eb5248ae8f 354 t.reset();
caarbelaezg 0:f0eb5248ae8f 355
caarbelaezg 0:f0eb5248ae8f 356 }
caarbelaezg 0:f0eb5248ae8f 357 }
caarbelaezg 0:f0eb5248ae8f 358 if (p==4)
caarbelaezg 0:f0eb5248ae8f 359 {
caarbelaezg 0:f0eb5248ae8f 360 if (kd==0) // no mostrar nada
caarbelaezg 0:f0eb5248ae8f 361 {
caarbelaezg 0:f0eb5248ae8f 362 }
caarbelaezg 0:f0eb5248ae8f 363 else
caarbelaezg 0:f0eb5248ae8f 364 {
caarbelaezg 0:f0eb5248ae8f 365 --kd;
caarbelaezg 0:f0eb5248ae8f 366 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 367 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 368 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 369 lcd.printf("%d", kd);
caarbelaezg 0:f0eb5248ae8f 370
caarbelaezg 0:f0eb5248ae8f 371 while(button2==0){
caarbelaezg 0:f0eb5248ae8f 372 if(t==0){t.start();}
caarbelaezg 0:f0eb5248ae8f 373
caarbelaezg 0:f0eb5248ae8f 374 if((t<7) && (t>=1)){
caarbelaezg 0:f0eb5248ae8f 375 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 376 --kd;
caarbelaezg 0:f0eb5248ae8f 377 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 378 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 379 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 380 lcd.printf("%d", kd);
caarbelaezg 0:f0eb5248ae8f 381 }
caarbelaezg 0:f0eb5248ae8f 382
caarbelaezg 0:f0eb5248ae8f 383 if((t>=7)&&(t<13)){
caarbelaezg 0:f0eb5248ae8f 384 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 385 kd=kd-10;
caarbelaezg 0:f0eb5248ae8f 386 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 387 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 388 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 389 lcd.printf("%d", kd);
caarbelaezg 0:f0eb5248ae8f 390 }
caarbelaezg 0:f0eb5248ae8f 391 if((t>=13) && (t<19)){
caarbelaezg 0:f0eb5248ae8f 392 wait(0.3);
caarbelaezg 0:f0eb5248ae8f 393 kd=kd-100;
caarbelaezg 0:f0eb5248ae8f 394 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 395 lcd.printf(" ");
caarbelaezg 0:f0eb5248ae8f 396 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 397 lcd.printf("%d", kd);
caarbelaezg 0:f0eb5248ae8f 398 }
caarbelaezg 0:f0eb5248ae8f 399 }
caarbelaezg 0:f0eb5248ae8f 400 t.reset();
caarbelaezg 0:f0eb5248ae8f 401
caarbelaezg 0:f0eb5248ae8f 402 }
caarbelaezg 0:f0eb5248ae8f 403 }
caarbelaezg 0:f0eb5248ae8f 404 }
caarbelaezg 0:f0eb5248ae8f 405 if (button3.falling())
caarbelaezg 0:f0eb5248ae8f 406 {
caarbelaezg 0:f0eb5248ae8f 407 led3 =!led3;
caarbelaezg 0:f0eb5248ae8f 408 if (p==1)
caarbelaezg 0:f0eb5248ae8f 409 {
caarbelaezg 0:f0eb5248ae8f 410 ++p;
caarbelaezg 0:f0eb5248ae8f 411 lcd.locate(11,0);
caarbelaezg 0:f0eb5248ae8f 412 lcd.printf("%d", kp);
caarbelaezg 0:f0eb5248ae8f 413
caarbelaezg 0:f0eb5248ae8f 414
caarbelaezg 0:f0eb5248ae8f 415 }
caarbelaezg 0:f0eb5248ae8f 416 else if (p==2)
caarbelaezg 0:f0eb5248ae8f 417 {
caarbelaezg 0:f0eb5248ae8f 418 ++p;
caarbelaezg 0:f0eb5248ae8f 419 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 420 lcd.printf("%d", ki);
caarbelaezg 0:f0eb5248ae8f 421 lcd.locate(3,1);
caarbelaezg 0:f0eb5248ae8f 422
caarbelaezg 0:f0eb5248ae8f 423 }
caarbelaezg 0:f0eb5248ae8f 424 else if (p==3)
caarbelaezg 0:f0eb5248ae8f 425 {
caarbelaezg 0:f0eb5248ae8f 426 ++p;
caarbelaezg 0:f0eb5248ae8f 427 lcd.locate(11,1);
caarbelaezg 0:f0eb5248ae8f 428 lcd.printf("%d", kd);
caarbelaezg 0:f0eb5248ae8f 429
caarbelaezg 0:f0eb5248ae8f 430
caarbelaezg 0:f0eb5248ae8f 431 }
caarbelaezg 0:f0eb5248ae8f 432 else if (p==4)
caarbelaezg 0:f0eb5248ae8f 433 {
caarbelaezg 0:f0eb5248ae8f 434 p=1;
caarbelaezg 0:f0eb5248ae8f 435 lcd.locate(3,0);
caarbelaezg 0:f0eb5248ae8f 436 lcd.printf("%d", sp);
caarbelaezg 0:f0eb5248ae8f 437
caarbelaezg 0:f0eb5248ae8f 438
caarbelaezg 0:f0eb5248ae8f 439 }
caarbelaezg 0:f0eb5248ae8f 440 }
caarbelaezg 0:f0eb5248ae8f 441
caarbelaezg 0:f0eb5248ae8f 442 if(button4.falling()){
caarbelaezg 0:f0eb5248ae8f 443 p=1;
caarbelaezg 0:f0eb5248ae8f 444 lcd.cls();
caarbelaezg 0:f0eb5248ae8f 445 lcd.printf("Iniciando PID\n");
caarbelaezg 0:f0eb5248ae8f 446 wait(1);
caarbelaezg 0:f0eb5248ae8f 447 while(p==1){ //La segunda vez que se presiona es para volver a la interfaz para modificar parametros
caarbelaezg 0:f0eb5248ae8f 448 if(button4.falling()){
caarbelaezg 0:f0eb5248ae8f 449 p=0;
caarbelaezg 0:f0eb5248ae8f 450 goto retorno;
caarbelaezg 0:f0eb5248ae8f 451 }
caarbelaezg 0:f0eb5248ae8f 452
caarbelaezg 0:f0eb5248ae8f 453 med=5*y.read();
caarbelaezg 0:f0eb5248ae8f 454 err=sp-med;
caarbelaezg 0:f0eb5248ae8f 455 ap = kp*err;
caarbelaezg 0:f0eb5248ae8f 456 // se verifica que la accion integral no sea muy grande
caarbelaezg 0:f0eb5248ae8f 457 if(ai<5)
caarbelaezg 0:f0eb5248ae8f 458 {
caarbelaezg 0:f0eb5248ae8f 459 ai =(ki*err)+ai; //calculo de la integral del error
caarbelaezg 0:f0eb5248ae8f 460 }
caarbelaezg 0:f0eb5248ae8f 461 ad = kd*(err-err_v); //calculo de la accion derivativa
caarbelaezg 0:f0eb5248ae8f 462 pid = (ap+ai+ad);
caarbelaezg 0:f0eb5248ae8f 463 // se verifica que pid sea positivo **************************************
caarbelaezg 0:f0eb5248ae8f 464 if(pid<=0){pid=0;}
caarbelaezg 0:f0eb5248ae8f 465 // se verifica que pid sea menor o igual la valor maximo *****************
caarbelaezg 0:f0eb5248ae8f 466 if(pid>=5){pid=5;}
caarbelaezg 0:f0eb5248ae8f 467 u.write(pid);
caarbelaezg 0:f0eb5248ae8f 468 t.start();
caarbelaezg 0:f0eb5248ae8f 469 if(t>=1){
caarbelaezg 0:f0eb5248ae8f 470 lcd.cls();
caarbelaezg 0:f0eb5248ae8f 471 lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
caarbelaezg 0:f0eb5248ae8f 472 lcd.locate(8,0);
caarbelaezg 0:f0eb5248ae8f 473 lcd.printf("Err=%0.1f", err);
caarbelaezg 0:f0eb5248ae8f 474 lcd.locate(0,1);
caarbelaezg 0:f0eb5248ae8f 475 lcd.printf("Med=%0.1f", med);
caarbelaezg 0:f0eb5248ae8f 476 lcd.locate(8,1);
caarbelaezg 0:f0eb5248ae8f 477 lcd.printf("PID=%0.1f", pid);
caarbelaezg 0:f0eb5248ae8f 478 lcd.locate(0,0);
caarbelaezg 0:f0eb5248ae8f 479 lcd.printf("Sp=%d", sp);
caarbelaezg 0:f0eb5248ae8f 480
caarbelaezg 0:f0eb5248ae8f 481 t.reset();
caarbelaezg 0:f0eb5248ae8f 482
caarbelaezg 0:f0eb5248ae8f 483 }
caarbelaezg 0:f0eb5248ae8f 484 }
caarbelaezg 0:f0eb5248ae8f 485
caarbelaezg 0:f0eb5248ae8f 486
caarbelaezg 0:f0eb5248ae8f 487 }
caarbelaezg 0:f0eb5248ae8f 488 }
caarbelaezg 0:f0eb5248ae8f 489
caarbelaezg 0:f0eb5248ae8f 490
caarbelaezg 0:f0eb5248ae8f 491 }