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

Dependencies:   EthernetNetIf HTTPServer QEI_hw RPCInterface mbed

Files at this revision

API Documentation at this revision

Comitter:
Yo_Robot
Date:
Fri Apr 06 02:19:39 2012 +0000
Parent:
9:6976ac1a430e
Child:
13:649543aa8b1d
Commit message:
v0.3 Ethernet RPC. Mismos archivos setup.h y setup.cpp para serial y ethernet

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
setup.cpp Show annotated file Show diff for this revision Revisions of this file
setup.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Apr 03 00:55:48 2012 +0000
+++ b/main.cpp	Fri Apr 06 02:19:39 2012 +0000
@@ -8,6 +8,7 @@
 #include "RPCVariable.h"
 #include "SerialRPCInterface.h"
 
+Serial     		pc( USBTX, USBRX );
 
 DigitalOut      pin_son( p30 );   // SON
 DigitalOut      pin_dir( p29 );   // SIGN+
@@ -19,12 +20,12 @@
 EthernetNetIf eth;  
 HTTPServer svr;
 
-void setAout_eth( char * input, char * output );  //Cambiar frecuencia
-void setPTO_eth( char * input, char * output );          // Encender/Apagar Pulse Train Output
-
-//Set up custom RPC
-RPCFunction SetFQ(&setPTO_eth, "PTO");
-RPCFunction SetAout(&setAout_eth, "AOUT");
+//  Añadir funciones al Protocolo RPC
+RPCFunction SetFQ  (&setPTO_eth,  "PTO");
+RPCFunction SetAOUT(&setAout_eth, "AOUT");
+RPCFunction SetDIR (&setDir_eth,  "DIR");
+RPCFunction SetSON (&setSON_eth,  "SON");
+// falta encoders y alarma.....
 
 
 int main() {
@@ -74,30 +75,3 @@
 }
 
 
-void setAout_eth( char * input, char * output )
-{
-    int    vout = atoi( input );
-    aout = (float)( vout + 100 ) / 200;   
-    sprintf( output, " Ok, Aout = %f ", aout.read() );    
-}
-
-void setPTO_eth( char * input, char * output )
-{
-    int freq = atoi( input );
-    
-    if( freq != 0 ){
-        LPC_TIM2->TC = 0x00;            // Resetear Timer
-        setMR2( getMRvalue( freq ) );  // Cambiar frefuencia
-        startTimer2();                // Iniciar Timer
-        sprintf( output, "Ok, Freq = %d", freq );
-    
-    }else{
-        stopTimer2();
-        LPC_TIM2->TC = 0x00;  // Resetear Timer
-        sprintf( output, "Ok, ALTO" );
-    }           
-}
-
-
-
-
--- a/setup.cpp	Tue Apr 03 00:55:48 2012 +0000
+++ b/setup.cpp	Fri Apr 06 02:19:39 2012 +0000
@@ -15,6 +15,10 @@
 
 // Salida Serial de mbed
 extern Serial pc;
+extern DigitalOut   pin_son;   // SON
+extern DigitalOut   pin_dir;   // SIGN+
+extern InterruptIn  pin_alm;   // ALM 
+extern AnalogOut    aout;      // +-10V
 
 
 void setTimer2()
@@ -48,16 +52,91 @@
    
 }
 
+void ISR_Serial()
+{
+    int value;        // Nuevo Valor
+    char command;     // Comando al que aplicar el nuevo valor
+
+    pc.scanf( "%d-%c", &value, &command ) ;
+    pc.printf("\n %d-%c \n", value, command );
+
+    // Establecer nueva frecuencia
+    if( command == 'H')
+        setPTO( value );
+    
+    else if( command == 'K' )
+        setPTO( value * 1000 );
+        
+    // Nuevo voltaje de salida
+    // Alguna formula para calcular el Vout necesario
+    // -100% a +100%
+    else if( command == 'A')
+        aout = (float)( value + 100.0 ) / 200.0;
+        
+        
+    // Cambiar la direccion
+    else if( command == 'D')
+        pin_dir = value;
+        
+        
+    //Encender el Servo
+    else if( command == 'S')
+        pin_son = value;
+    
+    //else if( command == 'E')
+      //  setDir( value );
+    
+}
+
+
+void setPTO( int freq )
+{
+    if( freq != 0 )
+    {
+        LPC_TIM2->TC = 0x00;  //Resetear Timer
+        setMR2( getMRvalue( freq ) ); 
+        startTimer2();
+    
+    }else{
+        
+        stopTimer2();
+        LPC_TIM2->TC = 0x00;  //Resetear Timer
+    }
+}
+
+void setPTO_eth( char * input, char * output )
+{
+    int freq = atoi( input );
+    
+    if( freq != 0 ){
+        LPC_TIM2->TC = 0x00;            // Resetear Timer
+        setMR2( getMRvalue( freq ) );  // Cambiar frefuencia
+        startTimer2();                // Iniciar Timer
+        sprintf( output, "Ok, Freq = %d", freq );
+    
+    }else{
+        stopTimer2();
+        LPC_TIM2->TC = 0x00;  // Resetear Timer
+        sprintf( output, "Ok, ALTO" );
+    }           
+}
+
+
+void ISR_Alarm()
+{
+    pin_son = 0 ;
+    stopTimer2();
+    aout =  0.5 ;
+ 
+    pc.printf( "\n\n ERROR: ALARMA \n\n " );
+ 
+}
 
 int getMRvalue( int fout  )
 {
-    float exact, error;
     int   toRegister;
     
-    exact = (24000000 /(fout*2) ) -1;
-    toRegister = exact;  // Valor redondeado;
-    error = exact - toRegister;
-//    printf( "\n\n MR value: %d\n error: %f\n" ,toRegister ,error );
+    toRegister = (24000000 /(fout*2.0) ) -1;
     return toRegister;
 }
 
