Controlador PID Digital para una planta de Primer Orden RC, con comunicación serial para ingreso de parámetros del controlador y actualización de las variables del sistema.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Giovani_Cardona
Date:
Wed Jun 12 20:06:42 2019 +0000
Commit message:
-

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 87552d9a8512 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jun 12 20:06:42 2019 +0000
@@ -0,0 +1,172 @@
+#include "mbed.h"
+#include "string.h"
+
+AnalogIn y(PTB3);//entrada analoga
+AnalogOut u(PTE30);//salida analoga OJO solo se le pueden drenar 1.5mA en circuitos use un Buffer
+//si se ignora esto se arruina la FRDMKL25Z
+Serial pc(USBTX, USBRX, "pc");
+Serial HC06(PTE0,PTE1);
+
+char buffer[128];
+int readptr = 0;
+int Kp, Ki, Kd, Sp;
+float pid,o,ai,ad,ap,med,err;
+float err_v;
+int checker1, checker2, checker3, checker4;
+
+int main(){
+    char c;
+    
+// Bluetooth
+    HC06.baud(9600);
+    HC06.printf("CONTROLADOR PID BLUETOOTH\n");
+
+// Interfaz para el pc
+    pc.printf("***********************************************\n");
+    pc.printf("CONTROLADOR PID \n");
+    pc.printf("***********************************************\n");
+    pc.printf("* Menu para los parametros del controlador PID: \n\n");
+    pc.printf("* Kp - Ingresar valor de la consotante proporcional \n");
+    pc.printf("* Ki - Ingresar valor de la consotante integral \n");
+    pc.printf("* Kd - Ingresar valor de la consotante derivativa \n");
+    pc.printf("* Sp - Ingresar valor del Set-Point \n\n");
+        
+    while(1){
+        
+        // Analizamos lo que ingresa el usuario
+        while( (c = pc.getc()) != '\n') {
+            buffer[readptr++] = c;
+        }
+        buffer[readptr++] = 0;
+        
+        //pc.printf("buffer= %s\n\r ",buffer);  //imprime el bufer
+        //pc.printf("buffer= %c  %c\n\r ",buffer[0],buffer[1]);//imprime el cero y el uno
+        
+        //////////////////////////////////////////
+        
+        if (strncmp(buffer, "Kp", 2) == 0) {
+            //Se lee el valor para Kp
+            pc.printf("Ingrese el valor para Kp: ");
+            pc.scanf("%d", &Kp);
+            checker1=1;
+        }
+        else if (strncmp(buffer, "Ki", 2) == 0) {
+            //Se lee el valor para Ki
+            pc.printf("Ingrese el valor para Ki: ");
+            pc.scanf("%d", &Ki);
+            checker2=1;
+        }
+        else if (strncmp(buffer, "Kd", 2) == 0) {
+            //Se lee el valor para Kd
+            pc.printf("Ingrese el valor para Kd: ");
+            pc.scanf("%d", &Kd);
+            checker3=1;
+        }
+        else if (strncmp(buffer, "Sp", 2) == 0) {
+            //Se lee el valor para Sp
+            pc.printf("Ingrese el valor para Sp: ");
+            pc.scanf("%d", &Sp);
+            if(Sp<=0){Sp=0;}
+            if(Sp > 999){Sp=999;}
+            checker4=1;
+        }
+        else {
+            pc.printf("Syntax error \n");       
+        }
+        readptr = 0;
+        
+        if (checker1 == 1 && checker2 == 1 && checker3 == 1 && checker4 == 1 ){
+            pc.printf("Parametros listos para iniciar \n");
+            pc.printf("*************************************\n");
+            pc.printf("Iniciar ahora (Si/No) \n");
+            
+            //Se lee la elección del usuario
+           while(1){
+                while( (c = pc.getc()) != '\n') {
+                    buffer[readptr++] = c;
+                }
+                buffer[readptr++] = 0;
+                
+                if (strncmp(buffer, "Si", 2) == 0){
+                    pc.printf("* Ingrese 'P' si desea cambiar el Set Ponit  \n\n");
+                    pc.printf("Iniciando... \n\n");
+                    wait_ms(1000);
+                    break;
+                }  
+                else{
+                    pc.printf("Iniciar ahora (Si/No) \n");
+                }
+            readptr = 0;
+            }
+            break;
+        }else{
+            pc.printf("*************************************\n");
+            pc.printf("Ingrese los valores restantes \n");
+            pc.printf("*************************************\n");
+            }
+        pc.printf("\n");
+    }
+    
+    // CICLO PRINCIPAL CONTROLADOR PID
+    
+    lop1:  
+        med = y.read()*999;
+        err = (Sp-med);  //se calcula el error
+        ap = Kp*err*0.01f;     //se calcula la accion proporcinal
+        ai =(Ki*err*0.01f)+ai;    //calculo de la integral del error
+        ad = Kd*(err-err_v)*0.01f; //calculo de la accion derivativa
+        pid = (ap+ai+ad);
+        // se verifica que pid sea positivo **************************************
+        if(pid<=0)
+        {
+            pid=0;
+        }
+
+        // se verifica que pid sea menor o igual al valor maximo *****************
+        if (pid > 999)
+        {
+            pid=999;
+        }
+        
+        //Normalizacion de la salida
+        // se actualizan las variables *******************************************
+        err_v = err;
+        o = pid/999;
+        u.write(o); //  se envia el valor pid a puerto analogico de salida (D/A) **************
+        
+        pc.printf("SetPoint\tEntrada\t\tError\t\tSalida\n");
+        pc.printf("%d\t\t%0.2f\t\t%0.2f\t\t%0.2f\n",Sp,med,err,pid);
+        pc.printf("%0.2fV\t\t%0.2fV\t\t%0.2fV\t\t%0.2fV\n\n",((Sp*3.3)/1000),((med*3.3)/1000),((err*3.3)/1000),((pid*3.3)/1000));
+        
+        HC06.printf("SetPoint: %d       %0.2fV\n\r",Sp,((Sp*3.3)/1000));
+        HC06.printf("Entrada:  %0.2f    %0.2fV\n\r",med,((med*3.3)/1000));
+        HC06.printf("Error:    %0.2f    %0.2fV\n\r",err,((err*3.3)/1000));
+        HC06.printf("Salida:   %0.2f    %0.2fV\n\r",pid,((pid*3.3)/1000));
+        HC06.printf("\n\r");
+        
+        ////-----------------------Nuevo Setpoint-------------------------------------------
+        if(pc.readable()){
+            if ((c = pc.getc()) == 'P') {
+                buffer[readptr++] = c;
+            }
+            buffer[readptr++] = 0;
+            //pc.printf("buffer= %s\n\r ",buffer);  //imprime el bufer
+            //pc.printf("buffer= %c  %c\n\r ",buffer[0],buffer[1]);//imprime el cero y el uno
+        }
+        if (strncmp(buffer, "P", 1) == 0){
+            buffer[0]='\0';
+            pc.printf("* Ingrese el nuevo SetPoint: ");
+            pc.scanf("%d", &Sp);
+            if(Sp<=0){Sp=0;}
+            if(Sp > 999){Sp=999;}
+            pc.printf("Iniciando... \n\n");
+            wait_ms(1000);
+        }  
+
+        readptr = 0;
+        /////------------------------------------------------------------------
+        
+        //  se repite el ciclo
+        wait_ms(500);
+        goto lop1;
+}
\ No newline at end of file
diff -r 000000000000 -r 87552d9a8512 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Jun 12 20:06:42 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file