Browse Source

Enable/disable magnitudes for Emon sensors

fastled
Xose Pérez 7 years ago
parent
commit
1b3927615e
5 changed files with 102 additions and 46 deletions
  1. +3
    -0
      code/espurna/config/sensors.h
  2. +28
    -14
      code/espurna/sensors/EmonADC121Sensor.h
  3. +32
    -18
      code/espurna/sensors/EmonADS1X15Sensor.h
  4. +28
    -14
      code/espurna/sensors/EmonAnalogSensor.h
  5. +11
    -0
      code/espurna/sensors/EmonSensor.h

+ 3
- 0
code/espurna/config/sensors.h View File

@ -146,6 +146,9 @@
#define EMON_MAX_TIME 250 // Max time in ms to sample
#define EMON_FILTER_SPEED 512 // Mobile average filter speed
#define EMON_MAINS_VOLTAGE 230 // Mains voltage
#define EMON_REPORT_CURRENT 0 // Calculate current
#define EMON_REPORT_POWER 1 // Calculate power
#define EMON_REPORT_ENERGY 1 // Calculate energy
//--------------------------------------------------------------------------------
// Energy Monitor based on interval analog GPIO


+ 28
- 14
code/espurna/sensors/EmonADC121Sensor.h View File

@ -32,7 +32,7 @@ class EmonADC121Sensor : public EmonSensor {
// Cache
_address = address;
_count = 4;
_count = _magnitudes;
// Init sensor
#if I2C_USE_BRZO
@ -69,10 +69,16 @@ class EmonADC121Sensor : public EmonSensor {
// Type for slot # index
magnitude_t type(unsigned char index) {
_error = SENSOR_ERROR_OK;
if (index == 0) return MAGNITUDE_CURRENT;
if (index == 1) return MAGNITUDE_POWER_APPARENT;
if (index == 2) return MAGNITUDE_ENERGY;
if (index == 3) return MAGNITUDE_ENERGY_DELTA;
unsigned char i = 0;
#if EMON_REPORT_CURRENT
if (index == i++) return MAGNITUDE_CURRENT;
#endif
#if EMON_REPORT_POWER
if (index == i++) return MAGNITUDE_POWER_APPARENT;
#endif
#if EMON_REPORT_ENERGY
if (index == i) return MAGNITUDE_ENERGY;
#endif
_error = SENSOR_ERROR_OUT_OF_RANGE;
return MAGNITUDE_NONE;
}
@ -85,19 +91,25 @@ class EmonADC121Sensor : public EmonSensor {
// Cache the value
static unsigned long last = 0;
static double current = 0;
static unsigned long energy_delta = 0;
if ((last == 0) || (millis() - last > 1000)) {
current = read(0, _pivot);
energy_delta = current * _voltage * (millis() - last) / 1000;
_energy += energy_delta;
#if EMON_REPORT_ENERGY
_energy += (current * _voltage * (millis() - last) / 1000);
#endif
last = millis();
}
if (index == 0) return current;
if (index == 1) return current * _voltage;
if (index == 2) return _energy;
if (index == 3) return energy_delta;
// Report
unsigned char i = 0;
#if EMON_REPORT_CURRENT
if (index == i++) return current;
#endif
#if EMON_REPORT_POWER
if (index == i++) return current * _voltage;
#endif
#if EMON_REPORT_ENERGY
if (index == i) return _energy;
#endif
_error = SENSOR_ERROR_OUT_OF_RANGE;
return 0;
@ -133,7 +145,9 @@ class EmonADC121Sensor : public EmonSensor {
}
unsigned char _address;
unsigned long _energy = 0;
double _pivot = 0;
#if EMON_REPORT_ENERGY
unsigned long _energy = 0;
#endif
};

+ 32
- 18
code/espurna/sensors/EmonADS1X15Sensor.h View File

@ -89,7 +89,6 @@
#define ADS1X15_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default)
#define ADS1X15_CHANNELS 4
#define EMON_ADS1X15_MAGNITUDES_PER_PORT 2
class EmonADS1X15Sensor : public EmonSensor {
@ -106,7 +105,7 @@ class EmonADS1X15Sensor : public EmonSensor {
if (mask & 0x01) ++_ports;
mask = mask >> 1;
}
_count = _ports * EMON_ADS1X15_MAGNITUDES_PER_PORT;
_count = _ports * _magnitudes;
// warmup
warmup();
@ -133,39 +132,53 @@ class EmonADS1X15Sensor : public EmonSensor {
if (index < _count) {
_error = SENSOR_ERROR_OK;
unsigned char magnitude = index / _ports;
if (magnitude == 0) return MAGNITUDE_CURRENT;
if (magnitude == 1) return MAGNITUDE_POWER_APPARENT;
//if (magnitude == 2) return MAGNITUDE_ENERGY;
//if (magnitude == 3) return MAGNITUDE_ENERGY_DELTA;
unsigned char i=0;
#if EMON_REPORT_CURRENT
if (magnitude == i++) return MAGNITUDE_CURRENT;
#endif
#if EMON_REPORT_POWER
if (magnitude == i++) return MAGNITUDE_POWER_APPARENT;
#endif
#if EMON_REPORT_ENERGY
if (magnitude == i) return MAGNITUDE_ENERGY;
#endif
}
_error = SENSOR_ERROR_OUT_OF_RANGE;
return MAGNITUDE_NONE;
}
void pre() {
//static unsigned long last = 0;
static unsigned long last = 0;
for (unsigned char port=0; port<_ports; port++) {
unsigned char channel = getChannel(port);
_current[port] = getCurrent(channel);
//if (last > 0) {
// _delta[port] = _current[port] * _voltage * (millis() - last) / 1000;
//}
//_energy[port] += _delta[port];
#if EMON_REPORT_ENERGY
_energy[port] += (_current[port] * _voltage * (millis() - last) / 1000);
#endif
}
//last = millis();
last = millis();
}
// Current value for slot # index
double value(unsigned char index) {
if (index < _count) {
_error = SENSOR_ERROR_OK;
unsigned char port = index % _ports;
unsigned char magnitude = index / _ports;
if (magnitude == 0) return _current[port];
if (magnitude == 1) return _current[port] * _voltage;
//if (magnitude == 2) return _energy[port];
//if (magnitude == 3) return _delta[port];
unsigned char i=0;
#if EMON_REPORT_CURRENT
if (magnitude == i++) return _current[port];
#endif
#if EMON_REPORT_POWER
if (magnitude == i++) return _current[port] * _voltage;
#endif
#if EMON_REPORT_ENERGY
if (magnitude == i) return _energy[port];
#endif
}
_error = SENSOR_ERROR_OUT_OF_RANGE;
@ -297,8 +310,9 @@ class EmonADS1X15Sensor : public EmonSensor {
unsigned char _ports;
double _pivot[ADS1X15_CHANNELS] = {0};
double _current[ADS1X15_CHANNELS] = {0};
//unsigned long _energy[ADS1X15_CHANNELS] = {0};
//unsigned long _delta[ADS1X15_CHANNELS] = {0};
#if EMON_REPORT_ENERGY
unsigned long _energy[ADS1X15_CHANNELS] = {0};
#endif
};

+ 28
- 14
code/espurna/sensors/EmonAnalogSensor.h View File

@ -16,7 +16,7 @@ class EmonAnalogSensor : public EmonSensor {
// Cache
_gpio = gpio;
_count = 4;
_count = _magnitudes;
// Prepare GPIO
pinMode(gpio, INPUT);
@ -41,10 +41,16 @@ class EmonAnalogSensor : public EmonSensor {
// Type for slot # index
magnitude_t type(unsigned char index) {
_error = SENSOR_ERROR_OK;
if (index == 0) return MAGNITUDE_CURRENT;
if (index == 1) return MAGNITUDE_POWER_APPARENT;
if (index == 2) return MAGNITUDE_ENERGY;
if (index == 3) return MAGNITUDE_ENERGY_DELTA;
unsigned char i=0;
#if EMON_REPORT_CURRENT
if (index == i++) return MAGNITUDE_CURRENT;
#endif
#if EMON_REPORT_POWER
if (index == i++) return MAGNITUDE_POWER_APPARENT;
#endif
#if EMON_REPORT_ENERGY
if (index == i) return MAGNITUDE_ENERGY;
#endif
_error = SENSOR_ERROR_OUT_OF_RANGE;
return MAGNITUDE_NONE;
}
@ -57,19 +63,25 @@ class EmonAnalogSensor : public EmonSensor {
// Cache the value
static unsigned long last = 0;
static double current = 0;
static unsigned long energy_delta = 0;
if ((last == 0) || (millis() - last > 1000)) {
current = read(0, _pivot);
energy_delta = current * _voltage * (millis() - last) / 1000;
_energy += energy_delta;
#if EMON_REPORT_ENERGY
_energy += (current * _voltage * (millis() - last) / 1000);
#endif
last = millis();
}
if (index == 0) return current;
if (index == 1) return current * _voltage;
if (index == 2) return _energy;
if (index == 3) return energy_delta;
// Report
unsigned char i=0;
#if EMON_REPORT_CURRENT
if (index == i++) return current;
#endif
#if EMON_REPORT_POWER
if (index == i++) return current * _voltage;
#endif
#if EMON_REPORT_ENERGY
if (index == i) return _energy;
#endif
_error = SENSOR_ERROR_OUT_OF_RANGE;
return 0;
@ -83,8 +95,10 @@ class EmonAnalogSensor : public EmonSensor {
}
unsigned char _gpio;
unsigned long _energy = 0;
double _pivot = 0;
#if EMON_REPORT_ENERGY
unsigned long _energy = 0;
#endif
};

+ 11
- 0
code/espurna/sensors/EmonSensor.h View File

@ -19,6 +19,16 @@ class EmonSensor : public BaseSensor {
_voltage = voltage;
_adc_counts = 1 << bits;
#if EMON_REPORT_CURRENT
++_magnitudes;
#endif
#if EMON_REPORT_POWER
++_magnitudes;
#endif
#if EMON_REPORT_ENERGY
++_magnitudes;
#endif
// Calculate factor
_current_factor = ratio * ref / _adc_counts;
@ -110,6 +120,7 @@ class EmonSensor : public BaseSensor {
}
double _voltage;
unsigned char _magnitudes = 0;
unsigned long _adc_counts;
unsigned int _multiplier = 1;
double _current_factor;


Loading…
Cancel
Save