Skip to content

Commit

Permalink
analog: make the usage of filters configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
paradajz committed Jan 10, 2022
1 parent 8e3ac0f commit 9fd7400
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
14 changes: 14 additions & 0 deletions scripts/gen_target.sh
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,20 @@ then
printf "%s\n" "DEFINES += ADC_EXT_REF" >> "$OUT_FILE_MAKEFILE_DEFINES"
fi

analog_filter_median=$($YAML_PARSER "$TARGET_DEF_FILE" analog.filter.median)
analog_filter_ema=$($YAML_PARSER "$TARGET_DEF_FILE" analog.filter.ema)

# use filters by default if not specified
if [[ $analog_filter_median != "false" ]]
then
printf "%s\n" "DEFINES += ANALOG_USE_MEDIAN_FILTER" >> "$OUT_FILE_MAKEFILE_DEFINES"
fi

if [[ $analog_filter_ema != "false" ]]
then
printf "%s\n" "DEFINES += ANALOG_USE_EMA_FILTER" >> "$OUT_FILE_MAKEFILE_DEFINES"
fi

analog_in_type=$($YAML_PARSER "$TARGET_DEF_FILE" analog.type)

declare -i nr_of_analog_inputs
Expand Down
28 changes: 21 additions & 7 deletions src/application/io/analog/Filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ limitations under the License.
#include "io/analog/Analog.h"
#include "core/src/general/Helpers.h"

#ifndef MEDIAN_SAMPLE_COUNT
#ifdef ANALOG_USE_MEDIAN_FILTER
#define MEDIAN_SAMPLE_COUNT 3
#endif

#ifndef MEDIAN_MIDDLE_VALUE
#define MEDIAN_MIDDLE_VALUE 1
#endif

Expand Down Expand Up @@ -90,6 +87,7 @@ namespace IO
}

#ifdef ADC_SUPPORTED
#ifdef ANALOG_USE_MEDIAN_FILTER
auto compare = [](const void* a, const void* b) {
if (*(uint16_t*)a < *(uint16_t*)b)
return -1;
Expand All @@ -98,6 +96,7 @@ namespace IO

return 0;
};
#endif

const bool fastFilter = (index < IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)) ? (core::timing::currentRunTimeMs() - _lastStableMovementTime[index]) < FAST_FILTER_ENABLE_AFTER_MS : true;
#else
Expand All @@ -118,8 +117,10 @@ namespace IO
if (abs(value - _lastStableValue[index]) < stepDiff)
{
#ifdef ADC_SUPPORTED
#ifdef ANALOG_USE_MEDIAN_FILTER
if (index < IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS))
_medianSampleCounter[index] = 0;
#endif
#endif

return false;
Expand All @@ -130,6 +131,7 @@ namespace IO
{
// don't filter the readings for touchscreen data

#ifdef ANALOG_USE_MEDIAN_FILTER
if (!fastFilter)
{
_analogSample[index][_medianSampleCounter[index]++] = value;
Expand All @@ -147,12 +149,15 @@ namespace IO
}
}
else
#endif
{
filteredValue = value;
}

#ifdef ANALOG_USE_EMA_FILTER
if (index < IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS))
filteredValue = _emaFilter[index].value(filteredValue);
#endif
}
else
{
Expand Down Expand Up @@ -198,7 +203,9 @@ namespace IO
#ifdef ADC_SUPPORTED
if (index < IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS))
{
_medianSampleCounter[index] = 0;
#ifdef ANALOG_USE_MEDIAN_FILTER
_medianSampleCounter[index] = 0;
#endif
_lastStableMovementTime[index] = 0;
}
#endif
Expand Down Expand Up @@ -260,10 +267,17 @@ namespace IO

// some filtering is needed for adc only
#ifdef ADC_SUPPORTED
EMA _emaFilter[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)];
#ifdef ANALOG_USE_EMA_FILTER
EMA _emaFilter[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)];
#endif

#ifdef ANALOG_USE_MEDIAN_FILTER
uint16_t _analogSample[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)][MEDIAN_SAMPLE_COUNT] = {};
uint8_t _medianSampleCounter[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)] = {};
uint32_t _lastStableMovementTime[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)] = {};
#else
uint16_t _analogSample[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)] = {};
#endif
uint32_t _lastStableMovementTime[IO::Analog::Collection::size(IO::Analog::GROUP_ANALOG_INPUTS)] = {};
#endif

uint8_t _lastStableDirection[IO::Analog::Collection::size() / 8 + 1] = {};
Expand Down

0 comments on commit 9fd7400

Please sign in to comment.