Skip to content

Commit

Permalink
FIX: More input filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-valkov committed Jun 1, 2016
1 parent 140c1ba commit a6cac2e
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 93 deletions.
Binary file modified IoT Firmware/bin/upgrade/user1.2048.new.3.bin
Binary file not shown.
Binary file modified IoT Firmware/bin/upgrade/user2.2048.new.3.bin
Binary file not shown.
8 changes: 4 additions & 4 deletions IoT Firmware/olimex/include/user_battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

#include "user_webserver.h"

#define BATTERY_DEBUG 0
#define BATTERY_DEBUG 0

#define BATTERY_URL "/battery"
#define BATTERY_STATE_REFRESH 1000
#define BATTERY_URL "/battery"
#define BATTERY_STATE_REFRESH 1000

#if DEVICE == BADGE
#define BATTERY_MIN_ADC 500
Expand All @@ -20,7 +20,7 @@
#endif

#define BATTERY_FILTER_COUNT 10
#define BATTERY_FILTER_SHIFT 4
#define BATTERY_FILTER_FACTOR 16

void user_battery_init();
uint8 battery_percent_get();
Expand Down
13 changes: 13 additions & 0 deletions IoT Firmware/olimex/user/modules/mod_emtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@
_uint64_ counter_apparent;
} emtr_sys_params;

typedef struct {
float current_rms;
float voltage_rms;
float active_power;
float reactive_power;
float apparent_power;
float power_factor;
float line_frequency;
float thermistor_voltage;
uint16 event_flag;
uint16 system_status;
} emtr_low_pass_output;

