Proyecto de Tesis en Mecatrónica. Universidad Técnica del Norte. Ernesto Palacios <mecatronica.mid@gmail.com>

Dependencies:   EthernetNetIf HTTPServer QEI_hw RPCInterface mbed

Revision:
22:d5431fff164b
Parent:
21:353b0fe8fc54
Child:
23:2126e38bb48c
--- a/setup.cpp	Thu Jun 21 03:34:46 2012 +0000
+++ b/setup.cpp	Mon Jul 02 04:35:28 2012 +0000
@@ -17,16 +17,42 @@
 // Salida Serial de mbed
 extern Serial       pc;
 extern Serial       RS_232;
+extern I2C          encoder;
 extern DigitalIn    isPC;
 extern DigitalOut   pin_son;   // SON
 extern DigitalOut   pin_dir;   // SIGN+
 extern InterruptIn  pin_alm;   // ALM 
 extern AnalogOut    aout;      // +-10V
+extern DigitalOut   led_verde; // Led verde del conector Ethernet
+extern DigitalOut   led_rojo; // Led naranja del conector Ethernet
 
 
 int fq_posicion = 10000;  // Variable global donde se almacenara 
                          // la velocidad de posicionamiento en Hz
 
+const int addres = 0x36;  // Direccion del enconder en modo esclavo
+
+int read_encoder()
+{
+    int count;
+    
+    char buffer[4];
+    encoder.read( addres, buffer, 1 ); // 8bit por ahora
+    count = (int) buffer[0];
+    
+    return count;
+}
+
+void clear_encoder()
+{
+    
+    char buffer[2];
+    buffer[0] = 0x01;  //Comando para borrar el contador
+    buffer[1] = 0x02; // Comando para cambiar estado de led
+    
+    encoder.write( addres, buffer, 1 );
+}
+
 void setTimer2()
 {
     // Encender Timer2 (PCONP[22])
@@ -96,9 +122,11 @@
     else if( command == 'S')
         pin_son = value;
     
+    //Setear la velocidad de Posicionamiento 
     else if( command == 'V')
       fq_posicion = value;
-      
+    
+    // Generar un numero definido de pulsos a la velocidad de posicionamiento  
     else if( command == 'G' )  
     {
         int pulsos = value;       //Numero de pulsos a generar
@@ -110,6 +138,19 @@
         wait( t_alto );             //Espera hasta llegar a la posicion
         stopTimer2();               //Posicion alcanzada ALTO. 
     }
+    
+    //Leer encoder
+    else if( command == 'E' )
+    {
+        if( isPC )
+            pc.printf( "%d",read_encoder() );
+        else
+            RS_232.printf( "%d",read_encoder() );
+    }
+    
+    //Limpiar contador encoder
+    else if( command == 'Z' )
+        clear_encoder();
 }
 
 
@@ -128,6 +169,103 @@
     }
 }
 
+
+
+void ISR_Alarm()
+{
+    pin_son = 0 ;
+    stopTimer2();
+    aout =  0.5 ;
+ 
+    if(isPC)
+        pc.printf( "AL" ); //ALARMA! solo es AL para que 
+                          //sea conciso con el modo ETH y funcione
+                         //bien en LabVIEW.
+    else
+        RS_232.printf( "AL" );
+}
+
+int getMRvalue( int fout  )
+{
+    int   toRegister;
+    
+    toRegister = (24000000 /(fout*2.0) ) -1;
+    return toRegister;
+}
+
+
+void setMR2( int newValue )
+{
+    LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas
+    LPC_TIM2->MR3 = newValue; // Para testear el programa.
+}
+
+
+
+void startTimer2()
+{
+    // Arrancer el Timer 2
+    LPC_TIM2->TCR = 1;
+}
+
+void stopTimer2()
+{
+    // Detener el Timer 2
+    LPC_TIM2->TCR = 0x2;
+}
+
+int getBaud()
+{
+        int baudios = 115200;  //Valor por defecto   
+        
+        FILE *fp = fopen("/local/config.mbd", "r"); // Abre el archivo y lo guarda en fp
+        
+        if(!fp)   // En caso de no encontrarse el archivo
+        {
+            printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n");
+            printf("Cree un archivo de texto: config.mbd dentro de la unidad Mbed\n");
+            printf("que contenga las lineas:\n\n");
+
+            printf("    1\n");
+            printf("    2\n");
+            printf("    3\n");
+            printf("    4\n");
+            printf("    baudios: 115200\n");
+
+            printf("Cambie el valor de 115200 por la velocidad a la que desea transmitir:\n");
+            printf("luego reinicie el microcontrolador\n");
+            exit(1);
+              
+        }
+        else
+        {
+            // Cadenas de caracteres desde el Archivo config.mbd
+            char notstr  [04];    // linea vacia
+            char baud    [40];    // valor en baudios
+            
+            // Leer linea a linea el archivo
+            // cuatro primeras lineas no sirven
+            fgets( notstr, 4, fp );
+            fgets( notstr, 4, fp );
+            fgets( notstr, 4, fp );
+            fgets( notstr, 4, fp );
+            
+            fgets( baud,  40, fp );
+            
+            printf("Cerrando archivo...\n");
+            fclose(fp);
+         
+            
+            // Extraer los valores numericos
+            sscanf( baud,"%*s %d",&baudios );
+            
+            
+        }   
+        
+        return baudios;
+}
+
+//    **** Funciones Liberia Ethernet  *****   //
 void setPTO_eth( char * input, char * output )
 {
     int freq = atoi( input );
@@ -181,63 +319,6 @@
         
 }
 
