![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
ddd
Dependencies: mbed CMSIS_DSP_5
main.cpp@10:d475a24f4530, 2021-12-18 (annotated)
- Committer:
- davidmateos
- Date:
- Sat Dec 18 17:19:26 2021 +0000
- Revision:
- 10:d475a24f4530
- Parent:
- 9:6fcbc011ef60
ddd
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 | |
davidmateos | 10:d475a24f4530 | 10 | struct estructuraMedidas { |
davidmateos | 10:d475a24f4530 | 11 | float vrms; |
davidmateos | 10:d475a24f4530 | 12 | float irms; |
davidmateos | 10:d475a24f4530 | 13 | float potenciaActiva; |
davidmateos | 10:d475a24f4530 | 14 | float potenciaReactiva; |
davidmateos | 10:d475a24f4530 | 15 | float potenciaAparente; |
davidmateos | 10:d475a24f4530 | 16 | float energiaConsumida; |
davidmateos | 10:d475a24f4530 | 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 | |
javiervicente | 0:0d5a524c7d25 | 25 | int main() |
javiervicente | 0:0d5a524c7d25 | 26 | { |
davidmateos | 10:d475a24f4530 | 27 | |
javiervicente | 1:bdbd76df2103 | 28 | timer.reset(); |
javiervicente | 0:0d5a524c7d25 | 29 | timer.start(); |
javiervicente | 8:99642487de6f | 30 | resultado=calcularRMS(datos, longitudTrama); |
javiervicente | 0:0d5a524c7d25 | 31 | timer.stop(); |
javiervicente | 3:44b632ea58aa | 32 | printf("****El valor Vrms es %f calculado en %d us ****\n",resultado,timer.read_us()); |
davidmateos | 10:d475a24f4530 | 33 | |
javiervicente | 8:99642487de6f | 34 | estructuraMedidas medidas; |
javiervicente | 8:99642487de6f | 35 | medidas.energiaConsumida=0; |
davidmateos | 10:d475a24f4530 | 36 | |
javiervicente | 8:99642487de6f | 37 | timer.reset(); |
javiervicente | 8:99642487de6f | 38 | timer.start(); |
javiervicente | 8:99642487de6f | 39 | calcularDatos(datosV,datosI,longitudTrama,&medidas); |
javiervicente | 8:99642487de6f | 40 | timer.stop(); |
javiervicente | 8:99642487de6f | 41 | printf("**** Datos calculados en %d us ****\n",timer.read_us()); |
javiervicente | 8:99642487de6f | 42 | printf("**** El valor Vrms es %f ****\n",medidas.vrms); |
javiervicente | 8:99642487de6f | 43 | printf("**** El valor Irms es %f ****\n",medidas.irms); |
javiervicente | 8:99642487de6f | 44 | printf("**** La potencia activa es %f ****\n",medidas.potenciaActiva); |
javiervicente | 8:99642487de6f | 45 | printf("**** La potencia reactiva es %f ****\n",medidas.potenciaReactiva); |
javiervicente | 8:99642487de6f | 46 | printf("**** La potencia aparente es %f ****\n",medidas.potenciaAparente); |
javiervicente | 8:99642487de6f | 47 | printf("**** La energia consumida es %f ****\n",medidas.energiaConsumida); |
javiervicente | 8:99642487de6f | 48 | printf("**** El factor de potencia es es %f ****\n",medidas.factorDePotencia); |
javiervicente | 1:bdbd76df2103 | 49 | |
javiervicente | 1:bdbd76df2103 | 50 | } |
javiervicente | 1:bdbd76df2103 | 51 | |
javiervicente | 2:69827542e976 | 52 | float calcularRMS(int16_t *datos, int longitud) |
javiervicente | 1:bdbd76df2103 | 53 | { |
davidmateos | 10:d475a24f4530 | 54 | float sumatorio; |
davidmateos | 10:d475a24f4530 | 55 | |
davidmateos | 10:d475a24f4530 | 56 | const float constante =6.6/65536.0*6.6/65536.0; |
davidmateos | 10:d475a24f4530 | 57 | int16_t dato; |
davidmateos | 10:d475a24f4530 | 58 | int64_t sum = 0; |
davidmateos | 10:d475a24f4530 | 59 | int32_t mult; |
davidmateos | 10:d475a24f4530 | 60 | |
davidmateos | 10:d475a24f4530 | 61 | |
daniferca | 9:6fcbc011ef60 | 62 | |
davidmateos | 10:d475a24f4530 | 63 | for (int n = 0; n<longitud; n++) { |
davidmateos | 10:d475a24f4530 | 64 | dato = datos[n]; |
davidmateos | 10:d475a24f4530 | 65 | mult=dato*dato; |
davidmateos | 10:d475a24f4530 | 66 | sum+=mult; |
davidmateos | 10:d475a24f4530 | 67 | } |
davidmateos | 10:d475a24f4530 | 68 | |
davidmateos | 10:d475a24f4530 | 69 | sumatorio = sum*constante/longitud; |
davidmateos | 10:d475a24f4530 | 70 | return sqrt(sumatorio); |
davidmateos | 10:d475a24f4530 | 71 | |
javiervicente | 1:bdbd76df2103 | 72 | } |
javiervicente | 1:bdbd76df2103 | 73 | |
javiervicente | 8:99642487de6f | 74 | void calcularDatos(int16_t *datosV, int16_t *datosI, int longitud, estructuraMedidas *medidas) |
javiervicente | 8:99642487de6f | 75 | { |
davidmateos | 10:d475a24f4530 | 76 | int16_t voltajes; |
davidmateos | 10:d475a24f4530 | 77 | int32_t product_volt; |
davidmateos | 10:d475a24f4530 | 78 | int64_t sumatorio_volt=0; |
davidmateos | 10:d475a24f4530 | 79 | float resultado_v; |
davidmateos | 10:d475a24f4530 | 80 | int16_t intens; |
davidmateos | 10:d475a24f4530 | 81 | int32_t product_i; |
davidmateos | 10:d475a24f4530 | 82 | int64_t sumatorio_i=0; |
davidmateos | 10:d475a24f4530 | 83 | float resultado_i; |
davidmateos | 10:d475a24f4530 | 84 | int32_t pot; |
davidmateos | 10:d475a24f4530 | 85 | int64_t sumatorio_pot=0; |
davidmateos | 10:d475a24f4530 | 86 | float resultado_pot; |
davidmateos | 10:d475a24f4530 | 87 | float cte=6.6/65536.0*6.6/65536.0; |
daniferca | 9:6fcbc011ef60 | 88 | |
davidmateos | 10:d475a24f4530 | 89 | for (int n=0;n<longitud;n++){ |
davidmateos | 10:d475a24f4530 | 90 | |
davidmateos | 10:d475a24f4530 | 91 | voltajes=datosV[n]; |
davidmateos | 10:d475a24f4530 | 92 | product_volt=voltajes*voltajes; |
davidmateos | 10:d475a24f4530 | 93 | sumatorio_volt+=product_volt; |
davidmateos | 10:d475a24f4530 | 94 | |
davidmateos | 10:d475a24f4530 | 95 | intens=datosI[n]; |
davidmateos | 10:d475a24f4530 | 96 | product_i=intens*intens; |
davidmateos | 10:d475a24f4530 | 97 | sumatorio_i+=product_i; |
davidmateos | 10:d475a24f4530 | 98 | |
davidmateos | 10:d475a24f4530 | 99 | pot=voltajes*intens; |
davidmateos | 10:d475a24f4530 | 100 | sumatorio_pot+=pot; |
davidmateos | 10:d475a24f4530 | 101 | |
davidmateos | 10:d475a24f4530 | 102 | } |
davidmateos | 10:d475a24f4530 | 103 | resultado_v=sumatorio_volt*cte/longitud; |
davidmateos | 10:d475a24f4530 | 104 | resultado_i=sumatorio_i*cte/longitud; |
davidmateos | 10:d475a24f4530 | 105 | resultado_pot=sumatorio_pot*cte/longitud; |
davidmateos | 10:d475a24f4530 | 106 | |
davidmateos | 10:d475a24f4530 | 107 | (*medidas).vrms=sqrt(resultado_v); |
davidmateos | 10:d475a24f4530 | 108 | (*medidas).irms=sqrt(resultado_i); |
davidmateos | 10:d475a24f4530 | 109 | (*medidas).potenciaActiva=resultado_pot; |
davidmateos | 10:d475a24f4530 | 110 | (*medidas).potenciaAparente=(*medidas).vrms*(*medidas).irms; |
davidmateos | 10:d475a24f4530 | 111 | (*medidas).potenciaReactiva=sqrt(-((*medidas).potenciaActiva*(*medidas).potenciaActiva)+((*medidas).potenciaAparente*(*medidas).potenciaAparente)); |
davidmateos | 10:d475a24f4530 | 112 | (*medidas).factorDePotencia=(*medidas).potenciaActiva/(*medidas).potenciaAparente; |
javiervicente | 8:99642487de6f | 113 | |
davidmateos | 10:d475a24f4530 | 114 | } |
davidmateos | 10:d475a24f4530 | 115 |