From abe13f74328975f9c3dcf595f5438a2cdc3ec4e1 Mon Sep 17 00:00:00 2001 From: jhon-p16 Date: Wed, 16 Mar 2022 15:51:52 -0500 Subject: [PATCH 01/10] feat: add standalone AREF config --- src/BslibEnergyMeter.cpp | 14 ++++++++++++-- src/BslibEnergyMeter.h | 6 ++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/BslibEnergyMeter.cpp b/src/BslibEnergyMeter.cpp index 3cbfe87..7249a71 100644 --- a/src/BslibEnergyMeter.cpp +++ b/src/BslibEnergyMeter.cpp @@ -95,6 +95,16 @@ void BslibEnergyMeter::SetCurrentReference(unsigned int _currentReference) currentReference = _currentReference; } +/** + * @brief Configura el valor de referencia analógica para el MCU + * + * @param _analogReference + */ +void BslibEnergyMeter::SetAnalogReference(float _analogReference) +{ + analogReference = _analogReference; +} + /** * @brief Obtiene la corriente del sensor motor * @@ -110,7 +120,7 @@ float BslibEnergyMeter::GetCurrent(unsigned int _numberOfSamples) filteredCurrent = 0; } - float convertValueADC = float(filteredCurrent) / ADC_SCALE * VOLT_INPUT_DRIVER; + float convertValueADC = float(filteredCurrent) / ADC_SCALE * analogReference; float current = convertValueADC / factorCurrent; return current; } @@ -124,7 +134,7 @@ float BslibEnergyMeter::GetCurrent(unsigned int _numberOfSamples) float BslibEnergyMeter::GetVoltage(unsigned int _numberOfSamples) { int filteredVoltage = FilterValueADC(inPinVoltage, _numberOfSamples); - float convertValueADC = float(filteredVoltage) / ADC_SCALE * VOLT_INPUT_MAIN; + float convertValueADC = float(filteredVoltage) / ADC_SCALE * analogReference; float voltage = (convertValueADC * factorVoltage) + offsetVoltage; return voltage; diff --git a/src/BslibEnergyMeter.h b/src/BslibEnergyMeter.h index 3ebc2b9..133b0c6 100644 --- a/src/BslibEnergyMeter.h +++ b/src/BslibEnergyMeter.h @@ -14,12 +14,11 @@ #include "Arduino.h" #define ADC_SCALE 1023.0 -#define VOLT_INPUT_MAIN 5.0 -#define VOLT_INPUT_DRIVER 3.3 class BslibEnergyMeter { public: + void SetSensorCurrent(unsigned int _inPinCurrent, unsigned int _inPinCurrentRef, float _factorCurrent); void SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage, float _offsetVoltage); void SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage); @@ -27,6 +26,8 @@ class BslibEnergyMeter unsigned int FilterValueADC(unsigned int pinADC, unsigned int samples); unsigned int AutoCalibrationCurrent(unsigned int _numberOfSamples); void SetCurrentReference(unsigned int _currentReference); + void SetAnalogReference(float _analogReference); + float GetCurrent(unsigned int _numberOfSamples); float GetVoltage(unsigned int _numberOfSamples); @@ -39,6 +40,7 @@ class BslibEnergyMeter unsigned int inPinCurrentRef; unsigned int inPinVoltage; + float analogReference; float factorCurrent; float factorVoltage; float offsetVoltage; From 7c1b8f21c40d7b36c14fb36a04521d3fac7713d3 Mon Sep 17 00:00:00 2001 From: jhon-p16 Date: Wed, 16 Mar 2022 16:58:25 -0500 Subject: [PATCH 02/10] feat: software DCA converter --- src/BslibEnergyMeter.cpp | 21 +++++++++++++++++---- src/BslibEnergyMeter.h | 5 ++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/BslibEnergyMeter.cpp b/src/BslibEnergyMeter.cpp index 7249a71..f8ccec6 100644 --- a/src/BslibEnergyMeter.cpp +++ b/src/BslibEnergyMeter.cpp @@ -105,6 +105,19 @@ void BslibEnergyMeter::SetAnalogReference(float _analogReference) analogReference = _analogReference; } +/** + * @brief Calcula el valor en voltaje de la lectura ADC del MCU + * + * @param digitalValue + * @return float + */ +float BslibEnergyMeter::SoftwareDCA (unsigned int digitalValue) +{ + float convertValueDCA = float(digitalValue) / ADC_SCALE * analogReference; + return convertValueDCA; + +} + /** * @brief Obtiene la corriente del sensor motor * @@ -120,8 +133,8 @@ float BslibEnergyMeter::GetCurrent(unsigned int _numberOfSamples) filteredCurrent = 0; } - float convertValueADC = float(filteredCurrent) / ADC_SCALE * analogReference; - float current = convertValueADC / factorCurrent; + float voltageSensor = SoftwareDCA(filteredCurrent); + float current = voltageSensor / factorCurrent; return current; } @@ -134,8 +147,8 @@ float BslibEnergyMeter::GetCurrent(unsigned int _numberOfSamples) float BslibEnergyMeter::GetVoltage(unsigned int _numberOfSamples) { int filteredVoltage = FilterValueADC(inPinVoltage, _numberOfSamples); - float convertValueADC = float(filteredVoltage) / ADC_SCALE * analogReference; - float voltage = (convertValueADC * factorVoltage) + offsetVoltage; + float voltageSensor = SoftwareDCA(filteredVoltage); + float voltage = (voltageSensor * factorVoltage) + offsetVoltage; return voltage; } diff --git a/src/BslibEnergyMeter.h b/src/BslibEnergyMeter.h index 133b0c6..ad864f5 100644 --- a/src/BslibEnergyMeter.h +++ b/src/BslibEnergyMeter.h @@ -15,6 +15,8 @@ #define ADC_SCALE 1023.0 +#if + class BslibEnergyMeter { public: @@ -28,9 +30,10 @@ class BslibEnergyMeter void SetCurrentReference(unsigned int _currentReference); void SetAnalogReference(float _analogReference); - + float SoftwareDCA (unsigned int digitalValue); float GetCurrent(unsigned int _numberOfSamples); float GetVoltage(unsigned int _numberOfSamples); + // float DebugDCA(); private: unsigned int currentReference; From 57ca6f6a071b555f0eb17350c8435b7ae4f6cdf7 Mon Sep 17 00:00:00 2001 From: jhon-p16 Date: Wed, 16 Mar 2022 17:19:35 -0500 Subject: [PATCH 03/10] fix: remove accidental conditional --- src/BslibEnergyMeter.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/BslibEnergyMeter.h b/src/BslibEnergyMeter.h index ad864f5..94ea7ad 100644 --- a/src/BslibEnergyMeter.h +++ b/src/BslibEnergyMeter.h @@ -15,8 +15,6 @@ #define ADC_SCALE 1023.0 -#if - class BslibEnergyMeter { public: From 9566958974315cd2db2764bc7c520469feaa5211 Mon Sep 17 00:00:00 2001 From: jhon-p16 Date: Thu, 17 Mar 2022 09:45:22 -0500 Subject: [PATCH 04/10] refactor: rename signal variable names --- src/BslibEnergyMeter.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/BslibEnergyMeter.cpp b/src/BslibEnergyMeter.cpp index f8ccec6..b9ee41f 100644 --- a/src/BslibEnergyMeter.cpp +++ b/src/BslibEnergyMeter.cpp @@ -78,9 +78,12 @@ unsigned int BslibEnergyMeter::FilterValueADC(unsigned int pinADC, unsigned int */ unsigned int BslibEnergyMeter::AutoCalibrationCurrent(unsigned int _numberOfSamples) { - calibrationCurrent = FilterValueADC(inPinCurrentRef, _numberOfSamples); + float vRef = FilterValueADC(inPinCurrentRef, _numberOfSamples); + float vOut = FilterValueADC(inPinCurrent, _numberOfSamples); + + calibrationCurrent = vRef; // algunos sensores tienen un offset (compensacion) cuando Vref Date: Sat, 19 Mar 2022 17:17:43 -0500 Subject: [PATCH 05/10] refactor: add Real-time calibration vRef --- src/BslibEnergyMeter.cpp | 18 +++++++++++++----- src/BslibEnergyMeter.h | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/BslibEnergyMeter.cpp b/src/BslibEnergyMeter.cpp index b9ee41f..dfefb70 100644 --- a/src/BslibEnergyMeter.cpp +++ b/src/BslibEnergyMeter.cpp @@ -76,15 +76,23 @@ unsigned int BslibEnergyMeter::FilterValueADC(unsigned int pinADC, unsigned int * @param _numberOfSamples número de muestras a tomar en cada lenctura * @return int */ -unsigned int BslibEnergyMeter::AutoCalibrationCurrent(unsigned int _numberOfSamples) +unsigned int BslibEnergyMeter::AutoCalibrationCurrent(unsigned int _numberOfSamples, unsigned int lastVRef) { + float vRef = FilterValueADC(inPinCurrentRef, _numberOfSamples); float vOut = FilterValueADC(inPinCurrent, _numberOfSamples); - - calibrationCurrent = vRef; + + if (lastVRef == vRef) + { + calibrationCurrent = lastVRef; + } + else + { + calibrationCurrent = vRef; + } // algunos sensores tienen un offset (compensacion) cuando Vref Date: Mon, 4 Apr 2022 15:25:05 -0500 Subject: [PATCH 06/10] feat: auto read VCC MCU --- src/BslibEnergyMeter.cpp | 35 +++++++++++++++++++++++++++++++++++ src/BslibEnergyMeter.h | 10 +++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/BslibEnergyMeter.cpp b/src/BslibEnergyMeter.cpp index dfefb70..9688530 100644 --- a/src/BslibEnergyMeter.cpp +++ b/src/BslibEnergyMeter.cpp @@ -163,3 +163,38 @@ float BslibEnergyMeter::GetVoltage(unsigned int _numberOfSamples) return voltage; } + +float BslibEnergyMeter::ReadVcc() +{ + long vccADC; + +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) + ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); +#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) + ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); +#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB1286__) + ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); + ADCSRB &= ~_BV(MUX5); // Without this the function always returns -1 on the ATmega2560 http://openenergymonitor.org/emon/node/2253#comment-11432 +#elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) + ADMUX = _BV(MUX5) | _BV(MUX0); +#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + ADMUX = _BV(MUX3) | _BV(MUX2); + +#endif + +#if defined(__AVR__) + delay(2); // Wait for Vref to settle + ADCSRA |= _BV(ADSC); // Convert + while (bit_is_set(ADCSRA, ADSC)) + ; + vccADC = ADCL; + vccADC |= ADCH << 8; + vccADC = READVCC_CALIBRATION_CONST / vccADC; //1100mV*1024 ADC steps + return vccADC / 1000.0; +#elif defined(__arm__) + return (3300); //Arduino Due +#else + return (3300); //Guess that other un-supported architectures will be running a 3.3V! +#endif +} + diff --git a/src/BslibEnergyMeter.h b/src/BslibEnergyMeter.h index e955c3a..cc5e4c1 100644 --- a/src/BslibEnergyMeter.h +++ b/src/BslibEnergyMeter.h @@ -15,6 +15,12 @@ #define ADC_SCALE 1023.0 +// define theoretical vref calibration constant for use in readvcc() +// 1100mV*1024 ADC steps +#ifndef READVCC_CALIBRATION_CONST +#define READVCC_CALIBRATION_CONST 1126400L +#endif + class BslibEnergyMeter { public: @@ -31,7 +37,9 @@ class BslibEnergyMeter float SoftwareDCA (unsigned int digitalValue); float GetCurrent(unsigned int _numberOfSamples); float GetVoltage(unsigned int _numberOfSamples); - // float DebugDCA(); + + float ReadVcc(); + private: unsigned int currentReference; From c9fbba7f20619b742ae735e65bdfb5c752adc62c Mon Sep 17 00:00:00 2001 From: jhon-p16 Date: Wed, 20 Jul 2022 18:03:01 -0500 Subject: [PATCH 07/10] refactor: global config for samples --- src/BslibEnergyMeter.cpp | 30 +++++++++++++++--------------- src/BslibEnergyMeter.h | 13 +++++++++---- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/BslibEnergyMeter.cpp b/src/BslibEnergyMeter.cpp index 9688530..fb44047 100644 --- a/src/BslibEnergyMeter.cpp +++ b/src/BslibEnergyMeter.cpp @@ -55,32 +55,30 @@ void BslibEnergyMeter::SetSensorVoltage(unsigned int _inPinVoltage, float _facto * @brief Filtro suave (promedio) de lecturas ADC * * @param pinADC pin analógico al cual leer - * @param samples número de muestras para el promedio * @return int */ -unsigned int BslibEnergyMeter::FilterValueADC(unsigned int pinADC, unsigned int samples) +unsigned int BslibEnergyMeter::FilterValueADC(unsigned int pinADC) { unsigned long valueADC = 0; unsigned int filteredValueADC = 0; - for (unsigned int i = 0; i < samples; i++) + for (unsigned int i = 0; i < numberOfSamples; i++) { valueADC += analogRead(pinADC); } - filteredValueADC = valueADC / samples; + filteredValueADC = valueADC / numberOfSamples; return filteredValueADC; } /** * @brief Calibrar automaticamente el sensor de corriente con Vref. Usar esta función cuando la corriente sea cero. * - * @param _numberOfSamples número de muestras a tomar en cada lenctura * @return int */ -unsigned int BslibEnergyMeter::AutoCalibrationCurrent(unsigned int _numberOfSamples, unsigned int lastVRef) +unsigned int BslibEnergyMeter::AutoCalibrationCurrent(unsigned int lastVRef) { - float vRef = FilterValueADC(inPinCurrentRef, _numberOfSamples); - float vOut = FilterValueADC(inPinCurrent, _numberOfSamples); + float vRef = FilterValueADC(inPinCurrentRef); + float vOut = FilterValueADC(inPinCurrent); if (lastVRef == vRef) { @@ -116,6 +114,11 @@ void BslibEnergyMeter::SetAnalogReference(float _analogReference) analogReference = _analogReference; } +void BslibEnergyMeter::SetFilterSamples(unsigned int _numberOfSamples) +{ + numberOfSamples = _numberOfSamples; +} + /** * @brief Calcula el valor en voltaje de la lectura ADC del MCU * @@ -126,18 +129,16 @@ float BslibEnergyMeter::SoftwareDCA (unsigned int digitalValue) { float convertValueDCA = float(digitalValue) / ADC_SCALE * analogReference; return convertValueDCA; - } /** * @brief Obtiene la corriente del sensor motor * - * @param _numberOfSamples número de muestras a tomar en cada lenctura * @return float */ -float BslibEnergyMeter::GetCurrent(unsigned int _numberOfSamples) +float BslibEnergyMeter::GetCurrent() { - int filteredCurrent = FilterValueADC(inPinCurrent, _numberOfSamples) - currentReference; + int filteredCurrent = FilterValueADC(inPinCurrent) - currentReference; if (filteredCurrent < 0) { @@ -152,12 +153,11 @@ float BslibEnergyMeter::GetCurrent(unsigned int _numberOfSamples) /** * @brief Obtiene el voltaje de la bateria * - * @param _numberOfSamples número de muestras a tomar en cada lenctura * @return float */ -float BslibEnergyMeter::GetVoltage(unsigned int _numberOfSamples) +float BslibEnergyMeter::GetVoltage() { - int filteredVoltage = FilterValueADC(inPinVoltage, _numberOfSamples); + int filteredVoltage = FilterValueADC(inPinVoltage); float voltageSensor = SoftwareDCA(filteredVoltage); float voltage = (voltageSensor * factorVoltage) + offsetVoltage; diff --git a/src/BslibEnergyMeter.h b/src/BslibEnergyMeter.h index cc5e4c1..97986de 100644 --- a/src/BslibEnergyMeter.h +++ b/src/BslibEnergyMeter.h @@ -29,14 +29,18 @@ class BslibEnergyMeter void SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage, float _offsetVoltage); void SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage); - unsigned int FilterValueADC(unsigned int pinADC, unsigned int samples); - unsigned int AutoCalibrationCurrent(unsigned int _numberOfSamples, unsigned int lastVRef); + unsigned int FilterValueADC(unsigned int pinADC); + unsigned int AutoCalibrationCurrent(unsigned int lastVRef); void SetCurrentReference(unsigned int _currentReference); void SetAnalogReference(float _analogReference); + void SetFilterSamples(unsigned int _numberOfSamples); + float SoftwareDCA (unsigned int digitalValue); - float GetCurrent(unsigned int _numberOfSamples); - float GetVoltage(unsigned int _numberOfSamples); + float GetCurrent(); + float GetCurrentADC(); + int GetCurrentRaw(); + float GetVoltage(); float ReadVcc(); @@ -49,6 +53,7 @@ class BslibEnergyMeter unsigned int inPinCurrentRef; unsigned int inPinVoltage; + unsigned int numberOfSamples; float analogReference; float factorCurrent; float factorVoltage; From fb4e8331f93a45070d7b3cd83a17dcb481566036 Mon Sep 17 00:00:00 2001 From: jhon-p16 Date: Thu, 28 Jul 2022 18:23:21 -0500 Subject: [PATCH 08/10] refactor: add getters and setters for sensors parameters --- src/BslibEnergyMeter.cpp | 151 +++++++++++++++++++++++++-------------- src/BslibEnergyMeter.h | 44 +++++++----- 2 files changed, 122 insertions(+), 73 deletions(-) diff --git a/src/BslibEnergyMeter.cpp b/src/BslibEnergyMeter.cpp index fb44047..b3fed43 100644 --- a/src/BslibEnergyMeter.cpp +++ b/src/BslibEnergyMeter.cpp @@ -11,6 +11,18 @@ #include "BslibEnergyMeter.h" + int currentReference; + int calibrationCurrent; + + int currentADC; + int currentRefADC; + float currentDAC; + float current; + + int voltageADC; + float voltageDAC; + float voltage; + /** * @brief Configuracion de los pines del sensor de corriente * @@ -18,7 +30,7 @@ * @param _inPinCurrentRef pin analógico Vref del sensor * @param _factorCurrent factor de sensiblidad del sensor */ -void BslibEnergyMeter::SetSensorCurrent(unsigned int _inPinCurrent, unsigned int _inPinCurrentRef, float _factorCurrent) +void BslibEnergyMeter::SetSensorCurrent(int _inPinCurrent, int _inPinCurrentRef, float _factorCurrent) { inPinCurrent = _inPinCurrent; inPinCurrentRef = _inPinCurrentRef; @@ -32,7 +44,7 @@ void BslibEnergyMeter::SetSensorCurrent(unsigned int _inPinCurrent, unsigned int * @param _factorVoltage factor de sensiblidad del sensor * @param _offsetVoltage compensacion para obtener voltaje real */ -void BslibEnergyMeter::SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage, float _offsetVoltage = 0) +void BslibEnergyMeter::SetSensorVoltage(int _inPinVoltage, float _factorVoltage, float _offsetVoltage = 0) { inPinVoltage = _inPinVoltage; factorVoltage = _factorVoltage; @@ -45,23 +57,48 @@ void BslibEnergyMeter::SetSensorVoltage(unsigned int _inPinVoltage, float _facto * @param _inPinVoltage pin analógico de sensor de voltaje * @param _factorVoltage factor de sensiblidad del sensor */ -void BslibEnergyMeter::SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage) +void BslibEnergyMeter::SetSensorVoltage(int _inPinVoltage, float _factorVoltage) { inPinVoltage = _inPinVoltage; factorVoltage = _factorVoltage; } +/** + * @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. + * + * @param _currentReference + */ +void BslibEnergyMeter::SetCurrentReference(int _currentReference) +{ + currentReference = _currentReference; +} + +/** + * @brief Configura el valor de referencia analógica para el MCU + * + * @param _analogReference + */ +void BslibEnergyMeter::SetAnalogReference(float _analogReference) +{ + analogReference = _analogReference; +} + +void BslibEnergyMeter::SetFilterSamples(int _numberOfSamples) +{ + numberOfSamples = _numberOfSamples; +} + /** * @brief Filtro suave (promedio) de lecturas ADC * * @param pinADC pin analógico al cual leer * @return int */ -unsigned int BslibEnergyMeter::FilterValueADC(unsigned int pinADC) +int BslibEnergyMeter::FilterValueADC(int pinADC) { unsigned long valueADC = 0; - unsigned int filteredValueADC = 0; - for (unsigned int i = 0; i < numberOfSamples; i++) + int filteredValueADC = 0; + for (int i = 0; i < numberOfSamples; i++) { valueADC += analogRead(pinADC); } @@ -69,12 +106,24 @@ unsigned int BslibEnergyMeter::FilterValueADC(unsigned int pinADC) return filteredValueADC; } +/** + * @brief Calcula el valor en voltaje de la lectura ADC del MCU + * + * @param digitalValue + * @return float + */ +float BslibEnergyMeter::SoftwareDAC (int digitalValue) +{ + float convertValueDCA = float(digitalValue) / ADC_SCALE * analogReference; + return convertValueDCA; +} + /** * @brief Calibrar automaticamente el sensor de corriente con Vref. Usar esta función cuando la corriente sea cero. * * @return int */ -unsigned int BslibEnergyMeter::AutoCalibrationCurrent(unsigned int lastVRef) +int BslibEnergyMeter::AutoCalibrationCurrent(int lastVRef) { float vRef = FilterValueADC(inPinCurrentRef); @@ -82,7 +131,7 @@ unsigned int BslibEnergyMeter::AutoCalibrationCurrent(unsigned int lastVRef) if (lastVRef == vRef) { - calibrationCurrent = lastVRef; + calibrationCurrent = vRef; } else { @@ -94,72 +143,64 @@ unsigned int BslibEnergyMeter::AutoCalibrationCurrent(unsigned int lastVRef) return calibrationCurrent; } -/** - * @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. - * - * @param _currentReference - */ -void BslibEnergyMeter::SetCurrentReference(unsigned int _currentReference) +void BslibEnergyMeter::CalCurrent() { - currentReference = _currentReference; + currentRefADC = FilterValueADC(inPinCurrentRef); + currentADC = FilterValueADC(inPinCurrent); + + int offsetCurrent = currentADC - currentRefADC; + + if (offsetCurrent < 0) + { + offsetCurrent = 0; + } + + currentDAC = SoftwareDAC(offsetCurrent); + current = currentDAC / factorCurrent; } -/** - * @brief Configura el valor de referencia analógica para el MCU - * - * @param _analogReference - */ -void BslibEnergyMeter::SetAnalogReference(float _analogReference) +int BslibEnergyMeter::GetCurrentRefADC() { - analogReference = _analogReference; + return currentRefADC; } -void BslibEnergyMeter::SetFilterSamples(unsigned int _numberOfSamples) +int BslibEnergyMeter::GetCurrentADC() { - numberOfSamples = _numberOfSamples; + return currentADC; } -/** - * @brief Calcula el valor en voltaje de la lectura ADC del MCU - * - * @param digitalValue - * @return float - */ -float BslibEnergyMeter::SoftwareDCA (unsigned int digitalValue) +float BslibEnergyMeter::GetCurrentDAC() { - float convertValueDCA = float(digitalValue) / ADC_SCALE * analogReference; - return convertValueDCA; + return currentDAC; } - -/** - * @brief Obtiene la corriente del sensor motor - * - * @return float - */ float BslibEnergyMeter::GetCurrent() { - int filteredCurrent = FilterValueADC(inPinCurrent) - currentReference; + CalCurrent(); + return current; +} - if (filteredCurrent < 0) - { - filteredCurrent = 0; - } +// ----------------------------------------------------------- - float voltageSensor = SoftwareDCA(filteredCurrent); - float current = voltageSensor / factorCurrent; - return current; +void BslibEnergyMeter::CalVoltage() +{ + voltageADC = FilterValueADC(inPinVoltage); + voltageDAC = SoftwareDAC(voltageADC); + voltage = (voltageDAC * factorVoltage) + offsetVoltage; +} + +int BslibEnergyMeter::GetVoltageADC() +{ + return voltageADC; +} + +float BslibEnergyMeter::GetVoltageDAC() +{ + return voltageDAC; } -/** - * @brief Obtiene el voltaje de la bateria - * - * @return float - */ float BslibEnergyMeter::GetVoltage() { - int filteredVoltage = FilterValueADC(inPinVoltage); - float voltageSensor = SoftwareDCA(filteredVoltage); - float voltage = (voltageSensor * factorVoltage) + offsetVoltage; + CalVoltage(); return voltage; } diff --git a/src/BslibEnergyMeter.h b/src/BslibEnergyMeter.h index 97986de..3b5a31a 100644 --- a/src/BslibEnergyMeter.h +++ b/src/BslibEnergyMeter.h @@ -25,35 +25,43 @@ class BslibEnergyMeter { public: - void SetSensorCurrent(unsigned int _inPinCurrent, unsigned int _inPinCurrentRef, float _factorCurrent); - void SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage, float _offsetVoltage); - void SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage); + void SetSensorCurrent(int _inPinCurrent, int _inPinCurrentRef, float _factorCurrent); + void SetSensorVoltage(int _inPinVoltage, float _factorVoltage, float _offsetVoltage); + void SetSensorVoltage(int _inPinVoltage, float _factorVoltage); - unsigned int FilterValueADC(unsigned int pinADC); - unsigned int AutoCalibrationCurrent(unsigned int lastVRef); - void SetCurrentReference(unsigned int _currentReference); + void SetCurrentReference(int _currentReference); void SetAnalogReference(float _analogReference); - void SetFilterSamples(unsigned int _numberOfSamples); + void SetFilterSamples(int _numberOfSamples); + + int FilterValueADC(int pinADC); + float SoftwareDAC (int digitalValue); + int AutoCalibrationCurrent(int lastVRef); - float SoftwareDCA (unsigned int digitalValue); + void CalCurrent(); + int GetCurrentRefADC(); + int GetCurrentADC(); + float GetCurrentDAC(); float GetCurrent(); - float GetCurrentADC(); - int GetCurrentRaw(); + + void CalVoltage(); + int GetVoltageADC(); + float GetVoltageDAC(); float GetVoltage(); - float ReadVcc(); + // int GetDistanceADC(); + // float GetDistanceDAC(); + // float GetDistance(); + float ReadVcc(); -private: - unsigned int currentReference; - unsigned int calibrationCurrent; +private: - unsigned int inPinCurrent; - unsigned int inPinCurrentRef; - unsigned int inPinVoltage; + int inPinCurrent; + int inPinCurrentRef; + int inPinVoltage; - unsigned int numberOfSamples; + int numberOfSamples; float analogReference; float factorCurrent; float factorVoltage; From 4ea615dd9751658d9775ee2239f328209f61284c Mon Sep 17 00:00:00 2001 From: jhon-p16 Date: Mon, 22 Aug 2022 10:19:19 -0500 Subject: [PATCH 09/10] docs: clang format based on LLVM --- src/BslibEnergyMeter.cpp | 249 ++++++++++++++++++--------------------- src/BslibEnergyMeter.h | 71 ++++++----- 2 files changed, 146 insertions(+), 174 deletions(-) diff --git a/src/BslibEnergyMeter.cpp b/src/BslibEnergyMeter.cpp index b3fed43..55a139d 100644 --- a/src/BslibEnergyMeter.cpp +++ b/src/BslibEnergyMeter.cpp @@ -4,238 +4,213 @@ * @brief Biblioteca medidor de energia, permite medir voltaje, y corriente con gran precisión. * @version 2.2.1 * @date 2021-04-16 - * + * * @copyright DataAnalitic (c) {2021} - * + * */ #include "BslibEnergyMeter.h" - int currentReference; - int calibrationCurrent; +int currentReference; +int calibrationCurrent; - int currentADC; - int currentRefADC; - float currentDAC; - float current; +int currentADC; +int currentRefADC; +float currentDAC; +float current; - int voltageADC; - float voltageDAC; - float voltage; +int voltageADC; +float voltageDAC; +float voltage; /** * @brief Configuracion de los pines del sensor de corriente - * + * * @param _inPinCurrent pin analógico Vout del sensor de voltaje * @param _inPinCurrentRef pin analógico Vref del sensor * @param _factorCurrent factor de sensiblidad del sensor */ -void BslibEnergyMeter::SetSensorCurrent(int _inPinCurrent, int _inPinCurrentRef, float _factorCurrent) -{ - inPinCurrent = _inPinCurrent; - inPinCurrentRef = _inPinCurrentRef; - factorCurrent = _factorCurrent; +void BslibEnergyMeter::SetSensorCurrent(int _inPinCurrent, int _inPinCurrentRef, float _factorCurrent) { + inPinCurrent = _inPinCurrent; + inPinCurrentRef = _inPinCurrentRef; + factorCurrent = _factorCurrent; } /** * @brief Configuracion de los pines del sensor de voltaje - * + * * @param _inPinVoltage pin analógico de Vout de sensor de voltaje * @param _factorVoltage factor de sensiblidad del sensor * @param _offsetVoltage compensacion para obtener voltaje real */ -void BslibEnergyMeter::SetSensorVoltage(int _inPinVoltage, float _factorVoltage, float _offsetVoltage = 0) -{ - inPinVoltage = _inPinVoltage; - factorVoltage = _factorVoltage; - offsetVoltage = _offsetVoltage; +void BslibEnergyMeter::SetSensorVoltage(int _inPinVoltage, float _factorVoltage, float _offsetVoltage = 0) { + inPinVoltage = _inPinVoltage; + factorVoltage = _factorVoltage; + offsetVoltage = _offsetVoltage; } /** * @brief Configuracion de los pines del sensor de voltaje - * + * * @param _inPinVoltage pin analógico de sensor de voltaje * @param _factorVoltage factor de sensiblidad del sensor */ -void BslibEnergyMeter::SetSensorVoltage(int _inPinVoltage, float _factorVoltage) -{ - inPinVoltage = _inPinVoltage; - factorVoltage = _factorVoltage; +void BslibEnergyMeter::SetSensorVoltage(int _inPinVoltage, float _factorVoltage) { + inPinVoltage = _inPinVoltage; + factorVoltage = _factorVoltage; } /** * @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. - * - * @param _currentReference + * + * @param _currentReference */ -void BslibEnergyMeter::SetCurrentReference(int _currentReference) -{ - currentReference = _currentReference; +void BslibEnergyMeter::SetCurrentReference(int _currentReference) { + currentReference = _currentReference; } /** * @brief Configura el valor de referencia analógica para el MCU - * - * @param _analogReference + * + * @param _analogReference */ -void BslibEnergyMeter::SetAnalogReference(float _analogReference) -{ - analogReference = _analogReference; +void BslibEnergyMeter::SetAnalogReference(float _analogReference) { + analogReference = _analogReference; } -void BslibEnergyMeter::SetFilterSamples(int _numberOfSamples) -{ - numberOfSamples = _numberOfSamples; +void BslibEnergyMeter::SetFilterSamples(int _numberOfSamples) { + numberOfSamples = _numberOfSamples; } /** * @brief Filtro suave (promedio) de lecturas ADC - * + * * @param pinADC pin analógico al cual leer - * @return int + * @return int */ -int BslibEnergyMeter::FilterValueADC(int pinADC) -{ - unsigned long valueADC = 0; - int filteredValueADC = 0; - for (int i = 0; i < numberOfSamples; i++) - { - valueADC += analogRead(pinADC); - } - filteredValueADC = valueADC / numberOfSamples; - return filteredValueADC; +int BslibEnergyMeter::FilterValueADC(int pinADC) { + unsigned long valueADC = 0; + int filteredValueADC = 0; + for (int i = 0; i < numberOfSamples; i++) { + valueADC += analogRead(pinADC); + } + filteredValueADC = valueADC / numberOfSamples; + return filteredValueADC; } /** * @brief Calcula el valor en voltaje de la lectura ADC del MCU - * - * @param digitalValue - * @return float + * + * @param digitalValue + * @return float */ -float BslibEnergyMeter::SoftwareDAC (int digitalValue) -{ - float convertValueDCA = float(digitalValue) / ADC_SCALE * analogReference; - return convertValueDCA; +float BslibEnergyMeter::SoftwareDAC(int digitalValue) { + float convertValueDCA = float(digitalValue) / ADC_SCALE * analogReference; + return convertValueDCA; } /** * @brief Calibrar automaticamente el sensor de corriente con Vref. Usar esta función cuando la corriente sea cero. - * - * @return int + * + * @return int */ -int BslibEnergyMeter::AutoCalibrationCurrent(int lastVRef) -{ +int BslibEnergyMeter::AutoCalibrationCurrent(int lastVRef) { - float vRef = FilterValueADC(inPinCurrentRef); - float vOut = FilterValueADC(inPinCurrent); + float vRef = FilterValueADC(inPinCurrentRef); + float vOut = FilterValueADC(inPinCurrent); - if (lastVRef == vRef) - { - calibrationCurrent = vRef; - } - else - { - calibrationCurrent = vRef; - } - // algunos sensores tienen un offset (compensacion) cuando Vref Date: Mon, 22 Aug 2022 14:20:06 -0500 Subject: [PATCH 10/10] refactor: optimize data type --- src/BslibEnergyMeter.cpp | 10 +++++----- src/BslibEnergyMeter.h | 18 +++++++++--------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/BslibEnergyMeter.cpp b/src/BslibEnergyMeter.cpp index 55a139d..1476f31 100644 --- a/src/BslibEnergyMeter.cpp +++ b/src/BslibEnergyMeter.cpp @@ -30,7 +30,7 @@ float voltage; * @param _inPinCurrentRef pin analógico Vref del sensor * @param _factorCurrent factor de sensiblidad del sensor */ -void BslibEnergyMeter::SetSensorCurrent(int _inPinCurrent, int _inPinCurrentRef, float _factorCurrent) { +void BslibEnergyMeter::SetSensorCurrent(unsigned int _inPinCurrent, unsigned int _inPinCurrentRef, float _factorCurrent) { inPinCurrent = _inPinCurrent; inPinCurrentRef = _inPinCurrentRef; factorCurrent = _factorCurrent; @@ -43,7 +43,7 @@ void BslibEnergyMeter::SetSensorCurrent(int _inPinCurrent, int _inPinCurrentRef, * @param _factorVoltage factor de sensiblidad del sensor * @param _offsetVoltage compensacion para obtener voltaje real */ -void BslibEnergyMeter::SetSensorVoltage(int _inPinVoltage, float _factorVoltage, float _offsetVoltage = 0) { +void BslibEnergyMeter::SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage, float _offsetVoltage = 0) { inPinVoltage = _inPinVoltage; factorVoltage = _factorVoltage; offsetVoltage = _offsetVoltage; @@ -55,7 +55,7 @@ void BslibEnergyMeter::SetSensorVoltage(int _inPinVoltage, float _factorVoltage, * @param _inPinVoltage pin analógico de sensor de voltaje * @param _factorVoltage factor de sensiblidad del sensor */ -void BslibEnergyMeter::SetSensorVoltage(int _inPinVoltage, float _factorVoltage) { +void BslibEnergyMeter::SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage) { inPinVoltage = _inPinVoltage; factorVoltage = _factorVoltage; } @@ -78,7 +78,7 @@ void BslibEnergyMeter::SetAnalogReference(float _analogReference) { analogReference = _analogReference; } -void BslibEnergyMeter::SetFilterSamples(int _numberOfSamples) { +void BslibEnergyMeter::SetFilterSamples(unsigned int _numberOfSamples) { numberOfSamples = _numberOfSamples; } @@ -88,7 +88,7 @@ void BslibEnergyMeter::SetFilterSamples(int _numberOfSamples) { * @param pinADC pin analógico al cual leer * @return int */ -int BslibEnergyMeter::FilterValueADC(int pinADC) { +int BslibEnergyMeter::FilterValueADC(unsigned int pinADC) { unsigned long valueADC = 0; int filteredValueADC = 0; for (int i = 0; i < numberOfSamples; i++) { diff --git a/src/BslibEnergyMeter.h b/src/BslibEnergyMeter.h index 22c7c9b..c5c06e9 100644 --- a/src/BslibEnergyMeter.h +++ b/src/BslibEnergyMeter.h @@ -23,15 +23,15 @@ class BslibEnergyMeter { public: - void SetSensorCurrent(int _inPinCurrent, int _inPinCurrentRef, float _factorCurrent); - void SetSensorVoltage(int _inPinVoltage, float _factorVoltage, float _offsetVoltage); - void SetSensorVoltage(int _inPinVoltage, float _factorVoltage); + void SetSensorCurrent(unsigned int _inPinCurrent, unsigned int _inPinCurrentRef, float _factorCurrent); + void SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage, float _offsetVoltage); + void SetSensorVoltage(unsigned int _inPinVoltage, float _factorVoltage); void SetCurrentReference(int _currentReference); void SetAnalogReference(float _analogReference); - void SetFilterSamples(int _numberOfSamples); + void SetFilterSamples(unsigned int _numberOfSamples); - int FilterValueADC(int pinADC); + int FilterValueADC(unsigned int pinADC); float SoftwareDAC(int digitalValue); int AutoCalibrationCurrent(int lastVRef); @@ -54,11 +54,11 @@ class BslibEnergyMeter { float ReadVcc(); private: - int inPinCurrent; - int inPinCurrentRef; - int inPinVoltage; + unsigned int inPinCurrent; + unsigned int inPinCurrentRef; + unsigned int inPinVoltage; - int numberOfSamples; + unsigned int numberOfSamples; float analogReference; float factorCurrent; float factorVoltage;