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.
Revision 0:86d8b8db5c66, committed 2019-06-14
- Comitter:
- General_Jeff
- Date:
- Fri Jun 14 20:49:18 2019 +0000
- Commit message:
- Control pid por medio de Termite
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 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jun 14 20:49:18 2019 +0000 @@ -0,0 +1,131 @@ +#include "mbed.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 +PwmOut up(PTE20);//Salida pwm + +Serial pc(USBTX, USBRX, "pc"); + +char buffer[128]; +int readptr = 0; +float pid,o,ai,ad,ap,med,err; +float err_v; +int sp=0,ki=0,kp=0,kd=0,ns,ni,np,nd; +int count; +int i = 0; + +void cleanBuffer(char *buffer, int count) //esta funcion limpia el bufer +{ + for(int i=0; i < count; i++) { + buffer[i] = '\0'; + } +} + + +int main() +{ +lop0: + char c; + pc.printf("****************************************************************\n"); + pc.printf("* Menu: *\n"); + pc.printf("* Para ingresar constantes del PID, escribir 'write' *\n"); + pc.printf("* y seguir las instrucciones. *\n"); + pc.printf("* Para finalizar la toma de datos, escribir 's'. *\n"); + pc.printf("****************************************************************\n"); + readptr=0; + + + + while( (c = pc.getc()) != '\n') //Mientras que lo que lea (que se almacena en c) sea diferente de 'nada, vacio', me meta las variables en el buffer(?) + { + buffer[readptr++] = c; + } + buffer[readptr++] = 0; //Me separa los caracteres ingresados (como une espacio), para saber cuando meto dos frases diferentes. + + if (strncmp(buffer, "write", 5) == 0){ + // perform write + pc.printf("Ingresar el Set Point\t"); + cleanBuffer(buffer,4); + pc.scanf("%d", &sp); + ns=sp/sp; + if(ns!=1){ + pc.printf("Hay un error en los parametros\n"); + goto lop0; + } + pc.printf("Ingresar la constante proporcional\t"); + cleanBuffer(buffer,4); + pc.scanf("%d", &kp); + np=kp/kp; + if(np!=1){ + pc.printf("Hay un error en los parametros\n"); + goto lop0; + } + pc.printf("Ingresar la constante derivativa\t"); + cleanBuffer(buffer,4); + pc.scanf("%d", &kd); + nd=kd/kd; + if(nd!=1){ + pc.printf("Hay un error en los parametros\n"); + goto lop0; + } + pc.printf("Ingresar la constante integral\t"); + cleanBuffer(buffer,4); + pc.scanf("%d", &ki); + ni=ki/ki; + if(ni!=1){ + pc.printf("Hay un error en los parametros\n"); + goto lop0; + } + pc.printf("Parametros completos\n"); + pc.printf("Iniciando...\n"); + wait(2); + } + readptr=0; + + +// 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); + readptr=0; + //se implementa la letra 's' para parar toma de datos + if(pc.readable()){ + if((c=pc.getc())=='s'){ + buffer[readptr++]=c; + } + buffer[readptr++]=0; + } + if (strncmp(buffer, "s", 1) == 0){ + buffer[0]='\0'; + pc.printf("Finalizando...\n"); + wait_ms(100); + goto lop0; + } + // se verifica que pid sea positivo ************************************** + if(pid<=0){ + pid=0; + } + // se verifica que pid sea menor o igual la valor maximo ***************** + if (pid > 999){ + pid=999; + } + //Normalizacion de la salida + // se actualizan las variables ******************************************* + err_v = err; + o = pid/999; + u.write(o); + up.write(o); + // se envia el valor pid a puerto analogico de salida (D/A) ************** + pc.printf("Error=%3.0f\tSetPoint=%d\tSalida=%3.0f\tMed=%3.0f\n",err,sp,pid,med); + // se repite el ciclo + wait_ms(600); + goto lop1; + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Jun 14 20:49:18 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file