-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/** | ||
* @file EnergyMeter.cpp | ||
* @author JP Merchan ([email protected]) | ||
* @brief Biblioteca medidor de energia, permite medir voltaje, y corriente con gran precisión. | ||
* @version 2.1 | ||
* @date 2020-12-19 | ||
* | ||
* @copyright DataAnalitic (c) {2020} | ||
* | ||
*/ | ||
|
||
#include "EnergyMeter.h" | ||
|
||
/** | ||
* @brief Configuracion de los pines del sensor de corriente | ||
* | ||
* @param _inPinCurrent pin analógico Vout del sensor | ||
* @param _inPinCurrentRef pin analógico Vref del sensor | ||
* @param _factorCurrent factor de sensiblidad del sensor | ||
*/ | ||
void EnergyMeter::SensorCurrent(unsigned int _inPinCurrent, unsigned int _inPinCurrentRef, float _factorCurrent) | ||
{ | ||
inPinCurrent = _inPinCurrent; | ||
inPinCurrentRef = _inPinCurrentRef; | ||
factorCurrent = _factorCurrent; | ||
} | ||
|
||
/** | ||
* @brief Configuracion de los pines del sensor de bateria | ||
* | ||
* @param _inPinBatt pin analógico de sensor de bateria | ||
* @param _factorBatt factor de sensiblidad del sensor | ||
* @param _offsetBatt compensacion para obtener voltaje real | ||
*/ | ||
void EnergyMeter::SensorBattery(unsigned int _inPinBatt, float _factorBatt, float _offsetBatt) | ||
{ | ||
inPinBatt = _inPinBatt; | ||
factorBatt = _factorBatt; | ||
offsetBatt = _offsetBatt; | ||
} | ||
|
||
/** | ||
* @brief Configuracion de los pines del seguidor de voltaje | ||
* | ||
* @param _inPinVolt pin analógico del sensor | ||
* @param _factorVolt factor de sensiblidad del sensor | ||
*/ | ||
void EnergyMeter::SensorVoltage(unsigned int _inPinVolt, float _factorVolt) | ||
{ | ||
inPinVolt = _inPinVolt; | ||
factorVolt = _factorVolt; | ||
} | ||
|
||
/** | ||
* @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 EnergyMeter::FilterValueADC(unsigned int pinADC, unsigned int samples) | ||
{ | ||
unsigned long valueADC = 0; | ||
unsigned int filteredValueADC = 0; | ||
for (unsigned int i = 0; i < samples; i++) | ||
{ | ||
valueADC += analogRead(pinADC); | ||
} | ||
filteredValueADC = valueADC / samples; | ||
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 EnergyMeter::AutoCalibrationCurrent(unsigned int _numberOfSamples) | ||
{ | ||
calibrationCurrent = FilterValueADC(inPinCurrentRef, _numberOfSamples); | ||
// algunos sensores tienen un offset (compensacion) cuando Vref<Vout | ||
float offset = FilterValueADC(inPinCurrent, _numberOfSamples) - calibrationCurrent; | ||
calibrationCurrent += offset; | ||
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 EnergyMeter::SetCurrentReference(unsigned int _currentReference) | ||
{ | ||
currentReference = _currentReference; | ||
} | ||
|
||
/** | ||
* @brief Obtiene la corriente del sensor motor | ||
* | ||
* @param _numberOfSamples número de muestras a tomar en cada lenctura | ||
* @return float | ||
*/ | ||
float EnergyMeter::GetCurrent(unsigned int _numberOfSamples) | ||
{ | ||
int filteredCurrent = FilterValueADC(inPinCurrent, _numberOfSamples) - currentReference; | ||
|
||
if (filteredCurrent < 0) | ||
{ | ||
filteredCurrent = 0; | ||
} | ||
|
||
float convertValueADC = float(filteredCurrent) / ADC_SCALE * VOLT_INPUT_DRIVER; | ||
float current = convertValueADC / factorCurrent; | ||
return current; | ||
} | ||
|
||
/** | ||
* @brief Obtiene el voltaje de la bateria | ||
* | ||
* @param _numberOfSamples número de muestras a tomar en cada lenctura | ||
* @return float | ||
*/ | ||
float EnergyMeter::GetBattery(unsigned int _numberOfSamples) | ||
{ | ||
int filteredBatt = FilterValueADC(inPinBatt, _numberOfSamples); | ||
float convertValueADC = float(filteredBatt) / ADC_SCALE * VOLT_INPUT_MAIN; | ||
float batt = (convertValueADC * factorBatt) + offsetBatt; | ||
return batt; | ||
} | ||
|
||
/** | ||
* @brief Obtiene el voltaje del seguidor de corriente | ||
* | ||
* @param _numberOfSamples número de muestras a tomar en cada lenctura | ||
* @return float | ||
*/ | ||
float EnergyMeter::GetVoltage(unsigned int _numberOfSamples) | ||
{ | ||
int filteredVolt = FilterValueADC(inPinVolt, _numberOfSamples); | ||
float convertValueADC = float(filteredVolt) / ADC_SCALE * VOLT_INPUT_DRIVER; | ||
float volt = convertValueADC * factorVolt; | ||
return volt; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @file EnergyMeter.h | ||
* @author JP Merchan ([email protected]) | ||
* @brief Biblioteca medidor de energia, permite medir voltaje, y corriente con gran precisión. | ||
* @version 2.1 | ||
* @date 2020-12-19 | ||
* | ||
* @copyright DataAnalitic (c) {2020} | ||
* | ||
*/ | ||
|
||
#ifndef EnergyMeter_h | ||
#define EnergyMeter_h | ||
#include "Arduino.h" | ||
|
||
#define ADC_SCALE 1023.0 | ||
#define VOLT_INPUT_MAIN 5.0 | ||
#define VOLT_INPUT_DRIVER 3.3 | ||
|
||
class EnergyMeter | ||
{ | ||
public: | ||
void SensorCurrent(unsigned int _inPinCurrent, unsigned int _inPinCurrentRef, float _factorCurrent); | ||
void SensorBattery(unsigned int _inPinBatt, float _factorBatt, float _offsetBatt); | ||
void SensorVoltage(unsigned int _inPinVolt, float _factorVolt); | ||
|
||
unsigned int FilterValueADC(unsigned int pinADC, unsigned int samples); | ||
unsigned int AutoCalibrationCurrent(unsigned int _numberOfSamples); | ||
void SetCurrentReference(unsigned int _currentReference); | ||
|
||
float GetCurrent(unsigned int _numberOfSamples); | ||
float GetVoltage(unsigned int _numberOfSamples); | ||
float GetBattery(unsigned int _numberOfSamples); | ||
|
||
private: | ||
unsigned int inPinCurrent; | ||
unsigned int inPinCurrentRef; | ||
unsigned int inPinVolt; | ||
unsigned int inPinBatt; | ||
|
||
float factorCurrent; | ||
float factorVolt; | ||
float factorBatt; | ||
|
||
float offsetBatt; | ||
unsigned int currentReference; | ||
unsigned int calibrationCurrent; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name=EnergyMeter | ||
version=2.1 | ||
author=DataAnalitic | ||
maintainer=jhon-p16 <[email protected]> | ||
sentence=Libreria para sensores analógicos de voltaje y corriente. | ||
category=Sensors | ||
url=https://gitlab.com/bluesensor/bluecontrol-firmware/-/tree/development/libraries/EnergyMeter | ||
architectures=* |