Browse Source

Improve hierarchi for EmonSensor

fastled
Xose Pérez 7 years ago
parent
commit
fa74776327
5 changed files with 104 additions and 71 deletions
  1. +12
    -8
      code/espurna/config/sensors.h
  2. +2
    -2
      code/espurna/sensor.ino
  3. +6
    -4
      code/espurna/sensors/BaseSensor.h
  4. +79
    -0
      code/espurna/sensors/EmonAnalogSensor.h
  5. +5
    -57
      code/espurna/sensors/EmonSensor.h

+ 12
- 8
code/espurna/config/sensors.h View File

@ -138,13 +138,22 @@
#define COUNTER_DEBOUNCE 10 // Do not register events within less than 10 millis
#define COUNTER_TOPIC "counter" // Default topic for MQTT, API and InfluxDB
//--------------------------------------------------------------------------------
// Energy Monitor
//--------------------------------------------------------------------------------
#define EMON_FILTER_SPEED 512
#define EMON_MODE_SAMPLES 1
#define EMON_MODE_MSECONDS 2
//--------------------------------------------------------------------------------
// Analog Energy Monitor
// Enable support by passing EMON_ANALOG_SUPPORT=1 build flag
//--------------------------------------------------------------------------------
#ifndef EMON_ANALOG_SUPPORT
#define EMON_ANALOG_SUPPORT 0 // Do not build support by default
#define EMON_ANALOG_SUPPORT 1 // Do not build support by default
#endif
#define EMON_ANALOG_MAINS_VOLTAGE 230 // Mains voltage
@ -152,16 +161,11 @@
#define EMON_ANALOG_ADC_BITS 10 // ADC depth
#define EMON_ANALOG_REFERENCE_VOLTAGE 3.3 // Reference voltage of the ADC
#define EMON_ANALOG_FILTER_SPEED 512
#define EMON_ANALOG_MODE_SAMPLES 1
#define EMON_ANALOG_MODE_MSECONDS 2
#define EMON_ANALOG_READ_VALUE 1000
#define EMON_ANALOG_READ_MODE EMON_ANALOG_MODE_SAMPLES
#define EMON_ANALOG_READ_MODE EMON_MODE_SAMPLES
#define EMON_ANALOG_WARMUP_VALUE 1000
#define EMON_ANALOG_WARMUP_MODE EMON_ANALOG_MODE_MSECONDS
#define EMON_ANALOG_WARMUP_MODE EMON_MODE_MSECONDS
#if EMON_ANALOG_SUPPORT


+ 2
- 2
code/espurna/sensor.ino View File

