|
4 | 4 | * @brief Biblioteca medidor de energia, permite medir voltaje, y corriente con gran precisión. |
5 | 5 | * @version 2.2.1 |
6 | 6 | * @date 2021-04-16 |
7 | | - * |
| 7 | + * |
8 | 8 | * @copyright DataAnalitic (c) {2021} |
9 | | - * |
| 9 | + * |
10 | 10 | */ |
11 | 11 |
|
12 | 12 | #include "BslibEnergyMeter.h" |
13 | 13 |
|
| 14 | +int currentReference; |
| 15 | +int calibrationCurrent; |
| 16 | + |
| 17 | +int currentADC; |
| 18 | +int currentRefADC; |
| 19 | +float currentDAC; |
| 20 | +float current; |
| 21 | + |
| 22 | +int voltageADC; |
| 23 | +float voltageDAC; |
| 24 | +float voltage; |
| 25 | + |
14 | 26 | /** |
15 | 27 | * @brief Configuracion de los pines del sensor de corriente |
16 | | - * |
| 28 | + * |
17 | 29 | * @param _inPinCurrent pin analógico Vout del sensor de voltaje |
18 | 30 | * @param _inPinCurrentRef pin analógico Vref del sensor |
19 | 31 | * @param _factorCurrent factor de sensiblidad del sensor |
20 | 32 | */ |
21 | | -void BslibEnergyMeter::SetSensorCurrent(unsigned int _inPinCurrent, unsigned int _inPinCurrentRef, float _factorCurrent) |
22 | | -{ |
23 | | - inPinCurrent = _inPinCurrent; |
24 | | - inPinCurrentRef = _inPinCurrentRef; |
25 | | - factorCurrent = _factorCurrent; |
| 33 | +void BslibEnergyMeter::SetSensorCurrent(unsigned int _inPinCurrent, unsigned int _inPinCurrentRef, float _factorCurrent) { |
| 34 | + inPinCurrent = _inPinCurrent; |
| 35 | + inPinCurrentRef = _inPinCurrentRef; |
| 36 | + factorCurrent = _factorCurrent; |
26 | 37 | } |
27 | 38 |
|
28 | 39 | /** |
29 | 40 | * @brief Configuracion de los pines del sensor de voltaje |
30 | | - * |
| 41 | + * |
31 | 42 | * @param _inPinVoltage pin analógico de Vout de sensor de voltaje |
32 | 43 | * @param _factorVoltage factor de sensiblidad del sensor |
33 | 44 | * @param _offsetVoltage compensacion para obtener voltaje real |
34 | 45 | */ |
35 | | -void BslibEnergyMeter::SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage, float _offsetVoltage = 0) |
36 | | -{ |
37 | | - inPinVoltage = _inPinVoltage; |
38 | | - factorVoltage = _factorVoltage; |
39 | | - offsetVoltage = _offsetVoltage; |
| 46 | +void BslibEnergyMeter::SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage, float _offsetVoltage = 0) { |
| 47 | + inPinVoltage = _inPinVoltage; |
| 48 | + factorVoltage = _factorVoltage; |
| 49 | + offsetVoltage = _offsetVoltage; |
40 | 50 | } |
41 | 51 |
|
42 | 52 | /** |
43 | 53 | * @brief Configuracion de los pines del sensor de voltaje |
44 | | - * |
| 54 | + * |
45 | 55 | * @param _inPinVoltage pin analógico de sensor de voltaje |
46 | 56 | * @param _factorVoltage factor de sensiblidad del sensor |
47 | 57 | */ |
48 | | -void BslibEnergyMeter::SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage) |
49 | | -{ |
50 | | - inPinVoltage = _inPinVoltage; |
51 | | - factorVoltage = _factorVoltage; |
| 58 | +void BslibEnergyMeter::SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage) { |
| 59 | + inPinVoltage = _inPinVoltage; |
| 60 | + factorVoltage = _factorVoltage; |
52 | 61 | } |
53 | 62 |
|
54 | 63 | /** |
55 | | - * @brief Filtro suave (promedio) de lecturas ADC |
56 | | - * |
57 | | - * @param pinADC pin analógico al cual leer |
58 | | - * @param samples número de muestras para el promedio |
59 | | - * @return int |
| 64 | + * @brief Se usar después de la función AutoCalibrationCurrent para configurar el valor de referencia de corriente debido a que no siempre la corriente es cero. |
| 65 | + * |
| 66 | + * @param _currentReference |
60 | 67 | */ |
61 | | -unsigned int BslibEnergyMeter::FilterValueADC(unsigned int pinADC, unsigned int samples) |
62 | | -{ |
63 | | - unsigned long valueADC = 0; |
64 | | - unsigned int filteredValueADC = 0; |
65 | | - for (unsigned int i = 0; i < samples; i++) |
66 | | - { |
67 | | - valueADC += analogRead(pinADC); |
68 | | - } |
69 | | - filteredValueADC = valueADC / samples; |
70 | | - return filteredValueADC; |
| 68 | +void BslibEnergyMeter::SetCurrentReference(int _currentReference) { |
| 69 | + currentReference = _currentReference; |
71 | 70 | } |
72 | 71 |
|
73 | 72 | /** |
74 | | - * @brief Calibrar automaticamente el sensor de corriente con Vref. Usar esta función cuando la corriente sea cero. |
75 | | - * |
76 | | - * @param _numberOfSamples número de muestras a tomar en cada lenctura |
77 | | - * @return int |
| 73 | + * @brief Configura el valor de referencia analógica para el MCU |
| 74 | + * |
| 75 | + * @param _analogReference |
78 | 76 | */ |
79 | | -unsigned int BslibEnergyMeter::AutoCalibrationCurrent(unsigned int _numberOfSamples) |
80 | | -{ |
81 | | - calibrationCurrent = FilterValueADC(inPinCurrentRef, _numberOfSamples); |
82 | | - // algunos sensores tienen un offset (compensacion) cuando Vref<Vout |
83 | | - float offset = FilterValueADC(inPinCurrent, _numberOfSamples) - calibrationCurrent; |
84 | | - calibrationCurrent += offset; |
85 | | - return calibrationCurrent; |
| 77 | +void BslibEnergyMeter::SetAnalogReference(float _analogReference) { |
| 78 | + analogReference = _analogReference; |
| 79 | +} |
| 80 | + |
| 81 | +void BslibEnergyMeter::SetFilterSamples(unsigned int _numberOfSamples) { |
| 82 | + numberOfSamples = _numberOfSamples; |
86 | 83 | } |
87 | 84 |
|
88 | 85 | /** |
89 | | - * @brief Se usar después de la función AutoCalibrationCurrent para configurar el valor de referencia de corriente debido a que no siempre la corriente es cero. |
90 | | - * |
91 | | - * @param _currentReference |
| 86 | + * @brief Filtro suave (promedio) de lecturas ADC |
| 87 | + * |
| 88 | + * @param pinADC pin analógico al cual leer |
| 89 | + * @return int |
92 | 90 | */ |
93 | | -void BslibEnergyMeter::SetCurrentReference(unsigned int _currentReference) |
94 | | -{ |
95 | | - currentReference = _currentReference; |
| 91 | +int BslibEnergyMeter::FilterValueADC(unsigned int pinADC) { |
| 92 | + unsigned long valueADC = 0; |
| 93 | + int filteredValueADC = 0; |
| 94 | + for (int i = 0; i < numberOfSamples; i++) { |
| 95 | + valueADC += analogRead(pinADC); |
| 96 | + } |
| 97 | + filteredValueADC = valueADC / numberOfSamples; |
| 98 | + return filteredValueADC; |
96 | 99 | } |
97 | 100 |
|
98 | 101 | /** |
99 | | - * @brief Obtiene la corriente del sensor motor |
100 | | - * |
101 | | - * @param _numberOfSamples número de muestras a tomar en cada lenctura |
102 | | - * @return float |
| 102 | + * @brief Calcula el valor en voltaje de la lectura ADC del MCU |
| 103 | + * |
| 104 | + * @param digitalValue |
| 105 | + * @return float |
103 | 106 | */ |
104 | | -float BslibEnergyMeter::GetCurrent(unsigned int _numberOfSamples) |
105 | | -{ |
106 | | - int filteredCurrent = FilterValueADC(inPinCurrent, _numberOfSamples) - currentReference; |
107 | | - |
108 | | - if (filteredCurrent < 0) |
109 | | - { |
110 | | - filteredCurrent = 0; |
111 | | - } |
112 | | - |
113 | | - float convertValueADC = float(filteredCurrent) / ADC_SCALE * VOLT_INPUT_DRIVER; |
114 | | - float current = convertValueADC / factorCurrent; |
115 | | - return current; |
| 107 | +float BslibEnergyMeter::SoftwareDAC(int digitalValue) { |
| 108 | + float convertValueDCA = float(digitalValue) / ADC_SCALE * analogReference; |
| 109 | + return convertValueDCA; |
116 | 110 | } |
117 | 111 |
|
118 | 112 | /** |
119 | | - * @brief Obtiene el voltaje de la bateria |
120 | | - * |
121 | | - * @param _numberOfSamples número de muestras a tomar en cada lenctura |
122 | | - * @return float |
| 113 | + * @brief Calibrar automaticamente el sensor de corriente con Vref. Usar esta función cuando la corriente sea cero. |
| 114 | + * |
| 115 | + * @return int |
123 | 116 | */ |
124 | | -float BslibEnergyMeter::GetVoltage(unsigned int _numberOfSamples) |
125 | | -{ |
126 | | - int filteredVoltage = FilterValueADC(inPinVoltage, _numberOfSamples); |
127 | | - float convertValueADC = float(filteredVoltage) / ADC_SCALE * VOLT_INPUT_MAIN; |
128 | | - float voltage = (convertValueADC * factorVoltage) + offsetVoltage; |
| 117 | +int BslibEnergyMeter::AutoCalibrationCurrent(int lastVRef) { |
| 118 | + |
| 119 | + float vRef = FilterValueADC(inPinCurrentRef); |
| 120 | + float vOut = FilterValueADC(inPinCurrent); |
| 121 | + |
| 122 | + if (lastVRef == vRef) { |
| 123 | + calibrationCurrent = vRef; |
| 124 | + } else { |
| 125 | + calibrationCurrent = vRef; |
| 126 | + } |
| 127 | + // algunos sensores tienen un offset (compensacion) cuando Vref<Vout |
| 128 | + // float offset = vOut - vRef; |
| 129 | + // calibrationCurrent += offset; |
| 130 | + return calibrationCurrent; |
| 131 | +} |
| 132 | + |
| 133 | +void BslibEnergyMeter::CalCurrent() { |
| 134 | + currentRefADC = FilterValueADC(inPinCurrentRef); |
| 135 | + currentADC = FilterValueADC(inPinCurrent); |
| 136 | + |
| 137 | + int offsetCurrent = currentADC - currentRefADC; |
| 138 | + |
| 139 | + if (offsetCurrent < 0) { |
| 140 | + offsetCurrent = 0; |
| 141 | + } |
| 142 | + |
| 143 | + currentDAC = SoftwareDAC(offsetCurrent); |
| 144 | + current = currentDAC / factorCurrent; |
| 145 | +} |
| 146 | + |
| 147 | +int BslibEnergyMeter::GetCurrentRefADC() { |
| 148 | + return currentRefADC; |
| 149 | +} |
| 150 | + |
| 151 | +int BslibEnergyMeter::GetCurrentADC() { |
| 152 | + return currentADC; |
| 153 | +} |
| 154 | + |
| 155 | +float BslibEnergyMeter::GetCurrentDAC() { |
| 156 | + return currentDAC; |
| 157 | +} |
| 158 | +float BslibEnergyMeter::GetCurrent() { |
| 159 | + CalCurrent(); |
| 160 | + return current; |
| 161 | +} |
| 162 | + |
| 163 | +// ----------------------------------------------------------- |
| 164 | + |
| 165 | +void BslibEnergyMeter::CalVoltage() { |
| 166 | + voltageADC = FilterValueADC(inPinVoltage); |
| 167 | + voltageDAC = SoftwareDAC(voltageADC); |
| 168 | + voltage = (voltageDAC * factorVoltage) + offsetVoltage; |
| 169 | +} |
| 170 | + |
| 171 | +int BslibEnergyMeter::GetVoltageADC() { |
| 172 | + return voltageADC; |
| 173 | +} |
| 174 | + |
| 175 | +float BslibEnergyMeter::GetVoltageDAC() { |
| 176 | + return voltageDAC; |
| 177 | +} |
| 178 | + |
| 179 | +float BslibEnergyMeter::GetVoltage() { |
| 180 | + CalVoltage(); |
| 181 | + |
| 182 | + return voltage; |
| 183 | +} |
| 184 | + |
| 185 | +float BslibEnergyMeter::ReadVcc() { |
| 186 | + long vccADC; |
| 187 | + |
| 188 | +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) |
| 189 | + ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); |
| 190 | +#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) |
| 191 | + ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); |
| 192 | +#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB1286__) |
| 193 | + ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); |
| 194 | + ADCSRB &= ~_BV(MUX5); // Without this the function always returns -1 on the ATmega2560 http://openenergymonitor.org/emon/node/2253#comment-11432 |
| 195 | +#elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) |
| 196 | + ADMUX = _BV(MUX5) | _BV(MUX0); |
| 197 | +#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) |
| 198 | + ADMUX = _BV(MUX3) | _BV(MUX2); |
| 199 | + |
| 200 | +#endif |
129 | 201 |
|
130 | | - return voltage; |
| 202 | +#if defined(__AVR__) |
| 203 | + delay(2); // Wait for Vref to settle |
| 204 | + ADCSRA |= _BV(ADSC); // Convert |
| 205 | + while (bit_is_set(ADCSRA, ADSC)) |
| 206 | + ; |
| 207 | + vccADC = ADCL; |
| 208 | + vccADC |= ADCH << 8; |
| 209 | + vccADC = READVCC_CALIBRATION_CONST / vccADC; // 1100mV*1024 ADC steps |
| 210 | + return vccADC / 1000.0; |
| 211 | +#elif defined(__arm__) |
| 212 | + return (3300); // Arduino Due |
| 213 | +#else |
| 214 | + return (3300); // Guess that other un-supported architectures will be running a 3.3V! |
| 215 | +#endif |
131 | 216 | } |
0 commit comments