![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
hOLA AGATHE
Dependencies: mbed CMSIS_DSP_5
main.cpp@10:4ee4ca605836, 2021-12-16 (annotated)
- Committer:
- aitor01
- Date:
- Thu Dec 16 12:04:56 2021 +0000
- Revision:
- 10:4ee4ca605836
- Parent:
- 9:7460651183ac
VRMS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
javiervicente | 0:0d5a524c7d25 | 1 | #include "mbed.h" |
javiervicente | 0:0d5a524c7d25 | 2 | #include "datos.h" |
javiervicente | 0:0d5a524c7d25 | 3 | |
javiervicente | 8:99642487de6f | 4 | #define longitudTrama 500 |
javiervicente | 8:99642487de6f | 5 | |
javiervicente | 0:0d5a524c7d25 | 6 | Timer timer; |
javiervicente | 2:69827542e976 | 7 | float resultado; |
javiervicente | 3:44b632ea58aa | 8 | int tiempo; |
javiervicente | 1:bdbd76df2103 | 9 | |
aitor01 | 10:4ee4ca605836 | 10 | struct estructuraMedidas { |
aitor01 | 10:4ee4ca605836 | 11 | float vrms; |
aitor01 | 10:4ee4ca605836 | 12 | float irms; |
aitor01 | 10:4ee4ca605836 | 13 | float potenciaActiva; |
aitor01 | 10:4ee4ca605836 | 14 | float potenciaReactiva; |
aitor01 | 10:4ee4ca605836 | 15 | float potenciaAparente; |
aitor01 | 10:4ee4ca605836 | 16 | float energiaConsumida; |
aitor01 | 10:4ee4ca605836 | 17 | float factorDePotencia; |
javiervicente | 8:99642487de6f | 18 | }; |
javiervicente | 8:99642487de6f | 19 | |
javiervicente | 2:69827542e976 | 20 | float calcularRMS(int16_t *datos, int longitud); |
javiervicente | 2:69827542e976 | 21 | |
javiervicente | 8:99642487de6f | 22 | void calcularDatos(int16_t *datosV, int16_t *datosI, int longitud, estructuraMedidas *medidas); |
javiervicente | 8:99642487de6f | 23 | |
javiervicente | 0:0d5a524c7d25 | 24 | int main() |
javiervicente | 0:0d5a524c7d25 | 25 | { |
aitor01 | 10:4ee4ca605836 | 26 | |
javiervicente | 1:bdbd76df2103 | 27 | timer.reset(); |
javiervicente | 0:0d5a524c7d25 | 28 | timer.start(); |
javiervicente | 8:99642487de6f | 29 | resultado=calcularRMS(datos, longitudTrama); |
javiervicente | 0:0d5a524c7d25 | 30 | timer.stop(); |
javiervicente | 3:44b632ea58aa | 31 | printf("****El valor Vrms es %f calculado en %d us ****\n",resultado,timer.read_us()); |
aitor01 | 10:4ee4ca605836 | 32 | |
javiervicente | 8:99642487de6f | 33 | estructuraMedidas medidas; |
javiervicente | 8:99642487de6f | 34 | medidas.energiaConsumida=0; |
aitor01 | 10:4ee4ca605836 | 35 | |
javiervicente | 8:99642487de6f | 36 | timer.reset(); |
javiervicente | 8:99642487de6f | 37 | timer.start(); |
javiervicente | 8:99642487de6f | 38 | calcularDatos(datosV,datosI,longitudTrama,&medidas); |
javiervicente | 8:99642487de6f | 39 | timer.stop(); |
javiervicente | 8:99642487de6f | 40 | printf("**** Datos calculados en %d us ****\n",timer.read_us()); |
javiervicente | 8:99642487de6f | 41 | printf("**** El valor Vrms es %f ****\n",medidas.vrms); |
javiervicente | 8:99642487de6f | 42 | printf("**** El valor Irms es %f ****\n",medidas.irms); |
javiervicente | 8:99642487de6f | 43 | printf("**** La potencia activa es %f ****\n",medidas.potenciaActiva); |
javiervicente | 8:99642487de6f | 44 | printf("**** La potencia reactiva es %f ****\n",medidas.potenciaReactiva); |
javiervicente | 8:99642487de6f | 45 | printf("**** La potencia aparente es %f ****\n",medidas.potenciaAparente); |
javiervicente | 8:99642487de6f | 46 | printf("**** La energia consumida es %f ****\n",medidas.energiaConsumida); |
javiervicente | 8:99642487de6f | 47 | printf("**** El factor de potencia es es %f ****\n",medidas.factorDePotencia); |
javiervicente | 1:bdbd76df2103 | 48 | |
javiervicente | 1:bdbd76df2103 | 49 | } |
javiervicente | 1:bdbd76df2103 | 50 | |
javiervicente | 2:69827542e976 | 51 | float calcularRMS(int16_t *datos, int longitud) |
javiervicente | 1:bdbd76df2103 | 52 | { |
aitor01 | 9:7460651183ac | 53 | |
aitor01 | 9:7460651183ac | 54 | float cont = 6.6/65536.0*6.6/65536.0; |
aitor01 | 9:7460651183ac | 55 | int16_t dato=0.0; |
aitor01 | 9:7460651183ac | 56 | float dato_1=0.0; |
aitor01 | 9:7460651183ac | 57 | int32_t producto=0.0; |
aitor01 | 9:7460651183ac | 58 | int64_t sumatorio=0.0; |
aitor01 | 10:4ee4ca605836 | 59 | |
aitor01 | 10:4ee4ca605836 | 60 | for (int n=0; n<longitud; n++) { |
aitor01 | 9:7460651183ac | 61 | dato=datos[n]; |
aitor01 | 9:7460651183ac | 62 | producto=dato*dato; |
aitor01 | 10:4ee4ca605836 | 63 | sumatorio+= producto; |
aitor01 | 10:4ee4ca605836 | 64 | } |
aitor01 | 10:4ee4ca605836 | 65 | dato_1=cont*sumatorio/longitud; |
aitor01 | 10:4ee4ca605836 | 66 | |
aitor01 | 9:7460651183ac | 67 | return sqrt(dato_1); |
javiervicente | 1:bdbd76df2103 | 68 | } |
javiervicente | 1:bdbd76df2103 | 69 | |
javiervicente | 8:99642487de6f | 70 | void calcularDatos(int16_t *datosV, int16_t *datosI, int longitud, estructuraMedidas *medidas) |
javiervicente | 8:99642487de6f | 71 | { |
aitor01 | 10:4ee4ca605836 | 72 | float cont =6.6/65536.0*6.6/65536.0; |
aitor01 | 10:4ee4ca605836 | 73 | int16_t dato_v=0.0; |
aitor01 | 10:4ee4ca605836 | 74 | int32_t mult_v=0.0; |
aitor01 | 10:4ee4ca605836 | 75 | int64_t sumatorio_v=0.0; |
aitor01 | 10:4ee4ca605836 | 76 | float emaitza_v=0.0; |
aitor01 | 10:4ee4ca605836 | 77 | int32_t dato_p=0.0; |
aitor01 | 10:4ee4ca605836 | 78 | int64_t sumatorio_p=0.0; |
aitor01 | 10:4ee4ca605836 | 79 | float emaitza_p=0.0; |
aitor01 | 10:4ee4ca605836 | 80 | int16_t dato_i=0.0; |
aitor01 | 10:4ee4ca605836 | 81 | int32_t mult_i=0.0; |
aitor01 | 10:4ee4ca605836 | 82 | int64_t sumatorio_i=0.0; |
aitor01 | 10:4ee4ca605836 | 83 | float emaitza_i=0.0; |
javiervicente | 8:99642487de6f | 84 | |
aitor01 | 10:4ee4ca605836 | 85 | for (int n=0; n<longitud; n++) { |
aitor01 | 10:4ee4ca605836 | 86 | //Cálculo voltaje |
aitor01 | 10:4ee4ca605836 | 87 | dato_v = datosV[n]; |
aitor01 | 10:4ee4ca605836 | 88 | mult_v = dato_v*dato_v; |
aitor01 | 10:4ee4ca605836 | 89 | sumatorio_v+=mult_v; |
aitor01 | 10:4ee4ca605836 | 90 | //Cálculo intensidad |
aitor01 | 10:4ee4ca605836 | 91 | dato_i = datosI[n]; |
aitor01 | 10:4ee4ca605836 | 92 | mult_i = dato_i*dato_i; |
aitor01 | 10:4ee4ca605836 | 93 | sumatorio_i+=mult_i; |
aitor01 | 10:4ee4ca605836 | 94 | //Cálculo de potencia |
aitor01 | 10:4ee4ca605836 | 95 | dato_p=dato_v*dato_i; |
aitor01 | 10:4ee4ca605836 | 96 | sumatorio_p+=dato_p; |
aitor01 | 10:4ee4ca605836 | 97 | } |
aitor01 | 10:4ee4ca605836 | 98 | //misma estructura que arriba |
aitor01 | 10:4ee4ca605836 | 99 | emaitza_v=sumatorio_v*cont/longitud; |
aitor01 | 10:4ee4ca605836 | 100 | emaitza_i=sumatorio_i*cont/longitud; |
aitor01 | 10:4ee4ca605836 | 101 | emaitza_p=sumatorio_p*cont/longitud; |
aitor01 | 10:4ee4ca605836 | 102 | |
aitor01 | 10:4ee4ca605836 | 103 | //mandar dato a medidas |
aitor01 | 10:4ee4ca605836 | 104 | medidas->vrms=sqrt(emaitza_v); |
aitor01 | 10:4ee4ca605836 | 105 | medidas->irms=sqrt(emaitza_i); |
aitor01 | 10:4ee4ca605836 | 106 | medidas->potenciaActiva=emaitza_p; |
aitor01 | 10:4ee4ca605836 | 107 | medidas->potenciaAparente=medidas->vrms*medidas->irms; |
aitor01 | 10:4ee4ca605836 | 108 | medidas->potenciaReactiva=sqrt(-(medidas->potenciaActiva*medidas->potenciaActiva)+(medidas->potenciaAparente*medidas->potenciaAparente)); |
aitor01 | 10:4ee4ca605836 | 109 | |
aitor01 | 10:4ee4ca605836 | 110 | //medidas->energiaConsumida Esta nos dijiste que habia que calcular el tiempo y que no hicieramos |
aitor01 | 10:4ee4ca605836 | 111 | medidas->factorDePotencia=medidas->potenciaActiva/medidas->potenciaAparente; |
aitor01 | 10:4ee4ca605836 | 112 | } |
aitor01 | 10:4ee4ca605836 | 113 |