Tarea3 procesadores: PID en FRDM-KL25Z con botones incrementales

Dependencies:   TextLCD-modificad mbed

Files at this revision

API Documentation at this revision

Comitter:
obifandres
Date:
Wed Apr 08 15:19:22 2015 +0000
Commit message:
Tarea 3 procesadores: PID en FKL26Z con botones de incrementales

Changed in this revision

DebouncedIn.cpp Show annotated file Show diff for this revision Revisions of this file
DebouncedIn.h Show annotated file Show diff for this revision Revisions of this file
TextLCD-modificad.lib Show annotated file Show diff for this revision Revisions of this file
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/DebouncedIn.cpp	Wed Apr 08 15:19:22 2015 +0000
@@ -0,0 +1,91 @@
+#include "DebouncedIn.h"
+#include "mbed.h"
+ 
+/*
+ * Constructor
+ */
+DebouncedIn::DebouncedIn(PinName in) 
+    : _in(in) {    
+        
+    // reset all the flags and counters    
+    _samples = 0;
+    _output = 0;
+    _output_last = 0;
+    _rising_flag = 0;
+    _falling_flag = 0;
+    _state_counter = 0;
+    
+    // Attach ticker
+    _ticker.attach(this, &DebouncedIn::_sample, 0.005);     
+}
+  
+void DebouncedIn::_sample() {
+ 
+    // take a sample
+    _samples = _samples >> 1; // shift left
+      
+    if (_in) {
+        _samples |= 0x80;
+    }  
+      
+    // examine the sample window, look for steady state
+    if (_samples == 0x00) {
+        _output = 0;
+    } 
+    else if (_samples == 0xFF) {
+        _output = 1;
+    }
+ 
+ 
+    // Rising edge detection
+    if ((_output == 1) && (_output_last == 0)) {
+        _rising_flag++;
+        _state_counter = 0;
+    }
+ 
+    // Falling edge detection
+    else if ((_output == 0) && (_output_last == 1)) {
+        _falling_flag++;
+        _state_counter = 0;
+    }
+    
+    // steady state
+    else {
+        _state_counter++;
+    }
+    
+   // update the output
+    _output_last = _output;
+    
+}
+ 
+ 
+ 
+// return number of rising edges
+int DebouncedIn::rising(void) {
+    int return_value = _rising_flag; 
+    _rising_flag = 0;
+    return(return_value);
+}
+ 
+// return number of falling edges
+int DebouncedIn::falling(void) {
+    int return_value = _falling_flag; 
+    _falling_flag = 0;
+    return(return_value);
+}
+ 
+// return number of ticsk we've bene steady for
+int DebouncedIn::steady(void) {
+return(_state_counter);
+}
+ 
+// return the debounced status
+int DebouncedIn::read(void) {
+    return(_output);
+}
+ 
+// shorthand for read()
+DebouncedIn::operator int() {
+    return read();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.h	Wed Apr 08 15:19:22 2015 +0000
@@ -0,0 +1,31 @@
+#include "mbed.h"
+ 
+    class DebouncedIn {
+        public:      
+             DebouncedIn(PinName in);
+ 
+             int read (void);
+             operator int();
+              
+             int rising(void);
+             int falling(void);
+             int steady(void);
+              
+        private :    
+               // objects
+               DigitalIn _in;    
+               Ticker _ticker;
+ 
+               // function to take a sample, and update flags
+               void _sample(void);
+ 
+               // counters and flags
+               int _samples;
+               int _output;
+               int _output_last;
+               int _rising_flag;
+               int _falling_flag;
+               int _state_counter;
+ 
+    };
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD-modificad.lib	Wed Apr 08 15:19:22 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/salondonog/code/TextLCD-modificad/#d5c3eb2b8d0e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Apr 08 15:19:22 2015 +0000
@@ -0,0 +1,488 @@
+#include "mbed.h"
+#include "TextLCD.h"
+#include "DebouncedIn.h"
+ 
+TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5);  //configura los puestos PTE0,PTE1,PTE2,PTE3,PTE4,PTE5, donde se conectara el LCD 16x2
+DebouncedIn button1(PTC12);
+DebouncedIn button2(PTC13);
+DebouncedIn button3(PTC16);
+DebouncedIn button4(PTC17);
+AnalogIn Vin(PTB0);
+AnalogOut Vout(PTE30);
+Timer t;
+int j;
+int flag;
+int q;
+int C1=0x0E; // solo muestra el cursor
+/*int C2=0x18; // desplaza izquierda
+int C3=0x1A; // desplaza derecha*/
+int C4=0x0C; // quito cursor bajo
+int sp=0;       //set point
+int kp=0;       //ganancia proporcional
+int ki=0;       //ganancia integral
+int kd=0;       //ganancia derivativa
+int k=0;       //variable de control}
+int err, med, yr, ap, ai, ad, err_v, cycle;
+int pid;
+int bb;
+
+int up(int incremento, int pos){
+if(incremento==999){
+return incremento;
+}
+else if(!button1.read()){
+    for(int t=0;t<10;t++){
+        if(button1.read()){
+        break;
+        }
+        if(incremento>995){
+        incremento=999;
+        if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+        lcd.printf("%d ",incremento);
+        break;
+        }
+    incremento++;
+    if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+    lcd.printf("%d ",incremento);    
+    wait(0.25);   
+    } 
+    
+    for(int t=0;t<10;t++){
+        if(button1.read()){
+        break;
+        }
+        if(incremento>970){
+        incremento=999;
+        if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+        lcd.printf("%d ",incremento);
+        break;
+        }
+    incremento+=10;
+    if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+    lcd.printf("%d ",incremento);    
+    wait(0.25);   
+    }
+    for(int t=0;t<20;t++){
+        if(button1.read()){
+        break;
+        }
+        if(incremento>920){
+        incremento=999;
+        if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+        lcd.printf("%d ",incremento);
+        break;
+        }
+    incremento+=100;
+    if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+    lcd.printf("%d ",incremento);    
+    wait(0.25);   
+    } 
+   } 
+   return incremento;
+   } 
+
+int down(int decremento, int pos){
+if(decremento==0){
+return decremento;
+}
+else if(!button2.read()){
+    for(int t=0;t<10;t++){
+        if(button2.read()){
+        break;
+        }
+        if(decremento<4){
+        decremento=0;
+        if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            }
+        lcd.printf("     "); 
+                if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            }
+        lcd.printf("%d ",decremento);
+        break;
+        }
+    decremento--;
+    if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+    lcd.printf("     "); 
+    if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+    lcd.printf("%d ",decremento);    
+    wait(0.25);   
+    } 
+    
+    for(int t=0;t<10;t++){
+        if(button2.read()){
+        break;
+        }
+        if(decremento<40){
+        decremento=0;
+        if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+        lcd.printf("     ");
+        if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+        lcd.printf("%d ",decremento);
+        break;
+        }
+    decremento-=10;
+    if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+    lcd.printf("     ");
+    if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+    lcd.printf("%d ",decremento);    
+    wait(0.25);   
+    }
+    for(int t=0;t<20;t++){
+        if(button2.read()){
+        break;
+        }
+        if(decremento<80){
+        decremento=0;
+        if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+        lcd.printf("     ");
+        if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+        lcd.printf("%d ",decremento);
+        break;
+        }
+    decremento-=100;
+    if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+    lcd.printf("     ");
+    if (pos==0){
+            lcd.locate(3,0); 
+            }
+            else if(pos==1){
+            lcd.locate(11,0);   
+            }
+            else if (pos==2){
+            lcd.locate(3,1);
+            }
+            else {
+            lcd.locate(11,1);
+            } 
+    lcd.printf("%d ",decremento);    
+    wait(0.25);   
+    } 
+   } 
+   return decremento;
+   } 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+int main() {
+lcd.cls();
+lcd.locate(8,0);
+lcd.printf("Kp=%d",kp);
+lcd.locate(0,1);
+lcd.printf("Ki=%d",ki);
+lcd.locate(8,1);
+lcd.printf("Kd=%d",kd);
+lcd.writeCommand(C1);   //permite que el cursor se vea y sea intermitente
+lcd.locate(0,0); 
+lcd.printf("Sp=%d",sp);
+while(1) {
+           if (button3.falling()) {              
+               ++j;
+               }                                     //INCREMENTA POSICION DEL MENU COB BOTON 3
+           if (j==0){
+               lcd.locate(3,0);
+               lcd.printf("%d",sp);
+               if(!button1.read()){
+                    sp=up(sp,j); 
+                }
+                if(!button2.read()){
+                    sp=down(sp,j); 
+                }
+                }
+           if (j==1) {
+               lcd.locate(11,0);
+               lcd.printf("%d",kp);
+                  if(!button1.read()){
+                    kp=up(kp,j); 
+                }
+               if(!button2.read()){
+                    kp=down(kp,j); 
+                }
+              }
+           if (j==2) {
+               lcd.locate(3,1);
+               lcd.printf("%d",ki);
+                if(!button1.read()){
+                    ki=up(ki,j); 
+                }
+                if(!button2.read()){
+                    ki=down(ki,j); 
+                }              
+              }
+           if (j==3) {
+               lcd.locate(11,1);
+               lcd.printf("%d",kd);
+                if(!button1.read()){
+                    kd=up(kd,j); 
+                }
+                if(!button2.read()){
+                    kd=down(kd,j); 
+                }       
+              } 
+           if (j==4) {
+               j=0;
+               }                          
+    
+           if (button4.falling()){
+           break;     //sale del bucle si pisan suiche4
+               }
+               }
+lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo
+lcd.cls(); //borra la pantalla
+lcd.printf("   GUARDADOS!"); 
+wait(2);
+lcd.cls();
+lcd.printf(" INICIA EL PID");
+wait(2);
+// se imprimen los parches del control  *****************************************
+lcd.cls();
+lcd.printf("Er=%d",err);
+lcd.locate(8,0);
+lcd.printf("Me=%d",med);
+lcd.locate(0,1);
+lcd.printf("Sp=%d",sp);
+lcd.locate(8,1);
+lcd.printf("Co=%d",pid);
+wait(5);         
+ 
+while (1){
+ 
+med=Vin.read()*1000;
+err = (sp-med);
+ap = kp*err;           
+ai =(ki*err)+ai;    //calculo de la integral del error
+ad = kd*(err-err_v); //calculo de la accion derivativa
+pid = (ap+ai+ad);
+//****se muestran las variables******************************************
+if (pid > 999){
+pid=1000;
+} 
+else if (pid<0){
+pid=0;
+}
+err_v = err;
+if(flag==0){
+t.start();
+flag=1;
+}
+if (t>=0.3){
+wait(0.02);
+lcd.locate(3,0);
+lcd.printf("     ");
+lcd.locate(3,0);
+lcd.printf("%d",err);
+lcd.locate(11,0);
+lcd.printf("     ");
+lcd.locate(11,0);
+lcd.printf("%d",med);
+lcd.locate(3,1);
+lcd.printf("     ");
+lcd.locate(3,1);
+lcd.printf("%d",sp);
+lcd.locate(11,1);
+lcd.printf("     ");
+lcd.locate(11,1);
+lcd.printf("%d ",pid);
+t.reset();
+flag=0;
+}
+Vout.write(pid/1000);
+wait(0.01);
+}
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Apr 08 15:19:22 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/487b796308b0
\ No newline at end of file