![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Primeira versao
Dependencies: mbed EthernetInterface mbed-rtos
main.cpp@1:6c73db131ebc, 2015-03-05 (annotated)
- Committer:
- rebonatto
- Date:
- Thu Mar 05 20:38:46 2015 +0000
- Revision:
- 1:6c73db131ebc
- Parent:
- 0:6218d050b1b9
Valor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rebonatto | 1:6c73db131ebc | 1 | #include <mbed.h> |
rebonatto | 1:6c73db131ebc | 2 | #include <EthernetInterface.h> |
rebonatto | 0:6218d050b1b9 | 3 | |
rebonatto | 1:6c73db131ebc | 4 | #include "Definitions.h" |
rebonatto | 1:6c73db131ebc | 5 | #include "calculos.c" |
rebonatto | 0:6218d050b1b9 | 6 | |
rebonatto | 1:6c73db131ebc | 7 | EthernetInterface eth; |
rebonatto | 1:6c73db131ebc | 8 | TCPSocketConnection socket; |
rebonatto | 0:6218d050b1b9 | 9 | |
rebonatto | 0:6218d050b1b9 | 10 | LocalFileSystem local("local"); |
rebonatto | 1:6c73db131ebc | 11 | AnalogIn captura(p20); |
rebonatto | 0:6218d050b1b9 | 12 | DigitalOut myled(LED1); |
rebonatto | 0:6218d050b1b9 | 13 | |
rebonatto | 1:6c73db131ebc | 14 | void IniciaEthernet(void); |
rebonatto | 0:6218d050b1b9 | 15 | int gravaAmostras(float *vet, int amostras); |
rebonatto | 0:6218d050b1b9 | 16 | int gravaDFT(float *seno, float *coss, int amostras); |
rebonatto | 1:6c73db131ebc | 17 | int sendMessage(float *vet, int amostras); |
rebonatto | 1:6c73db131ebc | 18 | |
rebonatto | 0:6218d050b1b9 | 19 | int main() { |
rebonatto | 0:6218d050b1b9 | 20 | float vet[AMOSTRAS]; |
rebonatto | 0:6218d050b1b9 | 21 | //float controle[AMOSTRAS]; |
rebonatto | 1:6c73db131ebc | 22 | //float seno[AMOSTRAS]; |
rebonatto | 1:6c73db131ebc | 23 | //float coss[AMOSTRAS]; |
rebonatto | 0:6218d050b1b9 | 24 | float vm; |
rebonatto | 0:6218d050b1b9 | 25 | int i, flag =0; |
rebonatto | 0:6218d050b1b9 | 26 | Timer t; |
rebonatto | 0:6218d050b1b9 | 27 | unsigned short int valor, maior=0, menor=0; |
rebonatto | 0:6218d050b1b9 | 28 | float espera,rms; |
rebonatto | 0:6218d050b1b9 | 29 | float media = 0; |
rebonatto | 0:6218d050b1b9 | 30 | |
rebonatto | 1:6c73db131ebc | 31 | IniciaEthernet(); |
rebonatto | 1:6c73db131ebc | 32 | |
rebonatto | 0:6218d050b1b9 | 33 | printf("CONVERSAOAD %1.10f\n", CONVERSAOAD); |
rebonatto | 1:6c73db131ebc | 34 | printf("PORTA p16 Diferencial 1\n"); |
rebonatto | 0:6218d050b1b9 | 35 | printf("Ciclos (HZ) %d\n", CICLO); |
rebonatto | 0:6218d050b1b9 | 36 | printf("Amostras %d\n", AMOSTRAS); |
rebonatto | 0:6218d050b1b9 | 37 | printf("Intervalo %1.10f\n", INTERVALO); |
rebonatto | 0:6218d050b1b9 | 38 | |
rebonatto | 1:6c73db131ebc | 39 | for(int j =0; j < 5000; j++){ |
rebonatto | 0:6218d050b1b9 | 40 | t.start(); |
rebonatto | 0:6218d050b1b9 | 41 | for(i=0; i < AMOSTRAS; i++){ |
rebonatto | 0:6218d050b1b9 | 42 | //controle[i] = t.read(); |
rebonatto | 0:6218d050b1b9 | 43 | valor = captura.read_u16(); |
rebonatto | 1:6c73db131ebc | 44 | vet[i] = (float) valor;//(float) (valor - 33575) / 10830; //acquire the value and convert the analog to digital |
rebonatto | 1:6c73db131ebc | 45 | |
rebonatto | 0:6218d050b1b9 | 46 | if(i == 0) |
rebonatto | 1:6c73db131ebc | 47 | maior = menor = vet[i]; |
rebonatto | 1:6c73db131ebc | 48 | if (valor > vet[i]) |
rebonatto | 1:6c73db131ebc | 49 | maior = vet[i]; |
rebonatto | 1:6c73db131ebc | 50 | if (valor < vet[i]) |
rebonatto | 1:6c73db131ebc | 51 | menor = vet[i]; |
rebonatto | 1:6c73db131ebc | 52 | media += vet[i]; |
rebonatto | 1:6c73db131ebc | 53 | |
rebonatto | 0:6218d050b1b9 | 54 | espera = (INTERVALO * (i+1)) - t.read(); |
rebonatto | 0:6218d050b1b9 | 55 | wait(espera); |
rebonatto | 0:6218d050b1b9 | 56 | } |
rebonatto | 1:6c73db131ebc | 57 | rms = RMS(vet, AMOSTRAS); |
rebonatto | 1:6c73db131ebc | 58 | printf("Acabou captura. Tempo %1.10f RMS %1.19f\n", t.read(), rms ); |
rebonatto | 0:6218d050b1b9 | 59 | |
rebonatto | 1:6c73db131ebc | 60 | for(i=0; i < AMOSTRAS; i++) |
rebonatto | 1:6c73db131ebc | 61 | printf("%.0f*", vet[i]); |
rebonatto | 1:6c73db131ebc | 62 | printf("\n"); |
rebonatto | 0:6218d050b1b9 | 63 | //printf("Tempo de Captura %1.10f\n", t.read()); |
rebonatto | 1:6c73db131ebc | 64 | /* |
rebonatto | 0:6218d050b1b9 | 65 | media=0; |
rebonatto | 0:6218d050b1b9 | 66 | for(i=0; i < AMOSTRAS; i++){ |
rebonatto | 0:6218d050b1b9 | 67 | if(i == 0) |
rebonatto | 0:6218d050b1b9 | 68 | maior = menor = valor; |
rebonatto | 0:6218d050b1b9 | 69 | if (vet[i] > maior) |
rebonatto | 0:6218d050b1b9 | 70 | maior = (unsigned short int) vet[i]; |
rebonatto | 0:6218d050b1b9 | 71 | if (vet[i] < menor) |
rebonatto | 0:6218d050b1b9 | 72 | menor = (unsigned short int) vet[i]; |
rebonatto | 0:6218d050b1b9 | 73 | media += vet[i]; |
rebonatto | 0:6218d050b1b9 | 74 | } |
rebonatto | 1:6c73db131ebc | 75 | */ |
rebonatto | 0:6218d050b1b9 | 76 | //t.reset(); |
rebonatto | 0:6218d050b1b9 | 77 | //vm = DFT(vet, seno, coss); |
rebonatto | 0:6218d050b1b9 | 78 | //CalculateFFT(vet, seno, coss, &vm, 1, 0); |
rebonatto | 0:6218d050b1b9 | 79 | //gravaDFT(seno, coss, AMOSTRAS); |
rebonatto | 0:6218d050b1b9 | 80 | //printf("Calculou DFT em %fs\n", t.read()); |
rebonatto | 1:6c73db131ebc | 81 | |
rebonatto | 1:6c73db131ebc | 82 | /* |
rebonatto | 1:6c73db131ebc | 83 | media /= AMOSTRAS; |
rebonatto | 1:6c73db131ebc | 84 | printf("[%d] Calculado RMS %1.10f VM %1.10f\n", j, rms, media); |
rebonatto | 1:6c73db131ebc | 85 | |
rebonatto | 1:6c73db131ebc | 86 | if (rms > LIMITE){ |
rebonatto | 0:6218d050b1b9 | 87 | flag++; |
rebonatto | 1:6c73db131ebc | 88 | /* |
rebonatto | 0:6218d050b1b9 | 89 | if (flag > 1){ |
rebonatto | 1:6c73db131ebc | 90 | sendMessage(vet, AMOSTRAS); |
rebonatto | 1:6c73db131ebc | 91 | printf("Calculado RMS Enviado %f\n", rms); |
rebonatto | 0:6218d050b1b9 | 92 | break; |
rebonatto | 0:6218d050b1b9 | 93 | } |
rebonatto | 1:6c73db131ebc | 94 | |
rebonatto | 0:6218d050b1b9 | 95 | } |
rebonatto | 1:6c73db131ebc | 96 | */ |
rebonatto | 1:6c73db131ebc | 97 | //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); |
rebonatto | 0:6218d050b1b9 | 98 | |
rebonatto | 0:6218d050b1b9 | 99 | //wait(5); |
rebonatto | 0:6218d050b1b9 | 100 | /* |
rebonatto | 0:6218d050b1b9 | 101 | for(i=0; i < AMOSTRAS; i++){ |
rebonatto | 0:6218d050b1b9 | 102 | printf("[%d] Valor %f\tTempo %1.10f\n", i, vet[i], controle[i]); |
rebonatto | 0:6218d050b1b9 | 103 | } |
rebonatto | 0:6218d050b1b9 | 104 | float maior =-10; |
rebonatto | 0:6218d050b1b9 | 105 | float media = 0; |
rebonatto | 0:6218d050b1b9 | 106 | for(i=2; i < AMOSTRAS; i++){ |
rebonatto | 0:6218d050b1b9 | 107 | espera = (controle[i] - controle[i-1] - INTERVALO); |
rebonatto | 0:6218d050b1b9 | 108 | media += espera; |
rebonatto | 0:6218d050b1b9 | 109 | if (espera > 0.000001){ |
rebonatto | 0:6218d050b1b9 | 110 | //printf("[%d] Diferenca %f\n", i, espera); |
rebonatto | 0:6218d050b1b9 | 111 | if (espera > maior) |
rebonatto | 0:6218d050b1b9 | 112 | maior = espera; |
rebonatto | 0:6218d050b1b9 | 113 | } |
rebonatto | 0:6218d050b1b9 | 114 | } |
rebonatto | 0:6218d050b1b9 | 115 | printf("[%d] MAior difrenca %1.10f Media %1.10f\n", j, maior, (float) media/AMOSTRAS); |
rebonatto | 0:6218d050b1b9 | 116 | */ |
rebonatto | 0:6218d050b1b9 | 117 | } |
rebonatto | 0:6218d050b1b9 | 118 | printf("FINAL"); |
rebonatto | 0:6218d050b1b9 | 119 | while(1) { |
rebonatto | 0:6218d050b1b9 | 120 | myled = 1; |
rebonatto | 0:6218d050b1b9 | 121 | wait(0.2); |
rebonatto | 0:6218d050b1b9 | 122 | myled = 0; |
rebonatto | 0:6218d050b1b9 | 123 | wait(0.2); |
rebonatto | 0:6218d050b1b9 | 124 | } |
rebonatto | 0:6218d050b1b9 | 125 | } |
rebonatto | 0:6218d050b1b9 | 126 | |
rebonatto | 1:6c73db131ebc | 127 | |
rebonatto | 1:6c73db131ebc | 128 | void IniciaEthernet(void){ |
rebonatto | 1:6c73db131ebc | 129 | eth.init("192.168.103.2","255.255.255.0","192.168.0.1"); |
rebonatto | 1:6c73db131ebc | 130 | |
rebonatto | 1:6c73db131ebc | 131 | //EthernetIf::Connect(); |
rebonatto | 1:6c73db131ebc | 132 | eth.connect(); |
rebonatto | 1:6c73db131ebc | 133 | //printf("IP Address is %s\n", EthernetIf::get_IpAddress()); |
rebonatto | 1:6c73db131ebc | 134 | printf("IP Address is %s\n", eth.getIPAddress()); |
rebonatto | 1:6c73db131ebc | 135 | |
rebonatto | 0:6218d050b1b9 | 136 | } |
rebonatto | 0:6218d050b1b9 | 137 | |
rebonatto | 0:6218d050b1b9 | 138 | |
rebonatto | 1:6c73db131ebc | 139 | int sendMessage(float *vet, int amostras){ |
rebonatto | 1:6c73db131ebc | 140 | int i, ret; |
rebonatto | 1:6c73db131ebc | 141 | float num; |
rebonatto | 0:6218d050b1b9 | 142 | |
rebonatto | 1:6c73db131ebc | 143 | char *msg; |
rebonatto | 1:6c73db131ebc | 144 | char aux[9]; |
rebonatto | 0:6218d050b1b9 | 145 | |
rebonatto | 1:6c73db131ebc | 146 | TCPSocketConnection sock; |
rebonatto | 1:6c73db131ebc | 147 | sock.connect("192.168.103.1", 12345); |
rebonatto | 1:6c73db131ebc | 148 | //printf("Nova Malloc4\n"); |
rebonatto | 1:6c73db131ebc | 149 | msg = (char *)malloc((amostras/2*9*sizeof(char)) + 1); |
rebonatto | 1:6c73db131ebc | 150 | if (msg == NULL){ |
rebonatto | 1:6c73db131ebc | 151 | printf("Sem memoria\n"); |
rebonatto | 0:6218d050b1b9 | 152 | } |
rebonatto | 1:6c73db131ebc | 153 | msg[0] = '\0'; |
rebonatto | 1:6c73db131ebc | 154 | printf("\n\n\nprimeira parte\n"); |
rebonatto | 1:6c73db131ebc | 155 | for(i=0; i<amostras/2; i++){ |
rebonatto | 1:6c73db131ebc | 156 | num = vet[i]; |
rebonatto | 1:6c73db131ebc | 157 | sprintf(aux, "%08X;", *(unsigned int*)&num ) ; |
rebonatto | 1:6c73db131ebc | 158 | if (i == 0 || i == 100 || i == 500 || i == 1023) |
rebonatto | 1:6c73db131ebc | 159 | printf("%d\t%s\n", i, aux); |
rebonatto | 0:6218d050b1b9 | 160 | |
rebonatto | 1:6c73db131ebc | 161 | strcat(msg, aux); |
rebonatto | 1:6c73db131ebc | 162 | } |
rebonatto | 1:6c73db131ebc | 163 | ret = sock.send(msg, strlen(msg)) ; |
rebonatto | 0:6218d050b1b9 | 164 | |
rebonatto | 1:6c73db131ebc | 165 | if (ret != -1){ |
rebonatto | 1:6c73db131ebc | 166 | msg[0] = '\0'; |
rebonatto | 1:6c73db131ebc | 167 | printf("\n\n\nsegunda parte\n"); |
rebonatto | 1:6c73db131ebc | 168 | for(i=amostras/2; i<amostras; i++){ |
rebonatto | 1:6c73db131ebc | 169 | num = vet[i]; |
rebonatto | 1:6c73db131ebc | 170 | sprintf(aux, "%08X;", *(unsigned int*)&num ) ; |
rebonatto | 1:6c73db131ebc | 171 | if (i == 1024 || i == 1124 || i == 1524 || i == 2047) |
rebonatto | 1:6c73db131ebc | 172 | printf("%d\t%s\n", i, aux); |
rebonatto | 1:6c73db131ebc | 173 | strcat(msg, aux); |
rebonatto | 1:6c73db131ebc | 174 | } |
rebonatto | 1:6c73db131ebc | 175 | ret = sock.send(msg, strlen(msg)) ; |
rebonatto | 1:6c73db131ebc | 176 | //printf("[%s]\n", msg); |
rebonatto | 1:6c73db131ebc | 177 | //sock.close(); |
rebonatto | 1:6c73db131ebc | 178 | } |
rebonatto | 1:6c73db131ebc | 179 | |
rebonatto | 1:6c73db131ebc | 180 | if (ret != -1) |
rebonatto | 1:6c73db131ebc | 181 | return (1); |
rebonatto | 0:6218d050b1b9 | 182 | |
rebonatto | 1:6c73db131ebc | 183 | return (0); |
rebonatto | 0:6218d050b1b9 | 184 | } |
rebonatto | 0:6218d050b1b9 | 185 | |
rebonatto | 0:6218d050b1b9 | 186 | |
rebonatto | 0:6218d050b1b9 | 187 | int gravaDFT(float *seno, float *coss, int amostras){ |
rebonatto | 0:6218d050b1b9 | 188 | int i; |
rebonatto | 0:6218d050b1b9 | 189 | FILE *f = fopen("/local/resultados.txt","w"); |
rebonatto | 0:6218d050b1b9 | 190 | |
rebonatto | 0:6218d050b1b9 | 191 | if(f == NULL) |
rebonatto | 0:6218d050b1b9 | 192 | { |
rebonatto | 0:6218d050b1b9 | 193 | printf("Error creating file resultados.txt\r\n"); |
rebonatto | 0:6218d050b1b9 | 194 | return 0; |
rebonatto | 0:6218d050b1b9 | 195 | } |
rebonatto | 0:6218d050b1b9 | 196 | |
rebonatto | 0:6218d050b1b9 | 197 | fprintf(f, "Arquivo de Resultados\n"); |
rebonatto | 0:6218d050b1b9 | 198 | for(i=0; i < amostras; i++){ |
rebonatto | 0:6218d050b1b9 | 199 | fprintf(f,"%1.4f\t%1.4f\n", seno[i], coss[i]); |
rebonatto | 0:6218d050b1b9 | 200 | } |
rebonatto | 0:6218d050b1b9 | 201 | fprintf(f,"\r\n"); |
rebonatto | 0:6218d050b1b9 | 202 | fclose(f); |
rebonatto | 0:6218d050b1b9 | 203 | return (1); |
rebonatto | 0:6218d050b1b9 | 204 | } |
rebonatto | 0:6218d050b1b9 | 205 | |
rebonatto | 0:6218d050b1b9 | 206 | |
rebonatto | 0:6218d050b1b9 | 207 | int gravaAmostras(float *vet, int amostras){ |
rebonatto | 0:6218d050b1b9 | 208 | int i; |
rebonatto | 0:6218d050b1b9 | 209 | FILE *f = fopen("/local/amostras.txt","w"); |
rebonatto | 0:6218d050b1b9 | 210 | |
rebonatto | 0:6218d050b1b9 | 211 | if(f == NULL) |
rebonatto | 0:6218d050b1b9 | 212 | { |
rebonatto | 0:6218d050b1b9 | 213 | printf("Error creating file amostras.txt\r\n"); |
rebonatto | 0:6218d050b1b9 | 214 | return 0; |
rebonatto | 0:6218d050b1b9 | 215 | } |
rebonatto | 0:6218d050b1b9 | 216 | |
rebonatto | 0:6218d050b1b9 | 217 | fprintf(f, "Arquivo de Amostras\n"); |
rebonatto | 0:6218d050b1b9 | 218 | for(i=0; i < amostras; i++){ |
rebonatto | 0:6218d050b1b9 | 219 | fprintf(f,"%1.4f\n", vet[i]); |
rebonatto | 0:6218d050b1b9 | 220 | printf("%1.4f*", vet[i]); |
rebonatto | 0:6218d050b1b9 | 221 | } |
rebonatto | 0:6218d050b1b9 | 222 | fprintf(f,"\r\n"); |
rebonatto | 0:6218d050b1b9 | 223 | fclose(f); |
rebonatto | 0:6218d050b1b9 | 224 | return (1); |
rebonatto | 0:6218d050b1b9 | 225 | } |
rebonatto | 0:6218d050b1b9 | 226 | |
rebonatto | 0:6218d050b1b9 | 227 | |
rebonatto | 1:6c73db131ebc | 228 |