Programa para llenar un tanque de una cisterna por medio de un control de cámara sony. el sistema puede emitir sonidos por medio de un módulo de voz de Parallax EMIC.

Dependencies:   Pulse1 mbed

Fork of Sanitario_IRDA by Gustavo Ramirez

Committer:
tony63
Date:
Thu May 05 06:34:08 2016 +0000
Revision:
7:79b0dc5d537e
Parent:
6:8cc9f4849678
Sistema de llenado autom?tico de sanitarios con agua gris. Para hacer la apertura de la v?lvula de llenado se usa un control infrarrojo Sony. el sistema detecta si no fluye agua por la tuber?a y da un aviso por voz. El sistema usa sensor de flujo.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tony63 0:74d57f8ae247 1 #include "mbed.h"
tony63 0:74d57f8ae247 2 #include <Pulse1.h>
tony63 4:150bc6a5f5b4 3 #include "stdio.h"
tony63 4:150bc6a5f5b4 4 //control remoto sony disparo camara
tony63 4:150bc6a5f5b4 5 //puede convertir la trama en un entero
tony63 4:150bc6a5f5b4 6 //para cualquier tecla
tony63 7:79b0dc5d537e 7 InterruptIn H2O(PTA13);//sensor de pulsos
tony63 2:6a15ab0305c8 8 PulseInOut irda(PTD5);// en este puerto se pone el sensor infrarrojo
tony63 0:74d57f8ae247 9 Serial pc(USBTX, USBRX);
tony63 7:79b0dc5d537e 10 //DigitalOut ledrojo(LED3);
tony63 7:79b0dc5d537e 11 //DigitalOut ledverde(LED2);
tony63 7:79b0dc5d537e 12 //DigitalOut ledazul(LED1);
tony63 7:79b0dc5d537e 13 DigitalOut LedVerde(PTE4);//leds sobre la FRDMKL25Z
tony63 7:79b0dc5d537e 14 DigitalOut LedRojo(PTE2);
tony63 7:79b0dc5d537e 15 DigitalOut LedAzul(PTE5);
tony63 7:79b0dc5d537e 16
tony63 7:79b0dc5d537e 17 DigitalOut dir(PTD6); //puerto dem puente H
tony63 7:79b0dc5d537e 18 DigitalOut breake(PTD7);//puerto del puente H
tony63 7:79b0dc5d537e 19 Timer t; //Configura el timer
tony63 2:6a15ab0305c8 20 int header =0; //tiempo de cabecera pulso abajo
tony63 4:150bc6a5f5b4 21 const int head_H = 2976; //+10% medida con osciloscopio en microsegundos
tony63 4:150bc6a5f5b4 22 const int head_L = 1984;//-10% medida con osciloscopio
tony63 4:150bc6a5f5b4 23 int i=0, bin, potencia;
tony63 4:150bc6a5f5b4 24 const int T_alto=1200;//ponga su tiempo de la prueba
tony63 4:150bc6a5f5b4 25 const int T_bajo=639;//ponga su tiempo de la prueba
tony63 4:150bc6a5f5b4 26 const int num_bits = 20;//ponga su numero de bits
tony63 2:6a15ab0305c8 27 int num[num_bits];//cadena para almacenar todos los tiempos que conforman los bits de datos
tony63 2:6a15ab0305c8 28 int dato; // tiempo de cada dato que se lee
tony63 7:79b0dc5d537e 29 int tiempo,j,f,k;
tony63 4:150bc6a5f5b4 30 int numero;
tony63 4:150bc6a5f5b4 31 int binM[20];
tony63 4:150bc6a5f5b4 32 int bin_max[20];
tony63 7:79b0dc5d537e 33 Serial GSM(PTE0,PTE1); //puertos del FRDM para el modulo de voz Emic 2 (parallax) pasa de texto a voz
tony63 7:79b0dc5d537e 34
tony63 7:79b0dc5d537e 35 //***********************configuro funciones que seran llamadas por las interupciones******************************************
tony63 7:79b0dc5d537e 36 //que se disparan con los dos flancos de los pulsos del sensor de flujo de agua*****************
tony63 7:79b0dc5d537e 37
tony63 7:79b0dc5d537e 38 void H2O_Cae(void) //esta me dispara el timer iniciando en 0.0000, se llama con el flanco de caida
tony63 7:79b0dc5d537e 39 {
tony63 7:79b0dc5d537e 40 t.reset();
tony63 7:79b0dc5d537e 41 t.start();
tony63 7:79b0dc5d537e 42 i=0; //note que si i=0 es por que se dio pulso completo
tony63 7:79b0dc5d537e 43 }
tony63 4:150bc6a5f5b4 44
tony63 7:79b0dc5d537e 45 void H2O_Sube(void) //esta detiene el timer (primero) y luego lee el valor en microsegundos, con tiempo en valor entero
tony63 7:79b0dc5d537e 46 {
tony63 7:79b0dc5d537e 47 t.stop();
tony63 7:79b0dc5d537e 48 tiempo=t.read_us();
tony63 7:79b0dc5d537e 49
tony63 7:79b0dc5d537e 50 }
tony63 7:79b0dc5d537e 51
tony63 7:79b0dc5d537e 52 //*********************************************Inicia el programa principal****************************************************
tony63 0:74d57f8ae247 53 int main(){
tony63 7:79b0dc5d537e 54 LedAzul=1;
tony63 7:79b0dc5d537e 55 LedVerde=1;
tony63 7:79b0dc5d537e 56 LedRojo=1;
tony63 7:79b0dc5d537e 57 LedAzul=0;
tony63 7:79b0dc5d537e 58 __disable_irq(); // inabilito interupciones
tony63 5:3036b626379b 59 GSM.baud(9600);
tony63 7:79b0dc5d537e 60 GSM.format(8,Serial::None,1);//formato de la comunicacion serie
tony63 7:79b0dc5d537e 61 breake=0; //cero es moverse (sin freno)
tony63 7:79b0dc5d537e 62 dir=0; //direccion de cerrado
tony63 7:79b0dc5d537e 63 wait(5); //hay que esperar
tony63 7:79b0dc5d537e 64 breake=1; //frenarse
tony63 7:79b0dc5d537e 65 LedAzul=1;
tony63 7:79b0dc5d537e 66 LedRojo=0;
tony63 7:79b0dc5d537e 67 //*************se indaga por las posibles interupciones********************************************************************
tony63 7:79b0dc5d537e 68 H2O.fall(&H2O_Cae);
tony63 7:79b0dc5d537e 69 H2O.rise(&H2O_Sube);
tony63 7:79b0dc5d537e 70 //************************************se configura la parte ciclica del programa puede ser un ciclo inutil***idle cycle****
tony63 7:79b0dc5d537e 71 GSM.printf("N1\r\n"); //comando del modulo emic2
tony63 7:79b0dc5d537e 72 wait(1);//espero un poco
tony63 7:79b0dc5d537e 73 GSM.printf("W230\r\n");//comando del modulo emic2
tony63 7:79b0dc5d537e 74 wait(1);
tony63 7:79b0dc5d537e 75 GSM.printf("L2\r\n");//comando del moduloemic2
tony63 7:79b0dc5d537e 76 wait(1);
tony63 7:79b0dc5d537e 77 tiempo=0; //hacerlo cero para, evitar releerlo
tony63 7:79b0dc5d537e 78
tony63 0:74d57f8ae247 79 while(1){
tony63 7:79b0dc5d537e 80
tony63 7:79b0dc5d537e 81 ini1: fflush( stdin ); //borrar bufer serial por si toca leer cadenas
tony63 7:79b0dc5d537e 82 header=0;//cabecera en cero
tony63 3:82bebaf2a06a 83 header = irda.read_low_us(); //funcion para leer un pulso de caida o bajo en header
tony63 2:6a15ab0305c8 84 if (header > head_L && header < head_H) goto seguir;//verificar que este en la tolerancia +-20%
tony63 7:79b0dc5d537e 85 goto ini1;
tony63 2:6a15ab0305c8 86 seguir:
tony63 7:79b0dc5d537e 87 LedVerde=0;
tony63 7:79b0dc5d537e 88 //leo los datos de la trama y se meten a un arreglo
tony63 5:3036b626379b 89 wait_us(40);// ES EL TIEMPO DE HEADER QUE NO SE Lee O EL ALTO
tony63 2:6a15ab0305c8 90 for(i=0;i<(num_bits-1);++i){ // POR OSCILOSCOPIO se determina que llegan (num_bits),datos
tony63 4:150bc6a5f5b4 91 dato = irda.read_low_us(); //leer un bit de datos que es pulso arriba en este control
tony63 2:6a15ab0305c8 92 num[i]=dato;
tony63 7:79b0dc5d537e 93 wait_us(400);//insero algo de tiempo entre bits para rechazar ruido
tony63 2:6a15ab0305c8 94 }
tony63 7:79b0dc5d537e 95 wait(0.1); //espero un poquito luego de leer todo el arreglo y ponerlo en pantalla
tony63 4:150bc6a5f5b4 96 //pc.printf(",%d",header);
tony63 2:6a15ab0305c8 97 for(i=0;i<(num_bits-1);++i){
tony63 4:150bc6a5f5b4 98 //pc.printf(",%d",num[i]);
tony63 2:6a15ab0305c8 99 }
tony63 2:6a15ab0305c8 100 wait(0.1); //espero e imprimo en binario
tony63 4:150bc6a5f5b4 101 //pc.printf("\n\n");
tony63 2:6a15ab0305c8 102 for(i=0;i<(num_bits-1);++i){
tony63 4:150bc6a5f5b4 103 if(num[i] > ((T_alto+T_bajo)/2)){
tony63 4:150bc6a5f5b4 104 bin_max[i]=1;
tony63 4:150bc6a5f5b4 105 }
tony63 4:150bc6a5f5b4 106 else{
tony63 4:150bc6a5f5b4 107 bin_max[i]=0;
tony63 4:150bc6a5f5b4 108 }
tony63 4:150bc6a5f5b4 109
tony63 4:150bc6a5f5b4 110 }
tony63 4:150bc6a5f5b4 111 binM[0]=1;
tony63 4:150bc6a5f5b4 112 for(i=1;i<(num_bits-1);++i){
tony63 4:150bc6a5f5b4 113 binM[i]=binM[i-1]*2;
tony63 4:150bc6a5f5b4 114
tony63 4:150bc6a5f5b4 115 }
tony63 4:150bc6a5f5b4 116 numero=0;
tony63 4:150bc6a5f5b4 117 for(i=0;i<(num_bits-1);++i){
tony63 4:150bc6a5f5b4 118
tony63 4:150bc6a5f5b4 119 numero=numero+(binM[i]*bin_max[i]);
tony63 4:150bc6a5f5b4 120
tony63 2:6a15ab0305c8 121 }
tony63 4:150bc6a5f5b4 122 pc.printf("numero=%d",numero);
tony63 7:79b0dc5d537e 123 if (numero==466221){//si el codigo es el esperado inicio apertura de la valvula y mensajes de voz
tony63 7:79b0dc5d537e 124 LedVerde=0;
tony63 7:79b0dc5d537e 125 LedRojo=1;
tony63 7:79b0dc5d537e 126 breake=0; //abro la valvula
tony63 5:3036b626379b 127 dir=1;//abrir
tony63 7:79b0dc5d537e 128 wait(4);//esperar que abra y paro la valvula
tony63 5:3036b626379b 129 breake=1;//parar
tony63 7:79b0dc5d537e 130 GSM.printf("S se inicia llenado del tanque\r\n");
tony63 7:79b0dc5d537e 131 wait(6); // espero un poco la respuesta estable del sensor de flujo
tony63 7:79b0dc5d537e 132 __enable_irq(); // Habilito interupciones
tony63 7:79b0dc5d537e 133
tony63 7:79b0dc5d537e 134 //*****en esta situacion hay que esta indagando por los pulsos de flujo para dar
tony63 7:79b0dc5d537e 135 // alarma si estos faltan que es indicio de que no pasa agua al tanque o eñll agua se ha acabado
tony63 7:79b0dc5d537e 136 // en este caso toca cerar la valvula de inmediato.
tony63 7:79b0dc5d537e 137
tony63 7:79b0dc5d537e 138 lop2: i++; //usa la variable i para detectar perdida de pulsos si i crece es por que se perdieron
tony63 7:79b0dc5d537e 139 wait_ms(400);
tony63 7:79b0dc5d537e 140 pc.printf("Tus=%d,Perdidos=%d\n",tiempo,i);//para hacer debugging con la pc
tony63 7:79b0dc5d537e 141 if(i>4){//umbral de reaccion de perdida de pulsos
tony63 7:79b0dc5d537e 142 //si no hay pulsos y la valvula esta ompletamente abierta es por que
tony63 7:79b0dc5d537e 143 //se acabo el agua o no baja del tanque
tony63 7:79b0dc5d537e 144 //cerrar la valvula inmediaramente y emitir mensaje de voz
tony63 7:79b0dc5d537e 145 GSM.printf("Shay problemas\r\n");
tony63 7:79b0dc5d537e 146 wait(2);
tony63 7:79b0dc5d537e 147 GSM.printf("Shay problemas\r\n");
tony63 7:79b0dc5d537e 148 wait(2);
tony63 7:79b0dc5d537e 149 GSM.printf("Sel agua se ha agotado o hay taponamiento\r\n");
tony63 7:79b0dc5d537e 150 wait(5);
tony63 7:79b0dc5d537e 151 GSM.printf("se va a cerrar la valvula de nuevo\r\n");
tony63 7:79b0dc5d537e 152 breake=0;
tony63 7:79b0dc5d537e 153 dir=0;//direccion de cerrado
tony63 7:79b0dc5d537e 154 LedVerde=1;
tony63 7:79b0dc5d537e 155 LedRojo=0;
tony63 7:79b0dc5d537e 156 wait(5); //esperamos a que se cierre y inabilitamos las interupciones para dar cabida
tony63 7:79b0dc5d537e 157 //sin molestias de los pulsos IRDA
tony63 7:79b0dc5d537e 158 breake=1;
tony63 7:79b0dc5d537e 159 i=4;
tony63 7:79b0dc5d537e 160 __disable_irq(); // desabilito interupciones
tony63 7:79b0dc5d537e 161 tiempo=0;
tony63 7:79b0dc5d537e 162 goto ini1;
tony63 7:79b0dc5d537e 163
tony63 7:79b0dc5d537e 164 }
tony63 7:79b0dc5d537e 165
tony63 7:79b0dc5d537e 166 else{ //no hacer nada todo esta normal
tony63 7:79b0dc5d537e 167 LedVerde=0;
tony63 7:79b0dc5d537e 168 LedRojo=1;
tony63 7:79b0dc5d537e 169 LedAzul=1;
tony63 7:79b0dc5d537e 170 k++;// cuenta ciclos de 1 ms para ajustar temporizacion
tony63 7:79b0dc5d537e 171 wait_ms(450);//espera que llene
tony63 7:79b0dc5d537e 172 if (k>70) goto cerrarOK;
tony63 7:79b0dc5d537e 173 LedVerde=0;
tony63 7:79b0dc5d537e 174 LedRojo=1;
tony63 7:79b0dc5d537e 175 goto lop2;
tony63 7:79b0dc5d537e 176 cerrarOK: __disable_irq(); // Disable Interrupts
tony63 7:79b0dc5d537e 177 dir=0;//CERRAR
tony63 7:79b0dc5d537e 178 k=0;
tony63 7:79b0dc5d537e 179 breake=0;
tony63 7:79b0dc5d537e 180 LedVerde=1;
tony63 7:79b0dc5d537e 181 LedRojo=0;
tony63 7:79b0dc5d537e 182 wait(5);//esperar que cierre
tony63 7:79b0dc5d537e 183 breake=1;//parar
tony63 7:79b0dc5d537e 184 GSM.printf("Sel tanque esta lleno\r\n");
tony63 7:79b0dc5d537e 185 wait(3);
tony63 7:79b0dc5d537e 186 GSM.printf("Spuede vaciar el sanitario\r\n");
tony63 7:79b0dc5d537e 187 wait(3);
tony63 7:79b0dc5d537e 188 GSM.printf("Sno olvide limpiarse muy bien su trasero\r\n");
tony63 7:79b0dc5d537e 189 wait(4);
tony63 7:79b0dc5d537e 190 GSM.printf("Srecuerde que lo estoy viendo ja ja ja ja ja ja ja ja\r\n");
tony63 7:79b0dc5d537e 191 goto ini1;
tony63 7:79b0dc5d537e 192 } //i>3
tony63 7:79b0dc5d537e 193 }//chequeo numero
tony63 7:79b0dc5d537e 194
tony63 7:79b0dc5d537e 195 //chequeo el oro numero
tony63 7:79b0dc5d537e 196 if (numero==466231){
tony63 7:79b0dc5d537e 197 LedAzul=0;
tony63 7:79b0dc5d537e 198 wait(2);
tony63 7:79b0dc5d537e 199 LedAzul=1;
tony63 4:150bc6a5f5b4 200 goto ini1;
tony63 7:79b0dc5d537e 201 }
tony63 7:79b0dc5d537e 202
tony63 7:79b0dc5d537e 203 //*****************************************
tony63 7:79b0dc5d537e 204 } // while
tony63 7:79b0dc5d537e 205 } //main