Skip to content

Commit

Permalink
Merge branch 'feat/ntc_driver_add_filter_v2.0' into 'release/v2.0'
Browse files Browse the repository at this point in the history
feat(ntc_driver): improve adc result using filter (v2.0)

See merge request ae_group/esp-iot-solution!1145
  • Loading branch information
leeebo committed Nov 15, 2024
2 parents e9fb383 + 60d6ab7 commit 32eb5cb
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ The registered components in ESP-IoT-Solution are listed below:
| [knob](https://components.espressif.com/components/espressif/knob/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/knob/versions/1.0.0) |
| [led_indicator](https://components.espressif.com/components/espressif/led_indicator/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/led_indicator/versions/1.0.0) |
| [lightbulb_driver](https://components.espressif.com/components/espressif/lightbulb_driver/versions/1.3.3) | [![1.3.3](https://img.shields.io/badge/Stable-1.3.3-blue)](https://components.espressif.com/components/espressif/lightbulb_driver/versions/1.3.3) |
| [ntc_driver](https://components.espressif.com/components/espressif/ntc_driver/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/ntc_driver/versions/1.0.0) |
| [ntc_driver](https://components.espressif.com/components/espressif/ntc_driver/versions/1.1.0) | [![1.1.0](https://img.shields.io/badge/Stable-1.1.0-blue)](https://components.espressif.com/components/espressif/ntc_driver/versions/1.1.0) |
| [openai](https://components.espressif.com/components/espressif/openai/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/openai/versions/1.0.0) |
| [pwm_audio](https://components.espressif.com/components/espressif/pwm_audio/versions/1.1.2) | [![1.1.2](https://img.shields.io/badge/Stable-1.1.2-blue)](https://components.espressif.com/components/espressif/pwm_audio/versions/1.1.2) |
| [spi_bus](https://components.espressif.com/components/espressif/spi_bus/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/spi_bus/versions/1.0.0) |
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ ESP-IoT-Solution 中注册的组件如下:
| [knob](https://components.espressif.com/components/espressif/knob/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/knob/versions/1.0.0) |
| [led_indicator](https://components.espressif.com/components/espressif/led_indicator/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/led_indicator/versions/1.0.0) |
| [lightbulb_driver](https://components.espressif.com/components/espressif/lightbulb_driver/versions/1.3.3) | [![1.3.3](https://img.shields.io/badge/Stable-1.3.3-blue)](https://components.espressif.com/components/espressif/lightbulb_driver/versions/1.3.3) |
| [ntc_driver](https://components.espressif.com/components/espressif/ntc_driver/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/ntc_driver/versions/1.0.0) |
| [ntc_driver](https://components.espressif.com/components/espressif/ntc_driver/versions/1.1.0) | [![1.1.0](https://img.shields.io/badge/Stable-1.1.0-blue)](https://components.espressif.com/components/espressif/ntc_driver/versions/1.1.0) |
| [openai](https://components.espressif.com/components/espressif/openai/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/openai/versions/1.0.0) |
| [pwm_audio](https://components.espressif.com/components/espressif/pwm_audio/versions/1.1.2) | [![1.1.2](https://img.shields.io/badge/Stable-1.1.2-blue)](https://components.espressif.com/components/espressif/pwm_audio/versions/1.1.2) |
| [spi_bus](https://components.espressif.com/components/espressif/spi_bus/versions/1.0.0) | [![1.0.0](https://img.shields.io/badge/Stable-1.0.0-blue)](https://components.espressif.com/components/espressif/spi_bus/versions/1.0.0) |
Expand Down
6 changes: 6 additions & 0 deletions components/sensors/ntc_driver/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ChangeLog

## v1.1.0 - 2024-11-5

### Improvements

* Add NTC ADC filter to reduce the noise.

## v1.0.0 - 2024-10-15

* Update the version of ntc_driver component.
Expand Down
19 changes: 19 additions & 0 deletions components/sensors/ntc_driver/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
menu "NTC Driver Configuration"

config ENABLE_NTC_ADC_FILTER
bool "Enable ADC Filter for NTC"
default y
help
Enable ADC Filter for NTC, the driver will take multiple ADC measurements
and then filters them to avoid ADC noise

config NTC_FILTER_WINDOW_SIZE
int "NTC Filter Window Size"
default 50
depends on ENABLE_NTC_ADC_FILTER
help
The number of ADC measurements to take before filtering the NTC value, the
higher the value the more stable the NTC value will be, but will take longer
to update

endmenu
2 changes: 1 addition & 1 deletion components/sensors/ntc_driver/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "1.0.0"
version: "1.1.0"
description: ntc driver
url: https://github.com/espressif/esp-iot-solution/tree/master/components/sensors/ntc_driver
repository: https://github.com/espressif/esp-iot-solution.git
Expand Down
50 changes: 48 additions & 2 deletions components/sensors/ntc_driver/ntc_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,67 @@ static esp_err_t ntc_voltage_to_temperature(ntc_device_handle_t ntc_handle, uint
return ESP_FAIL;
}

#ifdef CONFIG_ENABLE_NTC_ADC_FILTER
static esp_err_t adc_oneshot_get_filtered_result(adc_oneshot_unit_handle_t handle, adc_cali_handle_t cali_handle, adc_channel_t chan, int *filtered_result)
{
NTC_DRIVER_CHECK(filtered_result != NULL, "Pointer of filtered_result is invalid", ESP_FAIL);
int vol[CONFIG_NTC_FILTER_WINDOW_SIZE] = {0};
int avg = 0;
for (int i = 0; i < sizeof(vol) / sizeof(vol[0]); i++) {
int raw = 0;
NTC_DRIVER_CHECK(adc_oneshot_read(handle, chan, &raw) == ESP_OK, "adc oneshot read fail", ESP_FAIL);
ESP_LOGV(TAG, "raw: %d", raw);
NTC_DRIVER_CHECK(adc_cali_raw_to_voltage(cali_handle, raw, &vol[i]) == ESP_OK, "adc calibration fail", ESP_FAIL);
ESP_LOGV(TAG, "vol: %d", vol[i]);
avg += vol[i];
}
// calculate the average value
avg /= sizeof(vol) / sizeof(vol[0]);
*filtered_result = avg;
// calculate the standard deviation
int std_vol = 0;
for (int i = 0; i < sizeof(vol) / sizeof(vol[0]); i++) {
std_vol += (vol[i] - avg) * (vol[i] - avg);
}
std_vol = (int)sqrt(std_vol / (sizeof(vol) / sizeof(vol[0])));
// filter the result, if the difference between the value and the average value is greater than the standard deviation, the value is discarded
int count = 0;
int filtered_vol = 0;
for (int i = 0; i < sizeof(vol) / sizeof(vol[0]); i++) {
if (abs(vol[i] - avg) < std_vol) {
count++;
filtered_vol += vol[i];
} else {
ESP_LOGV(TAG, "filtered out: %d", vol[i]);
}
}
if (count > 0) {
*filtered_result = filtered_vol / count;
}

return ESP_OK;
}
#endif

esp_err_t ntc_dev_get_temperature(ntc_device_handle_t ntc_handle, float *temperature)
{
int voltage, adc_raw = 0;
int voltage = 0;

if (ntc_handle == NULL) {
ESP_LOGW(TAG, "Pointer of handle is invalid");
return ESP_ERR_INVALID_ARG;
}

ntc_driver_dev_t *ndd = (ntc_driver_dev_t *)ntc_handle;
#ifdef CONFIG_ENABLE_NTC_ADC_FILTER
esp_err_t ret = adc_oneshot_get_filtered_result(ndd->adc_handle, ndd->adc_cali_handle, ndd->s_ntc_config.channel, &voltage);
#else
int adc_raw = 0;
esp_err_t ret = adc_oneshot_read(ndd->adc_handle, ndd->s_ntc_config.channel, &adc_raw);
NTC_DRIVER_CHECK(ret == ESP_OK, "adc oneshot read fail", ESP_FAIL);
NTC_DRIVER_CHECK(adc_raw != 0, "adc raw data equal to 0", ESP_FAIL);

ret = adc_cali_raw_to_voltage(ndd->adc_cali_handle, adc_raw, &voltage);
#endif
NTC_DRIVER_CHECK(ret == ESP_OK, "adc calibration raw to voltage fail", ESP_FAIL);
NTC_DRIVER_CHECK(voltage != 0, "voltage equal to 0", ESP_FAIL);
ntc_voltage_to_temperature(ndd, voltage, temperature);
Expand Down

0 comments on commit 32eb5cb

Please sign in to comment.