Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of tarefa1 by
Tarefa1.cpp@4:ee911115dcb6, 2015-03-19 (annotated)
- Committer:
- pmcmendes
- Date:
- Thu Mar 19 12:50:26 2015 +0000
- Revision:
- 4:ee911115dcb6
- Parent:
- 3:14310c47674c
para fazer tarefa 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmcmendes | 0:cce430bf36f7 | 1 | // Mede dois sinais |
pmcmendes | 0:cce430bf36f7 | 2 | #include <stdio.h> |
pmcmendes | 0:cce430bf36f7 | 3 | |
pmcmendes | 0:cce430bf36f7 | 4 | #include <stdlib.h> |
pmcmendes | 0:cce430bf36f7 | 5 | #include "mbed.h" |
pmcmendes | 0:cce430bf36f7 | 6 | #include "math.h" // Descomente esta linha caso queira usar funções como sin(x) |
pmcmendes | 0:cce430bf36f7 | 7 | |
pmcmendes | 0:cce430bf36f7 | 8 | #define ESCALA_DE_TENSAO 3.3 |
pmcmendes | 4:ee911115dcb6 | 9 | |
pmcmendes | 4:ee911115dcb6 | 10 | #define ESCALA_DE_TENSAO_AC 3.3 |
pmcmendes | 4:ee911115dcb6 | 11 | #define ESCALA_DE_TENSAO_DC 3.3 |
pmcmendes | 0:cce430bf36f7 | 12 | #define TAM_VALORES_MEDIA_AMPLITUDE 10 |
pmcmendes | 4:ee911115dcb6 | 13 | #define LIMITE_RECONHECIMENTO_PICO 0.8 |
pmcmendes | 0:cce430bf36f7 | 14 | |
pmcmendes | 3:14310c47674c | 15 | Timer t; |
pmcmendes | 0:cce430bf36f7 | 16 | Ticker flipper; |
pmcmendes | 0:cce430bf36f7 | 17 | AnalogIn ain0(PTB0); |
pmcmendes | 0:cce430bf36f7 | 18 | AnalogIn ain1(PTB1); |
pmcmendes | 0:cce430bf36f7 | 19 | DigitalOut myled(LED1); |
pmcmendes | 0:cce430bf36f7 | 20 | int aux2 = 0; // Variavel auxiliar, para indicar quando realizar uma amostragem |
pmcmendes | 0:cce430bf36f7 | 21 | |
pmcmendes | 3:14310c47674c | 22 | typedef struct { |
pmcmendes | 3:14310c47674c | 23 | float valorMedido; |
pmcmendes | 3:14310c47674c | 24 | float tempo; |
pmcmendes | 3:14310c47674c | 25 | } Medida; |
pmcmendes | 3:14310c47674c | 26 | |
pmcmendes | 0:cce430bf36f7 | 27 | void flip() { |
pmcmendes | 0:cce430bf36f7 | 28 | aux2 = !aux2; |
pmcmendes | 0:cce430bf36f7 | 29 | } |
pmcmendes | 0:cce430bf36f7 | 30 | |
pmcmendes | 0:cce430bf36f7 | 31 | int compare (const void* a, const void * b) { |
pmcmendes | 4:ee911115dcb6 | 32 | const float *ia = (const float *) a; |
pmcmendes | 4:ee911115dcb6 | 33 | const float *ib = (const float *) b; |
pmcmendes | 4:ee911115dcb6 | 34 | |
pmcmendes | 4:ee911115dcb6 | 35 | if ((*ia) < (*ib)) return 1; |
pmcmendes | 4:ee911115dcb6 | 36 | if ((*ia) > (*ib)) return -1; |
pmcmendes | 4:ee911115dcb6 | 37 | else return 0; |
pmcmendes | 0:cce430bf36f7 | 38 | } |
pmcmendes | 0:cce430bf36f7 | 39 | |
pmcmendes | 0:cce430bf36f7 | 40 | float calculaMedia(float *vetorValores, int tamVetorValores) { |
pmcmendes | 4:ee911115dcb6 | 41 | float soma = 0; |
pmcmendes | 4:ee911115dcb6 | 42 | for (int i = 0; i < tamVetorValores; i++) { |
pmcmendes | 4:ee911115dcb6 | 43 | if (vetorValores[i] < ESCALA_DE_TENSAO) soma += vetorValores[i]; |
pmcmendes | 4:ee911115dcb6 | 44 | } |
pmcmendes | 4:ee911115dcb6 | 45 | return (soma / tamVetorValores); |
pmcmendes | 0:cce430bf36f7 | 46 | } |
pmcmendes | 0:cce430bf36f7 | 47 | |
pmcmendes | 4:ee911115dcb6 | 48 | double calculaAmplitude(float * vetorMedidas, int tamVetorMedidas) { |
pmcmendes | 4:ee911115dcb6 | 49 | const int tamValores = TAM_VALORES_MEDIA_AMPLITUDE; |
pmcmendes | 4:ee911115dcb6 | 50 | float valoresMaximos[tamValores]; |
pmcmendes | 4:ee911115dcb6 | 51 | float valoresMinimos[tamValores]; |
pmcmendes | 4:ee911115dcb6 | 52 | float mediaValoresMaximos, mediaValoresMinimos; |
pmcmendes | 4:ee911115dcb6 | 53 | qsort(vetorMedidas, tamVetorMedidas, sizeof(float), compare); |
pmcmendes | 1:6e573b77c775 | 54 | |
pmcmendes | 4:ee911115dcb6 | 55 | for (int i = 0; i < tamValores; i++) { |
pmcmendes | 4:ee911115dcb6 | 56 | valoresMaximos[i] = vetorMedidas[i]; |
pmcmendes | 4:ee911115dcb6 | 57 | valoresMinimos[i] = vetorMedidas[(tamVetorMedidas - 1) - i]; |
pmcmendes | 4:ee911115dcb6 | 58 | } |
pmcmendes | 0:cce430bf36f7 | 59 | |
pmcmendes | 4:ee911115dcb6 | 60 | mediaValoresMaximos = calculaMedia(valoresMaximos, tamValores); |
pmcmendes | 4:ee911115dcb6 | 61 | mediaValoresMinimos = calculaMedia(valoresMinimos, tamValores); |
pmcmendes | 0:cce430bf36f7 | 62 | |
pmcmendes | 4:ee911115dcb6 | 63 | float amplitude = (mediaValoresMaximos - mediaValoresMinimos); |
pmcmendes | 4:ee911115dcb6 | 64 | |
pmcmendes | 0:cce430bf36f7 | 65 | |
pmcmendes | 4:ee911115dcb6 | 66 | printf("Media superior: %.2f Media inferior: %.2f Amplitude pico-a-pico: %.2f\n\r", mediaValoresMaximos, mediaValoresMinimos, amplitude); |
pmcmendes | 4:ee911115dcb6 | 67 | return amplitude; |
pmcmendes | 1:6e573b77c775 | 68 | } |
pmcmendes | 1:6e573b77c775 | 69 | |
pmcmendes | 4:ee911115dcb6 | 70 | float * filtrarMatrizPorAmplitudeMinima(float * vetorMedidas, int tamVetorMedidas, float limite) { |
pmcmendes | 4:ee911115dcb6 | 71 | return NULL; |
pmcmendes | 4:ee911115dcb6 | 72 | } |
pmcmendes | 4:ee911115dcb6 | 73 | |
pmcmendes | 4:ee911115dcb6 | 74 | float calculaTensaoDC(float *vetorMedidas, int tamVetorMedidas) { |
pmcmendes | 4:ee911115dcb6 | 75 | return calculaMedia(vetorMedidas, tamVetorMedidas); |
pmcmendes | 4:ee911115dcb6 | 76 | } |
pmcmendes | 1:6e573b77c775 | 77 | |
pmcmendes | 4:ee911115dcb6 | 78 | float calculaTensaoACSenoidal(float *vetorMedidas, int tamVetorMedidas, float tensaoDC) { |
pmcmendes | 4:ee911115dcb6 | 79 | float amplitude = calculaAmplitude(vetorMedidas, tamVetorMedidas); |
pmcmendes | 4:ee911115dcb6 | 80 | float tensaoEficazAC = amplitude/sqrt((float) 2); |
pmcmendes | 4:ee911115dcb6 | 81 | float tensaoTotal = sqrt(tensaoEficazAC*tensaoEficazAC + tensaoDC*tensaoDC); |
pmcmendes | 4:ee911115dcb6 | 82 | //printf("Amplitude:%.2f Tensao Eficaz AC: %.2f Tensao DC: %.2f \n\r", amplitude, tensaoEficazAC, tensaoDC); |
pmcmendes | 4:ee911115dcb6 | 83 | return tensaoTotal; |
pmcmendes | 0:cce430bf36f7 | 84 | } |
pmcmendes | 0:cce430bf36f7 | 85 | |
pmcmendes | 4:ee911115dcb6 | 86 | float calculaTensaoACPeriodica(float *vetorMedidas, int tamVetorMedidas) { |
pmcmendes | 4:ee911115dcb6 | 87 | // detectar picos, tempo de execucao, preciso de periodo e velocidade |
pmcmendes | 4:ee911115dcb6 | 88 | // v = lambda * f |
pmcmendes | 4:ee911115dcb6 | 89 | // mesmo assim preciso de ideia de tempo |
pmcmendes | 4:ee911115dcb6 | 90 | float n = 0; |
pmcmendes | 4:ee911115dcb6 | 91 | for(int i = 0; i < tamVetorMedidas; i++) |
pmcmendes | 4:ee911115dcb6 | 92 | n += vetorMedidas[i]*vetorMedidas[i] * 0.0001; |
pmcmendes | 4:ee911115dcb6 | 93 | return sqrt(n / 0.1); |
pmcmendes | 4:ee911115dcb6 | 94 | |
pmcmendes | 4:ee911115dcb6 | 95 | } |
pmcmendes | 4:ee911115dcb6 | 96 | |
pmcmendes | 4:ee911115dcb6 | 97 | |
pmcmendes | 0:cce430bf36f7 | 98 | int main() |
pmcmendes | 0:cce430bf36f7 | 99 | { // valor mínimo de ts: ~0.0001 (10 kHz) |
pmcmendes | 3:14310c47674c | 100 | int contador = 0, tamanho=1000; // Tamanho dos vetores de amostras. |
pmcmendes | 0:cce430bf36f7 | 101 | float ts = 0.0001, sinal0[tamanho], sinal1[tamanho], n1= 0, sinalDC; |
pmcmendes | 4:ee911115dcb6 | 102 | wait(1); |
pmcmendes | 0:cce430bf36f7 | 103 | myled = 0; // Acende led |
pmcmendes | 0:cce430bf36f7 | 104 | flipper.attach(&flip, ts); // chama a funcao flip apos ts segundos |
pmcmendes | 0:cce430bf36f7 | 105 | while(contador<tamanho) { |
pmcmendes | 0:cce430bf36f7 | 106 | if(aux2 == 1){ |
pmcmendes | 4:ee911115dcb6 | 107 | sinal0[contador] = ain0 * ESCALA_DE_TENSAO; // lê sinais analógicos de dois canais |
pmcmendes | 4:ee911115dcb6 | 108 | sinal1[contador] = ain1 * ESCALA_DE_TENSAO; |
pmcmendes | 0:cce430bf36f7 | 109 | aux2 = 0; |
pmcmendes | 0:cce430bf36f7 | 110 | n1 += sinal1[contador]; |
pmcmendes | 0:cce430bf36f7 | 111 | contador++; |
pmcmendes | 0:cce430bf36f7 | 112 | } |
pmcmendes | 0:cce430bf36f7 | 113 | } |
pmcmendes | 0:cce430bf36f7 | 114 | |
pmcmendes | 0:cce430bf36f7 | 115 | |
pmcmendes | 0:cce430bf36f7 | 116 | flipper.detach(); /* para o ticker */ |
pmcmendes | 0:cce430bf36f7 | 117 | myled = 1; |
pmcmendes | 4:ee911115dcb6 | 118 | printf ("############# MULTIMETRO UPP ##############\n\r"); |
pmcmendes | 4:ee911115dcb6 | 119 | |
pmcmendes | 3:14310c47674c | 120 | |
pmcmendes | 4:ee911115dcb6 | 121 | float tensaoDC = calculaTensaoDC(sinal1, tamanho); |
pmcmendes | 4:ee911115dcb6 | 122 | //float tensaoAC = calculaTensaoACSenoidal(sinal1, tamanho, tensaoDC); |
pmcmendes | 4:ee911115dcb6 | 123 | float tensaoAC = calculaTensaoACPeriodica(sinal1, tamanho); |
pmcmendes | 4:ee911115dcb6 | 124 | printf("\n\n\n\n"); |
pmcmendes | 4:ee911115dcb6 | 125 | printf ("A tensao DC e: %.2f \n\r", tensaoDC); |
pmcmendes | 4:ee911115dcb6 | 126 | printf ("A tensao AC e: %.2f \n\n\n\r", tensaoAC); |
pmcmendes | 0:cce430bf36f7 | 127 | |
pmcmendes | 3:14310c47674c | 128 | |
pmcmendes | 0:cce430bf36f7 | 129 | |
pmcmendes | 0:cce430bf36f7 | 130 | // Apaga led quando termina |
pmcmendes | 0:cce430bf36f7 | 131 | // Coloque aqui o seu programa para tratamento dos dados. |
pmcmendes | 0:cce430bf36f7 | 132 | } |
pmcmendes | 3:14310c47674c | 133 | |
pmcmendes | 3:14310c47674c | 134 | |
pmcmendes | 3:14310c47674c | 135 |