encoder

Dependencies:   QEI TextLCD-modificada mbed

Files at this revision

API Documentation at this revision

Comitter:
salondonog
Date:
Fri Nov 15 17:45:21 2013 +0000
Commit message:
encoder

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
QEI.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD.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	Fri Nov 15 17:45:21 2013 +0000
@@ -0,0 +1,93 @@
+#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();
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.h	Fri Nov 15 17:45:21 2013 +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/QEI.lib	Fri Nov 15 17:45:21 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/aberk/code/QEI/#5c2ad81551aa
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Fri Nov 15 17:45:21 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/salondonog/code/TextLCD-modificada/#74616aa6c0c7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Nov 15 17:45:21 2013 +0000
@@ -0,0 +1,194 @@
+#include "mbed.h"
+#include "TextLCD.h"
+#include "DebouncedIn.h"
+#include "QEI.h"
+
+TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5);  //configura los puestos PTE0,PTE1,PTE2,PTE3,PTE4,PTE5, donde se conectara el LCD 16x2
+QEI conteo(PTA13,PTD5,NC,12);
+DebouncedIn salto(PTC16);
+DebouncedIn set(PTC17);
+AnalogIn Vin(PTB0);
+AnalogOut Vout(PTE30);
+
+Timer t;
+int flag;
+int q;
+int C1=0x0E;    //configurar el lcd para mostrar el guin bajo
+int C2=0x0C;    //configurar el lcd para no mostrar el guion bajo
+int s=0;       //set point
+int p=0;       //ganancia proporcional
+int i=0;       //ganancia integral
+int d=0;       //ganancia derivativa
+int k=0;       //variable de control}
+int err, med, yr, ap, ai, ad, err_v, cycle;
+int pid;
+int viejo=0;
+int cont=0;
+
+void def_posicion(int h){
+    if (h==0){
+    lcd.locate(3,0); lcd.printf("%d",s);
+    lcd.locate(3,0); 
+    }
+    else if(h==1){
+    lcd.locate(11,0);   lcd.printf("%d",p);
+    lcd.locate(11,0);   
+    }
+    else if (h==2){
+    lcd.locate(3,1); lcd.printf("%d",i);
+    lcd.locate(3,1); 
+    }
+    else {
+    lcd.locate(11,1);   lcd.printf("%d",d);
+    lcd.locate(11,1);  
+    }
+    }
+
+
+void incremento (int r) {            //Funcion que incremente los valores 
+if(k==0){
+        s+=r;
+        if(s<=0){
+        s=0;
+        }
+        else if (s>=999){
+        s=999;
+        }
+        lcd.locate(3,0);            
+        lcd.printf("%d ",s);
+      }
+else if(k==1){
+        p+=r;
+        if(p<=0){
+        p=0;
+        }
+        else if (p>=999){
+        p=999;
+        }
+        lcd.locate(11,0);            
+        lcd.printf("%d ",p);
+      }
+else if(k==2){
+        i+=r;
+        if(i<=0){
+        i=0;
+        }
+        else if (i>=999){
+        i=999;
+        }
+        lcd.locate(3,1);            
+        lcd.printf("%d ",i);  
+      }
+else{
+        d+=r;
+        if(d<=0){
+        d=0;
+        }
+        else if (d>=999){
+        d=999;
+        }
+        lcd.locate(11,1);            
+        lcd.printf("%d ",d);
+    }
+ }
+ 
+ 
+void star_patch1(void){  //funcion que imprime los caracteres que no van a variar en el display
+lcd.cls();
+lcd.locate(8,0);
+lcd.printf("Kp=%d",p);
+lcd.locate(0,1);
+lcd.printf("Ki=%d",i);
+lcd.locate(8,1);
+lcd.printf("Kd=%d",d);
+lcd.writeCommand(C1);   //permite que el cursor se vea y sea intermitente
+lcd.locate(0,0); 
+lcd.printf("Sp=%d",s);
+}
+
+void star_patch2(void){  //funcion que imprime los caracteres que no van a variar en el display
+lcd.writeCommand(C2);
+lcd.cls();
+lcd.printf("***INICIA PID***");    lcd.printf("***************");
+wait(2);
+lcd.cls();
+lcd.printf("Er%=d",err);
+lcd.locate(8,0);    lcd.printf("Me=%d",med); 
+lcd.locate(0,1);    lcd.printf("Sp=%d",s);
+lcd.locate(8,1);    lcd.printf("Co=%d",pid);
+wait(3);
+}
+
+int main(){
+star_patch1();
+viejo=0;
+cont=0;
+while(1){
+    if(k>=3 && salto.falling()){
+    k=0;    
+    def_posicion(k);
+    }  
+    if(salto.falling()){  
+    ++k;
+    def_posicion(k);
+    }
+    else if(set.falling()){
+        break;
+    }
+    viejo=conteo.getPulses()-cont;
+    cont=conteo.getPulses();
+    if(viejo==0){
+    //no hace nada
+    }
+    else {
+    incremento(viejo);
+    }}
+    
+star_patch2();
+
+while (1){
+
+med=Vin.read()*1000;
+err = (s-med);
+ap = k*err;           
+ai =(i*err)+ai;    //calculo de la integral del error
+// se verifica que la accion integral no sea muy grande
+ad = d*(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;
+}
+Vout.write(pid/1000);
+if(flag==0){
+t.start();
+flag=1;
+}
+if (t>0.3){
+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",s);
+lcd.locate(11,1);
+lcd.printf("     ");
+lcd.locate(11,1);
+lcd.printf("%d ",pid);
+t.reset();
+flag=0;
+wait(.1);
+}
+// se actualizan las variables *******************************************
+err_v = err;
+}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Nov 15 17:45:21 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f
\ No newline at end of file