-
-void getENC_eth( char * input, char * output )
-{
-    //Leer el Encoder  *******  F A L T A
-    if( pin_alm == 0 )
-        sprintf( output,"Ok" );
-    else
-        sprintf( output,"AL" );    
-}
-
-
-void ISR_Alarm()
-{
-    pin_son = 0 ;
-    stopTimer2();
-    aout =  0.5 ;
- 
-    if(isPC)
-        pc.printf( "AL" ); //ALARMA! solo es AL para que 
-                          //sea conciso con el modo ETH y funcione
-                         //bien en LabVIEW.
-    else
-        RS_232.printf( "AL" );
-}
-
-int getMRvalue( int fout  )
-{
-    int   toRegister;
-    
-    toRegister = (24000000 /(fout*2.0) ) -1;
-    return toRegister;
-}
-
-
-void setMR2( int newValue )
-{
-    LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas
-    LPC_TIM2->MR3 = newValue; // Para testear el programa.
-}
-
-
-
-void startTimer2()
-{
-    // Arrancer el Timer 2
-    LPC_TIM2->TCR = 1;
-}
-
-void stopTimer2()
-{
-    // Detener el Timer 2
-    LPC_TIM2->TCR = 0x2;
-}
-
-
-//    **** Funciones Liberia Ethernet  *****   //
-
 void setAout_eth( char * input, char * output )
 {
     int    vout = atoi( input );
@@ -278,58 +359,31 @@
 }
 
 
-int getBaud();
+void getENC_eth( char * input, char * output )
 {
-        int baudios = 115200;  //Valor por defecto   
-        
-        FILE *fp = fopen("/local/config.mbd", "r"); // Abre el archivo y lo guarda en fp
-        
-        if(!fp)   // En caso de no encontrarse el archivo
-        {
-            printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n");
-            printf("Cree un archivo de texto: config.mbd dentro de la unidad Mbed\n");
-            printf("que contenga las líneas:\n\n");
+    if( pin_alm == 0 )
+        sprintf( output,"%d", read_encoder() );
+    else
+        sprintf( output,"AL" );
+    
+}
 
-            printf("    1\n");
-            printf("    2\n");
-            printf("    3\n");
-            printf("    4\n");
-            printf("    baudios: 115200\n");
 
-            printf("Cambie el valor de 115200 por la velocidad a la que desea transmitir:\n");
-            printf("luego reinicie el microcontrolador\n");
-            exit(1);
-              
-        }
-        else
-        {
-            // Cadenas de caracteres desde el Archivo config.mbd
-            char notstr  [04];    // linea vacia
-            char baud    [40];    // valor en baudios
-            
-            // Leer linea a linea el archivo
-            // cuatro primeras lineas no sirven
-            fgets( notstr, 4, fp );
-            fgets( notstr, 4, fp );
-            fgets( notstr, 4, fp );
-            fgets( notstr, 4, fp );
-            
-            fgets( baud,  40, fp );
-            
-            printf("Cerrando archivo...\n");
-            fclose(fp);
-         
-            
-            // Extraer los valores numericos
-            sscanf( baud,"%*s %d",&baudios );
-            
-            
-        }   
-        
-        return baudios;
+void setENC_eth( char * input, char * output )
+{
+    clear_encoder();
+    
+    if( pin_alm == 0 )
+        sprintf( output,"Ok" );
+    else
+        sprintf( output,"AL" );
+    
 }
 
 
+
+
+
 /*  LEGACY FUNCTIONS
  *
  *  El siguiente codigo no es utilizado por el