ref_app_tank_level.c

This is a reference application for measuring liquid level in tanks
This reference application executes as follows:

- Retrieve HAL integration
- Initialize application resources:
  - Create application configuration
  - Create distance detector configuration
  - Update configuration settings
  - Create distance detector handle
  - Create buffer for detector calibration data
  - Create buffer for sensor data
- Create and calibrate the sensor
- Calibrate the detector
- Measure distances with the detector (loop):
  - Prepare sensor with the detector
  - Measure and wait until a read can be done
  - Process sensor measurement and get distance detector result
  - Process distance detector result and print the result
  - Handle "sensor_calibration_needed" indication
- Cleanup:
  - Destroy detector configuration
  - Destroy detector handle
  - Destroy sensor data buffer
  - Destroy detector calibration data buffer

// Copyright (c) Acconeer AB, 2023
// All rights reserved
// This file is subject to the terms and conditions defined in the file
// 'LICENSES/license_acconeer.txt', (BSD 3-Clause License) which is part
// of this source code package.
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "acc_definitions_a121.h"
#include "acc_detector_distance.h"
#include "acc_hal_definitions_a121.h"
#include "acc_hal_integration_a121.h"
#include "acc_integration.h"
#include "acc_integration_log.h"
#include "acc_rss_a121.h"
#include "acc_sensor.h"
#include "acc_version.h"
#define SENSOR_ID 1U
#define SENSOR_TIMEOUT_MS 1000U
#define DEFAULT_UPDATE_RATE 1.0f
// Settings for a small tank
#define SMALL_TANK_MEDIAN_FILTER_LENGTH 5U
#define SMALL_TANK_NUM_MEDIANS_TO_AVERAGE 5U
#define SMALL_TANK_RANGE_START_M 0.03f
#define SMALL_TANK_RANGE_END_M 0.5f
#define SMALL_TANK_MAX_STEP_LENGTH 2U
#define SMALL_TANK_MAX_PROFILE ACC_CONFIG_PROFILE_2
#define SMALL_TANK_NUM_FRAMES_REC 50U
#define SMALL_TANK_PEAK_SORTING ACC_DETECTOR_DISTANCE_PEAK_SORTING_CLOSEST
#define SMALL_TANK_REFLECTOR_SHAPE ACC_DETECTOR_DISTANCE_REFLECTOR_SHAPE_PLANAR
#define SMALL_TANK_THRESHOLD_SENSITIVITY 0.0f
#define SMALL_TANK_SIGNAL_QUALITY 20.0f
#define SMALL_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION true
// Settings for a medium tank
#define MEDIUM_TANK_MEDIAN_FILTER_LENGTH 3U
#define MEDIUM_TANK_NUM_MEDIANS_TO_AVERAGE 3U
#define MEDIUM_TANK_RANGE_START_M 0.05f
#define MEDIUM_TANK_RANGE_END_M 6.0f
#define MEDIUM_TANK_MAX_STEP_LENGTH 0U // 0 means no limit
#define MEDIUM_TANK_MAX_PROFILE ACC_CONFIG_PROFILE_3
#define MEDIUM_TANK_NUM_FRAMES_REC 50U
#define MEDIUM_TANK_PEAK_SORTING ACC_DETECTOR_DISTANCE_PEAK_SORTING_STRONGEST
#define MEDIUM_TANK_REFLECTOR_SHAPE ACC_DETECTOR_DISTANCE_REFLECTOR_SHAPE_PLANAR
#define MEDIUM_TANK_THRESHOLD_SENSITIVITY 0.0f
#define MEDIUM_TANK_SIGNAL_QUALITY 20.0f
#define MEDIUM_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION true
// Settings for a large tank
#define LARGE_TANK_MEDIAN_FILTER_LENGTH 3U
#define LARGE_TANK_NUM_MEDIANS_TO_AVERAGE 1U
#define LARGE_TANK_RANGE_START_M 0.1f
#define LARGE_TANK_RANGE_END_M 10.0f
#define LARGE_TANK_MAX_STEP_LENGTH 0U // 0 means no limit
#define LARGE_TANK_MAX_PROFILE ACC_CONFIG_PROFILE_5
#define LARGE_TANK_NUM_FRAMES_REC 50U
#define LARGE_TANK_PEAK_SORTING ACC_DETECTOR_DISTANCE_PEAK_SORTING_STRONGEST
#define LARGE_TANK_REFLECTOR_SHAPE ACC_DETECTOR_DISTANCE_REFLECTOR_SHAPE_PLANAR
#define LARGE_TANK_THRESHOLD_SENSITIVITY 0.0f
#define LARGE_TANK_SIGNAL_QUALITY 20.0f
#define LARGE_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION true
/** \example ref_app_tank_level.c
* @brief This is a reference application for measuring liquid level in tanks
* @n
* This reference application executes as follows:
* - Retrieve HAL integration
* - Initialize application resources:
* + Create application configuration
* + Create distance detector configuration
* + Update configuration settings
* + Create distance detector handle
* + Create buffer for detector calibration data
* + Create buffer for sensor data
* - Create and calibrate the sensor
* - Calibrate the detector
* - Measure distances with the detector (loop):
* + Prepare sensor with the detector
* + Measure and wait until a read can be done
* + Process sensor measurement and get distance detector result
* + Process distance detector result and print the result
* + Handle "sensor_calibration_needed" indication
* - Cleanup:
* + Destroy detector configuration
* + Destroy detector handle
* + Destroy sensor data buffer
* + Destroy detector calibration data buffer
*/
typedef enum
{
TANK_LEVEL_PRESET_CONFIG_NONE = 0,
TANK_LEVEL_PRESET_CONFIG_SMALL_TANK,
TANK_LEVEL_PRESET_CONFIG_MEDIUM_TANK,
TANK_LEVEL_PRESET_CONFIG_LARGE_TANK,
} tank_level_preset_config_t;
#define DEFAULT_PRESET_CONFIG TANK_LEVEL_PRESET_CONFIG_SMALL_TANK
typedef struct
{
float tank_range_start_m;
float tank_range_end_m;
uint16_t median_filter_length;
uint16_t num_medians_to_average;
acc_detector_distance_config_t *distance_config;
} acc_ref_app_tank_level_config_t;
typedef struct
{
acc_ref_app_tank_level_config_t *app_config;
acc_sensor_t *sensor;
float *level_history;
uint16_t level_history_length;
float *median_vector;
uint16_t median_vector_length;
uint16_t median_counter;
uint16_t mean_counter;
uint16_t median_edge_status_count;
uint16_t mean_edge_status_count;
acc_detector_distance_handle_t *detector_handle;
void *buffer;
uint32_t buffer_size;
void *calibration_buffer;
uint32_t calibration_buffer_size;
} app_context_t;
typedef enum
{
PEAK_STATUS_IN_RANGE,
PEAK_STATUS_NO_DETECTION,
PEAK_STATUS_OVERFLOW,
PEAK_STATUS_OUT_OF_RANGE
} peak_status_t;
typedef struct
{
bool peak_detected;
peak_status_t peak_status;
float level;
bool result_ready;
} app_result_t;
static void cleanup(app_context_t *context);
static float get_detector_start_m(acc_ref_app_tank_level_config_t *app_config);
static float get_detector_end_m(acc_ref_app_tank_level_config_t *app_config);
static void set_config(acc_ref_app_tank_level_config_t *app_config, tank_level_preset_config_t preset);
static bool initialize_application_resources(app_context_t *context);
static bool sensor_calibration(acc_sensor_t *sensor,
acc_cal_result_t *cal_result,
void *buffer,
uint32_t buffer_size);
static bool full_detector_calibration(app_context_t *context,
const acc_cal_result_t *sensor_cal_result);
static bool detector_recalibration(app_context_t *context,
const acc_cal_result_t *sensor_cal_result);
static bool detector_get_next(app_context_t *context,
const acc_cal_result_t *sensor_cal_result,
acc_detector_distance_result_t *detector_result);
static void swap(float *array, uint16_t idx_a, uint16_t idx_b);
static void sort(float *array, uint16_t array_length);
static float median(float *array, uint16_t array_length);
static float nanmean(float *array, uint16_t array_length);
static void process_detector_result(const acc_detector_distance_result_t *distance_result, app_result_t *app_result,
app_context_t *context);
static void print_result(const app_result_t *app_result, const acc_ref_app_tank_level_config_t *app_config);
int acconeer_main(int argc, char *argv[]);
int acconeer_main(int argc, char *argv[])
{
(void)argc;
(void)argv;
app_context_t context = { 0 };
acc_ref_app_tank_level_config_t app_config = { 0 };
context.app_config = &app_config;
context.sensor = NULL;
printf("Acconeer software version %s\n", acc_version_get());
const acc_hal_a121_t *hal = acc_hal_rss_integration_get_implementation();
if (!acc_rss_hal_register(hal))
{
return EXIT_FAILURE;
}
context.app_config->distance_config = acc_detector_distance_config_create();
if (context.app_config->distance_config == NULL)
{
printf("acc_detector_distance_config_create() failed\n");
cleanup(&context);
return EXIT_FAILURE;
}
set_config(&app_config, DEFAULT_PRESET_CONFIG);
uint32_t sleep_time_ms = (uint32_t)(1000.0f / DEFAULT_UPDATE_RATE);
acc_integration_set_periodic_wakeup(sleep_time_ms);
if (!initialize_application_resources(&context))
{
printf("Initializing detector context failed\n");
cleanup(&context);
return EXIT_FAILURE;
}
/* Turn the sensor on */
acc_hal_integration_sensor_supply_on(SENSOR_ID);
acc_hal_integration_sensor_enable(SENSOR_ID);
context.sensor = acc_sensor_create(SENSOR_ID);
if (context.sensor == NULL)
{
printf("acc_sensor_create() failed\n");
cleanup(&context);
return EXIT_FAILURE;
}
acc_cal_result_t cal_result;
if (!sensor_calibration(context.sensor, &cal_result, context.buffer, context.buffer_size))
{
printf("Sensor calibration failed\n");
cleanup(&context);
return EXIT_FAILURE;
}
if (!full_detector_calibration(&context, &cal_result))
{
printf("Detector calibration failed\n");
cleanup(&context);
return EXIT_FAILURE;
}
while (true)
{
acc_detector_distance_result_t detector_result = { 0 };
app_result_t app_result = { 0 };
if (!detector_get_next(&context, &cal_result, &detector_result))
{
printf("Could not get next result\n");
cleanup(&context);
return EXIT_FAILURE;
}
process_detector_result(&detector_result, &app_result, &context);
/* If "sensor calibration needed" is indicated, the sensor and detector needs to be recalibrated */
if (detector_result.sensor_calibration_needed)
{
printf("Sensor- and detector recalibration needed ... \n");
if (!sensor_calibration(context.sensor, &cal_result, context.buffer, context.buffer_size))
{
printf("Sensor calibration failed\n");
cleanup(&context);
return EXIT_FAILURE;
}
/* Once the sensor is recalibrated, the detector should be recalibrated and measuring can continue. */
if (!detector_recalibration(&context, &cal_result))
{
printf("Detector recalibration failed\n");
cleanup(&context);
return EXIT_FAILURE;
}
printf("Sensor- and detector recalibration done!\n");
}
if (app_result.result_ready)
{
acc_hal_integration_sensor_disable(SENSOR_ID);
print_result(&app_result, context.app_config);
acc_integration_sleep_until_periodic_wakeup();
acc_hal_integration_sensor_enable(SENSOR_ID);
}
}
cleanup(&context);
printf("Application finished OK\n");
return EXIT_SUCCESS;
}
static void cleanup(app_context_t *context)
{
acc_hal_integration_sensor_disable(SENSOR_ID);
acc_hal_integration_sensor_supply_off(SENSOR_ID);
acc_detector_distance_config_destroy(context->app_config->distance_config);
acc_detector_distance_destroy(context->detector_handle);
acc_integration_mem_free(context->buffer);
acc_integration_mem_free(context->calibration_buffer);
acc_integration_mem_free(context->level_history);
acc_integration_mem_free(context->median_vector);
if (context->sensor != NULL)
{
acc_sensor_destroy(context->sensor);
}
}
static float get_detector_start_m(acc_ref_app_tank_level_config_t *app_config)
{
// Decrease start point by 15 mm to make sure that start of the tank is fully covered
return app_config->tank_range_start_m - 0.015f;
}
static float get_detector_end_m(acc_ref_app_tank_level_config_t *app_config)
{
// Increase end point by 5% to make sure that the bottom of the tank is fully covered
return app_config->tank_range_end_m * 1.05f;
}
static void set_config(acc_ref_app_tank_level_config_t *app_config, tank_level_preset_config_t preset)
{
switch (preset)
{
case TANK_LEVEL_PRESET_CONFIG_NONE:
break;
case TANK_LEVEL_PRESET_CONFIG_SMALL_TANK:
app_config->tank_range_start_m = SMALL_TANK_RANGE_START_M;
app_config->tank_range_end_m = SMALL_TANK_RANGE_END_M;
app_config->median_filter_length = SMALL_TANK_MEDIAN_FILTER_LENGTH;
app_config->num_medians_to_average = SMALL_TANK_NUM_MEDIANS_TO_AVERAGE;
acc_detector_distance_config_start_set(app_config->distance_config, get_detector_start_m(app_config));
acc_detector_distance_config_end_set(app_config->distance_config, get_detector_end_m(app_config));
acc_detector_distance_config_max_step_length_set(app_config->distance_config, SMALL_TANK_MAX_STEP_LENGTH);
acc_detector_distance_config_max_profile_set(app_config->distance_config, SMALL_TANK_MAX_PROFILE);
acc_detector_distance_config_num_frames_recorded_threshold_set(app_config->distance_config, SMALL_TANK_NUM_FRAMES_REC);
acc_detector_distance_config_peak_sorting_set(app_config->distance_config, SMALL_TANK_PEAK_SORTING);
acc_detector_distance_config_reflector_shape_set(app_config->distance_config, SMALL_TANK_REFLECTOR_SHAPE);
acc_detector_distance_config_threshold_sensitivity_set(app_config->distance_config, SMALL_TANK_THRESHOLD_SENSITIVITY);
acc_detector_distance_config_signal_quality_set(app_config->distance_config, SMALL_TANK_SIGNAL_QUALITY);
acc_detector_distance_config_close_range_leakage_cancellation_set(app_config->distance_config,
SMALL_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION);
break;
case TANK_LEVEL_PRESET_CONFIG_MEDIUM_TANK:
app_config->tank_range_start_m = MEDIUM_TANK_RANGE_START_M;
app_config->tank_range_end_m = MEDIUM_TANK_RANGE_END_M;
app_config->median_filter_length = MEDIUM_TANK_MEDIAN_FILTER_LENGTH;
app_config->num_medians_to_average = MEDIUM_TANK_NUM_MEDIANS_TO_AVERAGE;
acc_detector_distance_config_start_set(app_config->distance_config, get_detector_start_m(app_config));
acc_detector_distance_config_end_set(app_config->distance_config, get_detector_end_m(app_config));
acc_detector_distance_config_max_step_length_set(app_config->distance_config, MEDIUM_TANK_MAX_STEP_LENGTH);
acc_detector_distance_config_max_profile_set(app_config->distance_config, MEDIUM_TANK_MAX_PROFILE);
acc_detector_distance_config_num_frames_recorded_threshold_set(app_config->distance_config, MEDIUM_TANK_NUM_FRAMES_REC);
acc_detector_distance_config_peak_sorting_set(app_config->distance_config, MEDIUM_TANK_PEAK_SORTING);
acc_detector_distance_config_reflector_shape_set(app_config->distance_config, MEDIUM_TANK_REFLECTOR_SHAPE);
acc_detector_distance_config_threshold_sensitivity_set(app_config->distance_config, MEDIUM_TANK_THRESHOLD_SENSITIVITY);
acc_detector_distance_config_signal_quality_set(app_config->distance_config, MEDIUM_TANK_SIGNAL_QUALITY);
acc_detector_distance_config_close_range_leakage_cancellation_set(app_config->distance_config,
MEDIUM_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION);
break;
case TANK_LEVEL_PRESET_CONFIG_LARGE_TANK:
app_config->tank_range_start_m = LARGE_TANK_RANGE_START_M;
app_config->tank_range_end_m = LARGE_TANK_RANGE_END_M;
app_config->median_filter_length = LARGE_TANK_MEDIAN_FILTER_LENGTH;
app_config->num_medians_to_average = LARGE_TANK_NUM_MEDIANS_TO_AVERAGE;
acc_detector_distance_config_start_set(app_config->distance_config, get_detector_start_m(app_config));
acc_detector_distance_config_end_set(app_config->distance_config, get_detector_end_m(app_config));
acc_detector_distance_config_max_step_length_set(app_config->distance_config, LARGE_TANK_MAX_STEP_LENGTH);
acc_detector_distance_config_max_profile_set(app_config->distance_config, LARGE_TANK_MAX_PROFILE);
acc_detector_distance_config_num_frames_recorded_threshold_set(app_config->distance_config, LARGE_TANK_NUM_FRAMES_REC);
acc_detector_distance_config_peak_sorting_set(app_config->distance_config, LARGE_TANK_PEAK_SORTING);
acc_detector_distance_config_reflector_shape_set(app_config->distance_config, LARGE_TANK_REFLECTOR_SHAPE);
acc_detector_distance_config_threshold_sensitivity_set(app_config->distance_config, LARGE_TANK_THRESHOLD_SENSITIVITY);
acc_detector_distance_config_signal_quality_set(app_config->distance_config, LARGE_TANK_SIGNAL_QUALITY);
acc_detector_distance_config_close_range_leakage_cancellation_set(app_config->distance_config,
LARGE_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION);
break;
}
}
static bool initialize_application_resources(app_context_t *context)
{
context->detector_handle = acc_detector_distance_create(context->app_config->distance_config);
if (context->detector_handle == NULL)
{
printf("acc_detector_distance_create() failed\n");
return false;
}
if (!acc_detector_distance_get_buffer_sizes(context->detector_handle, &(context->buffer_size), &(context->calibration_buffer_size)))
{
printf("acc_detector_distance_get_buffer_sizes() failed\n");
return false;
}
context->buffer = acc_integration_mem_alloc(context->buffer_size);
if (context->buffer == NULL)
{
printf("sensor buffer allocation failed\n");
return false;
}
context->calibration_buffer = acc_integration_mem_alloc(context->calibration_buffer_size);
if (context->calibration_buffer == NULL)
{
printf("calibration buffer allocation failed\n");
return false;
}
context->level_history_length = context->app_config->median_filter_length;
context->level_history = acc_integration_mem_alloc(context->level_history_length * sizeof(*context->level_history));
if (context->level_history == NULL)
{
printf("level history buffer allocation failed\n");
return false;
}
context->median_vector_length = context->app_config->num_medians_to_average;
context->median_vector = acc_integration_mem_alloc(context->median_vector_length * sizeof(*context->median_vector));
if (context->median_vector == NULL)
{
printf("median vector allocation failed\n");
return false;
}
context->median_counter = 0U;
context->mean_counter = 0U;
context->median_edge_status_count = 0U;
context->mean_edge_status_count = 0U;
return true;
}
static bool sensor_calibration(acc_sensor_t *sensor,
acc_cal_result_t *sensor_cal_result,
void *buffer,
uint32_t buffer_size)
{
bool status = false;
bool cal_complete = false;
const uint16_t calibration_retries = 1U;
// Random disturbances may cause the calibration to fail. At failure, retry at least once.
for (uint16_t i = 0; !status && (i <= calibration_retries); i++)
{
// Reset sensor before calibration by disabling/enabling it
acc_hal_integration_sensor_disable(SENSOR_ID);
acc_hal_integration_sensor_enable(SENSOR_ID);
do
{
status = acc_sensor_calibrate(sensor, &cal_complete, sensor_cal_result, buffer, buffer_size);
if (status && !cal_complete)
{
status = acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS);
}
} while (status && !cal_complete);
}
if (status)
{
/* Reset sensor after calibration by disabling/enabling it */
acc_hal_integration_sensor_disable(SENSOR_ID);
acc_hal_integration_sensor_enable(SENSOR_ID);
}
else
{
printf("acc_sensor_calibrate() failed\n");
acc_sensor_status(sensor);
}
return status;
}
static bool full_detector_calibration(app_context_t *context,
const acc_cal_result_t *sensor_cal_result)
{
bool done = false;
bool status;
do
{
status = acc_detector_distance_calibrate(context->sensor,
context->detector_handle,
sensor_cal_result,
context->buffer,
context->buffer_size,
context->calibration_buffer,
context->calibration_buffer_size,
&done);
} while (status && !done && acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS));
return status;
}
static bool detector_recalibration(app_context_t *context,
const acc_cal_result_t *sensor_cal_result)
{
bool done = false;
bool status;
do
{
status = acc_detector_distance_recalibrate(context->sensor,
context->detector_handle,
sensor_cal_result,
context->buffer,
context->buffer_size,
context->calibration_buffer,
context->calibration_buffer_size,
&done);
} while (status && !done && acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS));
return status;
}
static bool detector_get_next(app_context_t *context,
const acc_cal_result_t *sensor_cal_result,
acc_detector_distance_result_t *detector_result)
{
bool result_available = false;
do
{
if (!acc_detector_distance_prepare(context->detector_handle, context->app_config->distance_config, context->sensor, sensor_cal_result,
context->buffer,
context->buffer_size))
{
printf("acc_detector_distance_prepare() failed\n");
return false;
}
if (!acc_sensor_measure(context->sensor))
{
printf("acc_sensor_measure() failed\n");
return false;
}
if (!acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS))
{
printf("Sensor interrupt timeout\n");
return false;
}
if (!acc_sensor_read(context->sensor, context->buffer, context->buffer_size))
{
printf("acc_sensor_read() failed\n");
return false;
}
if (!acc_detector_distance_process(context->detector_handle, context->buffer, context->calibration_buffer, &result_available,
detector_result))
{
printf("acc_detector_distance_process() failed\n");
return false;
}
} while (!result_available);
return true;
}
static void swap(float *array, uint16_t idx_a, uint16_t idx_b)
{
float tmp = array[idx_a];
array[idx_a] = array[idx_b];
array[idx_b] = tmp;
}
static void sort(float *array, uint16_t array_length)
{
for (uint16_t i = 0; i < array_length - 1; i++)
{
for (uint16_t j = 0; j < array_length - i - 1; j++)
{
if (array[j] > array[j + 1])
{
swap(array, j, j + 1);
}
}
}
}
static float median(float *array, uint16_t array_length)
{
bool status = true;
float result = 0.0f;
for (uint16_t i = 0U; i < array_length && status; i++)
{
if (isnan(array[i]))
{
result = (float)NAN;
status = false;
}
}
if (status)
{
sort(array, array_length);
if (array_length % 2U == 0U)
{
uint16_t n = array_length / 2U;
result = (array[n - 1] + array[n]) / 2.0f;
}
else
{
uint16_t n = array_length / 2;
result = array[n];
}
}
return result;
}
static float nanmean(float *array, uint16_t array_length)
{
uint16_t samples = 0U;
float sum = 0.0f;
for (uint16_t i = 0U; i < array_length; i++)
{
if (!isnan(array[i]))
{
samples++;
sum += array[i];
}
}
return samples > 0U ? sum / (float)samples : (float)NAN;
}
static void process_detector_result(const acc_detector_distance_result_t *distance_result, app_result_t *app_result,
app_context_t *context)
{
app_result->peak_detected = false;
app_result->peak_status = PEAK_STATUS_NO_DETECTION;
app_result->level = 0.0f;
app_result->result_ready = false;
float level = 0.0f;
if (distance_result->num_distances > 0)
{
app_result->peak_detected = true;
level = context->app_config->tank_range_end_m - distance_result->distances[0];
}
else
{
level = (float)NAN;
}
if (distance_result->near_start_edge_status)
{
context->median_edge_status_count++;
}
context->level_history[context->median_counter++] = level;
if (context->median_counter == context->level_history_length)
{
float med = median(context->level_history, context->level_history_length);
context->median_vector[context->mean_counter++] = med;
if (context->median_edge_status_count > context->level_history_length / 2)
{
context->mean_edge_status_count++;
}
context->median_counter = 0U;
context->median_edge_status_count = 0U;
}
if (context->mean_counter == context->median_vector_length)
{
level = nanmean(context->median_vector, context->median_vector_length);
if (!isnan(level))
{
if (level < 0.0f)
{
app_result->peak_status = PEAK_STATUS_OUT_OF_RANGE;
}
else if (level > (context->app_config->tank_range_end_m - context->app_config->tank_range_start_m))
{
app_result->peak_status = PEAK_STATUS_OVERFLOW;
}
else
{
app_result->peak_status = PEAK_STATUS_IN_RANGE;
app_result->level = level;
}
}
else if (context->mean_edge_status_count > context->median_vector_length / 2)
{
app_result->peak_status = PEAK_STATUS_OVERFLOW;
}
if (app_result->peak_status == PEAK_STATUS_OVERFLOW || app_result->peak_status == PEAK_STATUS_OUT_OF_RANGE)
{
app_result->level = (float)NAN;
}
app_result->result_ready = true;
context->mean_counter = 0U;
context->mean_edge_status_count = 0U;
}
}
static void print_result(const app_result_t *app_result, const acc_ref_app_tank_level_config_t *app_config)
{
switch (app_result->peak_status)
{
case PEAK_STATUS_IN_RANGE:
printf("Level within range\n");
printf("Level: %" PRIfloat " cm, %" PRIfloat " %%\n", ACC_LOG_FLOAT_TO_INTEGER(
app_result->level * 100.0f),
ACC_LOG_FLOAT_TO_INTEGER((app_result->level /
(app_config->tank_range_end_m - app_config->tank_range_start_m) *
100.f)));
break;
case PEAK_STATUS_NO_DETECTION:
printf("No detection\n");
break;
case PEAK_STATUS_OVERFLOW:
printf("Overflow!\n");
break;
case PEAK_STATUS_OUT_OF_RANGE:
printf("Out of range\n");
break;
default:
break;
}
}
app_result_t::level
float level
Definition: ref_app_tank_level.c:149
SMALL_TANK_RANGE_START_M
#define SMALL_TANK_RANGE_START_M
Definition: ref_app_tank_level.c:31
full_detector_calibration
static bool full_detector_calibration(app_context_t *context, const acc_cal_result_t *sensor_cal_result)
Definition: ref_app_tank_level.c:531
SMALL_TANK_SIGNAL_QUALITY
#define SMALL_TANK_SIGNAL_QUALITY
Definition: ref_app_tank_level.c:39
app_context_t::median_counter
uint16_t median_counter
Definition: ref_app_tank_level.c:126
app_context_t::buffer_size
uint32_t buffer_size
Definition: ref_app_tank_level.c:132
acc_hal_integration_sensor_supply_off
void acc_hal_integration_sensor_supply_off(acc_sensor_id_t sensor_id)
Power off sensor supply.
Definition: acc_hal_integration_stm32cube_xm.c:113
app_context_t::level_history_length
uint16_t level_history_length
Definition: ref_app_tank_level.c:123
acc_rss_a121.h
PEAK_STATUS_IN_RANGE
@ PEAK_STATUS_IN_RANGE
Definition: ref_app_tank_level.c:139
TANK_LEVEL_PRESET_CONFIG_MEDIUM_TANK
@ TANK_LEVEL_PRESET_CONFIG_MEDIUM_TANK
Definition: ref_app_tank_level.c:102
acc_ref_app_tank_level_config_t::median_filter_length
uint16_t median_filter_length
Definition: ref_app_tank_level.c:113
app_context_t::app_config
acc_ref_app_smart_presence_config_t * app_config
Definition: ref_app_smart_presence.c:148
acc_detector_distance_config_peak_sorting_set
void acc_detector_distance_config_peak_sorting_set(acc_detector_distance_config_t *config, acc_detector_distance_peak_sorting_t peak_sorting)
Set the peak sorting method.
app_result_t::peak_status
peak_status_t peak_status
Definition: ref_app_tank_level.c:148
app_context_t::mean_counter
uint16_t mean_counter
Definition: ref_app_tank_level.c:127
acc_hal_integration_sensor_supply_on
void acc_hal_integration_sensor_supply_on(acc_sensor_id_t sensor_id)
Power on sensor supply.
Definition: acc_hal_integration_stm32cube_xm.c:107
nanmean
static float nanmean(float *array, uint16_t array_length)
Definition: ref_app_tank_level.c:679
TANK_LEVEL_PRESET_CONFIG_SMALL_TANK
@ TANK_LEVEL_PRESET_CONFIG_SMALL_TANK
Definition: ref_app_tank_level.c:101
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
LARGE_TANK_SIGNAL_QUALITY
#define LARGE_TANK_SIGNAL_QUALITY
Definition: ref_app_tank_level.c:67
acc_version.h
acc_ref_app_tank_level_config_t::tank_range_end_m
float tank_range_end_m
Definition: ref_app_tank_level.c:112
SMALL_TANK_MAX_STEP_LENGTH
#define SMALL_TANK_MAX_STEP_LENGTH
Definition: ref_app_tank_level.c:33
app_context_t::buffer
void * buffer
Definition: ref_app_smart_presence.c:156
PEAK_STATUS_OVERFLOW
@ PEAK_STATUS_OVERFLOW
Definition: ref_app_tank_level.c:141
app_context_t
Definition: ref_app_smart_presence.c:146
app_result_t::peak_detected
bool peak_detected
Definition: ref_app_tank_level.c:147
process_detector_result
static void process_detector_result(const acc_detector_distance_result_t *distance_result, app_result_t *app_result, app_context_t *context)
Definition: ref_app_tank_level.c:697
SMALL_TANK_PEAK_SORTING
#define SMALL_TANK_PEAK_SORTING
Definition: ref_app_tank_level.c:36
acc_ref_app_tank_level_config_t::tank_range_start_m
float tank_range_start_m
Definition: ref_app_tank_level.c:111
set_config
static void set_config(acc_ref_app_tank_level_config_t *app_config, tank_level_preset_config_t preset)
Definition: ref_app_tank_level.c:368
MEDIUM_TANK_MAX_STEP_LENGTH
#define MEDIUM_TANK_MAX_STEP_LENGTH
Definition: ref_app_tank_level.c:47
acc_cal_result_t
Definition: acc_definitions_a121.h:19
acc_detector_distance_config_start_set
void acc_detector_distance_config_start_set(acc_detector_distance_config_t *config, float start_m)
Set the start of measured interval in meters.
MEDIUM_TANK_MAX_PROFILE
#define MEDIUM_TANK_MAX_PROFILE
Definition: ref_app_tank_level.c:48
SMALL_TANK_RANGE_END_M
#define SMALL_TANK_RANGE_END_M
Definition: ref_app_tank_level.c:32
MEDIUM_TANK_PEAK_SORTING
#define MEDIUM_TANK_PEAK_SORTING
Definition: ref_app_tank_level.c:50
get_detector_end_m
static float get_detector_end_m(acc_ref_app_tank_level_config_t *app_config)
Definition: ref_app_tank_level.c:361
median
static float median(float *array, uint16_t array_length)
Definition: ref_app_tank_level.c:645
app_context_t::detector_handle
acc_detector_distance_handle_t * detector_handle
Definition: ref_app_tank_level.c:130
acc_detector_distance_get_buffer_sizes
bool acc_detector_distance_get_buffer_sizes(const acc_detector_distance_handle_t *handle, uint32_t *buffer_size, uint32_t *calibration_buffer_size)
Get the buffer sizes needed given the provided detector handle.
app_context_t::calibration_buffer
void * calibration_buffer
Definition: ref_app_tank_level.c:133
acc_detector_distance_recalibrate
bool acc_detector_distance_recalibrate(acc_sensor_t *sensor, acc_detector_distance_handle_t *handle, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size, void *calibration_buffer, uint32_t calibration_buffer_size, bool *calibration_complete)
Do a detector recalibration.
acc_integration.h
acc_detector_distance_result_t::num_distances
uint8_t num_distances
Definition: acc_detector_distance.h:62
swap
static void swap(float *array, uint16_t idx_a, uint16_t idx_b)
Definition: ref_app_tank_level.c:621
acc_detector_distance_create
acc_detector_distance_handle_t * acc_detector_distance_create(const acc_detector_distance_config_t *config)
Create a distance detector with the provided configuration.
LARGE_TANK_PEAK_SORTING
#define LARGE_TANK_PEAK_SORTING
Definition: ref_app_tank_level.c:64
acc_detector_distance_config_destroy
void acc_detector_distance_config_destroy(acc_detector_distance_config_t *config)
Destroy a configuration for a distance detector.
acc_hal_rss_integration_get_implementation
const acc_hal_a121_t * acc_hal_rss_integration_get_implementation(void)
Get hal implementation reference.
Definition: acc_hal_integration_stm32cube_xm.c:166
SMALL_TANK_NUM_FRAMES_REC
#define SMALL_TANK_NUM_FRAMES_REC
Definition: ref_app_tank_level.c:35
app_context_t::mean_edge_status_count
uint16_t mean_edge_status_count
Definition: ref_app_tank_level.c:129
MEDIUM_TANK_MEDIAN_FILTER_LENGTH
#define MEDIUM_TANK_MEDIAN_FILTER_LENGTH
Definition: ref_app_tank_level.c:43
acc_detector_distance_process
bool acc_detector_distance_process(acc_detector_distance_handle_t *handle, void *buffer, void *calibration_buffer, bool *result_available, acc_detector_distance_result_t *result)
Process the data according to the configuration used in acc_detector_distance_config_create.
TANK_LEVEL_PRESET_CONFIG_LARGE_TANK
@ TANK_LEVEL_PRESET_CONFIG_LARGE_TANK
Definition: ref_app_tank_level.c:103
PEAK_STATUS_NO_DETECTION
@ PEAK_STATUS_NO_DETECTION
Definition: ref_app_tank_level.c:140
app_result_t::result_ready
bool result_ready
Definition: ref_app_tank_level.c:150
acc_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_stm32.c:632
LARGE_TANK_RANGE_START_M
#define LARGE_TANK_RANGE_START_M
Definition: ref_app_tank_level.c:59
MEDIUM_TANK_THRESHOLD_SENSITIVITY
#define MEDIUM_TANK_THRESHOLD_SENSITIVITY
Definition: ref_app_tank_level.c:52
acc_ref_app_tank_level_config_t
Definition: ref_app_tank_level.c:109
acc_hal_a121_t
Definition: acc_hal_definitions_a121.h:82
acc_detector_distance_calibrate
bool acc_detector_distance_calibrate(acc_sensor_t *sensor, acc_detector_distance_handle_t *handle, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size, void *calibration_buffer, uint32_t calibration_buffer_size, bool *calibration_complete)
Do a detector calibration.
SMALL_TANK_NUM_MEDIANS_TO_AVERAGE
#define SMALL_TANK_NUM_MEDIANS_TO_AVERAGE
Definition: ref_app_tank_level.c:30
LARGE_TANK_MAX_PROFILE
#define LARGE_TANK_MAX_PROFILE
Definition: ref_app_tank_level.c:62
MEDIUM_TANK_SIGNAL_QUALITY
#define MEDIUM_TANK_SIGNAL_QUALITY
Definition: ref_app_tank_level.c:53
acc_rss_hal_register
bool acc_rss_hal_register(const acc_hal_a121_t *hal)
Register an integration.
acc_integration_set_periodic_wakeup
void acc_integration_set_periodic_wakeup(uint32_t time_msec)
Set up a periodic timer used to wake up the system from sleep.
Definition: acc_integration_stm32.c:531
acc_sensor.h
detector_get_next
static bool detector_get_next(app_context_t *context, const acc_cal_result_t *sensor_cal_result, acc_detector_distance_result_t *detector_result)
Definition: ref_app_tank_level.c:575
acc_detector_distance_config_close_range_leakage_cancellation_set
void acc_detector_distance_config_close_range_leakage_cancellation_set(acc_detector_distance_config_t *config, bool enable)
Enable the close range leakage cancellation logic.
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: ref_app_tank_level.c:210
LARGE_TANK_REFLECTOR_SHAPE
#define LARGE_TANK_REFLECTOR_SHAPE
Definition: ref_app_tank_level.c:65
acc_detector_distance_config_max_profile_set
void acc_detector_distance_config_max_profile_set(acc_detector_distance_config_t *config, acc_config_profile_t max_profile)
Set the max profile.
acc_detector_distance_destroy
void acc_detector_distance_destroy(acc_detector_distance_handle_t *handle)
Destroy the distance detector handle, freeing its resources.
acc_hal_integration_wait_for_sensor_interrupt
bool acc_hal_integration_wait_for_sensor_interrupt(acc_sensor_id_t sensor_id, uint32_t timeout_ms)
Wait for a sensor interrupt.
Definition: acc_hal_integration_stm32cube_xm.c:142
acc_hal_integration_a121.h
acc_detector_distance_config_signal_quality_set
void acc_detector_distance_config_signal_quality_set(acc_detector_distance_config_t *config, float signal_quality)
Set the signal quality.
acc_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
app_context_t::median_vector_length
uint16_t median_vector_length
Definition: ref_app_tank_level.c:125
printf
#define printf
Definition: printf.h:60
sensor_calibration
static bool sensor_calibration(acc_sensor_t *sensor, acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size)
Definition: ref_app_tank_level.c:488
acc_hal_integration_sensor_enable
void acc_hal_integration_sensor_enable(acc_sensor_id_t sensor_id)
Enable sensor.
Definition: acc_hal_integration_stm32cube_xm.c:119
LARGE_TANK_NUM_FRAMES_REC
#define LARGE_TANK_NUM_FRAMES_REC
Definition: ref_app_tank_level.c:63
acc_hal_definitions_a121.h
SMALL_TANK_THRESHOLD_SENSITIVITY
#define SMALL_TANK_THRESHOLD_SENSITIVITY
Definition: ref_app_tank_level.c:38
acc_ref_app_tank_level_config_t::num_medians_to_average
uint16_t num_medians_to_average
Definition: ref_app_tank_level.c:114
TANK_LEVEL_PRESET_CONFIG_NONE
@ TANK_LEVEL_PRESET_CONFIG_NONE
Definition: ref_app_tank_level.c:100
acc_detector_distance_prepare
bool acc_detector_distance_prepare(const acc_detector_distance_handle_t *handle, const acc_detector_distance_config_t *config, acc_sensor_t *sensor, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size)
Prepare the detector for measurements.
acc_ref_app_tank_level_config_t::distance_config
acc_detector_distance_config_t * distance_config
Definition: ref_app_tank_level.c:115
acc_detector_distance_result_t
Distance detector result.
Definition: acc_detector_distance.h:49
SMALL_TANK_MAX_PROFILE
#define SMALL_TANK_MAX_PROFILE
Definition: ref_app_tank_level.c:34
app_context_t::calibration_buffer_size
uint32_t calibration_buffer_size
Definition: ref_app_tank_level.c:134
MEDIUM_TANK_RANGE_END_M
#define MEDIUM_TANK_RANGE_END_M
Definition: ref_app_tank_level.c:46
MEDIUM_TANK_REFLECTOR_SHAPE
#define MEDIUM_TANK_REFLECTOR_SHAPE
Definition: ref_app_tank_level.c:51
MEDIUM_TANK_RANGE_START_M
#define MEDIUM_TANK_RANGE_START_M
Definition: ref_app_tank_level.c:45
acc_hal_integration_sensor_disable
void acc_hal_integration_sensor_disable(acc_sensor_id_t sensor_id)
Disable sensor.
Definition: acc_hal_integration_stm32cube_xm.c:130
sort
static void sort(float *array, uint16_t array_length)
Definition: ref_app_tank_level.c:630
acc_detector_distance_config_end_set
void acc_detector_distance_config_end_set(acc_detector_distance_config_t *config, float end_m)
Set the end of measured interval in meters.
app_context_t::median_edge_status_count
uint16_t median_edge_status_count
Definition: ref_app_tank_level.c:128
acc_detector_distance_result_t::sensor_calibration_needed
bool sensor_calibration_needed
Definition: acc_detector_distance.h:74
DEFAULT_UPDATE_RATE
#define DEFAULT_UPDATE_RATE
Definition: ref_app_tank_level.c:26
initialize_application_resources
static bool initialize_application_resources(app_context_t *context)
Definition: ref_app_tank_level.c:432
cleanup
static void cleanup(app_context_t *context)
Definition: ref_app_tank_level.c:334
acc_detector_distance_config_num_frames_recorded_threshold_set
void acc_detector_distance_config_num_frames_recorded_threshold_set(acc_detector_distance_config_t *config, uint16_t num_frames)
Set the number frames to use for recorded threshold.
acc_integration_log.h
LARGE_TANK_NUM_MEDIANS_TO_AVERAGE
#define LARGE_TANK_NUM_MEDIANS_TO_AVERAGE
Definition: ref_app_tank_level.c:58
acc_detector_distance_handle_t
struct acc_detector_distance_handle acc_detector_distance_handle_t
Definition: acc_detector_distance.h:35
LARGE_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
#define LARGE_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
Definition: ref_app_tank_level.c:68
SMALL_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
#define SMALL_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
Definition: ref_app_tank_level.c:40
tank_level_preset_config_t
tank_level_preset_config_t
Definition: ref_app_tank_level.c:98
ACC_LOG_FLOAT_TO_INTEGER
#define ACC_LOG_FLOAT_TO_INTEGER(a)
Definition: acc_integration_log.h:26
SENSOR_ID
#define SENSOR_ID
Definition: ref_app_tank_level.c:24
LARGE_TANK_RANGE_END_M
#define LARGE_TANK_RANGE_END_M
Definition: ref_app_tank_level.c:60
acc_sensor_status
void acc_sensor_status(const acc_sensor_t *sensor)
Check the status of the sensor.
acc_detector_distance_result_t::near_start_edge_status
bool near_start_edge_status
Definition: acc_detector_distance.h:66
acc_integration_mem_free
void acc_integration_mem_free(void *ptr)
Free dynamic memory.
Definition: acc_integration_stm32.c:644
acc_detector_distance_config_reflector_shape_set
void acc_detector_distance_config_reflector_shape_set(acc_detector_distance_config_t *config, acc_detector_distance_reflector_shape_t reflector_shape)
Set reflector shape.
MEDIUM_TANK_NUM_FRAMES_REC
#define MEDIUM_TANK_NUM_FRAMES_REC
Definition: ref_app_tank_level.c:49
acc_integration_sleep_until_periodic_wakeup
void acc_integration_sleep_until_periodic_wakeup(void)
Put the system in sleep until the periodic timer triggers.
Definition: acc_integration_stm32.c:590
app_context_t::sensor
acc_sensor_t * sensor
Definition: ref_app_smart_presence.c:155
acc_detector_distance_config_threshold_sensitivity_set
void acc_detector_distance_config_threshold_sensitivity_set(acc_detector_distance_config_t *config, float threshold_sensitivity)
Set threshold sensitivity.
acc_detector_distance_config_create
acc_detector_distance_config_t * acc_detector_distance_config_create(void)
Create a configuration for a distance detector.
SMALL_TANK_MEDIAN_FILTER_LENGTH
#define SMALL_TANK_MEDIAN_FILTER_LENGTH
Definition: ref_app_tank_level.c:29
app_context_t::level_history
float * level_history
Definition: ref_app_tank_level.c:122
acc_detector_distance_result_t::distances
float distances[(10U)]
Definition: acc_detector_distance.h:54
acc_detector_distance.h
acc_sensor_calibrate
bool acc_sensor_calibrate(acc_sensor_t *sensor, bool *cal_complete, acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size)
Calibrate a sensor.
PRIfloat
#define PRIfloat
Specifier for printing float type using integers.
Definition: acc_integration_log.h:31
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: ref_app_tank_level.c:25
app_result_t
Definition: ref_app_tank_level.c:145
acc_detector_distance_config_t
struct acc_detector_distance_config acc_detector_distance_config_t
Definition: acc_detector_distance.h:43
LARGE_TANK_THRESHOLD_SENSITIVITY
#define LARGE_TANK_THRESHOLD_SENSITIVITY
Definition: ref_app_tank_level.c:66
LARGE_TANK_MEDIAN_FILTER_LENGTH
#define LARGE_TANK_MEDIAN_FILTER_LENGTH
Definition: ref_app_tank_level.c:57
acc_detector_distance_config_max_step_length_set
void acc_detector_distance_config_max_step_length_set(acc_detector_distance_config_t *config, uint16_t max_step_length)
Set the maximum step length.
print_result
static void print_result(const app_result_t *app_result, const acc_ref_app_tank_level_config_t *app_config)
Definition: ref_app_tank_level.c:775
peak_status_t
peak_status_t
Definition: ref_app_tank_level.c:137
acc_sensor_measure
bool acc_sensor_measure(acc_sensor_t *sensor)
Start a radar measurement with previously prepared configuration.
get_detector_start_m
static float get_detector_start_m(acc_ref_app_tank_level_config_t *app_config)
Definition: ref_app_tank_level.c:354
detector_recalibration
static bool detector_recalibration(app_context_t *context, const acc_cal_result_t *sensor_cal_result)
Definition: ref_app_tank_level.c:553
PEAK_STATUS_OUT_OF_RANGE
@ PEAK_STATUS_OUT_OF_RANGE
Definition: ref_app_tank_level.c:142
MEDIUM_TANK_NUM_MEDIANS_TO_AVERAGE
#define MEDIUM_TANK_NUM_MEDIANS_TO_AVERAGE
Definition: ref_app_tank_level.c:44
acc_sensor_t
struct acc_sensor acc_sensor_t
Definition: acc_sensor.h:31
DEFAULT_PRESET_CONFIG
#define DEFAULT_PRESET_CONFIG
Definition: ref_app_tank_level.c:106
app_context_t::median_vector
float * median_vector
Definition: ref_app_tank_level.c:124
MEDIUM_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
#define MEDIUM_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
Definition: ref_app_tank_level.c:54
SMALL_TANK_REFLECTOR_SHAPE
#define SMALL_TANK_REFLECTOR_SHAPE
Definition: ref_app_tank_level.c:37
acc_sensor_destroy
void acc_sensor_destroy(acc_sensor_t *sensor)
Destroy a sensor instance freeing any resources allocated.
acc_definitions_a121.h
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.
LARGE_TANK_MAX_STEP_LENGTH
#define LARGE_TANK_MAX_STEP_LENGTH
Definition: ref_app_tank_level.c:61