![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
PID + Teclas + LCD
Dependencies: Debounced TextLCD mbed
main.cpp@0:fd20467a764b, 2015-04-10 (annotated)
- Committer:
- Equipo3JohnAndresx2
- Date:
- Fri Apr 10 03:14:07 2015 +0000
- Revision:
- 0:fd20467a764b
Tarea tres de Procesadores de la Universidad Nacional de Colombia sede Medellin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Equipo3JohnAndresx2 | 0:fd20467a764b | 1 | // Tarea Tres - Procesadores - 01-2015 |
Equipo3JohnAndresx2 | 0:fd20467a764b | 2 | // PID con opcion de incremento, decremento y cambio de posicion en LCD |
Equipo3JohnAndresx2 | 0:fd20467a764b | 3 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 4 | // Se incluyen las librerias correspondientes para el codigo |
Equipo3JohnAndresx2 | 0:fd20467a764b | 5 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 6 | #include "mbed.h" |
Equipo3JohnAndresx2 | 0:fd20467a764b | 7 | #include "DebouncedIn.h" |
Equipo3JohnAndresx2 | 0:fd20467a764b | 8 | #include "TextLCD.h" |
Equipo3JohnAndresx2 | 0:fd20467a764b | 9 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 10 | // Caracterizacion de Puertos |
Equipo3JohnAndresx2 | 0:fd20467a764b | 11 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 12 | AnalogIn Vin(PTC2); // Definicion de puerto para entrada analoga |
Equipo3JohnAndresx2 | 0:fd20467a764b | 13 | AnalogOut Vout(PTE30); // Definicion del puerto para salida analoga |
Equipo3JohnAndresx2 | 0:fd20467a764b | 14 | TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 15 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 16 | DebouncedIn button1(PTC12); // Definicion del boton1 como boton para incrementar |
Equipo3JohnAndresx2 | 0:fd20467a764b | 17 | DebouncedIn button4(PTC17); // Definicion del boton4 como boton de enter |
Equipo3JohnAndresx2 | 0:fd20467a764b | 18 | DebouncedIn button3(PTC16); // Definicion del boton3 como boton de menu |
Equipo3JohnAndresx2 | 0:fd20467a764b | 19 | DebouncedIn button2(PTC13); // Definicion del boton2 como boton para decrementar |
Equipo3JohnAndresx2 | 0:fd20467a764b | 20 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 21 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 22 | int C1=0x0F; // Pone el cursor |
Equipo3JohnAndresx2 | 0:fd20467a764b | 23 | int C4=0x0C; // Quito el cursor bajo |
Equipo3JohnAndresx2 | 0:fd20467a764b | 24 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 25 | // Deficion de las variables para el controlador PID |
Equipo3JohnAndresx2 | 0:fd20467a764b | 26 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 27 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 28 | int ref=0; // Incluimos la variable de referencia |
Equipo3JohnAndresx2 | 0:fd20467a764b | 29 | int Cint=0; // Incluimos la constante integral |
Equipo3JohnAndresx2 | 0:fd20467a764b | 30 | int Cder=0; // Inlcuimos la constante derivativa |
Equipo3JohnAndresx2 | 0:fd20467a764b | 31 | int Cpro=0; // Incluimos la constante proporcional |
Equipo3JohnAndresx2 | 0:fd20467a764b | 32 | int pos=0; // Inlcuimos la variable de posicion en el LCD |
Equipo3JohnAndresx2 | 0:fd20467a764b | 33 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 34 | int med; // Medida de la señal |
Equipo3JohnAndresx2 | 0:fd20467a764b | 35 | int er=0; // Error en la medida |
Equipo3JohnAndresx2 | 0:fd20467a764b | 36 | int Apid; // Accion del PID |
Equipo3JohnAndresx2 | 0:fd20467a764b | 37 | int Ai=0; // Accion Integral |
Equipo3JohnAndresx2 | 0:fd20467a764b | 38 | int Ad; // Accion Derivativa |
Equipo3JohnAndresx2 | 0:fd20467a764b | 39 | int Ap; // Accion Proporcional |
Equipo3JohnAndresx2 | 0:fd20467a764b | 40 | int ev; // error viejo |
Equipo3JohnAndresx2 | 0:fd20467a764b | 41 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 42 | int Int; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 43 | int FactEsc; // Factor de Escalado |
Equipo3JohnAndresx2 | 0:fd20467a764b | 44 | float pidn; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 45 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 46 | // Inicio de la funcion principal |
Equipo3JohnAndresx2 | 0:fd20467a764b | 47 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 48 | int main() |
Equipo3JohnAndresx2 | 0:fd20467a764b | 49 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 50 | lcd.cls(); // Inicio el LCD y lo limpio por ahí derecho |
Equipo3JohnAndresx2 | 0:fd20467a764b | 51 | lcd.writeCommand(C1); // Se escribe segun el modulo del LCD |
Equipo3JohnAndresx2 | 0:fd20467a764b | 52 | lcd.locate(8,0); // Me ubico en el LCD donde deseo imprimir |
Equipo3JohnAndresx2 | 0:fd20467a764b | 53 | lcd.printf("FactEsc =%d", FactEsc); // Se Presenta el factor de escalado(FactEsc) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 54 | lcd.locate(0,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 55 | lcd.printf("Pro =%d", Cpro); // Se presenta la contante proporcional(Cpro) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 56 | lcd.locate(6,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 57 | lcd.printf("Int =%d", Cint); // Se presenta la constante integrativa(Cint) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 58 | lcd.locate(11,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 59 | lcd.printf("Der =%d", Cder); // Se presenta la constante derivativa(Cder) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 60 | lcd.locate(0,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 61 | lcd.printf("Ref=%d", ref); // Se presenta el valor de la referencia (ref) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 62 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 63 | while(1) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 64 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 65 | if (button1.falling()) // Boton de incremento |
Equipo3JohnAndresx2 | 0:fd20467a764b | 66 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 67 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 68 | if (pos==1) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 69 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 70 | ++ref; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 71 | lcd.locate(3,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 72 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 73 | lcd.locate(3,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 74 | lcd.printf("%d", ref); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 75 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 76 | else if (pos==2) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 77 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 78 | ++Int; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 79 | if (Int==1){ |
Equipo3JohnAndresx2 | 0:fd20467a764b | 80 | FactEsc =1;} |
Equipo3JohnAndresx2 | 0:fd20467a764b | 81 | if (Int==2){ |
Equipo3JohnAndresx2 | 0:fd20467a764b | 82 | FactEsc =10;} |
Equipo3JohnAndresx2 | 0:fd20467a764b | 83 | if (Int==3){ |
Equipo3JohnAndresx2 | 0:fd20467a764b | 84 | FactEsc =100; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 85 | Int=0;} |
Equipo3JohnAndresx2 | 0:fd20467a764b | 86 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 87 | lcd.locate(11,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 88 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 89 | lcd.locate(12,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 90 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 91 | lcd.locate(13,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 92 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 93 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 94 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 95 | lcd.locate(11,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 96 | lcd.printf("%d", FactEsc); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 97 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 98 | else if (pos==3) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 99 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 100 | ++Cpro; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 101 | lcd.locate(2,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 102 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 103 | lcd.locate(2,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 104 | lcd.printf("%d", Cpro); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 105 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 106 | else if (pos==4) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 107 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 108 | ++Cint; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 109 | lcd.locate(8,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 110 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 111 | lcd.locate(8,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 112 | lcd.printf("%d", Cint); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 113 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 114 | else if (pos==5) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 115 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 116 | ++Cder; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 117 | lcd.locate(13,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 118 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 119 | lcd.locate(13,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 120 | lcd.printf("%d", Cder); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 121 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 122 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 123 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 124 | if (button2.falling()) // Boton de decremento |
Equipo3JohnAndresx2 | 0:fd20467a764b | 125 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 126 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 127 | if (pos==1) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 128 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 129 | if (ref==0) // no mostrar nada |
Equipo3JohnAndresx2 | 0:fd20467a764b | 130 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 131 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 132 | else |
Equipo3JohnAndresx2 | 0:fd20467a764b | 133 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 134 | --ref; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 135 | lcd.locate(3,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 136 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 137 | lcd.locate(3,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 138 | lcd.printf("%d", ref); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 139 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 140 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 141 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 142 | if (pos==3) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 143 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 144 | if (Cpro==0) // no mostrar nada |
Equipo3JohnAndresx2 | 0:fd20467a764b | 145 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 146 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 147 | else |
Equipo3JohnAndresx2 | 0:fd20467a764b | 148 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 149 | --Cpro; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 150 | lcd.locate(2,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 151 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 152 | lcd.locate(2,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 153 | lcd.printf("%d", Cpro); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 154 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 155 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 156 | if (pos==4) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 157 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 158 | if (Cint==0) // no mostrar nada |
Equipo3JohnAndresx2 | 0:fd20467a764b | 159 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 160 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 161 | else |
Equipo3JohnAndresx2 | 0:fd20467a764b | 162 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 163 | --Cint; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 164 | lcd.locate(8,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 165 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 166 | lcd.locate(8,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 167 | lcd.printf("%d", Cint); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 168 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 169 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 170 | if (pos==5) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 171 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 172 | if (Cder==0) // no mostrar nada |
Equipo3JohnAndresx2 | 0:fd20467a764b | 173 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 174 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 175 | else |
Equipo3JohnAndresx2 | 0:fd20467a764b | 176 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 177 | --Cder; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 178 | lcd.locate(13,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 179 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 180 | lcd.locate(13,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 181 | lcd.printf("%d", Cder); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 182 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 183 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 184 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 185 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 186 | if (button3.falling()) // Boton de menu |
Equipo3JohnAndresx2 | 0:fd20467a764b | 187 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 188 | if (pos==1) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 189 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 190 | ++pos; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 191 | lcd.locate(11,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 192 | lcd.printf("%d", FactEsc); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 193 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 194 | else if (pos==2) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 195 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 196 | ++pos; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 197 | lcd.locate(2,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 198 | lcd.printf("%d", Cpro); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 199 | lcd.locate(2,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 200 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 201 | else if (pos==3) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 202 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 203 | ++pos; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 204 | lcd.locate(8,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 205 | lcd.printf("%d", Cint); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 206 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 207 | else if (pos==4) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 208 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 209 | ++pos; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 210 | lcd.locate(13,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 211 | lcd.printf("%d", Cpro); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 212 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 213 | else if (pos==5) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 214 | { |
Equipo3JohnAndresx2 | 0:fd20467a764b | 215 | pos=1; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 216 | lcd.locate(3,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 217 | lcd.printf("%d", ref); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 218 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 219 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 220 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 221 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 222 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 223 | if (button4.falling()){ |
Equipo3JohnAndresx2 | 0:fd20467a764b | 224 | break; //sale del bucle si pisan suiche4 |
Equipo3JohnAndresx2 | 0:fd20467a764b | 225 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 226 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 227 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 228 | lcd.writeCommand(C4); //escribimos un comando segun el manual del modulo LCD para quitar cursor bajo |
Equipo3JohnAndresx2 | 0:fd20467a764b | 229 | lcd.cls(); //borra la pantalla |
Equipo3JohnAndresx2 | 0:fd20467a764b | 230 | lcd.printf(" GRABADOS!"); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 231 | wait(1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 232 | lcd.cls(); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 233 | lcd.printf(" EMPIEZA EL PID"); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 234 | wait(1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 235 | // se imprimen los parches del control ***************************************** |
Equipo3JohnAndresx2 | 0:fd20467a764b | 236 | lcd.cls(); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 237 | lcd.printf("Error=%d",er); // imprime el error de la medida(error) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 238 | lcd.locate(8,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 239 | lcd.printf("Med=%d",med); // imprime la medida de la señal(med) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 240 | lcd.locate(0,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 241 | lcd.printf("Ref=%d",ref); // imprime la referencia (ref) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 242 | lcd.locate(8,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 243 | lcd.printf("AC=%d",Apid); // imprime el la Accion de control del pid (AC) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 244 | wait(1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 245 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 246 | // CICLO PRINCIPAL CONTROLADOR PID |
Equipo3JohnAndresx2 | 0:fd20467a764b | 247 | // DEFINIMOS LOS PARAMETROS Kp,Ki y Kd teniendo en cuenta la escala |
Equipo3JohnAndresx2 | 0:fd20467a764b | 248 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 249 | Cpro= Cpro*FactEsc; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 250 | Cint = Cint*FactEsc; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 251 | Cder= Cder*FactEsc; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 252 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 253 | cicloPID : |
Equipo3JohnAndresx2 | 0:fd20467a764b | 254 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 255 | med=10*Vin.read(); //Lee el puerto analogo y asigna el valor leiro |
Equipo3JohnAndresx2 | 0:fd20467a764b | 256 | er = (ref-med); // halla en error de entre la medida y la referencia |
Equipo3JohnAndresx2 | 0:fd20467a764b | 257 | Ap= Cpro*er; // calculo de la accion proporcional |
Equipo3JohnAndresx2 | 0:fd20467a764b | 258 | Ai=(Cint*er)+Ai; //calculo de la accion integral |
Equipo3JohnAndresx2 | 0:fd20467a764b | 259 | Ad = Cder*(er-ev); //calculo de la accion derivativa |
Equipo3JohnAndresx2 | 0:fd20467a764b | 260 | Apid = (Ap+Ai+Ad); // es la señal de control que nos da el PID |
Equipo3JohnAndresx2 | 0:fd20467a764b | 261 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 262 | if (Apid < 0){ // limite inferior de la accion de control |
Equipo3JohnAndresx2 | 0:fd20467a764b | 263 | Apid=0; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 264 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 265 | if (Apid > 99){ // limite superiro de la accion de control |
Equipo3JohnAndresx2 | 0:fd20467a764b | 266 | Apid=100; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 267 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 268 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 269 | wait(.5); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 270 | if(error >= 0){ |
Equipo3JohnAndresx2 | 0:fd20467a764b | 271 | lcd.locate(4,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 272 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 273 | lcd.locate(3,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 274 | lcd.printf("%d",er); // Muestro el error |
Equipo3JohnAndresx2 | 0:fd20467a764b | 275 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 276 | if (error<0){ |
Equipo3JohnAndresx2 | 0:fd20467a764b | 277 | lcd.locate(3,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 278 | lcd.printf("%d",er); // Muestro el error |
Equipo3JohnAndresx2 | 0:fd20467a764b | 279 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 280 | lcd.locate(12,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 281 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 282 | lcd.locate(11,0); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 283 | lcd.printf("%d",med); // imprime el valor de la medición |
Equipo3JohnAndresx2 | 0:fd20467a764b | 284 | lcd.locate(3,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 285 | lcd.printf("%d",ref); // imprime el valor de la referenica |
Equipo3JohnAndresx2 | 0:fd20467a764b | 286 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 287 | if(Apid < 100){ |
Equipo3JohnAndresx2 | 0:fd20467a764b | 288 | lcd.locate(13,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 289 | lcd.printf(" "); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 290 | lcd.locate(11,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 291 | lcd.printf("%d",Apid);} // imprime el valor de la señal de control |
Equipo3JohnAndresx2 | 0:fd20467a764b | 292 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 293 | if(Apid >= 100){ |
Equipo3JohnAndresx2 | 0:fd20467a764b | 294 | lcd.locate(11,1); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 295 | lcd.printf("%d",Apid);} // imprime el valor de la señal de control |
Equipo3JohnAndresx2 | 0:fd20467a764b | 296 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 297 | ev = er; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 298 | pidn=Apid*0.01; //Normalizacion de la salida |
Equipo3JohnAndresx2 | 0:fd20467a764b | 299 | Vout.write(pidn); //se envia el valor pid a puerto analogico de salida (D/A) |
Equipo3JohnAndresx2 | 0:fd20467a764b | 300 | wait(0.005); |
Equipo3JohnAndresx2 | 0:fd20467a764b | 301 | goto cicloPID ; |
Equipo3JohnAndresx2 | 0:fd20467a764b | 302 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 303 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 304 | } |
Equipo3JohnAndresx2 | 0:fd20467a764b | 305 | |
Equipo3JohnAndresx2 | 0:fd20467a764b | 306 |