typedef struct {
uint32 current_rms;
uint16 voltage_rms;
Expand Down
50 changes: 24 additions & 26 deletions IoT Firmware/olimex/user/user_battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,29 @@
LOCAL uint8 battery_state = 0;
LOCAL uint8 battery_percent = 0;

LOCAL uint16 ICACHE_FLASH_ATTR battery_adc_filter() {
LOCAL uint32 adc = 0;
LOCAL bool first = true;

if (first) {
adc = system_adc_read();
first = false;
}

adc = adc - (adc >> BATTERY_FILTER_SHIFT) + (system_adc_read() >> BATTERY_FILTER_SHIFT);

return adc;
}

uint8 ICACHE_FLASH_ATTR battery_percent_get() {
uint16 adc = battery_adc_filter();
uint8 percent;
LOCAL float percent = 0;
LOCAL bool first = true;

float p;
uint16 adc = system_adc_read();

if (adc <= BATTERY_MIN_ADC) {
percent = 0;
p = 0;
} else if (adc >= BATTERY_MAX_ADC) {
percent = 100;
p = 100;
} else {
p = 100 * (adc - BATTERY_MIN_ADC) / (BATTERY_MAX_ADC - BATTERY_MIN_ADC);
}

if (first) {
percent = p;
first = false;
} else {
percent = 100 * (adc - BATTERY_MIN_ADC) / (BATTERY_MAX_ADC - BATTERY_MIN_ADC);
percent = percent + (p - percent) / BATTERY_FILTER_FACTOR;
}

return percent;
return (percent + 0.5);
}

LOCAL void ICACHE_FLASH_ATTR battery_set_response(char *response) {
Expand Down Expand Up @@ -80,21 +76,23 @@ LOCAL void ICACHE_FLASH_ATTR battery_set_response(char *response) {
}

LOCAL void ICACHE_FLASH_ATTR battery_state_get() {
LOCAL uint8 count = 0;
LOCAL int count = 0;

char response[WEBSERVER_MAX_VALUE];
uint8 state = gpio16_input_get();
uint8 percent = battery_percent_get();

if (percent != battery_percent) {
if (percent > battery_percent && battery_state != 0) {
count++;
} else if (percent < battery_percent && battery_state == 0) {
count--;
} else {
count = 0;
}

if (
state != battery_state ||
(percent != battery_percent && count > BATTERY_FILTER_COUNT)
(percent != battery_percent && abs(count) > BATTERY_FILTER_COUNT)
) {
count = 0;
battery_state = state;
Expand All @@ -115,9 +113,6 @@ void ICACHE_FLASH_ATTR battery_handler(
char *response,
uint16 response_len
) {
battery_state = gpio16_input_get();
battery_percent = battery_percent_get();

battery_set_response(response);
}

Expand All @@ -127,6 +122,9 @@ void ICACHE_FLASH_ATTR user_battery_init() {
webserver_register_handler_callback(BATTERY_URL, battery_handler);
device_register(NATIVE, 0, ESP8266, BATTERY_URL, NULL, NULL);

battery_state = gpio16_input_get();
battery_percent = battery_percent_get();

setInterval(battery_state_get, NULL, BATTERY_STATE_REFRESH);
}
#endif
149 changes: 89 additions & 60 deletions IoT Firmware/olimex/user/user_mod_emtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ LOCAL struct {
emtr_output_registers *output;
} emtr_registers = {NULL, NULL, NULL};

LOCAL emtr_low_pass_output *last_event = NULL;
LOCAL emtr_low_pass_output *current_event = NULL;

LOCAL emtr_mode emtr_current_mode = EMTR_LOG;
LOCAL uint32 emtr_read_interval = EMTR_DEFAULT_READ_INTERVAL;
LOCAL uint32 emtr_single_wire_read = false;
Expand Down Expand Up @@ -189,40 +192,18 @@ LOCAL bool ICACHE_FLASH_ATTR emtr_over_treshold(_sint64_ a, _sint64_ b, uint32 t
return (abs(a - b) > treshold);
}

LOCAL void ICACHE_FLASH_ATTR emtr_read_event(uint32 interval) {
LOCAL emtr_output_registers *last_event = NULL;

bool event = false;
if (last_event == NULL) {
last_event = (emtr_output_registers *)os_zalloc(sizeof(emtr_output_registers));
event = true;
LOCAL float ICACHE_FLASH_ATTR emtr_low_pass(float old, float new, uint8 factor, uint32 treshold) {
if (abs(new - old) > treshold) {
return new;
}

event = event || emtr_over_treshold(emtr_registers.output->current_rms, last_event->current_rms, 10);
event = event || emtr_over_treshold(emtr_registers.output->voltage_rms, last_event->voltage_rms, 5);
event = event || emtr_over_treshold(emtr_registers.output->active_power, last_event->active_power, 5);
event = event || emtr_over_treshold(emtr_registers.output->reactive_power, last_event->reactive_power, 5);
event = event || emtr_over_treshold(emtr_registers.output->apparent_power, last_event->apparent_power, 5);
event = event || emtr_over_treshold(emtr_registers.output->power_factor, last_event->power_factor, 50);
event = event || emtr_over_treshold(emtr_registers.output->line_frequency, last_event->line_frequency, 50);
event = event || emtr_over_treshold(emtr_registers.output->event_flag, last_event->event_flag, 0);
event = event || emtr_over_treshold(emtr_registers.output->system_status, last_event->system_status, 0);

if (!event) {
return (old + (new - old) / factor);
}

LOCAL void ICACHE_FLASH_ATTR emtr_read_format(char *response, uint32 interval) {
if (last_event == NULL) {
return;
}

last_event->current_rms = emtr_registers.output->current_rms;
last_event->voltage_rms = emtr_registers.output->voltage_rms;
last_event->active_power = emtr_registers.output->active_power;
last_event->reactive_power = emtr_registers.output->reactive_power;
last_event->apparent_power = emtr_registers.output->apparent_power;
last_event->power_factor = emtr_registers.output->power_factor;
last_event->line_frequency = emtr_registers.output->line_frequency;
last_event->event_flag = emtr_registers.output->event_flag;
last_event->system_status = emtr_registers.output->system_status;

char response[WEBSERVER_MAX_RESPONSE_LEN];
char data_str[WEBSERVER_MAX_RESPONSE_LEN];
json_data(
response, MOD_EMTR, OK_STR,
Expand All @@ -248,19 +229,86 @@ LOCAL void ICACHE_FLASH_ATTR emtr_read_event(uint32 interval) {
emtr_counter_apparent(),
interval,

emtr_registers.output->current_rms,
emtr_registers.output->voltage_rms,
emtr_registers.output->active_power,
emtr_registers.output->reactive_power,
emtr_registers.output->apparent_power,
emtr_registers.output->power_factor,
emtr_registers.output->line_frequency,
emtr_registers.output->thermistor_voltage,
emtr_registers.output->event_flag,
emtr_registers.output->system_status
(_sint64_)last_event->current_rms,
(_sint64_)last_event->voltage_rms,
(_sint64_)last_event->active_power,
(_sint64_)last_event->reactive_power,
(_sint64_)last_event->apparent_power,
(_sint64_)last_event->power_factor,
(_sint64_)last_event->line_frequency,
(_sint64_)last_event->thermistor_voltage,
(_sint64_)last_event->event_flag,
(_sint64_)last_event->system_status
),
NULL
);
}

LOCAL void ICACHE_FLASH_ATTR emtr_read_event(uint32 interval) {
bool event = false;
if (last_event == NULL) {
current_event = (emtr_low_pass_output *)os_zalloc(sizeof(emtr_low_pass_output));
last_event = (emtr_low_pass_output *)os_zalloc(sizeof(emtr_low_pass_output));

current_event->current_rms = emtr_registers.output->current_rms;
current_event->voltage_rms = emtr_registers.output->voltage_rms;
current_event->active_power = emtr_registers.output->active_power;
current_event->reactive_power = emtr_registers.output->reactive_power;
current_event->apparent_power = emtr_registers.output->apparent_power;
current_event->power_factor = emtr_registers.output->power_factor;
current_event->line_frequency = emtr_registers.output->line_frequency;
current_event->event_flag = emtr_registers.output->event_flag;
current_event->system_status = emtr_registers.output->system_status;

last_event->current_rms = emtr_registers.output->current_rms;
last_event->voltage_rms = emtr_registers.output->voltage_rms;
last_event->active_power = emtr_registers.output->active_power;
last_event->reactive_power = emtr_registers.output->reactive_power;
last_event->apparent_power = emtr_registers.output->apparent_power;
last_event->power_factor = emtr_registers.output->power_factor;
last_event->line_frequency = emtr_registers.output->line_frequency;
last_event->event_flag = emtr_registers.output->event_flag;
last_event->system_status = emtr_registers.output->system_status;

event = true;
}

current_event->current_rms = emtr_low_pass(current_event->current_rms, emtr_registers.output->current_rms, 4, 20);
current_event->voltage_rms = emtr_low_pass(current_event->voltage_rms, emtr_registers.output->voltage_rms, 16, 10);
current_event->active_power = emtr_low_pass(current_event->active_power, emtr_registers.output->active_power, 8, 10);
current_event->reactive_power = emtr_low_pass(current_event->reactive_power, emtr_registers.output->reactive_power, 8, 10);
current_event->apparent_power = emtr_low_pass(current_event->apparent_power, emtr_registers.output->apparent_power, 8, 10);
current_event->power_factor = emtr_low_pass(current_event->power_factor, emtr_registers.output->power_factor, 8, 100);
current_event->line_frequency = emtr_low_pass(current_event->line_frequency, emtr_registers.output->line_frequency, 10, 100);
current_event->event_flag = emtr_registers.output->event_flag;
current_event->system_status = emtr_registers.output->system_status;

event = event || emtr_over_treshold(last_event->current_rms, current_event->current_rms, 10);
event = event || emtr_over_treshold(last_event->voltage_rms, current_event->voltage_rms, 5);
event = event || emtr_over_treshold(last_event->active_power, current_event->active_power, 5);
event = event || emtr_over_treshold(last_event->reactive_power, current_event->reactive_power, 5);
event = event || emtr_over_treshold(last_event->apparent_power, current_event->apparent_power, 5);
event = event || emtr_over_treshold(last_event->power_factor, current_event->power_factor, 50);
event = event || emtr_over_treshold(last_event->line_frequency, current_event->line_frequency, 50);
event = event || emtr_over_treshold(last_event->event_flag, current_event->event_flag, 0);
event = event || emtr_over_treshold(last_event->system_status, current_event->system_status, 0);

if (!event) {
return;
}

last_event->current_rms = current_event->current_rms;
last_event->voltage_rms = current_event->voltage_rms;
last_event->active_power = current_event->active_power;
last_event->reactive_power = current_event->reactive_power;
last_event->apparent_power = current_event->apparent_power;
last_event->power_factor = current_event->power_factor;
last_event->line_frequency = current_event->line_frequency;
last_event->event_flag = current_event->event_flag;
last_event->system_status = current_event->system_status;

char response[WEBSERVER_MAX_RESPONSE_LEN];
emtr_read_format(response, interval);
user_event_raise(EMTR_URL, response);
}

Expand Down Expand Up @@ -666,8 +714,6 @@ void ICACHE_FLASH_ATTR emtr_handler(
}
}

char data_str[WEBSERVER_MAX_RESPONSE_LEN];

if (emtr_current_mode == EMTR_CONFIGURE) {
setTimeout(emtr_events_read, NULL, 1000);
}
Expand All @@ -676,24 +722,7 @@ void ICACHE_FLASH_ATTR emtr_handler(
setTimeout(emtr_calibration_read, NULL, 1000);
}

json_data(
response, MOD_EMTR, OK_STR,
json_sprintf(
data_str,
"\"Address\" : \"0x%04X\", "
"\"Mode\" : \"%s\", "
"\"CounterActive\" : %d, "
"\"CounterApparent\" : %d, "
"\"ReadInterval\" : %d",
emtr_address(),
emtr_mode_str(emtr_current_mode),
emtr_counter_active(),
emtr_counter_apparent(),
emtr_read_interval
),
NULL
);

emtr_read_format(response, emtr_read_interval);
emtr_start_read();
}

Expand Down
16 changes: 13 additions & 3 deletions IoT Firmware/olimex/user/user_mod_tc_mk2.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ LOCAL void ICACHE_FLASH_ATTR mod_tc_mk2_read(i2c_config *config, char *response,
LOCAL void ICACHE_FLASH_ATTR mod_tc_mk2_event(i2c_config *config) {
char response[WEBSERVER_MAX_RESPONSE_LEN];
LOCAL sint16 old_temperature = 0;
LOCAL uint8 count = 0;
LOCAL int count = 0;

i2c_status status;

Expand All @@ -76,10 +76,20 @@ LOCAL void ICACHE_FLASH_ATTR mod_tc_mk2_event(i2c_config *config) {
return;
}

count++;
if (config_data->temperature > old_temperature) {
count++;
} else if (config_data->temperature < old_temperature) {
count--;
} else {
count = 0;
}

mod_tc_mk2_read(config, response, false);
if (abs(config_data->temperature - old_temperature) > tc_threshold || (count > tc_each && config_data->temperature != old_temperature)) {
if (
abs(config_data->temperature - old_temperature) > tc_threshold
||
(abs(count) > tc_each && config_data->temperature != old_temperature)
) {
#if MOD_TC_MO2_DEBUG
debug("MOD-TC-MK2: Temperature change [%d] -> [%d]\n", old_temperature, config_data->temperature);
#endif
Expand Down

0 comments on commit a6cac2e

Please sign in to comment.