@ -239,8 +239,8 @@ void sensorInit() {
#endif
#if EMON_ANALOG_SUPPORT
#include "sensors/AnalogEmonSensor.h"
sensorRegister(new AnalogEmonSensor(A0, EMON_ANALOG_MAINS_VOLTAGE, EMON_ANALOG_ADC_BITS, EMON_ANALOG_REFERENCE_VOLTAGE, EMON_ANALOG_CURRENT_RATIO));
#include "sensors/EmonAnalogSensor.h"
sensorRegister((BaseSensor *) new EmonAnalogSensor(A0, EMON_ANALOG_MAINS_VOLTAGE, EMON_ANALOG_ADC_BITS, EMON_ANALOG_REFERENCE_VOLTAGE, EMON_ANALOG_CURRENT_RATIO));
#endif


+ 6
- 4
code/espurna/sensors/BaseSensor.h View File

@ -4,6 +4,8 @@
#pragma once
#include <Arduino.h>
typedef enum magnitude_t {
MAGNITUDE_NONE = 0,
@ -54,16 +56,16 @@ class BaseSensor {
virtual void post() {}
// Descriptive name of the sensor
virtual String name();
virtual String name() {}
// Descriptive name of the slot # index
virtual String slot(unsigned char index);
virtual String slot(unsigned char index) {}
// Type for slot # index
virtual magnitude_t type(unsigned char index);
virtual magnitude_t type(unsigned char index) {}
// Current value for slot # index
virtual double value(unsigned char index);
virtual double value(unsigned char index) {}
// Return sensor status (true for ready)
bool status() { return _error == 0; }


+ 79
- 0
code/espurna/sensors/EmonAnalogSensor.h View File

@ -0,0 +1,79 @@
// -----------------------------------------------------------------------------
// Eergy monitor sensor
// -----------------------------------------------------------------------------
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
#include "EmonSensor.h"
class EmonAnalogSensor : public EmonSensor {
public:
EmonAnalogSensor(unsigned char gpio, double voltage, unsigned char bits, double ref, double ratio): EmonSensor(voltage, bits, ref, ratio) {
// Cache
_gpio = gpio;
// Prepare GPIO
pinMode(gpio, INPUT);
// warmup
read(EMON_ANALOG_WARMUP_VALUE, EMON_ANALOG_WARMUP_MODE, _gpio);
}
// Descriptive name of the sensor
String name() {
char buffer[20];
snprintf(buffer, sizeof(buffer), "ANALOG EMON @ GPIO%d", _gpio);
return String(buffer);
}
// Descriptive name of the slot # index
String slot(unsigned char index) {
return name();
}
// 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;
_error = SENSOR_ERROR_OUT_OF_RANGE;
return MAGNITUDE_NONE;
}
// Current value for slot # index
double value(unsigned char index) {
_error = SENSOR_ERROR_OK;
// Cache the value
static unsigned long last = 0;
static double current = 0;
if ((last == 0) || (millis() - last > 1000)) {
current = read(EMON_ANALOG_READ_VALUE, EMON_ANALOG_READ_MODE, _gpio);
last = millis();
}
if (index == 0) return current;
if (index == 1) return current * _voltage;
_error = SENSOR_ERROR_OUT_OF_RANGE;
return 0;
}
protected:
unsigned int readADC(unsigned char port) {
return analogRead(port);
}
unsigned char _gpio;
};

code/espurna/sensors/AnalogEmonSensor.h → code/espurna/sensors/EmonSensor.h View File

@ -7,17 +7,13 @@
#include "Arduino.h"
#include "BaseSensor.h"
class AnalogEmonSensor : public BaseSensor {
class EmonSensor : public BaseSensor {
public:
AnalogEmonSensor(unsigned char gpio, double voltage, unsigned char bits, double ref, double ratio): BaseSensor() {
// Prepare GPIO
pinMode(gpio, INPUT);
EmonSensor(double voltage, unsigned char bits, double ref, double ratio): BaseSensor() {
// Cache
_gpio = gpio;
_voltage = voltage;
_adc_counts = 1 << bits;
_pivot = _adc_counts >> 1;
@ -29,58 +25,11 @@ class AnalogEmonSensor : public BaseSensor {
// Calculate multiplier
calculateMultiplier();
// warmup
read(EMON_ANALOG_WARMUP_VALUE, EMON_ANALOG_WARMUP_MODE, _gpio);
}
// Descriptive name of the sensor
String name() {
char buffer[20];
snprintf(buffer, sizeof(buffer), "ANALOG EMON @ GPIO%d", _gpio);
return String(buffer);
}
// Descriptive name of the slot # index
String slot(unsigned char index) {
return name();
}
// 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;
_error = SENSOR_ERROR_OUT_OF_RANGE;
return MAGNITUDE_NONE;
}
// Current value for slot # index
double value(unsigned char index) {
_error = SENSOR_ERROR_OK;
// Cache the value
static unsigned long last = 0;
static double current = 0;
if ((last == 0) || (millis() - last > 1000)) {
current = read(EMON_ANALOG_READ_VALUE, EMON_ANALOG_READ_MODE, _gpio);
last = millis();
}
if (index == 0) return current;
if (index == 1) return current * _voltage;
_error = SENSOR_ERROR_OUT_OF_RANGE;
return 0;
}
protected:
unsigned int readADC(unsigned char port) {
return analogRead(port);
}
virtual unsigned int readADC(unsigned char port) {}
void calculateMultiplier() {
unsigned int s = 1;
@ -113,7 +62,7 @@ class AnalogEmonSensor : public BaseSensor {
if (sample < min) min = sample;
// Digital low pass filter extracts the VDC offset
_pivot = (_pivot + (sample - _pivot) / EMON_ANALOG_FILTER_SPEED);
_pivot = (_pivot + (sample - _pivot) / EMON_FILTER_SPEED);
filtered = sample - _pivot;
// Root-mean-square method
@ -121,7 +70,7 @@ class AnalogEmonSensor : public BaseSensor {
++samples;
// Exit condition
if (mode == EMON_ANALOG_MODE_SAMPLES) {
if (mode == EMON_MODE_SAMPLES) {
if (samples >= value) break;
} else {
if (millis() - start >= value) break;
@ -146,7 +95,6 @@ class AnalogEmonSensor : public BaseSensor {
}
double _voltage;
unsigned char _gpio;
unsigned int _adc_counts;
unsigned int _multiplier = 1;
double _current_factor;

Loading…
Cancel
Save