a
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #define bufferLength 500 00003 #define muestras 200 00004 #include "math.h" 00005 #include "TextLCD.h" 00006 00007 00008 00009 TextLCD lcd(D8, D9, D2, D3, D4, D5, TextLCD::LCD16x2); // rs, e, d4-d7 00010 Serial pc(USBTX, USBRX); // tx, rx 00011 00012 00013 int contadorboton=1; 00014 00015 00016 struct eResultados { 00017 float Vrms; 00018 float Vmax; 00019 float Vmin; 00020 }; 00021 00022 struct estructuraMedidas { 00023 float vrms; 00024 float irms; 00025 float potenciaActiva; 00026 float potenciaReactiva; 00027 float potenciaAparente; 00028 float energiaConsumida; 00029 float factorDePotencia; 00030 }; 00031 00032 estructuraMedidas medidas; 00033 00034 00035 Serial blutooth(PC_0, PC_1);//Tx, Rx 00036 00037 AnalogIn adc_in(A0); 00038 AnalogIn int_in(A1); 00039 AnalogIn cont_in(A3); 00040 InterruptIn boton(USER_BUTTON); 00041 00042 Ticker tMuestrea; 00043 Ticker tVisualiza; 00044 00045 void visualiza(); 00046 void procesado(int length, int16_t *entrada, int16_t *entrada2); 00047 void calcularDatos(int16_t *datosV, int16_t *datosI, int longitud, estructuraMedidas *medidas); 00048 void calcularRMS(int16_t *buffer, int longitud,eResultados *resul); 00049 void muestrea(); 00050 00051 int flag=0; 00052 int contador=1; 00053 int bufferActivo=0; 00054 00055 struct buffers { 00056 int16_t vA[muestras]; 00057 int16_t vB[muestras]; 00058 int16_t iA[muestras]; 00059 int16_t iB[muestras]; 00060 }; 00061 00062 00063 buffers bufferEntrada; 00064 buffers bufferSalida; 00065 00066 eResultados resultados; 00067 00068 void flip() 00069 { 00070 contadorboton++; 00071 if (contadorboton > 7) { 00072 contadorboton=1; 00073 } 00074 } 00075 00076 int main() 00077 { 00078 00079 tMuestrea.attach_us(&muestrea, muestras); 00080 boton.rise(&flip); 00081 tVisualiza.attach(&visualiza, 1); 00082 blutooth.baud(9600); 00083 00084 while(1) { 00085 if (flag==1) { 00086 00087 flag=0; 00088 if(bufferActivo==1) { 00089 procesado(muestras,(int16_t *)&bufferEntrada.vA,(int16_t *)&bufferEntrada.iA); 00090 } else { 00091 procesado(muestras,(int16_t *)&bufferEntrada.vB,(int16_t *)&bufferEntrada.iB); 00092 } 00093 00094 } 00095 00096 } 00097 00098 } 00099 00100 void muestrea() 00101 { 00102 int16_t dato=(int16_t)(adc_in.read_u16()^0x8000); 00103 int16_t dato2=(int16_t)(int_in.read_u16()^0x8000); 00104 int16_t cont=(int16_t)(cont_in.read_u16()^0x8000); 00105 00106 if (bufferActivo==0) { 00107 00108 bufferEntrada.vA[contador]=dato-cont; 00109 00110 bufferEntrada.iA[contador]=dato2-cont; 00111 00112 } 00113 00114 else { 00115 00116 bufferEntrada.vB[contador]=dato-cont; 00117 00118 bufferEntrada.iB[contador]=dato2-cont; 00119 00120 } 00121 00122 contador++; 00123 if(contador>=muestras) { 00124 bufferActivo=!bufferActivo; 00125 contador=0; 00126 flag=1; 00127 } 00128 00129 } 00130 void procesado(int length, int16_t *entrada, int16_t *entrada2) 00131 { 00132 00133 calcularDatos(entrada, entrada2, 100, &medidas); 00134 00135 } 00136 00137 00138 void visualiza() 00139 { 00140 00141 blutooth.printf("%.3f|",medidas.vrms); //VRMS 00142 blutooth.printf("%.3f|",medidas.irms); //IRMS 00143 blutooth.printf("%.3f|",medidas.energiaConsumida/36000); //EnergiaConsumida 00144 blutooth.printf("%.3f|",medidas.factorDePotencia); //Factor de Potencia 00145 blutooth.printf("%.3f|",medidas.potenciaAparente); //Potencia Aparente 00146 blutooth.printf("%.3f|",medidas.potenciaActiva); //Potencia Activa 00147 blutooth.printf("%.3f|",medidas.potenciaReactiva); //Potencia Reactiva 00148 00149 lcd.cls(); 00150 switch (contadorboton) { 00151 case 1: 00152 lcd.printf ("VRMS\n%.3f V",medidas.vrms); 00153 break; 00154 case 2: 00155 lcd.printf ("irms\n%.3f A",medidas.irms); 00156 break; 00157 case 3: 00158 lcd.printf ("E consum\n%.3f W/s",medidas.energiaConsumida/36000); 00159 break; 00160 case 4: 00161 lcd.printf ("factor P\n%.3f",medidas.factorDePotencia); 00162 break; 00163 case 5: 00164 lcd.printf ("S\n%.3f VA",medidas.potenciaAparente); 00165 break; 00166 case 6: 00167 lcd.printf ("P\n%.3f W",medidas.potenciaActiva); 00168 break; 00169 case 7: 00170 lcd.printf ("Q\n%.3f VAr",medidas.potenciaReactiva); 00171 break; 00172 00173 } 00174 00175 } 00176 00177 void calcularDatos(int16_t *datosV, int16_t *datosI, int longitud, estructuraMedidas *medidas) 00178 { 00179 float consV=0.01447645724720217710371819960861; 00180 00181 00182 float consI=3.6915945822492998686005019138794e-4*1.1674827586206896551724137931034; 00183 float consP=consV*consI; 00184 00185 int64_t sumaV=0; 00186 int64_t sumaI=0; 00187 int64_t sumaP=0; 00188 00189 00190 00191 float datoV; 00192 float datoI; 00193 float datoP; 00194 float datoS; 00195 float datoQ; 00196 00197 00198 for (int i=0; i<longitud; i++) { 00199 00200 sumaV+=datosV[i]*datosV[i]; 00201 sumaI+=datosI[i]*datosI[i]; 00202 sumaP+=datosV[i]*datosI[i]; 00203 } 00204 00205 datoV = consV*sqrt(((float)sumaV)/longitud); 00206 datoI = consI*sqrt(((float)sumaI)/longitud); 00207 datoP = consP*((float)sumaP)/longitud; 00208 datoS=datoV*datoI; 00209 datoQ = sqrt((datoS*datoS)-(datoP*datoP)); 00210 00211 (*medidas).energiaConsumida += datoP; 00212 (*medidas).factorDePotencia = datoP/datoS; 00213 (*medidas).potenciaAparente = datoS; 00214 (*medidas).potenciaActiva = datoP; 00215 (*medidas).potenciaReactiva = datoQ; 00216 (*medidas).vrms = datoV; 00217 (*medidas).irms = datoI; 00218 00219 }
Generated on Thu Jul 14 2022 20:30:31 by
1.7.2