holi
Dependencies: mbed
main.cpp@0:4ebff00cfa5d, 2018-11-27 (annotated)
- Committer:
- carmenron
- Date:
- Tue Nov 27 09:32:17 2018 +0000
- Revision:
- 0:4ebff00cfa5d
proyecto
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
carmenron | 0:4ebff00cfa5d | 1 | #include "mbed.h" |
carmenron | 0:4ebff00cfa5d | 2 | #include "math.h" |
carmenron | 0:4ebff00cfa5d | 3 | #define constanteTension 1.4709e-06 |
carmenron | 0:4ebff00cfa5d | 4 | |
carmenron | 0:4ebff00cfa5d | 5 | Serial pc(USBTX, USBRX); // tx, rx |
carmenron | 0:4ebff00cfa5d | 6 | |
carmenron | 0:4ebff00cfa5d | 7 | #define muestras 100 |
carmenron | 0:4ebff00cfa5d | 8 | |
carmenron | 0:4ebff00cfa5d | 9 | //datos para la simulación |
carmenron | 0:4ebff00cfa5d | 10 | uint16_t SimulaADCTension[muestras]={60202,60330,58625,59923,58998,57466,57255,57119,57170,56327,53777,54359,53219,51091,50453,47815,46978,46509,44763,43549,41361,38513,38484,36990,34806,33279,31567,29193,28053,25447,24892,21968,20909,18951,17602,17651,16065,14052,14118,11186,10956, 9896, 9801, 9096, 7219, 7250, 6692, 6723, 6585, 6516, 5513, 6368, 6477, 5763, 6043, 7267, 8753, 8192, 9443, 9583,11577,11624,13236,14810,16455,17921,18496,19134,20656,22417,25159,25612,28368,28898,31937,32470,33848,35633,38022,39382,40758,43294,44352,45782,47961,48101,50367,51624,52074,53559,53615,54518,56322,57580,58557,57530,58872,59042,58397,59201}; |
carmenron | 0:4ebff00cfa5d | 11 | uint16_t SimulaADCIntensidad[muestras]={50233,51231,50075,49752,48077,47954,47431,47039,45074,43363,42265,42317,41528,40167,38300,38291,36222,35848,32856,31874,31756,30792,29301,27121,25643,25859,24596,22473,21324,20488,20525,19587,18219,17412,16095,16230,15805,14552,14084,13718,13018,13552,13830,13742,13402,12494,13483,12590,12865,13365,13717,15102,14670,16809,17329,18512,18492,19710,21125,21425,21959,24420,23675,25403,27867,27863,28974,29835,32658,32356,33992,36355,36794,38237,39365,40761,40570,42662,42741,45341,45347,47276,46952,48428,49884,48802,49433,49915,51705,51945,51311,52919,52741,51751,51769,51843,53002,51405,51402,51047}; |
carmenron | 0:4ebff00cfa5d | 12 | //datos para la simulación |
carmenron | 0:4ebff00cfa5d | 13 | uint16_t SimulaADCMedio[muestras]={33393,33574,32027,33589,33031,31969,32328,32861,33677,33691,32088,33703,33676,32739,33364,32057,32613,33594,33348,33681,33077,31846,33461,33630,33123,33280,33251,32554,33077,32115,33177,31838,32325,31867,31968,33410,33155,32405,33662,31843,32646,32533,33295,33354,32146,32748,32660,33059,33184,33274,32323,33125,33076,32098,32011,32765,33681,32451,32937,32220,33267,32282,32780,33163,33544,33680,32862,32050,32072,32286,33445,32280,33392,32259,33620,32470,32165,32274,32998,32715,32473,33425,32937,32867,33597,32343,33279,33272,32531,32903,31926,31882,32829,33322,33630,32033,32905,32707,31799,32445}; |
carmenron | 0:4ebff00cfa5d | 14 | |
carmenron | 0:4ebff00cfa5d | 15 | int16_t vTensionA[muestras]; |
carmenron | 0:4ebff00cfa5d | 16 | int16_t vTensionB[muestras]; |
carmenron | 0:4ebff00cfa5d | 17 | |
carmenron | 0:4ebff00cfa5d | 18 | int16_t vIntensidadA[muestras]; |
carmenron | 0:4ebff00cfa5d | 19 | int16_t vIntensidadB[muestras]; |
carmenron | 0:4ebff00cfa5d | 20 | |
carmenron | 0:4ebff00cfa5d | 21 | float rms; |
carmenron | 0:4ebff00cfa5d | 22 | |
carmenron | 0:4ebff00cfa5d | 23 | |
carmenron | 0:4ebff00cfa5d | 24 | int contador=0; |
carmenron | 0:4ebff00cfa5d | 25 | |
carmenron | 0:4ebff00cfa5d | 26 | //Para la simulacion |
carmenron | 0:4ebff00cfa5d | 27 | uint16_t adcTension() |
carmenron | 0:4ebff00cfa5d | 28 | { |
carmenron | 0:4ebff00cfa5d | 29 | return SimulaADCTension[contador]; |
carmenron | 0:4ebff00cfa5d | 30 | } |
carmenron | 0:4ebff00cfa5d | 31 | |
carmenron | 0:4ebff00cfa5d | 32 | uint16_t adcIntensidad() |
carmenron | 0:4ebff00cfa5d | 33 | { |
carmenron | 0:4ebff00cfa5d | 34 | return SimulaADCIntensidad[contador]; |
carmenron | 0:4ebff00cfa5d | 35 | } |
carmenron | 0:4ebff00cfa5d | 36 | |
carmenron | 0:4ebff00cfa5d | 37 | //Para la simulacion |
carmenron | 0:4ebff00cfa5d | 38 | uint16_t adcMedio() |
carmenron | 0:4ebff00cfa5d | 39 | { |
carmenron | 0:4ebff00cfa5d | 40 | return SimulaADCMedio[contador]; |
carmenron | 0:4ebff00cfa5d | 41 | } |
carmenron | 0:4ebff00cfa5d | 42 | |
carmenron | 0:4ebff00cfa5d | 43 | |
carmenron | 0:4ebff00cfa5d | 44 | void visualizacion() |
carmenron | 0:4ebff00cfa5d | 45 | { |
carmenron | 0:4ebff00cfa5d | 46 | printf("RMS: %3.4f\n\r", rms); |
carmenron | 0:4ebff00cfa5d | 47 | } |
carmenron | 0:4ebff00cfa5d | 48 | |
carmenron | 0:4ebff00cfa5d | 49 | int16_t medidaTension; |
carmenron | 0:4ebff00cfa5d | 50 | int16_t medidaIntensidad; |
carmenron | 0:4ebff00cfa5d | 51 | int16_t medidaOffset; |
carmenron | 0:4ebff00cfa5d | 52 | int16_t medidaTensionSinOffset; |
carmenron | 0:4ebff00cfa5d | 53 | int16_t medidaIntensidadSinOffset; |
carmenron | 0:4ebff00cfa5d | 54 | int bufferActivo=0; |
carmenron | 0:4ebff00cfa5d | 55 | int flag=0; |
carmenron | 0:4ebff00cfa5d | 56 | int eBoton=0; |
carmenron | 0:4ebff00cfa5d | 57 | |
carmenron | 0:4ebff00cfa5d | 58 | void captura() |
carmenron | 0:4ebff00cfa5d | 59 | { |
carmenron | 0:4ebff00cfa5d | 60 | medidaTension=(int16_t)(adcTension()^0x8000); |
carmenron | 0:4ebff00cfa5d | 61 | medidaIntensidad=(int16_t)(adcIntensidad()^0x8000); |
carmenron | 0:4ebff00cfa5d | 62 | medidaOffset=(int16_t)(adcMedio()^0x8000); |
carmenron | 0:4ebff00cfa5d | 63 | medidaTensionSinOffset=medidaTension-medidaOffset; |
carmenron | 0:4ebff00cfa5d | 64 | medidaIntensidadSinOffset=medidaIntensidad-medidaOffset; |
carmenron | 0:4ebff00cfa5d | 65 | |
carmenron | 0:4ebff00cfa5d | 66 | if(bufferActivo) { |
carmenron | 0:4ebff00cfa5d | 67 | vTensionA[contador] = medidaTensionSinOffset; |
carmenron | 0:4ebff00cfa5d | 68 | vIntensidadA[contador] = medidaIntensidadSinOffset; |
carmenron | 0:4ebff00cfa5d | 69 | } else { |
carmenron | 0:4ebff00cfa5d | 70 | vTensionB[contador] = medidaTensionSinOffset; |
carmenron | 0:4ebff00cfa5d | 71 | vIntensidadB[contador] = medidaIntensidadSinOffset; |
carmenron | 0:4ebff00cfa5d | 72 | } |
carmenron | 0:4ebff00cfa5d | 73 | contador++; |
carmenron | 0:4ebff00cfa5d | 74 | if(contador==muestras) { |
carmenron | 0:4ebff00cfa5d | 75 | contador=0; |
carmenron | 0:4ebff00cfa5d | 76 | bufferActivo=!bufferActivo; |
carmenron | 0:4ebff00cfa5d | 77 | flag=1; |
carmenron | 0:4ebff00cfa5d | 78 | } |
carmenron | 0:4ebff00cfa5d | 79 | } |
carmenron | 0:4ebff00cfa5d | 80 | |
carmenron | 0:4ebff00cfa5d | 81 | int main() |
carmenron | 0:4ebff00cfa5d | 82 | { |
carmenron | 0:4ebff00cfa5d | 83 | |
carmenron | 0:4ebff00cfa5d | 84 | Ticker timerCaptura; |
carmenron | 0:4ebff00cfa5d | 85 | Ticker timerVisualizacion; |
carmenron | 0:4ebff00cfa5d | 86 | timerCaptura.attach_us(&captura,200); |
carmenron | 0:4ebff00cfa5d | 87 | timerVisualizacion.attach(&visualizacion,1.0); |
carmenron | 0:4ebff00cfa5d | 88 | while(1) { |
carmenron | 0:4ebff00cfa5d | 89 | if(flag) { |
carmenron | 0:4ebff00cfa5d | 90 | flag=0; |
carmenron | 0:4ebff00cfa5d | 91 | int16_t meas; |
carmenron | 0:4ebff00cfa5d | 92 | int32_t producto; |
carmenron | 0:4ebff00cfa5d | 93 | int64_t sumatorio=0; |
carmenron | 0:4ebff00cfa5d | 94 | |
carmenron | 0:4ebff00cfa5d | 95 | for (int b=0; b<muestras; b++) { |
carmenron | 0:4ebff00cfa5d | 96 | if (!bufferActivo) { |
carmenron | 0:4ebff00cfa5d | 97 | meas=vTensionA[b]; |
carmenron | 0:4ebff00cfa5d | 98 | } else { |
carmenron | 0:4ebff00cfa5d | 99 | meas=vTensionB[b]; |
carmenron | 0:4ebff00cfa5d | 100 | } |
carmenron | 0:4ebff00cfa5d | 101 | producto=meas*meas; |
carmenron | 0:4ebff00cfa5d | 102 | sumatorio+=producto; |
carmenron | 0:4ebff00cfa5d | 103 | |
carmenron | 0:4ebff00cfa5d | 104 | } |
carmenron | 0:4ebff00cfa5d | 105 | rms=sqrt((float)constanteTension*(float(sumatorio))); |
carmenron | 0:4ebff00cfa5d | 106 | } |
carmenron | 0:4ebff00cfa5d | 107 | } |
carmenron | 0:4ebff00cfa5d | 108 | } |