Versao com funcionalidades de cálculo de período e defasagem.

Dependencies:   mbed

Committer:
virgilio
Date:
Sun Apr 19 01:08:28 2015 +0000
Revision:
0:05ed3baa204d
Versao 2.06

Who changed what in which revision?

UserRevisionLine numberNew contents of line
virgilio 0:05ed3baa204d 1 // Mede dois sinais
virgilio 0:05ed3baa204d 2 #include "mbed.h"
virgilio 0:05ed3baa204d 3 #include "math.h" // Descomente esta linha caso queira usar funções como sin(x)
virgilio 0:05ed3baa204d 4
virgilio 0:05ed3baa204d 5 #include "signal.h"
virgilio 0:05ed3baa204d 6
virgilio 0:05ed3baa204d 7
virgilio 0:05ed3baa204d 8 Ticker flipper;
virgilio 0:05ed3baa204d 9 AnalogIn ain0(PTB0);
virgilio 0:05ed3baa204d 10 AnalogIn ain1(PTB1);
virgilio 0:05ed3baa204d 11 DigitalOut myled(LED1);
virgilio 0:05ed3baa204d 12 int aux2 = 0; // Variavel auxiliar, para indicar quando realizar uma amostragem
virgilio 0:05ed3baa204d 13
virgilio 0:05ed3baa204d 14 int i =0;
virgilio 0:05ed3baa204d 15 float soma_canal0 = 0, soma_canal1, soma_quadrados_canal0, soma_quadrados_canal1 =0;
virgilio 0:05ed3baa204d 16
virgilio 0:05ed3baa204d 17 float media_dc_canal0, media_dc_canal1, media_ac_canal0, media_ac_canal1;
virgilio 0:05ed3baa204d 18
virgilio 0:05ed3baa204d 19 float tensao_maxima = 3.3;
virgilio 0:05ed3baa204d 20
virgilio 0:05ed3baa204d 21 void flip() {
virgilio 0:05ed3baa204d 22 aux2 = !aux2;
virgilio 0:05ed3baa204d 23 }
virgilio 0:05ed3baa204d 24 // Classe Signal ------------------------
virgilio 0:05ed3baa204d 25 class Signal {
virgilio 0:05ed3baa204d 26 public:
virgilio 0:05ed3baa204d 27 float signal_array[1000];
virgilio 0:05ed3baa204d 28 char signal_type;
virgilio 0:05ed3baa204d 29 void generate(char signal_tyoe, float amplitude, float ordinary_frequency);
virgilio 0:05ed3baa204d 30 // q for quadrate, s for senoidal, t for triangular
virgilio 0:05ed3baa204d 31 void erase();
virgilio 0:05ed3baa204d 32 float soma();
virgilio 0:05ed3baa204d 33 float eficaz();
virgilio 0:05ed3baa204d 34 float amplitude();
virgilio 0:05ed3baa204d 35 float period(float dc_value);
virgilio 0:05ed3baa204d 36 };
virgilio 0:05ed3baa204d 37
virgilio 0:05ed3baa204d 38 void Signal::generate(char signal_tyoe, float amplitude, float ordinary_frequency){
virgilio 0:05ed3baa204d 39 int count;
virgilio 0:05ed3baa204d 40 float angular_frequency = 2*3.1415*ordinary_frequency;
virgilio 0:05ed3baa204d 41
virgilio 0:05ed3baa204d 42 for(count = 0; count <1000; count++){
virgilio 0:05ed3baa204d 43 if (signal_tyoe == 's') {
virgilio 0:05ed3baa204d 44 angular_frequency = count;
virgilio 0:05ed3baa204d 45 signal_array[count] = amplitude*cos(angular_frequency);
virgilio 0:05ed3baa204d 46
virgilio 0:05ed3baa204d 47 }
virgilio 0:05ed3baa204d 48 }
virgilio 0:05ed3baa204d 49 }
virgilio 0:05ed3baa204d 50
virgilio 0:05ed3baa204d 51 void Signal::erase(){
virgilio 0:05ed3baa204d 52 int count;
virgilio 0:05ed3baa204d 53
virgilio 0:05ed3baa204d 54 for(count = 0; count <=1000; count++){
virgilio 0:05ed3baa204d 55 signal_array[count] = 0;
virgilio 0:05ed3baa204d 56 }
virgilio 0:05ed3baa204d 57 }
virgilio 0:05ed3baa204d 58
virgilio 0:05ed3baa204d 59 float Signal::soma(){
virgilio 0:05ed3baa204d 60 int count;
virgilio 0:05ed3baa204d 61 float soma = 0;
virgilio 0:05ed3baa204d 62
virgilio 0:05ed3baa204d 63 for(count = 0; count <1000; count++){
virgilio 0:05ed3baa204d 64 soma += signal_array[count];
virgilio 0:05ed3baa204d 65 }
virgilio 0:05ed3baa204d 66 return soma;
virgilio 0:05ed3baa204d 67 }
virgilio 0:05ed3baa204d 68
virgilio 0:05ed3baa204d 69 float Signal::eficaz(){
virgilio 0:05ed3baa204d 70 int count;
virgilio 0:05ed3baa204d 71 float soma_quadrados = 0;
virgilio 0:05ed3baa204d 72
virgilio 0:05ed3baa204d 73 for(count = 0; count <1000; count++){
virgilio 0:05ed3baa204d 74 soma_quadrados += signal_array[count]*signal_array[count];
virgilio 0:05ed3baa204d 75 }
virgilio 0:05ed3baa204d 76
virgilio 0:05ed3baa204d 77 return sqrt(soma_quadrados);
virgilio 0:05ed3baa204d 78 }
virgilio 0:05ed3baa204d 79
virgilio 0:05ed3baa204d 80 float Signal::amplitude(){
virgilio 0:05ed3baa204d 81 int count;
virgilio 0:05ed3baa204d 82 float amplitude = signal_array[0];
virgilio 0:05ed3baa204d 83
virgilio 0:05ed3baa204d 84 for(count = 1; count <1000; count++){
virgilio 0:05ed3baa204d 85 if (amplitude < signal_array[count])
virgilio 0:05ed3baa204d 86 amplitude = signal_array[count];
virgilio 0:05ed3baa204d 87 }
virgilio 0:05ed3baa204d 88
virgilio 0:05ed3baa204d 89 return amplitude;
virgilio 0:05ed3baa204d 90 }
virgilio 0:05ed3baa204d 91
virgilio 0:05ed3baa204d 92 float Signal::period(float dc_value){
virgilio 0:05ed3baa204d 93 int count;
virgilio 0:05ed3baa204d 94 int position, pre_position;
virgilio 0:05ed3baa204d 95 int crossings = 0;
virgilio 0:05ed3baa204d 96 float period = 0;
virgilio 0:05ed3baa204d 97 if (signal_array[0] > dc_value) {
virgilio 0:05ed3baa204d 98 pre_position = 1; //acima da linha dc
virgilio 0:05ed3baa204d 99 }
virgilio 0:05ed3baa204d 100 else{
virgilio 0:05ed3baa204d 101 pre_position = 0; //abaixo da linha dc
virgilio 0:05ed3baa204d 102 }
virgilio 0:05ed3baa204d 103
virgilio 0:05ed3baa204d 104 for(count = 1; count <1000; count++){
virgilio 0:05ed3baa204d 105 if (signal_array[count] > dc_value) {
virgilio 0:05ed3baa204d 106 position = 1; //acima da linha dc
virgilio 0:05ed3baa204d 107 }
virgilio 0:05ed3baa204d 108 else{
virgilio 0:05ed3baa204d 109 position = 0; //abaixo da linha dc
virgilio 0:05ed3baa204d 110 }
virgilio 0:05ed3baa204d 111
virgilio 0:05ed3baa204d 112 if (position != pre_position){
virgilio 0:05ed3baa204d 113 period += (count - period);
virgilio 0:05ed3baa204d 114 crossings += 1;
virgilio 0:05ed3baa204d 115 pre_position = position;
virgilio 0:05ed3baa204d 116 }
virgilio 0:05ed3baa204d 117 }
virgilio 0:05ed3baa204d 118 return (period/crossings)*2;
virgilio 0:05ed3baa204d 119 }
virgilio 0:05ed3baa204d 120 // Classe Signal - END
virgilio 0:05ed3baa204d 121
virgilio 0:05ed3baa204d 122 // ---------------MAIN------------------------------
virgilio 0:05ed3baa204d 123
virgilio 0:05ed3baa204d 124 int main()
virgilio 0:05ed3baa204d 125 {
virgilio 0:05ed3baa204d 126 // valor mínimo de ts: ~0.0001 (10 kHz)
virgilio 0:05ed3baa204d 127 int contador = 0, tamanho=1000;
virgilio 0:05ed3baa204d 128 // Tamanho dos vetores de amostras.
virgilio 0:05ed3baa204d 129 float ts = 0.0001, sinal0[tamanho], sinal1[tamanho];
virgilio 0:05ed3baa204d 130
virgilio 0:05ed3baa204d 131 myled = 0; // Acende led
virgilio 0:05ed3baa204d 132 flipper.attach(&flip, ts); // chama a funcao flip apos ts segundos
virgilio 0:05ed3baa204d 133
virgilio 0:05ed3baa204d 134 while(contador<tamanho) {
virgilio 0:05ed3baa204d 135 if(aux2 == 1){
virgilio 0:05ed3baa204d 136 sinal0[contador] = ain0; // lê sinais analógicos de dois canais
virgilio 0:05ed3baa204d 137 sinal1[contador] = ain1;
virgilio 0:05ed3baa204d 138 aux2 = 0;
virgilio 0:05ed3baa204d 139 contador++;
virgilio 0:05ed3baa204d 140 }
virgilio 0:05ed3baa204d 141 }
virgilio 0:05ed3baa204d 142
virgilio 0:05ed3baa204d 143 flipper.detach(); /* para o ticker */
virgilio 0:05ed3baa204d 144 myled = 1;
virgilio 0:05ed3baa204d 145 // Apaga led quando termina
virgilio 0:05ed3baa204d 146
virgilio 0:05ed3baa204d 147 // Coloque aqui o seu programa para tratamento dos dados.
virgilio 0:05ed3baa204d 148
virgilio 0:05ed3baa204d 149 printf("\n MULTIMETRO DIGITAL versao 1.06 \n\r");
virgilio 0:05ed3baa204d 150 //gera sinais artificiais para teste
virgilio 0:05ed3baa204d 151 // deixe comentado se quiser utilizar a amostragem normal
virgilio 0:05ed3baa204d 152
virgilio 0:05ed3baa204d 153 Signal canal0;
virgilio 0:05ed3baa204d 154 Signal canal1;
virgilio 0:05ed3baa204d 155
virgilio 0:05ed3baa204d 156 canal0.generate('s', 1, 1000);
virgilio 0:05ed3baa204d 157 // gera sinal s - senoidal, amplitude = 1 e frequencia = 1kHz
virgilio 0:05ed3baa204d 158
virgilio 0:05ed3baa204d 159 canal1.generate('s', 1, 2000);
virgilio 0:05ed3baa204d 160 // gera sinal s - senoidal, amplitude = 1 e frequencia = 2kHz
virgilio 0:05ed3baa204d 161
virgilio 0:05ed3baa204d 162
virgilio 0:05ed3baa204d 163 printf("\n Terminada a aquisicao \n \r");
virgilio 0:05ed3baa204d 164
virgilio 0:05ed3baa204d 165 // printf("%.2f ",sinal0[i]);
virgilio 0:05ed3baa204d 166 // soma_canal0 += sinal0[i];
virgilio 0:05ed3baa204d 167 // soma_quadrados_canal0 += sinal0[i]*sinal0[i];
virgilio 0:05ed3baa204d 168
virgilio 0:05ed3baa204d 169 //printf("Soma Canal 0 %.2f ",soma_canal0);
virgilio 0:05ed3baa204d 170 //printf("Soma dos quadrados canal 0 %.2f ",soma_quadrados_canal0);
virgilio 0:05ed3baa204d 171
virgilio 0:05ed3baa204d 172 media_dc_canal0 = canal0.soma()*ts/0.1;
virgilio 0:05ed3baa204d 173 media_dc_canal1 = canal1.soma()*ts/0.1;
virgilio 0:05ed3baa204d 174 media_ac_canal0 = canal0.eficaz()*sqrt(ts/0.1);
virgilio 0:05ed3baa204d 175 media_ac_canal1 = canal1.eficaz()*sqrt(ts/0.1);
virgilio 0:05ed3baa204d 176
virgilio 0:05ed3baa204d 177
virgilio 0:05ed3baa204d 178 printf("Tensao DC Canal 0: %4.2f \n\r",tensao_maxima*media_dc_canal0);
virgilio 0:05ed3baa204d 179 printf("Tensao eficaz AC canal 0: %4.2f \n\r",tensao_maxima*media_ac_canal0);
virgilio 0:05ed3baa204d 180 printf("Tensao DC Canal 1: %4.2f \n\r",tensao_maxima*media_dc_canal1);
virgilio 0:05ed3baa204d 181 printf("Tensao eficaz AC canal 1: %4.2f \n\r",tensao_maxima*media_ac_canal1);
virgilio 0:05ed3baa204d 182
virgilio 0:05ed3baa204d 183 // Calculo do delta t sendo Canal 0 como referencia
virgilio 0:05ed3baa204d 184
virgilio 0:05ed3baa204d 185
virgilio 0:05ed3baa204d 186
virgilio 0:05ed3baa204d 187
virgilio 0:05ed3baa204d 188
virgilio 0:05ed3baa204d 189 }
virgilio 0:05ed3baa204d 190 //fim