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:
Wed Feb 05 18:55:37 2014 +0000
Revision:
30:413d1a6648b5
Parent:
25:1910a55ff0a3
Child:
31:7e2cdd547cb2
Agregados los pines de alarma optoacopladores, aun sin probar y sin agregar codigo

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