From ccf7ebf5e9ebfafbe44344e63afc1e90df574ef9 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 15 Jun 2023 14:20:08 +0200 Subject: [PATCH] auto-range --- main/configuration.h | 1 + main/power_profiler.c | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/main/configuration.h b/main/configuration.h index e048604..6ac95f5 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -7,4 +7,5 @@ typedef struct { uint32_t* offsets; uint16_t zero_cali_nsamp; int range; + int auto_range; } configuration; diff --git a/main/power_profiler.c b/main/power_profiler.c index c8b0e3c..91f4cd1 100644 --- a/main/power_profiler.c +++ b/main/power_profiler.c @@ -24,6 +24,13 @@ #define TAG "main" +// mV value that we consider the threshold for activating the next higher resistance +#define UNDERRANGE_MV 10 +// source for the comparison used for the range switching +#define UNDERRANGE_SRC X10 +// number of consecutive times the input has to be below the threshold before switching +#define UNDERRANGE_NUM 3 + resistor_range ranges[] = { [R1] = { .pin = GPIO_NUM_6, @@ -81,11 +88,17 @@ static bool IRAM_ATTR on_conv_done(adc_continuous_handle_t handle, const adc_con return false; } -char overrange_flag = 0; +configuration main_conf = { + .refresh_delay = 1000, + .range = R100, + .zero_cali_nsamp = 10000, + .auto_range = 1, +}; static void IRAM_ATTR overrange_handler(void* arg) { - overrange_flag = 1; + activate_range(ranges, 0, R_NUM - 1); + main_conf.range = 0; } void app_main(void){ @@ -122,12 +135,8 @@ void app_main(void){ ESP_ERROR_CHECK(nvs_flash_init()); uint32_t offsets[INPUTS_NUM] = {0}; - configuration main_conf = { - .refresh_delay = 1000, - .offsets = offsets, - .range = R100, - .zero_cali_nsamp = 10000, - }; + + main_conf.offsets = offsets; uint32_t meas_volts[INPUTS_NUM]; uint32_t meas_amp[INPUTS_NUM]; @@ -144,6 +153,8 @@ void app_main(void){ initBLE(&main_conf, &meas, &meas_ctxt); + int underrange_counter = 0; + while(1){ uint32_t meas_res_buff[INPUTS_NUM]; uint32_t meas_nb_buff[INPUTS_NUM]; @@ -168,11 +179,19 @@ void app_main(void){ meas_amp[i] = mv * 1000000 / ranges[main_conf.range].resistance; } + if(meas_volts[UNDERRANGE_SRC] < UNDERRANGE_MV){ + underrange_counter++; + if(underrange_counter > UNDERRANGE_NUM){ + main_conf.range--; + activate_range(ranges, main_conf.range, R_NUM - 1); + } + } + else{ + underrange_counter = 0; + } + notify_update(&meas); - ESP_LOGI(TAG, "overrange %d", overrange_flag); - overrange_flag = 0; - vTaskDelay(main_conf.refresh_delay / portTICK_PERIOD_MS); } }