Housekeeping - Documentation

This commit is contained in:
Theo Arends 2019-01-06 15:17:22 +01:00
parent cb8963a7bf
commit e9c687cf09

View File

@ -37,35 +37,36 @@
#define HJL_UREF 822 #define HJL_UREF 822
#define HJL_IREF 3300 #define HJL_IREF 3300
#define HLW_POWER_PROBE_TIME 10 // Number of seconds to probe for power before deciding none used #define HLW_POWER_PROBE_TIME 10 // Number of seconds to probe for power before deciding none used (low power pulse can take up to 10 seconds)
#define HLW_PROBE_COUNT 10 // Max number of probes per cycle #define HLW_SAMPLE_COUNT 10 // Max number of samples per cycle
byte hlw_select_ui_flag; //#define HLW_DEBUG
byte hlw_ui_flag = 1; #ifdef HLW_DEBUG
byte hlw_model_type = 0; unsigned long hlw_debug[HLW_SAMPLE_COUNT];
byte hlw_load_off; #endif
byte hlw_cf1_timer;
unsigned long hlw_cf_pulse_length;
unsigned long hlw_cf_pulse_last_time;
unsigned long hlw_cf_power_pulse_length;
unsigned long hlw_cf1_pulse_length; unsigned long hlw_cf_pulse_length = 0;
unsigned long hlw_cf1_pulse_last_time; unsigned long hlw_cf_pulse_last_time = 0;
unsigned long hlw_cf1_summed_pulse_length; unsigned long hlw_cf_power_pulse_length = 0;
unsigned long hlw_cf1_pulse_counter;
unsigned long hlw_cf1_voltage_pulse_length;
unsigned long hlw_cf1_current_pulse_length;
unsigned long hlw_energy_period_counter; unsigned long hlw_cf1_pulse_length = 0;
unsigned long hlw_cf1_pulse_last_time = 0;
unsigned long hlw_cf1_summed_pulse_length = 0;
unsigned long hlw_cf1_pulse_counter = 0;
unsigned long hlw_cf1_voltage_pulse_length = 0;
unsigned long hlw_cf1_current_pulse_length = 0;
unsigned long hlw_energy_period_counter = 0;
unsigned long hlw_power_ratio = 0; unsigned long hlw_power_ratio = 0;
unsigned long hlw_voltage_ratio = 0; unsigned long hlw_voltage_ratio = 0;
unsigned long hlw_current_ratio = 0; unsigned long hlw_current_ratio = 0;
unsigned long hlw_cf1_voltage_max_pulse_counter; byte hlw_select_ui_flag = 0;
unsigned long hlw_cf1_current_max_pulse_counter; byte hlw_ui_flag = 1;
byte hlw_model_type = 0;
//unsigned long hlw_debug[HLW_PROBE_COUNT]; byte hlw_load_off = 1;
byte hlw_cf1_timer = 0;
#ifndef USE_WS2812_DMA // Collides with Neopixelbus but solves exception #ifndef USE_WS2812_DMA // Collides with Neopixelbus but solves exception
void HlwCfInterrupt(void) ICACHE_RAM_ATTR; void HlwCfInterrupt(void) ICACHE_RAM_ATTR;
@ -94,12 +95,12 @@ void HlwCf1Interrupt(void) // Service Voltage and Current
hlw_cf1_pulse_last_time = us; hlw_cf1_pulse_last_time = us;
if ((hlw_cf1_timer > 2) && (hlw_cf1_timer < 8)) { // Allow for 300 mSec set-up time and measure for up to 1 second if ((hlw_cf1_timer > 2) && (hlw_cf1_timer < 8)) { // Allow for 300 mSec set-up time and measure for up to 1 second
hlw_cf1_summed_pulse_length += hlw_cf1_pulse_length; hlw_cf1_summed_pulse_length += hlw_cf1_pulse_length;
#ifdef HLW_DEBUG
// hlw_debug[hlw_cf1_pulse_counter] = hlw_cf1_pulse_length; hlw_debug[hlw_cf1_pulse_counter] = hlw_cf1_pulse_length;
#endif
hlw_cf1_pulse_counter++; hlw_cf1_pulse_counter++;
if (HLW_PROBE_COUNT == hlw_cf1_pulse_counter) { if (HLW_SAMPLE_COUNT == hlw_cf1_pulse_counter) {
hlw_cf1_timer = 8; // We need up to HLW_PROBE_COUNT samples within 1 second (low current could take up to 0.3 second) hlw_cf1_timer = 8; // We need up to HLW_SAMPLE_COUNT samples within 1 second (low current could take up to 0.3 second)
} }
} }
} }
@ -113,7 +114,6 @@ void HlwEvery200ms(void)
unsigned long hlw_u = 0; unsigned long hlw_u = 0;
unsigned long hlw_i = 0; unsigned long hlw_i = 0;
if (micros() - hlw_cf_pulse_last_time > (HLW_POWER_PROBE_TIME * 1000000)) { if (micros() - hlw_cf_pulse_last_time > (HLW_POWER_PROBE_TIME * 1000000)) {
hlw_cf_pulse_length = 0; // No load for some time hlw_cf_pulse_length = 0; // No load for some time
hlw_load_off = 1; hlw_load_off = 1;
@ -121,7 +121,7 @@ void HlwEvery200ms(void)
hlw_cf_power_pulse_length = hlw_cf_pulse_length; hlw_cf_power_pulse_length = hlw_cf_pulse_length;
if (hlw_cf_power_pulse_length && energy_power_on && !hlw_load_off) { if (hlw_cf_power_pulse_length && energy_power_on && !hlw_load_off) {
hlw_w = (hlw_power_ratio * Settings.energy_power_calibration) / hlw_cf_power_pulse_length; hlw_w = (hlw_power_ratio * Settings.energy_power_calibration) / hlw_cf_power_pulse_length; // W *10
energy_active_power = (float)hlw_w / 10; energy_active_power = (float)hlw_w / 10;
} else { } else {
energy_active_power = 0; energy_active_power = 0;
@ -137,32 +137,31 @@ void HlwEvery200ms(void)
cf1_pulse_length = hlw_cf1_summed_pulse_length / hlw_cf1_pulse_counter; cf1_pulse_length = hlw_cf1_summed_pulse_length / hlw_cf1_pulse_counter;
} }
/* #ifdef HLW_DEBUG
// Debugging for calculating mean and median // Debugging for calculating mean and median
char stemp[100]; char stemp[100];
stemp[0] = '\0'; stemp[0] = '\0';
for (uint8_t i = 0; i < hlw_cf1_pulse_counter; i++) { for (uint8_t i = 0; i < hlw_cf1_pulse_counter; i++) {
snprintf_P(stemp, sizeof(stemp), PSTR("%s %d"), stemp, hlw_debug[i]); snprintf_P(stemp, sizeof(stemp), PSTR("%s %d"), stemp, hlw_debug[i]);
} }
for (uint8_t i = 0; i < hlw_cf1_pulse_counter; i++) { for (uint8_t i = 0; i < hlw_cf1_pulse_counter; i++) {
for (uint8_t j = i + 1; j < hlw_cf1_pulse_counter; j++) { for (uint8_t j = i + 1; j < hlw_cf1_pulse_counter; j++) {
if (hlw_debug[i] > hlw_debug[j]) { // Sort ascending if (hlw_debug[i] > hlw_debug[j]) { // Sort ascending
std::swap(hlw_debug[i], hlw_debug[j]); std::swap(hlw_debug[i], hlw_debug[j]);
}
} }
} }
} unsigned long median = hlw_debug[(hlw_cf1_pulse_counter +1) / 2];
unsigned long median = hlw_debug[(hlw_cf1_pulse_counter +1) / 2]; snprintf_P(log_data, sizeof(log_data), PSTR("NRG: power %d, ui %d, cnt %d, smpl%s, sum %d, mean %d, median %d"),
snprintf_P(log_data, sizeof(log_data), PSTR("NRG: power %d, ui %d, count %d, samples%s, sum %d, mean %d, median %d"), hlw_cf_power_pulse_length, hlw_select_ui_flag, hlw_cf1_pulse_counter, stemp, hlw_cf1_summed_pulse_length, cf1_pulse_length, median);
hlw_cf_power_pulse_length, hlw_select_ui_flag, hlw_cf1_pulse_counter, stemp, hlw_cf1_summed_pulse_length, cf1_pulse_length, median); AddLog(LOG_LEVEL_DEBUG);
AddLog(LOG_LEVEL_DEBUG); #endif
*/
if (hlw_select_ui_flag == hlw_ui_flag) { if (hlw_select_ui_flag == hlw_ui_flag) {
hlw_cf1_voltage_pulse_length = cf1_pulse_length; hlw_cf1_voltage_pulse_length = cf1_pulse_length;
hlw_cf1_voltage_max_pulse_counter = hlw_cf1_pulse_counter;
if (hlw_cf1_voltage_pulse_length && energy_power_on) { // If powered on always provide voltage if (hlw_cf1_voltage_pulse_length && energy_power_on) { // If powered on always provide voltage
hlw_u = (hlw_voltage_ratio * Settings.energy_voltage_calibration) / hlw_cf1_voltage_pulse_length; hlw_u = (hlw_voltage_ratio * Settings.energy_voltage_calibration) / hlw_cf1_voltage_pulse_length; // V *10
energy_voltage = (float)hlw_u / 10; energy_voltage = (float)hlw_u / 10;
} else { } else {
energy_voltage = 0; energy_voltage = 0;
@ -170,10 +169,9 @@ void HlwEvery200ms(void)
} else { } else {
hlw_cf1_current_pulse_length = cf1_pulse_length; hlw_cf1_current_pulse_length = cf1_pulse_length;
hlw_cf1_current_max_pulse_counter = hlw_cf1_pulse_counter;
if (hlw_cf1_current_pulse_length && energy_active_power) { // No current if no power being consumed if (hlw_cf1_current_pulse_length && energy_active_power) { // No current if no power being consumed
hlw_i = (hlw_current_ratio * Settings.energy_current_calibration) / hlw_cf1_current_pulse_length; hlw_i = (hlw_current_ratio * Settings.energy_current_calibration) / hlw_cf1_current_pulse_length; // mA
energy_current = (float)hlw_i / 1000; energy_current = (float)hlw_i / 1000;
} else { } else {
energy_current = 0; energy_current = 0;
@ -217,29 +215,12 @@ void HlwSnsInit(void)
hlw_current_ratio = HLW_IREF; hlw_current_ratio = HLW_IREF;
} }
hlw_cf_pulse_length = 0;
hlw_cf_pulse_last_time = 0;
hlw_cf_power_pulse_length = 0;
hlw_cf1_pulse_length = 0;
hlw_cf1_pulse_last_time = 0;
hlw_cf1_voltage_pulse_length = 0;
hlw_cf1_current_pulse_length = 0;
hlw_cf1_voltage_max_pulse_counter = 0;
hlw_cf1_current_max_pulse_counter = 0;
hlw_load_off = 1;
hlw_energy_period_counter = 0;
hlw_select_ui_flag = 0; // Voltage;
pinMode(pin[GPIO_NRG_SEL], OUTPUT); pinMode(pin[GPIO_NRG_SEL], OUTPUT);
digitalWrite(pin[GPIO_NRG_SEL], hlw_select_ui_flag); digitalWrite(pin[GPIO_NRG_SEL], hlw_select_ui_flag);
pinMode(pin[GPIO_NRG_CF1], INPUT_PULLUP); pinMode(pin[GPIO_NRG_CF1], INPUT_PULLUP);
attachInterrupt(pin[GPIO_NRG_CF1], HlwCf1Interrupt, FALLING); attachInterrupt(pin[GPIO_NRG_CF1], HlwCf1Interrupt, FALLING);
pinMode(pin[GPIO_HLW_CF], INPUT_PULLUP); pinMode(pin[GPIO_HLW_CF], INPUT_PULLUP);
attachInterrupt(pin[GPIO_HLW_CF], HlwCfInterrupt, FALLING); attachInterrupt(pin[GPIO_HLW_CF], HlwCfInterrupt, FALLING);
hlw_cf1_timer = 0;
} }
void HlwDrvInit(void) void HlwDrvInit(void)