programa para hacer control PID simple
Fork of DebouncedIn_HelloWorld by
este programa permite hacer control PID simple, se configuran 4 parametros como: SETPOINT KP KI KD
USA 4 PULSADORES ASI AUMENTAR: aumenta el parametro seleccionado por menu DISMINUIR : disminuye el parametro seleccionado por menu MENU: cambia de menu o campo de ingreso ENTER: confirma el menu y se sale
main.cpp@1:83ffe66ee756, 2013-10-11 (annotated)
- Committer:
- tony63
- Date:
- Fri Oct 11 05:41:20 2013 +0000
- Revision:
- 1:83ffe66ee756
- Parent:
- 0:672241227e0d
programa que implementa un control PID usando cuatro pulsadores y un LCD; de 16x2 la libreria TextLCD.h debe ser modificada en las lineas 45 descomentar...y 101 comentar de lo contrario te compila con error
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 0:672241227e0d | 1 | #include "mbed.h" |
chris | 0:672241227e0d | 2 | #include "DebouncedIn.h" |
tony63 | 1:83ffe66ee756 | 3 | #include "TextLCD.h" |
tony63 | 1:83ffe66ee756 | 4 | AnalogIn Vin(PTC2); |
tony63 | 1:83ffe66ee756 | 5 | TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 |
tony63 | 1:83ffe66ee756 | 6 | |
tony63 | 1:83ffe66ee756 | 7 | DigitalOut led1(LED1); |
tony63 | 1:83ffe66ee756 | 8 | DigitalOut led2(LED2); |
tony63 | 1:83ffe66ee756 | 9 | DebouncedIn button1(PTC12); |
tony63 | 1:83ffe66ee756 | 10 | DebouncedIn button2(PTC13); |
tony63 | 1:83ffe66ee756 | 11 | DebouncedIn button3(PTC16); |
tony63 | 1:83ffe66ee756 | 12 | DebouncedIn button4(PTC17); |
tony63 | 1:83ffe66ee756 | 13 | |
tony63 | 1:83ffe66ee756 | 14 | /* codigos movimiento del curzor |
tony63 | 1:83ffe66ee756 | 15 | 18 para izquierda |
tony63 | 1:83ffe66ee756 | 16 | 1A para derecha |
tony63 | 1:83ffe66ee756 | 17 | */ |
chris | 0:672241227e0d | 18 | |
chris | 0:672241227e0d | 19 | |
tony63 | 1:83ffe66ee756 | 20 | int C1=0x0E; // solo muestra el curzor |
tony63 | 1:83ffe66ee756 | 21 | int C2=0x18; // desplaza izquierda |
tony63 | 1:83ffe66ee756 | 22 | int C3=0x1A; // desplaza derecha |
tony63 | 1:83ffe66ee756 | 23 | int C4=0x0C; // quito cursor bajo |
tony63 | 1:83ffe66ee756 | 24 | int i; // indice de la variable |
tony63 | 1:83ffe66ee756 | 25 | int j; |
tony63 | 1:83ffe66ee756 | 26 | int kp, ki, kd, sp, err, med, yr, pid, ap, ai, ad, err_v, cycle; |
chris | 0:672241227e0d | 27 | |
chris | 0:672241227e0d | 28 | int main() { |
tony63 | 1:83ffe66ee756 | 29 | lcd.cls(); |
tony63 | 1:83ffe66ee756 | 30 | lcd.printf("Sp%d",sp); |
tony63 | 1:83ffe66ee756 | 31 | lcd.locate(8,0); |
tony63 | 1:83ffe66ee756 | 32 | lcd.printf("Kp%d",kp); |
tony63 | 1:83ffe66ee756 | 33 | lcd.locate(0,1); |
tony63 | 1:83ffe66ee756 | 34 | lcd.printf("Ki%d",ki); |
tony63 | 1:83ffe66ee756 | 35 | lcd.locate(8,1); |
tony63 | 1:83ffe66ee756 | 36 | lcd.printf("Kd%d",kd); |
tony63 | 1:83ffe66ee756 | 37 | lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD |
tony63 | 1:83ffe66ee756 | 38 | lcd.locate(0,0); |
tony63 | 1:83ffe66ee756 | 39 | lcd.printf("Sp%d",sp); |
tony63 | 1:83ffe66ee756 | 40 | |
tony63 | 1:83ffe66ee756 | 41 | |
chris | 0:672241227e0d | 42 | while(1) { |
tony63 | 1:83ffe66ee756 | 43 | if (button3.falling()) { |
tony63 | 1:83ffe66ee756 | 44 | ++j; |
tony63 | 1:83ffe66ee756 | 45 | } //INCREMENTA POSICION DEL MENU COB BOTON 3 |
tony63 | 1:83ffe66ee756 | 46 | if (j==0){ |
tony63 | 1:83ffe66ee756 | 47 | lcd.locate(2,0); |
tony63 | 1:83ffe66ee756 | 48 | lcd.printf("%d",sp); |
tony63 | 1:83ffe66ee756 | 49 | if (button1.falling()) { |
tony63 | 1:83ffe66ee756 | 50 | ++sp; // ademas revisa boton 1 para incrementar |
tony63 | 1:83ffe66ee756 | 51 | } |
tony63 | 1:83ffe66ee756 | 52 | if (button2.falling()) { |
tony63 | 1:83ffe66ee756 | 53 | --sp; // ademas revisa boton 2 para decrementar |
tony63 | 1:83ffe66ee756 | 54 | } |
tony63 | 1:83ffe66ee756 | 55 | } |
tony63 | 1:83ffe66ee756 | 56 | if (j==1) { |
tony63 | 1:83ffe66ee756 | 57 | lcd.locate(10,0); |
tony63 | 1:83ffe66ee756 | 58 | lcd.printf("%d",kp); |
tony63 | 1:83ffe66ee756 | 59 | if (button1.falling()) { |
tony63 | 1:83ffe66ee756 | 60 | ++kp; |
tony63 | 1:83ffe66ee756 | 61 | } |
tony63 | 1:83ffe66ee756 | 62 | if (button2.falling()) { |
tony63 | 1:83ffe66ee756 | 63 | --kp; |
tony63 | 1:83ffe66ee756 | 64 | } |
tony63 | 1:83ffe66ee756 | 65 | } |
tony63 | 1:83ffe66ee756 | 66 | if (j==2) { |
tony63 | 1:83ffe66ee756 | 67 | lcd.locate(2,1); |
tony63 | 1:83ffe66ee756 | 68 | lcd.printf("%d",ki); |
tony63 | 1:83ffe66ee756 | 69 | if (button1.falling()){ |
tony63 | 1:83ffe66ee756 | 70 | ++ki; |
tony63 | 1:83ffe66ee756 | 71 | } |
tony63 | 1:83ffe66ee756 | 72 | if (button2.falling()){ |
tony63 | 1:83ffe66ee756 | 73 | --ki; |
tony63 | 1:83ffe66ee756 | 74 | } |
tony63 | 1:83ffe66ee756 | 75 | } |
tony63 | 1:83ffe66ee756 | 76 | if (j==3) { |
tony63 | 1:83ffe66ee756 | 77 | lcd.locate(10,1); |
tony63 | 1:83ffe66ee756 | 78 | lcd.printf("%d",kd); |
tony63 | 1:83ffe66ee756 | 79 | if (button1.falling()){ |
tony63 | 1:83ffe66ee756 | 80 | ++kd; |
tony63 | 1:83ffe66ee756 | 81 | } |
tony63 | 1:83ffe66ee756 | 82 | if (button2.falling()){ |
tony63 | 1:83ffe66ee756 | 83 | --kd; |
tony63 | 1:83ffe66ee756 | 84 | } |
tony63 | 1:83ffe66ee756 | 85 | } |
tony63 | 1:83ffe66ee756 | 86 | if (j==4) { |
tony63 | 1:83ffe66ee756 | 87 | j=0; |
tony63 | 1:83ffe66ee756 | 88 | } |
tony63 | 1:83ffe66ee756 | 89 | |
tony63 | 1:83ffe66ee756 | 90 | if (button4.falling()){ |
tony63 | 1:83ffe66ee756 | 91 | break; //sale del bucle si pisan suiche4 |
tony63 | 1:83ffe66ee756 | 92 | } |
tony63 | 1:83ffe66ee756 | 93 | |
tony63 | 1:83ffe66ee756 | 94 | } |
tony63 | 1:83ffe66ee756 | 95 | lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo |
tony63 | 1:83ffe66ee756 | 96 | lcd.cls(); //borra la pantalla |
tony63 | 1:83ffe66ee756 | 97 | lcd.printf(" GUARDADOS!"); |
tony63 | 1:83ffe66ee756 | 98 | wait(2); |
tony63 | 1:83ffe66ee756 | 99 | lcd.cls(); |
tony63 | 1:83ffe66ee756 | 100 | lcd.printf(" INICIA EL PID"); |
tony63 | 1:83ffe66ee756 | 101 | wait(2); |
tony63 | 1:83ffe66ee756 | 102 | // se imprimen los parches del control ***************************************** |
tony63 | 1:83ffe66ee756 | 103 | lcd.cls(); |
tony63 | 1:83ffe66ee756 | 104 | lcd.printf("Er%d",err); |
tony63 | 1:83ffe66ee756 | 105 | lcd.locate(8,0); |
tony63 | 1:83ffe66ee756 | 106 | lcd.printf("Me%d",med); |
tony63 | 1:83ffe66ee756 | 107 | lcd.locate(0,1); |
tony63 | 1:83ffe66ee756 | 108 | lcd.printf("Sp%d",sp); |
tony63 | 1:83ffe66ee756 | 109 | lcd.locate(8,1); |
tony63 | 1:83ffe66ee756 | 110 | lcd.printf("Co%d",pid); |
tony63 | 1:83ffe66ee756 | 111 | wait(5); |
tony63 | 1:83ffe66ee756 | 112 | |
tony63 | 1:83ffe66ee756 | 113 | // CICLO PRINCIPAL CONTROLADOR PID |
tony63 | 1:83ffe66ee756 | 114 | |
tony63 | 1:83ffe66ee756 | 115 | while(1) { |
tony63 | 1:83ffe66ee756 | 116 | //leer puerto analogo y asignar a med |
tony63 | 1:83ffe66ee756 | 117 | err = (sp-med); |
tony63 | 1:83ffe66ee756 | 118 | ap = kp*err; |
tony63 | 1:83ffe66ee756 | 119 | ai =(ki*err)+ai; //calculo de la integral del error |
tony63 | 1:83ffe66ee756 | 120 | // se verifica que la accion integral no sea muy grande |
tony63 | 1:83ffe66ee756 | 121 | ad = kd*(err-err_v); //calculo de la accion derivativa |
tony63 | 1:83ffe66ee756 | 122 | pid = (ap+ai+ad); |
tony63 | 1:83ffe66ee756 | 123 | //****se muestran las variables****************************************** |
tony63 | 1:83ffe66ee756 | 124 | if (pid > 999){ |
tony63 | 1:83ffe66ee756 | 125 | pid=1000; |
tony63 | 1:83ffe66ee756 | 126 | } |
tony63 | 1:83ffe66ee756 | 127 | |
tony63 | 1:83ffe66ee756 | 128 | wait(.5); |
tony63 | 1:83ffe66ee756 | 129 | lcd.locate(2,0); |
tony63 | 1:83ffe66ee756 | 130 | lcd.printf("%d",err); |
tony63 | 1:83ffe66ee756 | 131 | lcd.locate(10,0); |
tony63 | 1:83ffe66ee756 | 132 | lcd.printf("%d",med); |
tony63 | 1:83ffe66ee756 | 133 | lcd.locate(2,1); |
tony63 | 1:83ffe66ee756 | 134 | lcd.printf("%d",sp); |
tony63 | 1:83ffe66ee756 | 135 | lcd.locate(10,1); |
tony63 | 1:83ffe66ee756 | 136 | lcd.printf("%d",pid); |
tony63 | 1:83ffe66ee756 | 137 | |
tony63 | 1:83ffe66ee756 | 138 | |
tony63 | 1:83ffe66ee756 | 139 | // se actualizan las variables ******************************************* |
tony63 | 1:83ffe66ee756 | 140 | err_v = err; |
tony63 | 1:83ffe66ee756 | 141 | // se verifica que pid sea positivo ************************************** |
tony63 | 1:83ffe66ee756 | 142 | |
tony63 | 1:83ffe66ee756 | 143 | // se verifica que pid sea menor o igual la valor maximo ***************** |
tony63 | 1:83ffe66ee756 | 144 | |
tony63 | 1:83ffe66ee756 | 145 | |
tony63 | 1:83ffe66ee756 | 146 | // se envia el valor pid a puerto analogico de salida (D/A) ************** |
tony63 | 1:83ffe66ee756 | 147 | // se repite el ciclo |
tony63 | 1:83ffe66ee756 | 148 | } |
tony63 | 1:83ffe66ee756 | 149 | } |