Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Debounced TextLCD1 mbed
Revision 2:12dac32e2a8b, committed 2013-11-14
- Comitter:
- jmcallef
- Date:
- Thu Nov 14 07:07:48 2013 +0000
- Parent:
- 1:ce04971eceb4
- Commit message:
- 3ra entrega a revizar
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Nov 01 00:51:04 2013 +0000
+++ b/main.cpp Thu Nov 14 07:07:48 2013 +0000
@@ -3,6 +3,7 @@
#include "TextLCD.h"
AnalogIn Vin(PTC2);
+AnalogOut Vout(PTE30);
TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
DigitalOut led1(LED1);
@@ -22,7 +23,9 @@
int i; // Indice de la variable.
int j; // Controla cambio de posición.
int kp, ki, kd, sp, cont;
-int med, pid, err, ap, ai, ad, err_v;
+int Error, Medida, Co, pid, ap, ai, ad, err_v;
+// float pidn, err, med, sal;
+int pidn, err, med, sal;
int main(){
lcd.writeCommand(C1); // Escribimos un comando segun el manual del modulo LCD
@@ -594,7 +597,8 @@
wait(0.3);
if(button2){
break;
- }
+ }
+
}
}
}
@@ -612,63 +616,82 @@
- lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo
- lcd.cls(); //borra la pantalla
+ lcd.writeCommand(C4); // Comando para quitar cursor bajo (segun el manual del modulo LCD).
+ lcd.cls(); // Borra la pantalla
lcd.printf(" GUARDADOS!");
- wait(2);
+ wait(1);
lcd.cls();
lcd.printf(" INICIA EL PID");
- wait(2);
- // se imprimen los parches del control *****************************************
+ wait(1);
+
+ // Se imprimen los parches del control.
+
lcd.cls();
- lcd.printf("Error%d",err);
+ lcd.printf("Err %d",err);
lcd.locate(8,0);
- lcd.printf("Medida%d",med);
+ lcd.printf("Med %d",med);
lcd.locate(0,1);
- lcd.printf("Sp%d",sp);
+ lcd.printf("Sp %d",sp);
lcd.locate(8,1);
- lcd.printf("Co%d",pid);
- wait(5);
-
+ lcd.printf("Co %d",pid);
+ wait(1);
+
+
// CICLO PRINCIPAL CONTROLADOR PID
while(1) {
- med=Vin;
- //leer puerto analogo y asignar a med
+ med=Vout*100; // Se le asigna al puerto Análogo la variable med.
err = (sp-med);
- ap = kp*err;
- ai =(ki*err)+ai; //calculo de la integral del error
- // se verifica que la accion integral no sea muy grande
- ad = kd*(err-err_v); //calculo de la accion derivativa
- pid = (ap+ai+ad);
- //****se muestran las variables******************************************
- if (pid > 999){
+
+ // Verificamos que la accion integral no sea muy grande.
+
+ if (ai < 1000){
+ ai =(ki*err)+ai; //Cálculo de la integral del error;
+ }
+
+ if (ap < 1000){
+ ap = kp*err; //Cálculo de la integral del error;
+ }
+
+ if (ad < 1000){
+ ad = kd*(err-err_v); //Cálculo de la integral del error;
+ }
+
+ pid = (ap+ai+ad); // Calculo del PID
+
+ if(pid<0){
+ pid=0;
+ }
+
+ if (pid > 1000){
pid=1000;
- }
-
- wait(.5);
- lcd.locate(5,0);
- lcd.printf("%d",err);
- lcd.locate(14,0);
- lcd.printf("%d",med);
- lcd.locate(2,1);
- lcd.printf("%d",sp);
- lcd.locate(10,1);
- lcd.printf("%d",pid);
+ }
+
+ //Vout=pid/999;
+ lcd.locate(4,0);
+ lcd.printf("%d",err);
+ lcd.locate(12,0);
+ lcd.printf("%d",med);
+ lcd.locate(3,1);
+ lcd.printf("%d",sp);
+ lcd.locate(12,1);
+ lcd.printf("%d",pid);
+ wait(0.2);
// se actualizan las variables *******************************************
- err_v = err;
+
// se verifica que pid sea positivo **************************************
-
- // se verifica que pid sea menor o igual la valor maximo *****************
+ if (pid < 0){
+ pid=0;
+ }
// se envia el valor pid a puerto analogico de salida (D/A) **************
- // se repite el ciclo
+
+ Vout=pid;
+ err_v = err;
+
}
-
-
-
- }
\ No newline at end of file
+ }
\ No newline at end of file