Tarea3 procesadores: PID en FRDM-KL25Z con botones incrementales
Dependencies: TextLCD-modificad mbed
main.cpp@0:6a56212dd414, 2015-04-08 (annotated)
- Committer:
- obifandres
- Date:
- Wed Apr 08 15:19:22 2015 +0000
- Revision:
- 0:6a56212dd414
Tarea 3 procesadores: PID en FKL26Z con botones de incrementales
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
obifandres | 0:6a56212dd414 | 1 | #include "mbed.h" |
obifandres | 0:6a56212dd414 | 2 | #include "TextLCD.h" |
obifandres | 0:6a56212dd414 | 3 | #include "DebouncedIn.h" |
obifandres | 0:6a56212dd414 | 4 | |
obifandres | 0:6a56212dd414 | 5 | TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); //configura los puestos PTE0,PTE1,PTE2,PTE3,PTE4,PTE5, donde se conectara el LCD 16x2 |
obifandres | 0:6a56212dd414 | 6 | DebouncedIn button1(PTC12); |
obifandres | 0:6a56212dd414 | 7 | DebouncedIn button2(PTC13); |
obifandres | 0:6a56212dd414 | 8 | DebouncedIn button3(PTC16); |
obifandres | 0:6a56212dd414 | 9 | DebouncedIn button4(PTC17); |
obifandres | 0:6a56212dd414 | 10 | AnalogIn Vin(PTB0); |
obifandres | 0:6a56212dd414 | 11 | AnalogOut Vout(PTE30); |
obifandres | 0:6a56212dd414 | 12 | Timer t; |
obifandres | 0:6a56212dd414 | 13 | int j; |
obifandres | 0:6a56212dd414 | 14 | int flag; |
obifandres | 0:6a56212dd414 | 15 | int q; |
obifandres | 0:6a56212dd414 | 16 | int C1=0x0E; // solo muestra el cursor |
obifandres | 0:6a56212dd414 | 17 | /*int C2=0x18; // desplaza izquierda |
obifandres | 0:6a56212dd414 | 18 | int C3=0x1A; // desplaza derecha*/ |
obifandres | 0:6a56212dd414 | 19 | int C4=0x0C; // quito cursor bajo |
obifandres | 0:6a56212dd414 | 20 | int sp=0; //set point |
obifandres | 0:6a56212dd414 | 21 | int kp=0; //ganancia proporcional |
obifandres | 0:6a56212dd414 | 22 | int ki=0; //ganancia integral |
obifandres | 0:6a56212dd414 | 23 | int kd=0; //ganancia derivativa |
obifandres | 0:6a56212dd414 | 24 | int k=0; //variable de control} |
obifandres | 0:6a56212dd414 | 25 | int err, med, yr, ap, ai, ad, err_v, cycle; |
obifandres | 0:6a56212dd414 | 26 | int pid; |
obifandres | 0:6a56212dd414 | 27 | int bb; |
obifandres | 0:6a56212dd414 | 28 | |
obifandres | 0:6a56212dd414 | 29 | int up(int incremento, int pos){ |
obifandres | 0:6a56212dd414 | 30 | if(incremento==999){ |
obifandres | 0:6a56212dd414 | 31 | return incremento; |
obifandres | 0:6a56212dd414 | 32 | } |
obifandres | 0:6a56212dd414 | 33 | else if(!button1.read()){ |
obifandres | 0:6a56212dd414 | 34 | for(int t=0;t<10;t++){ |
obifandres | 0:6a56212dd414 | 35 | if(button1.read()){ |
obifandres | 0:6a56212dd414 | 36 | break; |
obifandres | 0:6a56212dd414 | 37 | } |
obifandres | 0:6a56212dd414 | 38 | if(incremento>995){ |
obifandres | 0:6a56212dd414 | 39 | incremento=999; |
obifandres | 0:6a56212dd414 | 40 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 41 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 42 | } |
obifandres | 0:6a56212dd414 | 43 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 44 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 45 | } |
obifandres | 0:6a56212dd414 | 46 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 47 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 48 | } |
obifandres | 0:6a56212dd414 | 49 | else { |
obifandres | 0:6a56212dd414 | 50 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 51 | } |
obifandres | 0:6a56212dd414 | 52 | lcd.printf("%d ",incremento); |
obifandres | 0:6a56212dd414 | 53 | break; |
obifandres | 0:6a56212dd414 | 54 | } |
obifandres | 0:6a56212dd414 | 55 | incremento++; |
obifandres | 0:6a56212dd414 | 56 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 57 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 58 | } |
obifandres | 0:6a56212dd414 | 59 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 60 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 61 | } |
obifandres | 0:6a56212dd414 | 62 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 63 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 64 | } |
obifandres | 0:6a56212dd414 | 65 | else { |
obifandres | 0:6a56212dd414 | 66 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 67 | } |
obifandres | 0:6a56212dd414 | 68 | lcd.printf("%d ",incremento); |
obifandres | 0:6a56212dd414 | 69 | wait(0.25); |
obifandres | 0:6a56212dd414 | 70 | } |
obifandres | 0:6a56212dd414 | 71 | |
obifandres | 0:6a56212dd414 | 72 | for(int t=0;t<10;t++){ |
obifandres | 0:6a56212dd414 | 73 | if(button1.read()){ |
obifandres | 0:6a56212dd414 | 74 | break; |
obifandres | 0:6a56212dd414 | 75 | } |
obifandres | 0:6a56212dd414 | 76 | if(incremento>970){ |
obifandres | 0:6a56212dd414 | 77 | incremento=999; |
obifandres | 0:6a56212dd414 | 78 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 79 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 80 | } |
obifandres | 0:6a56212dd414 | 81 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 82 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 83 | } |
obifandres | 0:6a56212dd414 | 84 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 85 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 86 | } |
obifandres | 0:6a56212dd414 | 87 | else { |
obifandres | 0:6a56212dd414 | 88 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 89 | } |
obifandres | 0:6a56212dd414 | 90 | lcd.printf("%d ",incremento); |
obifandres | 0:6a56212dd414 | 91 | break; |
obifandres | 0:6a56212dd414 | 92 | } |
obifandres | 0:6a56212dd414 | 93 | incremento+=10; |
obifandres | 0:6a56212dd414 | 94 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 95 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 96 | } |
obifandres | 0:6a56212dd414 | 97 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 98 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 99 | } |
obifandres | 0:6a56212dd414 | 100 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 101 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 102 | } |
obifandres | 0:6a56212dd414 | 103 | else { |
obifandres | 0:6a56212dd414 | 104 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 105 | } |
obifandres | 0:6a56212dd414 | 106 | lcd.printf("%d ",incremento); |
obifandres | 0:6a56212dd414 | 107 | wait(0.25); |
obifandres | 0:6a56212dd414 | 108 | } |
obifandres | 0:6a56212dd414 | 109 | for(int t=0;t<20;t++){ |
obifandres | 0:6a56212dd414 | 110 | if(button1.read()){ |
obifandres | 0:6a56212dd414 | 111 | break; |
obifandres | 0:6a56212dd414 | 112 | } |
obifandres | 0:6a56212dd414 | 113 | if(incremento>920){ |
obifandres | 0:6a56212dd414 | 114 | incremento=999; |
obifandres | 0:6a56212dd414 | 115 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 116 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 117 | } |
obifandres | 0:6a56212dd414 | 118 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 119 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 120 | } |
obifandres | 0:6a56212dd414 | 121 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 122 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 123 | } |
obifandres | 0:6a56212dd414 | 124 | else { |
obifandres | 0:6a56212dd414 | 125 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 126 | } |
obifandres | 0:6a56212dd414 | 127 | lcd.printf("%d ",incremento); |
obifandres | 0:6a56212dd414 | 128 | break; |
obifandres | 0:6a56212dd414 | 129 | } |
obifandres | 0:6a56212dd414 | 130 | incremento+=100; |
obifandres | 0:6a56212dd414 | 131 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 132 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 133 | } |
obifandres | 0:6a56212dd414 | 134 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 135 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 136 | } |
obifandres | 0:6a56212dd414 | 137 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 138 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 139 | } |
obifandres | 0:6a56212dd414 | 140 | else { |
obifandres | 0:6a56212dd414 | 141 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 142 | } |
obifandres | 0:6a56212dd414 | 143 | lcd.printf("%d ",incremento); |
obifandres | 0:6a56212dd414 | 144 | wait(0.25); |
obifandres | 0:6a56212dd414 | 145 | } |
obifandres | 0:6a56212dd414 | 146 | } |
obifandres | 0:6a56212dd414 | 147 | return incremento; |
obifandres | 0:6a56212dd414 | 148 | } |
obifandres | 0:6a56212dd414 | 149 | |
obifandres | 0:6a56212dd414 | 150 | int down(int decremento, int pos){ |
obifandres | 0:6a56212dd414 | 151 | if(decremento==0){ |
obifandres | 0:6a56212dd414 | 152 | return decremento; |
obifandres | 0:6a56212dd414 | 153 | } |
obifandres | 0:6a56212dd414 | 154 | else if(!button2.read()){ |
obifandres | 0:6a56212dd414 | 155 | for(int t=0;t<10;t++){ |
obifandres | 0:6a56212dd414 | 156 | if(button2.read()){ |
obifandres | 0:6a56212dd414 | 157 | break; |
obifandres | 0:6a56212dd414 | 158 | } |
obifandres | 0:6a56212dd414 | 159 | if(decremento<4){ |
obifandres | 0:6a56212dd414 | 160 | decremento=0; |
obifandres | 0:6a56212dd414 | 161 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 162 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 163 | } |
obifandres | 0:6a56212dd414 | 164 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 165 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 166 | } |
obifandres | 0:6a56212dd414 | 167 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 168 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 169 | } |
obifandres | 0:6a56212dd414 | 170 | else { |
obifandres | 0:6a56212dd414 | 171 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 172 | } |
obifandres | 0:6a56212dd414 | 173 | lcd.printf(" "); |
obifandres | 0:6a56212dd414 | 174 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 175 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 176 | } |
obifandres | 0:6a56212dd414 | 177 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 178 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 179 | } |
obifandres | 0:6a56212dd414 | 180 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 181 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 182 | } |
obifandres | 0:6a56212dd414 | 183 | else { |
obifandres | 0:6a56212dd414 | 184 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 185 | } |
obifandres | 0:6a56212dd414 | 186 | lcd.printf("%d ",decremento); |
obifandres | 0:6a56212dd414 | 187 | break; |
obifandres | 0:6a56212dd414 | 188 | } |
obifandres | 0:6a56212dd414 | 189 | decremento--; |
obifandres | 0:6a56212dd414 | 190 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 191 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 192 | } |
obifandres | 0:6a56212dd414 | 193 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 194 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 195 | } |
obifandres | 0:6a56212dd414 | 196 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 197 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 198 | } |
obifandres | 0:6a56212dd414 | 199 | else { |
obifandres | 0:6a56212dd414 | 200 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 201 | } |
obifandres | 0:6a56212dd414 | 202 | lcd.printf(" "); |
obifandres | 0:6a56212dd414 | 203 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 204 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 205 | } |
obifandres | 0:6a56212dd414 | 206 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 207 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 208 | } |
obifandres | 0:6a56212dd414 | 209 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 210 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 211 | } |
obifandres | 0:6a56212dd414 | 212 | else { |
obifandres | 0:6a56212dd414 | 213 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 214 | } |
obifandres | 0:6a56212dd414 | 215 | lcd.printf("%d ",decremento); |
obifandres | 0:6a56212dd414 | 216 | wait(0.25); |
obifandres | 0:6a56212dd414 | 217 | } |
obifandres | 0:6a56212dd414 | 218 | |
obifandres | 0:6a56212dd414 | 219 | for(int t=0;t<10;t++){ |
obifandres | 0:6a56212dd414 | 220 | if(button2.read()){ |
obifandres | 0:6a56212dd414 | 221 | break; |
obifandres | 0:6a56212dd414 | 222 | } |
obifandres | 0:6a56212dd414 | 223 | if(decremento<40){ |
obifandres | 0:6a56212dd414 | 224 | decremento=0; |
obifandres | 0:6a56212dd414 | 225 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 226 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 227 | } |
obifandres | 0:6a56212dd414 | 228 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 229 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 230 | } |
obifandres | 0:6a56212dd414 | 231 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 232 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 233 | } |
obifandres | 0:6a56212dd414 | 234 | else { |
obifandres | 0:6a56212dd414 | 235 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 236 | } |
obifandres | 0:6a56212dd414 | 237 | lcd.printf(" "); |
obifandres | 0:6a56212dd414 | 238 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 239 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 240 | } |
obifandres | 0:6a56212dd414 | 241 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 242 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 243 | } |
obifandres | 0:6a56212dd414 | 244 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 245 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 246 | } |
obifandres | 0:6a56212dd414 | 247 | else { |
obifandres | 0:6a56212dd414 | 248 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 249 | } |
obifandres | 0:6a56212dd414 | 250 | lcd.printf("%d ",decremento); |
obifandres | 0:6a56212dd414 | 251 | break; |
obifandres | 0:6a56212dd414 | 252 | } |
obifandres | 0:6a56212dd414 | 253 | decremento-=10; |
obifandres | 0:6a56212dd414 | 254 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 255 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 256 | } |
obifandres | 0:6a56212dd414 | 257 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 258 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 259 | } |
obifandres | 0:6a56212dd414 | 260 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 261 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 262 | } |
obifandres | 0:6a56212dd414 | 263 | else { |
obifandres | 0:6a56212dd414 | 264 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 265 | } |
obifandres | 0:6a56212dd414 | 266 | lcd.printf(" "); |
obifandres | 0:6a56212dd414 | 267 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 268 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 269 | } |
obifandres | 0:6a56212dd414 | 270 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 271 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 272 | } |
obifandres | 0:6a56212dd414 | 273 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 274 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 275 | } |
obifandres | 0:6a56212dd414 | 276 | else { |
obifandres | 0:6a56212dd414 | 277 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 278 | } |
obifandres | 0:6a56212dd414 | 279 | lcd.printf("%d ",decremento); |
obifandres | 0:6a56212dd414 | 280 | wait(0.25); |
obifandres | 0:6a56212dd414 | 281 | } |
obifandres | 0:6a56212dd414 | 282 | for(int t=0;t<20;t++){ |
obifandres | 0:6a56212dd414 | 283 | if(button2.read()){ |
obifandres | 0:6a56212dd414 | 284 | break; |
obifandres | 0:6a56212dd414 | 285 | } |
obifandres | 0:6a56212dd414 | 286 | if(decremento<80){ |
obifandres | 0:6a56212dd414 | 287 | decremento=0; |
obifandres | 0:6a56212dd414 | 288 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 289 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 290 | } |
obifandres | 0:6a56212dd414 | 291 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 292 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 293 | } |
obifandres | 0:6a56212dd414 | 294 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 295 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 296 | } |
obifandres | 0:6a56212dd414 | 297 | else { |
obifandres | 0:6a56212dd414 | 298 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 299 | } |
obifandres | 0:6a56212dd414 | 300 | lcd.printf(" "); |
obifandres | 0:6a56212dd414 | 301 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 302 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 303 | } |
obifandres | 0:6a56212dd414 | 304 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 305 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 306 | } |
obifandres | 0:6a56212dd414 | 307 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 308 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 309 | } |
obifandres | 0:6a56212dd414 | 310 | else { |
obifandres | 0:6a56212dd414 | 311 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 312 | } |
obifandres | 0:6a56212dd414 | 313 | lcd.printf("%d ",decremento); |
obifandres | 0:6a56212dd414 | 314 | break; |
obifandres | 0:6a56212dd414 | 315 | } |
obifandres | 0:6a56212dd414 | 316 | decremento-=100; |
obifandres | 0:6a56212dd414 | 317 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 318 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 319 | } |
obifandres | 0:6a56212dd414 | 320 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 321 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 322 | } |
obifandres | 0:6a56212dd414 | 323 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 324 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 325 | } |
obifandres | 0:6a56212dd414 | 326 | else { |
obifandres | 0:6a56212dd414 | 327 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 328 | } |
obifandres | 0:6a56212dd414 | 329 | lcd.printf(" "); |
obifandres | 0:6a56212dd414 | 330 | if (pos==0){ |
obifandres | 0:6a56212dd414 | 331 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 332 | } |
obifandres | 0:6a56212dd414 | 333 | else if(pos==1){ |
obifandres | 0:6a56212dd414 | 334 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 335 | } |
obifandres | 0:6a56212dd414 | 336 | else if (pos==2){ |
obifandres | 0:6a56212dd414 | 337 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 338 | } |
obifandres | 0:6a56212dd414 | 339 | else { |
obifandres | 0:6a56212dd414 | 340 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 341 | } |
obifandres | 0:6a56212dd414 | 342 | lcd.printf("%d ",decremento); |
obifandres | 0:6a56212dd414 | 343 | wait(0.25); |
obifandres | 0:6a56212dd414 | 344 | } |
obifandres | 0:6a56212dd414 | 345 | } |
obifandres | 0:6a56212dd414 | 346 | return decremento; |
obifandres | 0:6a56212dd414 | 347 | } |
obifandres | 0:6a56212dd414 | 348 | |
obifandres | 0:6a56212dd414 | 349 | |
obifandres | 0:6a56212dd414 | 350 | |
obifandres | 0:6a56212dd414 | 351 | |
obifandres | 0:6a56212dd414 | 352 | |
obifandres | 0:6a56212dd414 | 353 | |
obifandres | 0:6a56212dd414 | 354 | |
obifandres | 0:6a56212dd414 | 355 | |
obifandres | 0:6a56212dd414 | 356 | |
obifandres | 0:6a56212dd414 | 357 | |
obifandres | 0:6a56212dd414 | 358 | |
obifandres | 0:6a56212dd414 | 359 | |
obifandres | 0:6a56212dd414 | 360 | |
obifandres | 0:6a56212dd414 | 361 | |
obifandres | 0:6a56212dd414 | 362 | int main() { |
obifandres | 0:6a56212dd414 | 363 | lcd.cls(); |
obifandres | 0:6a56212dd414 | 364 | lcd.locate(8,0); |
obifandres | 0:6a56212dd414 | 365 | lcd.printf("Kp=%d",kp); |
obifandres | 0:6a56212dd414 | 366 | lcd.locate(0,1); |
obifandres | 0:6a56212dd414 | 367 | lcd.printf("Ki=%d",ki); |
obifandres | 0:6a56212dd414 | 368 | lcd.locate(8,1); |
obifandres | 0:6a56212dd414 | 369 | lcd.printf("Kd=%d",kd); |
obifandres | 0:6a56212dd414 | 370 | lcd.writeCommand(C1); //permite que el cursor se vea y sea intermitente |
obifandres | 0:6a56212dd414 | 371 | lcd.locate(0,0); |
obifandres | 0:6a56212dd414 | 372 | lcd.printf("Sp=%d",sp); |
obifandres | 0:6a56212dd414 | 373 | while(1) { |
obifandres | 0:6a56212dd414 | 374 | if (button3.falling()) { |
obifandres | 0:6a56212dd414 | 375 | ++j; |
obifandres | 0:6a56212dd414 | 376 | } //INCREMENTA POSICION DEL MENU COB BOTON 3 |
obifandres | 0:6a56212dd414 | 377 | if (j==0){ |
obifandres | 0:6a56212dd414 | 378 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 379 | lcd.printf("%d",sp); |
obifandres | 0:6a56212dd414 | 380 | if(!button1.read()){ |
obifandres | 0:6a56212dd414 | 381 | sp=up(sp,j); |
obifandres | 0:6a56212dd414 | 382 | } |
obifandres | 0:6a56212dd414 | 383 | if(!button2.read()){ |
obifandres | 0:6a56212dd414 | 384 | sp=down(sp,j); |
obifandres | 0:6a56212dd414 | 385 | } |
obifandres | 0:6a56212dd414 | 386 | } |
obifandres | 0:6a56212dd414 | 387 | if (j==1) { |
obifandres | 0:6a56212dd414 | 388 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 389 | lcd.printf("%d",kp); |
obifandres | 0:6a56212dd414 | 390 | if(!button1.read()){ |
obifandres | 0:6a56212dd414 | 391 | kp=up(kp,j); |
obifandres | 0:6a56212dd414 | 392 | } |
obifandres | 0:6a56212dd414 | 393 | if(!button2.read()){ |
obifandres | 0:6a56212dd414 | 394 | kp=down(kp,j); |
obifandres | 0:6a56212dd414 | 395 | } |
obifandres | 0:6a56212dd414 | 396 | } |
obifandres | 0:6a56212dd414 | 397 | if (j==2) { |
obifandres | 0:6a56212dd414 | 398 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 399 | lcd.printf("%d",ki); |
obifandres | 0:6a56212dd414 | 400 | if(!button1.read()){ |
obifandres | 0:6a56212dd414 | 401 | ki=up(ki,j); |
obifandres | 0:6a56212dd414 | 402 | } |
obifandres | 0:6a56212dd414 | 403 | if(!button2.read()){ |
obifandres | 0:6a56212dd414 | 404 | ki=down(ki,j); |
obifandres | 0:6a56212dd414 | 405 | } |
obifandres | 0:6a56212dd414 | 406 | } |
obifandres | 0:6a56212dd414 | 407 | if (j==3) { |
obifandres | 0:6a56212dd414 | 408 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 409 | lcd.printf("%d",kd); |
obifandres | 0:6a56212dd414 | 410 | if(!button1.read()){ |
obifandres | 0:6a56212dd414 | 411 | kd=up(kd,j); |
obifandres | 0:6a56212dd414 | 412 | } |
obifandres | 0:6a56212dd414 | 413 | if(!button2.read()){ |
obifandres | 0:6a56212dd414 | 414 | kd=down(kd,j); |
obifandres | 0:6a56212dd414 | 415 | } |
obifandres | 0:6a56212dd414 | 416 | } |
obifandres | 0:6a56212dd414 | 417 | if (j==4) { |
obifandres | 0:6a56212dd414 | 418 | j=0; |
obifandres | 0:6a56212dd414 | 419 | } |
obifandres | 0:6a56212dd414 | 420 | |
obifandres | 0:6a56212dd414 | 421 | if (button4.falling()){ |
obifandres | 0:6a56212dd414 | 422 | break; //sale del bucle si pisan suiche4 |
obifandres | 0:6a56212dd414 | 423 | } |
obifandres | 0:6a56212dd414 | 424 | } |
obifandres | 0:6a56212dd414 | 425 | lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo |
obifandres | 0:6a56212dd414 | 426 | lcd.cls(); //borra la pantalla |
obifandres | 0:6a56212dd414 | 427 | lcd.printf(" GUARDADOS!"); |
obifandres | 0:6a56212dd414 | 428 | wait(2); |
obifandres | 0:6a56212dd414 | 429 | lcd.cls(); |
obifandres | 0:6a56212dd414 | 430 | lcd.printf(" INICIA EL PID"); |
obifandres | 0:6a56212dd414 | 431 | wait(2); |
obifandres | 0:6a56212dd414 | 432 | // se imprimen los parches del control ***************************************** |
obifandres | 0:6a56212dd414 | 433 | lcd.cls(); |
obifandres | 0:6a56212dd414 | 434 | lcd.printf("Er=%d",err); |
obifandres | 0:6a56212dd414 | 435 | lcd.locate(8,0); |
obifandres | 0:6a56212dd414 | 436 | lcd.printf("Me=%d",med); |
obifandres | 0:6a56212dd414 | 437 | lcd.locate(0,1); |
obifandres | 0:6a56212dd414 | 438 | lcd.printf("Sp=%d",sp); |
obifandres | 0:6a56212dd414 | 439 | lcd.locate(8,1); |
obifandres | 0:6a56212dd414 | 440 | lcd.printf("Co=%d",pid); |
obifandres | 0:6a56212dd414 | 441 | wait(5); |
obifandres | 0:6a56212dd414 | 442 | |
obifandres | 0:6a56212dd414 | 443 | while (1){ |
obifandres | 0:6a56212dd414 | 444 | |
obifandres | 0:6a56212dd414 | 445 | med=Vin.read()*1000; |
obifandres | 0:6a56212dd414 | 446 | err = (sp-med); |
obifandres | 0:6a56212dd414 | 447 | ap = kp*err; |
obifandres | 0:6a56212dd414 | 448 | ai =(ki*err)+ai; //calculo de la integral del error |
obifandres | 0:6a56212dd414 | 449 | ad = kd*(err-err_v); //calculo de la accion derivativa |
obifandres | 0:6a56212dd414 | 450 | pid = (ap+ai+ad); |
obifandres | 0:6a56212dd414 | 451 | //****se muestran las variables****************************************** |
obifandres | 0:6a56212dd414 | 452 | if (pid > 999){ |
obifandres | 0:6a56212dd414 | 453 | pid=1000; |
obifandres | 0:6a56212dd414 | 454 | } |
obifandres | 0:6a56212dd414 | 455 | else if (pid<0){ |
obifandres | 0:6a56212dd414 | 456 | pid=0; |
obifandres | 0:6a56212dd414 | 457 | } |
obifandres | 0:6a56212dd414 | 458 | err_v = err; |
obifandres | 0:6a56212dd414 | 459 | if(flag==0){ |
obifandres | 0:6a56212dd414 | 460 | t.start(); |
obifandres | 0:6a56212dd414 | 461 | flag=1; |
obifandres | 0:6a56212dd414 | 462 | } |
obifandres | 0:6a56212dd414 | 463 | if (t>=0.3){ |
obifandres | 0:6a56212dd414 | 464 | wait(0.02); |
obifandres | 0:6a56212dd414 | 465 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 466 | lcd.printf(" "); |
obifandres | 0:6a56212dd414 | 467 | lcd.locate(3,0); |
obifandres | 0:6a56212dd414 | 468 | lcd.printf("%d",err); |
obifandres | 0:6a56212dd414 | 469 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 470 | lcd.printf(" "); |
obifandres | 0:6a56212dd414 | 471 | lcd.locate(11,0); |
obifandres | 0:6a56212dd414 | 472 | lcd.printf("%d",med); |
obifandres | 0:6a56212dd414 | 473 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 474 | lcd.printf(" "); |
obifandres | 0:6a56212dd414 | 475 | lcd.locate(3,1); |
obifandres | 0:6a56212dd414 | 476 | lcd.printf("%d",sp); |
obifandres | 0:6a56212dd414 | 477 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 478 | lcd.printf(" "); |
obifandres | 0:6a56212dd414 | 479 | lcd.locate(11,1); |
obifandres | 0:6a56212dd414 | 480 | lcd.printf("%d ",pid); |
obifandres | 0:6a56212dd414 | 481 | t.reset(); |
obifandres | 0:6a56212dd414 | 482 | flag=0; |
obifandres | 0:6a56212dd414 | 483 | } |
obifandres | 0:6a56212dd414 | 484 | Vout.write(pid/1000); |
obifandres | 0:6a56212dd414 | 485 | wait(0.01); |
obifandres | 0:6a56212dd414 | 486 | } |
obifandres | 0:6a56212dd414 | 487 | } |
obifandres | 0:6a56212dd414 | 488 |