@@ -82,6 +161,47 @@
     LPC_TIM2->TCR = 0x2;
 }
 
+
+//    **** Funciones Liberia Ethernet  *****   //
+
+void setAout_eth( char * input, char * output )
+{
+    int    vout = atoi( input );
+    aout = (float)( vout + 100 ) / 200;   
+    sprintf( output, " Ok, Aout = %f ", aout.read() );    
+}
+
+
+
+void setDir_eth ( char * input, char * output )
+{
+    int value = atoi( input );
+    
+    pin_dir = value;
+    
+    if( value == 0 )
+        sprintf( output,"Derecha" );
+    else
+        sprintf( output,"Izquierda" );
+}
+
+
+
+void setSON_eth ( char * input, char * output )
+{
+    int value = atoi( input );
+    
+    pin_son = value;
+    
+    if( value == 0 )
+        sprintf( output,"Servo OFF" );
+    else
+        sprintf( output,"Servo ON" );
+        
+}
+
+
+
 /*  LEGACY FUNCTIONS
  *
  *  El codigo actual no hace referencia a estas funciones
@@ -97,5 +217,3 @@
 {
     LPC_TIM2->PR = newValue; 
 }
-
-
--- a/setup.h	Tue Apr 03 00:55:48 2012 +0000
+++ b/setup.h	Fri Apr 06 02:19:39 2012 +0000
@@ -19,19 +19,22 @@
 void ISR_Serial();
 
 
-/** @brief: Esta Funcion cambia el valor del Registro 2
+/** @brief: Esta es la rutina que cambia la frecuencia
+ *  de salida del Micro en Serial
  */
-void setMR2( int newValue );
+void setPTO( int freq );
 
 
-/** @brief: Esta Funcion detiene el timer
+/** @brief: Esta es la rutina que cambia la frecuencia
+ *  de salida del Micro en comunicacion Ethernet
  */
-void stopTimer2();
+void setPTO_eth ( char * input, char * output );    
 
 
-/** @brief: Esta Funcion arranca el timer
+/** @brief: Esta es la rutina que maneja la interrupcion
+ *  de alarma
  */
-void startTimer2();
+void ISR_Alarm();
 
 
 /** @brief: Esta Funcion calcula el valor necesario
@@ -43,6 +46,41 @@
 int getMRvalue( int fout  );
 
 
+/** @brief: Esta Funcion cambia el valor del Registro 2
+ */
+void setMR2( int newValue );
+
+
+/** @brief: Esta Funcion arranca el timer
+ */
+void startTimer2();
+
+
+/** @brief: Esta Funcion detiene el timer
+ */
+void stopTimer2();
+
+
+/** @brief: Esta Funcion cambia el voltaje de salida el 
+ *  valor ingresado debe ser en porcentaje.
+ *  Desde -100% hasta +100% dependiendo de la velocidad
+ *  y el sentido de giro 
+ */
+void setAout_eth( char * input, char * output );     
+
+
+/** @brief: Cambiar Direccion @ PTO
+ */
+void setDir_eth ( char * input, char * output );    
+
+
+/** @brief: Cambiar Direccion @ PTO
+ */
+void setSON_eth ( char * input, char * output );    
+
+
+
+
 // Legacy
 void setMR3( int newValue );   
 void setPrescaler( int newValue );