auto-range

This commit is contained in:
leo 2023-06-15 14:20:08 +02:00
parent 13e51f3943
commit ccf7ebf5e9
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
2 changed files with 31 additions and 11 deletions

View File

@ -7,4 +7,5 @@ typedef struct {
uint32_t* offsets;
uint16_t zero_cali_nsamp;
int range;
int auto_range;
} configuration;

View File

@ -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);
}
}