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

Dependencies:   EthernetNetIf HTTPServer QEI_hw RPCInterface mbed

Committer:
Yo_Robot
Date:
Thu Apr 11 04:12:57 2013 +0000
Revision:
24:a1d16835201c
Parent:
23:2126e38bb48c
Child:
25:1910a55ff0a3
Antes del cambio a Hardware Encoder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yo_Robot 20:4b154134ab20 1 /*******************************************************************\
Yo_Robot 20:4b154134ab20 2 * *
Yo_Robot 20:4b154134ab20 3 * DESLIZADOR LINEAL UTN *
Yo_Robot 20:4b154134ab20 4 * *
Yo_Robot 20:4b154134ab20 5 * Archivo: main.cpp *
Yo_Robot 20:4b154134ab20 6 * Autor: Ernesto Palacios <meacatronica.mid@gmail.com> *
Yo_Robot 22:d5431fff164b 7 * Version: v2.2 *
Yo_Robot 20:4b154134ab20 8 * Dependencias: setup.h, setup.cpp *
Yo_Robot 22:d5431fff164b 9 * Descripcion: Este es el archivo principal para el microcon- *
Yo_Robot 23:2126e38bb48c 10 * trolador mbed para la comunicacion y control *
Yo_Robot 20:4b154134ab20 11 * del deslizador *
Yo_Robot 20:4b154134ab20 12 * Fecha: Ibarra, 05 de junio de 2012 *
Yo_Robot 20:4b154134ab20 13 * *
Yo_Robot 20:4b154134ab20 14 *******************************************************************/
Yo_Robot 3:8d5a9e3cd680 15
Yo_Robot 3:8d5a9e3cd680 16 #include "mbed.h"
Yo_Robot 3:8d5a9e3cd680 17 #include "setup.h"
Yo_Robot 8:958dfe5052b9 18 #include "EthernetNetIf.h"
Yo_Robot 8:958dfe5052b9 19 #include "HTTPServer.h"
Yo_Robot 8:958dfe5052b9 20 #include "RPCFunction.h"
Yo_Robot 8:958dfe5052b9 21 #include "RPCVariable.h"
Yo_Robot 20:4b154134ab20 22
Yo_Robot 23:2126e38bb48c 23 // Configuraci&#65533;n de Entradas / Salidas
Yo_Robot 9:6976ac1a430e 24
Yo_Robot 18:cf1e07d82630 25 Serial pc( USBTX, USBRX ); //Comunicacion Serial directa al computador
Yo_Robot 18:cf1e07d82630 26 Serial RS_232(p9, p10); //Comunicacion Serial para MAX232
Yo_Robot 22:d5431fff164b 27 I2C encoder(p28, p27, "i2c_mbed"); //Comunicacion I2C para los encoders
Yo_Robot 23:2126e38bb48c 28 Timer crono; // Reloj Interno de mbed
Yo_Robot 18:cf1e07d82630 29
Yo_Robot 18:cf1e07d82630 30 DigitalOut pin_son( p7 ); // SON
Yo_Robot 18:cf1e07d82630 31 DigitalOut pin_dir( p26 ); // SIGN+
Yo_Robot 18:cf1e07d82630 32 InterruptIn pin_alm( p8 ); // ALM
Yo_Robot 20:4b154134ab20 33 AnalogOut aout( p18 ); // VOUT
Yo_Robot 24:a1d16835201c 34 //PIN_06 pto_generator // PULS+
Yo_Robot 9:6976ac1a430e 35
Yo_Robot 16:d50665706f21 36 DigitalIn isHTTP( p15 ); // Modo Ethernet/Serial
Yo_Robot 16:d50665706f21 37 DigitalIn isFast( p16 ); // Serial Alta velocidad/Baja Velocidad
Yo_Robot 18:cf1e07d82630 38 DigitalIn isPC ( p17 ); // Salida a compuador o MAX232
Yo_Robot 18:cf1e07d82630 39
Yo_Robot 19:c26cf8a48986 40 DigitalOut led_verde( p21 ); // Led verde del conector Ethernet
Yo_Robot 19:c26cf8a48986 41 DigitalOut led_rojo( p22 ); // Led naranja del conector Ethernet
Yo_Robot 19:c26cf8a48986 42
Yo_Robot 20:4b154134ab20 43 // Objetos Ethernet
Yo_Robot 18:cf1e07d82630 44
Yo_Robot 18:cf1e07d82630 45 EthernetNetIf eth;
Yo_Robot 18:cf1e07d82630 46 HTTPServer svr;
Yo_Robot 17:a4f380bc2516 47 LocalFileSystem fs("local");
Yo_Robot 5:c5aea1eb10bb 48
Yo_Robot 8:958dfe5052b9 49
Yo_Robot 20:4b154134ab20 50 // Anadir funciones al Protocolo RPC:
Yo_Robot 20:4b154134ab20 51 //
Yo_Robot 20:4b154134ab20 52 // estas son las funciones de acceso de la libreria HTTP_ASM sobre
Yo_Robot 20:4b154134ab20 53 // el Protocolo RPC
Yo_Robot 12:c02b08dacc45 54 RPCFunction SetFQ (&setPTO_eth, "PTO");
Yo_Robot 12:c02b08dacc45 55 RPCFunction SetAOUT(&setAout_eth, "AOUT");
Yo_Robot 12:c02b08dacc45 56 RPCFunction SetDIR (&setDir_eth, "DIR");
Yo_Robot 12:c02b08dacc45 57 RPCFunction SetSON (&setSON_eth, "SON");
Yo_Robot 16:d50665706f21 58 RPCFunction SetANG (&setANG_eth, "ANG");
Yo_Robot 16:d50665706f21 59 RPCFunction SetSpd (&setSPD_eth, "VAN");
Yo_Robot 22:d5431fff164b 60 RPCFunction ReadEnc(&getENC_eth, "ENC");
Yo_Robot 22:d5431fff164b 61 RPCFunction ClrEnc (&setENC_eth, "CLR");
Yo_Robot 23:2126e38bb48c 62 RPCFunction ReadRPM(&getRPM_eth, "RPM");
Yo_Robot 8:958dfe5052b9 63
Yo_Robot 3:8d5a9e3cd680 64
Yo_Robot 3:8d5a9e3cd680 65 int main() {
Yo_Robot 20:4b154134ab20 66 //*************** Configura Red Ethernet **************************************************//
Yo_Robot 20:4b154134ab20 67 if( isHTTP )
Yo_Robot 13:649543aa8b1d 68 {
Yo_Robot 20:4b154134ab20 69
Yo_Robot 22:d5431fff164b 70 // Anade clases base al protocolo RPC
Yo_Robot 13:649543aa8b1d 71 Base::add_rpc_class<AnalogIn>();
Yo_Robot 13:649543aa8b1d 72 Base::add_rpc_class<AnalogOut>();
Yo_Robot 13:649543aa8b1d 73 Base::add_rpc_class<DigitalIn>();
Yo_Robot 13:649543aa8b1d 74 Base::add_rpc_class<DigitalOut>();
Yo_Robot 13:649543aa8b1d 75 Base::add_rpc_class<DigitalInOut>();
Yo_Robot 13:649543aa8b1d 76 Base::add_rpc_class<PwmOut>();
Yo_Robot 13:649543aa8b1d 77 Base::add_rpc_class<Timer>();
Yo_Robot 13:649543aa8b1d 78 Base::add_rpc_class<BusOut>();
Yo_Robot 13:649543aa8b1d 79 Base::add_rpc_class<BusIn>();
Yo_Robot 13:649543aa8b1d 80 Base::add_rpc_class<BusInOut>();
Yo_Robot 13:649543aa8b1d 81 Base::add_rpc_class<Serial>();
Yo_Robot 17:a4f380bc2516 82
Yo_Robot 24:a1d16835201c 83 led_verde = 0; //Esperando configuracion
Yo_Robot 24:a1d16835201c 84 led_rojo = 1;
Yo_Robot 24:a1d16835201c 85
Yo_Robot 22:d5431fff164b 86 // Configura conexion Ethernet
Yo_Robot 19:c26cf8a48986 87 printf("Configurando...\n");
Yo_Robot 18:cf1e07d82630 88 EthernetErr ethErr = eth.setup();
Yo_Robot 18:cf1e07d82630 89 if(ethErr)
Yo_Robot 13:649543aa8b1d 90 {
Yo_Robot 21:353b0fe8fc54 91 printf("Error %d en la configuracion.\n", ethErr);
Yo_Robot 24:a1d16835201c 92 led_rojo = 0;
Yo_Robot 18:cf1e07d82630 93 return -1;
Yo_Robot 18:cf1e07d82630 94 }
Yo_Robot 24:a1d16835201c 95 printf("Configuracion correcta!\n");
Yo_Robot 24:a1d16835201c 96
Yo_Robot 24:a1d16835201c 97 led_verde = 1; //Configuracion correcta led verde encendido
Yo_Robot 24:a1d16835201c 98 led_rojo = 0;
Yo_Robot 24:a1d16835201c 99
Yo_Robot 18:cf1e07d82630 100 FSHandler::mount("/local", "/files"); //Monta /local en /files que es el directorio web
Yo_Robot 18:cf1e07d82630 101 FSHandler::mount("/local", "/"); //Mount /local en / que es el directorio raiz
Yo_Robot 13:649543aa8b1d 102
Yo_Robot 18:cf1e07d82630 103 //Handles para la comunicacion
Yo_Robot 13:649543aa8b1d 104 svr.addHandler<SimpleHandler>("/hello");
Yo_Robot 13:649543aa8b1d 105 svr.addHandler<RPCHandler>("/rpc");
Yo_Robot 13:649543aa8b1d 106 svr.addHandler<FSHandler>("/files");
Yo_Robot 18:cf1e07d82630 107 svr.addHandler<FSHandler>("/");
Yo_Robot 18:cf1e07d82630 108
Yo_Robot 18:cf1e07d82630 109 svr.bind(80); // Puerto de comunicacion.
Yo_Robot 18:cf1e07d82630 110
Yo_Robot 18:cf1e07d82630 111 printf("Listo para comunicacion...\n");
Yo_Robot 13:649543aa8b1d 112
Yo_Robot 18:cf1e07d82630 113 // Para probar la correcta configuracion acceder a mbed.htm
Yo_Robot 18:cf1e07d82630 114 // http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
Yo_Robot 18:cf1e07d82630 115
Yo_Robot 13:649543aa8b1d 116 }
Yo_Robot 20:4b154134ab20 117
Yo_Robot 20:4b154134ab20 118
Yo_Robot 20:4b154134ab20 119 //*************** Configura Comunicacion Serial **************************************************//
Yo_Robot 15:a1ffa32ce9d1 120 else // Caso Contrario Anadir modo Serial
Yo_Robot 8:958dfe5052b9 121 {
Yo_Robot 22:d5431fff164b 122 int fast_baud = getBaud();
Yo_Robot 20:4b154134ab20 123 if( isPC ) // Utiliza el Puerto Virtual Incluido.
Yo_Robot 19:c26cf8a48986 124 {
Yo_Robot 19:c26cf8a48986 125 pc.attach( &ISR_Serial );
Yo_Robot 20:4b154134ab20 126 if( isFast ) //Configurar Serial a alta velocidad
Yo_Robot 21:353b0fe8fc54 127 pc.baud( fast_baud );
Yo_Robot 19:c26cf8a48986 128 }
Yo_Robot 20:4b154134ab20 129 else // Utiiliza el puerto serial hacia MAX232
Yo_Robot 19:c26cf8a48986 130 {
Yo_Robot 19:c26cf8a48986 131 RS_232.attach( &ISR_Serial );
Yo_Robot 20:4b154134ab20 132 if( isFast ) //Configurar Serial a alta velocidad
Yo_Robot 21:353b0fe8fc54 133 RS_232.baud( fast_baud );
Yo_Robot 19:c26cf8a48986 134 }
Yo_Robot 8:958dfe5052b9 135 }
Yo_Robot 8:958dfe5052b9 136
Yo_Robot 20:4b154134ab20 137 //*******************************************************************************************************//
Yo_Robot 20:4b154134ab20 138
Yo_Robot 20:4b154134ab20 139
Yo_Robot 24:a1d16835201c 140 //________ Ajustes iniciales del Driver _________//
Yo_Robot 24:a1d16835201c 141
Yo_Robot 20:4b154134ab20 142 setTimer2(); // Configurar PTO
Yo_Robot 20:4b154134ab20 143 pin_son = 0; // Servo Apagado por defecto
Yo_Robot 13:649543aa8b1d 144 aout = 0.5; // Voltaje de salida 0V por defecto( +-10V! )
Yo_Robot 8:958dfe5052b9 145
Yo_Robot 21:353b0fe8fc54 146
Yo_Robot 21:353b0fe8fc54 147 pin_alm.fall( &ISR_Alarm ); // Entrada de la alarma driver
Yo_Robot 14:039d070732d5 148
Yo_Robot 13:649543aa8b1d 149 while(1)
Yo_Robot 13:649543aa8b1d 150 {
Yo_Robot 13:649543aa8b1d 151 if( isHTTP )
Yo_Robot 24:a1d16835201c 152 {
Yo_Robot 15:a1ffa32ce9d1 153 Net::poll(); // Revisa la red solo si la coneccion es HTTP
Yo_Robot 24:a1d16835201c 154 led_verde = led_verde ^ 1; // Toggle Led Verde... esperando transmicion
Yo_Robot 24:a1d16835201c 155 }
Yo_Robot 13:649543aa8b1d 156 }
Yo_Robot 8:958dfe5052b9 157 }
Yo_Robot 8:958dfe5052b9 158