Primeira versao
Dependencies: mbed EthernetInterface mbed-rtos
Revision 1:6c73db131ebc, committed 2015-03-05
- Comitter:
- rebonatto
- Date:
- Thu Mar 05 20:38:46 2015 +0000
- Parent:
- 0:6218d050b1b9
- Commit message:
- Valor
Changed in this revision
diff -r 6218d050b1b9 -r 6c73db131ebc Definitions.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Definitions.h Thu Mar 05 20:38:46 2015 +0000 @@ -0,0 +1,18 @@ +/* +* Definitions.h +* Definicoes de constantes +*/ + +#define CONVERSAOAD 0.000021 + +#define CICLO 60//1 +#define AMOSTRAS 256//2048 +//#define INTERVALO (float) (1/CICLO)/AMOSTRAS +#define INTERVALO 0.000065104//0.0004882812 + +#define LIMITE 0.07 + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <string.h>
diff -r 6218d050b1b9 -r 6c73db131ebc EthernetInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Thu Mar 05 20:38:46 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#e6b79f0ccd95
diff -r 6218d050b1b9 -r 6c73db131ebc calculos.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/calculos.c Thu Mar 05 20:38:46 2015 +0000 @@ -0,0 +1,220 @@ +/* +* Arquivo calculos.c +* Conten rotinas para calculo do RMS, DFT e FFT +*/ + +#include "Definitions.h" + +#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr +#define PI 3.14159265358979323846F + +float RMS(float *vet, int amostras); +void CalculateFFT(float *buffer, float *sen, float *cos, float *vm, int sign, int ch); +float* ComplexFFT(float* data, int sign, int ch); +float DFT(float *data, float *seno, float *coss); + +float RMS(float *vet, int amostras){ + int i; + float aux, soma=0; + + for(i=0; i < amostras; i++){ + aux = vet[i] * vet[i]; + soma += aux; + } + soma = (float) soma / amostras; + return (sqrt(soma)); +} + +void CalculateFFT(float *buffer, float *sen, float *cos, float *vm, int sign, int ch) +{ + int i; + //float value[256]; + /* + printf("Tamanho float %lu\n", sizeof(float)); + printf("Tamanho double %lu\n", sizeof(double)); + printf("Tamanho unsigned short int %lu\n", sizeof(unsigned short int)); + printf("Tamanho unsigned long %lu\n", sizeof(unsigned long)); + printf("Tamanho unsigned long long %lu\n", sizeof(unsigned long long)); + + + for(int i=0; i < Settings::get_Samples(); i++) + printf("%d*",buffer[i]); + printf("\n"); + */ + //printf("[0] %d %d %d %d\n", buffer[0], buffer[100], buffer[200], buffer[255]); + /* + for(i=0; i<Settings::get_Samples();i++) + value[i]= (float) ( (buffer[i] - Settings::get_Offset(ch)) / Settings::get_Gain(ch) ); + */ + + printf("Chegou chamada\n"); + float* fft = ComplexFFT(buffer,1, 0); //deve desalocar memoria do ptr retornado + + /* + Mapa do vetor fft. + O vetor tem 2 vezes o no. de amostras. Cada par de valores (portanto n e n+1), representam, respectivamente + COS e SEN. + Os dois primeiros valores reprensetam a frequencia 0Hz, portanto sao atribuidas ao valor medio. + Os demais pares de valores representam a fundamental e suas harmonicas, + sendo que se a fundamental for 60Hz, teremos: 60,120,180,240... + Para a nossa aplicacao apenas as 12 primeiras harmonicas serao utilizadas (720Hz) + */ + + //*vm = DFT(value, sen, cos); + *vm = fft[0]; + + for(int i=1;i<AMOSTRAS/2;i++) + { + cos[i-1] = fft[i*2]; + sen[i-1] = fft[i*2+1]; + } + + for(int i=0;i<AMOSTRAS;i++) + { + printf("[%dHz]\tsen %.4f\tcos %.4f\n", (i+1)*60, sen[i], cos[i]); + if (i > 100) + break; + } + + free(fft); + //printf("[3] %d %d %d %d\n", buffer[0], buffer[100], buffer[200], buffer[255]); +} + + +float* ComplexFFT(float* data, int sign, int ch) +{ + + //variables for the fft + unsigned long n,mmax,m,j,istep,i; + //double wtemp,wr,wpr,wpi,wi,theta,tempr,tempi; + float wtemp,wr,wpr,wpi,wi,theta,tempr,tempi; + float *vector; + //the complex array is real+complex so the array + //as a size n = 2* number of complex samples + //real part is the data[index] and + //the complex part is the data[index+1] + + //new complex array of size n=2*sample_rate + //if(vector==0) + //vector=(float*)malloc(2*SAMPLE_RATE*sizeof(float)); era assim, define estava em Capture.h + + printf("Chegou compex\n"); + + vector=(float*)malloc(2*AMOSTRAS*sizeof(float)); + if (vector == NULL) + printf("Sem memoria\n"); + memset(vector,0,2*AMOSTRAS*sizeof(float)); + + //put the real array in a complex array + //the complex part is filled with 0's + //the remaining vector with no data is filled with 0's + //for(n=0; n<SAMPLE_RATE;n++)era assim, define estava em Capture.h + + printf("Alocou\n"); + + for(n=0; n<AMOSTRAS;n++) + { + if(n<AMOSTRAS){ + vector[2*n]= (float) data[n] ; + // printf("%.4f$", vector[2*n]); + } + else + vector[2*n]=0; + vector[2*n+1]=0; + } + + printf("\n"); + + //printf("[1] %d %d %d %d\n", data[0], data[100], data[200], data[255]); + + //binary inversion (note that the indexes + //start from 0 witch means that the + //real part of the complex is on the even-indexes + //and the complex part is on the odd-indexes) + //n=SAMPLE_RATE << 1; //multiply by 2era assim, define estava em Capture.h + n=AMOSTRAS << 1; //multiply by 2 + j=0; + for (i=0;i<n/2;i+=2) { + if (j > i) { + SWAP(vector[j],vector[i]); + SWAP(vector[j+1],vector[i+1]); + if((j/2)<(n/4)){ + SWAP(vector[(n-(i+2))],vector[(n-(j+2))]); + SWAP(vector[(n-(i+2))+1],vector[(n-(j+2))+1]); + } + } + m=n >> 1; + while (m >= 2 && j >= m) { + j -= m; + m >>= 1; + } + j += m; + } + //end of the bit-reversed order algorithm + + //Danielson-Lanzcos routine + mmax=2; + while (n > mmax) { + istep=mmax << 1; + theta=sign*(2*PI/mmax); + wtemp=sin(0.5*theta); + wpr = -2.0*wtemp*wtemp; + wpi=sin(theta); + wr=1.0; + wi=0.0; + for (m=1;m<mmax;m+=2) { + for (i=m;i<=n;i+=istep) { + j=i+mmax; + tempr=wr*vector[j-1]-wi*vector[j]; + tempi=wr*vector[j]+wi*vector[j-1]; + vector[j-1]=vector[i-1]-tempr; + vector[j]=vector[i]-tempi; + vector[i-1] += tempr; + vector[i] += tempi; + } + wr=(wtemp=wr)*wpr-wi*wpi+wr; + wi=wi*wpr+wtemp*wpi+wi; + } + mmax=istep; + } + //end of the algorithm + + // Ajustes a FFT + for(i = 0; i < AMOSTRAS; i++ ){ + vector[i] = (float) ((2 * vector[i]) / AMOSTRAS ); + /* + if (i % 2 == 1) + vector[i] = vector[i] * -1; + */ + } + + //printf("[2] %d %d %d %d\n", data[0], data[100], data[200], data[255]); + + return vector; +} + +float DFT(float *data, float *seno, float *coss){ + int i, j; + + printf("Entrou DFT\n"); + + for(i=0; i < AMOSTRAS; i++) + seno[i] = coss[i] = 0; + + for(i=0; i < AMOSTRAS; i++){ + for(j = 0; j < AMOSTRAS; j++ ){ + coss[j] += (data[i] * (cos( (2 * PI * i * j) / AMOSTRAS ) ) ) ; + seno[j] += (data[i] * (sin( (2 * PI * i * j) / AMOSTRAS ) ) ) ; + } + } + printf("Primeiro Laco\n"); + for(j = 1; j < AMOSTRAS; j++ ){ + coss[j] = 2 * coss[j] / AMOSTRAS; + seno[j] = 2 * seno[j] / AMOSTRAS ; + } + + printf("Segundo Laco\n"); + coss[0] = coss[0] / AMOSTRAS; + seno[0] = seno[0] / AMOSTRAS; + return (float) (coss[0] + seno[0] ); +}
diff -r 6218d050b1b9 -r 6c73db131ebc main.cpp --- a/main.cpp Wed Jun 25 14:56:54 2014 +0000 +++ b/main.cpp Thu Mar 05 20:38:46 2015 +0000 @@ -1,34 +1,26 @@ -#include "mbed.h" -#include "stdio.h" - -#define CONVERSAOAD 0.000021 +#include <mbed.h> +#include <EthernetInterface.h> -#define CICLO 1 -#define AMOSTRAS 2048 -//#define INTERVALO (float) (1/CICLO)/AMOSTRAS -#define INTERVALO 0.0004882812 +#include "Definitions.h" +#include "calculos.c" -#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr -#define PI 3.14159265358979323846F +EthernetInterface eth; +TCPSocketConnection socket; LocalFileSystem local("local"); - -AnalogIn captura(p16); - +AnalogIn captura(p20); DigitalOut myled(LED1); -float RMS(float *vet, int amostras); +void IniciaEthernet(void); int gravaAmostras(float *vet, int amostras); -void CalculateFFT(float *buffer, float *sen, float *cos, float *vm, int sign, int ch); -float* ComplexFFT(float* data, int sign, int ch); -float DFT(float *data, float *seno, float *coss); int gravaDFT(float *seno, float *coss, int amostras); - +int sendMessage(float *vet, int amostras); + int main() { float vet[AMOSTRAS]; //float controle[AMOSTRAS]; - float seno[AMOSTRAS]; - float coss[AMOSTRAS]; + //float seno[AMOSTRAS]; + //float coss[AMOSTRAS]; float vm; int i, flag =0; Timer t; @@ -36,33 +28,40 @@ float espera,rms; float media = 0; + IniciaEthernet(); + printf("CONVERSAOAD %1.10f\n", CONVERSAOAD); - printf("PORTA p16 Diferencial 2\n"); + printf("PORTA p16 Diferencial 1\n"); printf("Ciclos (HZ) %d\n", CICLO); printf("Amostras %d\n", AMOSTRAS); printf("Intervalo %1.10f\n", INTERVALO); - for(int j =0; j < 50; j++){ + for(int j =0; j < 5000; j++){ t.start(); for(i=0; i < AMOSTRAS; i++){ //controle[i] = t.read(); valor = captura.read_u16(); - vet[i] = (float) (valor - 33575) / 10830; //acquire the value and convert the analog to digital -/* + vet[i] = (float) valor;//(float) (valor - 33575) / 10830; //acquire the value and convert the analog to digital + if(i == 0) - maior = menor = valor; - if (valor > maior) - maior = valor; - if (valor < menor) - menor = valor; - */ + maior = menor = vet[i]; + if (valor > vet[i]) + maior = vet[i]; + if (valor < vet[i]) + menor = vet[i]; + media += vet[i]; + espera = (INTERVALO * (i+1)) - t.read(); wait(espera); } - printf("Acabou captura. Tempo %1.10f\n", t.read() ); + rms = RMS(vet, AMOSTRAS); + printf("Acabou captura. Tempo %1.10f RMS %1.19f\n", t.read(), rms ); + for(i=0; i < AMOSTRAS; i++) + printf("%.0f*", vet[i]); + printf("\n"); //printf("Tempo de Captura %1.10f\n", t.read()); - + /* media=0; for(i=0; i < AMOSTRAS; i++){ if(i == 0) @@ -73,23 +72,29 @@ menor = (unsigned short int) vet[i]; media += vet[i]; } - + */ //t.reset(); //vm = DFT(vet, seno, coss); //CalculateFFT(vet, seno, coss, &vm, 1, 0); //gravaDFT(seno, coss, AMOSTRAS); //printf("Calculou DFT em %fs\n", t.read()); - rms = RMS(vet, AMOSTRAS); - if (rms > 0.6){ + + /* + media /= AMOSTRAS; + printf("[%d] Calculado RMS %1.10f VM %1.10f\n", j, rms, media); + + if (rms > LIMITE){ flag++; + /* if (flag > 1){ - gravaAmostras(vet, AMOSTRAS); + sendMessage(vet, AMOSTRAS); + printf("Calculado RMS Enviado %f\n", rms); break; - printf("Gravou amostras\n"); - flag = -1000; } + } - printf("[%d] RMS %1.8f Menor %d Maior %d Media %1.8f VM %1.8f\n", j, RMS(vet, AMOSTRAS), menor, maior, vm, (float) media/AMOSTRAS); + */ + //printf("[%d] RMS %1.8f Menor %d Maior %d Media %1.8f VM %1.8f\n", j, RMS(vet, AMOSTRAS), menor, maior, vm, (float) media/AMOSTRAS); //wait(5); /* @@ -119,199 +124,65 @@ } } -void CalculateFFT(float *buffer, float *sen, float *cos, float *vm, int sign, int ch) -{ - int i; - //float value[256]; - /* - printf("Tamanho float %lu\n", sizeof(float)); - printf("Tamanho double %lu\n", sizeof(double)); - printf("Tamanho unsigned short int %lu\n", sizeof(unsigned short int)); - printf("Tamanho unsigned long %lu\n", sizeof(unsigned long)); - printf("Tamanho unsigned long long %lu\n", sizeof(unsigned long long)); - - - for(int i=0; i < Settings::get_Samples(); i++) - printf("%d*",buffer[i]); - printf("\n"); - */ - //printf("[0] %d %d %d %d\n", buffer[0], buffer[100], buffer[200], buffer[255]); - /* - for(i=0; i<Settings::get_Samples();i++) - value[i]= (float) ( (buffer[i] - Settings::get_Offset(ch)) / Settings::get_Gain(ch) ); - */ - - printf("Chegou chamada\n"); - float* fft = ComplexFFT(buffer,1, 0); //deve desalocar memoria do ptr retornado - - /* - Mapa do vetor fft. - O vetor tem 2 vezes o no. de amostras. Cada par de valores (portanto n e n+1), representam, respectivamente - COS e SEN. - Os dois primeiros valores reprensetam a frequencia 0Hz, portanto sao atribuidas ao valor medio. - Os demais pares de valores representam a fundamental e suas harmonicas, - sendo que se a fundamental for 60Hz, teremos: 60,120,180,240... - Para a nossa aplicacao apenas as 12 primeiras harmonicas serao utilizadas (720Hz) - */ - - //*vm = DFT(value, sen, cos); - *vm = fft[0]; - - for(int i=1;i<AMOSTRAS/2;i++) - { - cos[i-1] = fft[i*2]; - sen[i-1] = fft[i*2+1]; - } - - for(int i=0;i<AMOSTRAS;i++) - { - printf("[%dHz]\tsen %.4f\tcos %.4f\n", (i+1)*60, sen[i], cos[i]); - if (i > 100) - break; - } - - free(fft); - //printf("[3] %d %d %d %d\n", buffer[0], buffer[100], buffer[200], buffer[255]); + +void IniciaEthernet(void){ + eth.init("192.168.103.2","255.255.255.0","192.168.0.1"); + + //EthernetIf::Connect(); + eth.connect(); + //printf("IP Address is %s\n", EthernetIf::get_IpAddress()); + printf("IP Address is %s\n", eth.getIPAddress()); + } -float* ComplexFFT(float* data, int sign, int ch) -{ - - //variables for the fft - unsigned long n,mmax,m,j,istep,i; - //double wtemp,wr,wpr,wpi,wi,theta,tempr,tempi; - float wtemp,wr,wpr,wpi,wi,theta,tempr,tempi; - float *vector; - //the complex array is real+complex so the array - //as a size n = 2* number of complex samples - //real part is the data[index] and - //the complex part is the data[index+1] - - //new complex array of size n=2*sample_rate - //if(vector==0) - //vector=(float*)malloc(2*SAMPLE_RATE*sizeof(float)); era assim, define estava em Capture.h - - printf("Chegou compex\n"); +int sendMessage(float *vet, int amostras){ + int i, ret; + float num; - vector=(float*)malloc(2*AMOSTRAS*sizeof(float)); - if (vector == NULL) - printf("Sem memoria\n"); - memset(vector,0,2*AMOSTRAS*sizeof(float)); - - //put the real array in a complex array - //the complex part is filled with 0's - //the remaining vector with no data is filled with 0's - //for(n=0; n<SAMPLE_RATE;n++)era assim, define estava em Capture.h - - printf("Alocou\n"); - - for(n=0; n<AMOSTRAS;n++) - { - if(n<AMOSTRAS){ - vector[2*n]= (float) data[n] ; - // printf("%.4f$", vector[2*n]); - } - else - vector[2*n]=0; - vector[2*n+1]=0; - } - - printf("\n"); - - //printf("[1] %d %d %d %d\n", data[0], data[100], data[200], data[255]); + char *msg; + char aux[9]; - //binary inversion (note that the indexes - //start from 0 witch means that the - //real part of the complex is on the even-indexes - //and the complex part is on the odd-indexes) - //n=SAMPLE_RATE << 1; //multiply by 2era assim, define estava em Capture.h - n=AMOSTRAS << 1; //multiply by 2 - j=0; - for (i=0;i<n/2;i+=2) { - if (j > i) { - SWAP(vector[j],vector[i]); - SWAP(vector[j+1],vector[i+1]); - if((j/2)<(n/4)){ - SWAP(vector[(n-(i+2))],vector[(n-(j+2))]); - SWAP(vector[(n-(i+2))+1],vector[(n-(j+2))+1]); - } - } - m=n >> 1; - while (m >= 2 && j >= m) { - j -= m; - m >>= 1; - } - j += m; + TCPSocketConnection sock; + sock.connect("192.168.103.1", 12345); + //printf("Nova Malloc4\n"); + msg = (char *)malloc((amostras/2*9*sizeof(char)) + 1); + if (msg == NULL){ + printf("Sem memoria\n"); } - //end of the bit-reversed order algorithm + msg[0] = '\0'; + printf("\n\n\nprimeira parte\n"); + for(i=0; i<amostras/2; i++){ + num = vet[i]; + sprintf(aux, "%08X;", *(unsigned int*)&num ) ; + if (i == 0 || i == 100 || i == 500 || i == 1023) + printf("%d\t%s\n", i, aux); - //Danielson-Lanzcos routine - mmax=2; - while (n > mmax) { - istep=mmax << 1; - theta=sign*(2*PI/mmax); - wtemp=sin(0.5*theta); - wpr = -2.0*wtemp*wtemp; - wpi=sin(theta); - wr=1.0; - wi=0.0; - for (m=1;m<mmax;m+=2) { - for (i=m;i<=n;i+=istep) { - j=i+mmax; - tempr=wr*vector[j-1]-wi*vector[j]; - tempi=wr*vector[j]+wi*vector[j-1]; - vector[j-1]=vector[i-1]-tempr; - vector[j]=vector[i]-tempi; - vector[i-1] += tempr; - vector[i] += tempi; - } - wr=(wtemp=wr)*wpr-wi*wpi+wr; - wi=wi*wpr+wtemp*wpi+wi; - } - mmax=istep; - } - //end of the algorithm + strcat(msg, aux); + } + ret = sock.send(msg, strlen(msg)) ; - // Ajustes a FFT - for(i = 0; i < AMOSTRAS; i++ ){ - vector[i] = (float) ((2 * vector[i]) / AMOSTRAS ); - /* - if (i % 2 == 1) - vector[i] = vector[i] * -1; - */ - } + if (ret != -1){ + msg[0] = '\0'; + printf("\n\n\nsegunda parte\n"); + for(i=amostras/2; i<amostras; i++){ + num = vet[i]; + sprintf(aux, "%08X;", *(unsigned int*)&num ) ; + if (i == 1024 || i == 1124 || i == 1524 || i == 2047) + printf("%d\t%s\n", i, aux); + strcat(msg, aux); + } + ret = sock.send(msg, strlen(msg)) ; + //printf("[%s]\n", msg); + //sock.close(); + } + + if (ret != -1) + return (1); - //printf("[2] %d %d %d %d\n", data[0], data[100], data[200], data[255]); - - return vector; + return (0); } -float DFT(float *data, float *seno, float *coss){ - int i, j; - - printf("Entrou DFT\n"); - - for(i=0; i < AMOSTRAS; i++) - seno[i] = coss[i] = 0; - - for(i=0; i < AMOSTRAS; i++){ - for(j = 0; j < AMOSTRAS; j++ ){ - coss[j] += (data[i] * (cos( (2 * PI * i * j) / AMOSTRAS ) ) ) ; - seno[j] += (data[i] * (sin( (2 * PI * i * j) / AMOSTRAS ) ) ) ; - } - } - printf("Primeiro Laco\n"); - for(j = 1; j < AMOSTRAS; j++ ){ - coss[j] = 2 * coss[j] / AMOSTRAS; - seno[j] = 2 * seno[j] / AMOSTRAS ; - } - - printf("Segundo Laco\n"); - coss[0] = coss[0] / AMOSTRAS; - seno[0] = seno[0] / AMOSTRAS; - return (float) (coss[0] + seno[0] ); -} int gravaDFT(float *seno, float *coss, int amostras){ int i; @@ -354,14 +225,4 @@ } -float RMS(float *vet, int amostras){ - int i; - float aux, soma=0; - - for(i=0; i < amostras; i++){ - aux = vet[i] * vet[i]; - soma += aux; - } - soma = (float) soma / amostras; - return (sqrt(soma)); -} +
diff -r 6218d050b1b9 -r 6c73db131ebc mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Thu Mar 05 20:38:46 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#ac8d036315ed