ref_app_tank_level.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2023-2025
2 // All rights reserved
3 // This file is subject to the terms and conditions defined in the file
4 // 'LICENSES/license_acconeer.txt', (BSD 3-Clause License) which is part
5 // of this source code package.
6 
7 #include <math.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 
13 #include "acc_algorithm.h"
14 #include "acc_definitions_a121.h"
15 #include "acc_detector_distance.h"
18 #include "acc_integration.h"
19 #include "acc_integration_log.h"
20 #include "acc_rss_a121.h"
21 #include "acc_sensor.h"
22 #include "acc_version.h"
23 
24 #define SENSOR_ID 1U
25 #define SENSOR_TIMEOUT_MS 1000U
26 #define DEFAULT_UPDATE_RATE 1.0f
27 #define CLOSE_RANGE_START 0.07f
28 
29 // Settings for a small tank
30 #define SMALL_TANK_MEDIAN_FILTER_LENGTH 3U
31 #define SMALL_TANK_NUM_MEDIANS_TO_AVERAGE 2U
32 #define SMALL_TANK_RANGE_START_M 0.03f
33 #define SMALL_TANK_RANGE_END_M 0.5f
34 #define SMALL_TANK_MAX_STEP_LENGTH 12U
35 #define SMALL_TANK_MAX_PROFILE ACC_CONFIG_PROFILE_3
36 #define SMALL_TANK_NUM_FRAMES_REC 50U
37 #define SMALL_TANK_PEAK_SORTING ACC_DETECTOR_DISTANCE_PEAK_SORTING_CLOSEST
38 #define SMALL_TANK_REFLECTOR_SHAPE ACC_DETECTOR_DISTANCE_REFLECTOR_SHAPE_PLANAR
39 #define SMALL_TANK_THRESHOLD_METHOD ACC_DETECTOR_DISTANCE_THRESHOLD_METHOD_CFAR
40 #define SMALL_TANK_FIXED_AMPLITUDE_THRESHOLD 100.0f
41 #define SMALL_TANK_FIXED_STRENGTH_THRESHOLD 0.0f
42 #define SMALL_TANK_THRESHOLD_SENSITIVITY 0.0f
43 #define SMALL_TANK_SIGNAL_QUALITY 3.0f
44 #define SMALL_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION false
45 
46 // Settings for a medium tank
47 #define MEDIUM_TANK_MEDIAN_FILTER_LENGTH 3U
48 #define MEDIUM_TANK_NUM_MEDIANS_TO_AVERAGE 1U
49 #define MEDIUM_TANK_RANGE_START_M 0.05f
50 #define MEDIUM_TANK_RANGE_END_M 6.0f
51 #define MEDIUM_TANK_MAX_STEP_LENGTH 0U // 0 means no limit
52 #define MEDIUM_TANK_MAX_PROFILE ACC_CONFIG_PROFILE_5
53 #define MEDIUM_TANK_NUM_FRAMES_REC 50U
54 #define MEDIUM_TANK_PEAK_SORTING ACC_DETECTOR_DISTANCE_PEAK_SORTING_STRONGEST
55 #define MEDIUM_TANK_REFLECTOR_SHAPE ACC_DETECTOR_DISTANCE_REFLECTOR_SHAPE_PLANAR
56 #define MEDIUM_TANK_THRESHOLD_METHOD ACC_DETECTOR_DISTANCE_THRESHOLD_METHOD_CFAR
57 #define MEDIUM_TANK_FIXED_AMPLITUDE_THRESHOLD 100.0f
58 #define MEDIUM_TANK_FIXED_STRENGTH_THRESHOLD 3.0f
59 #define MEDIUM_TANK_THRESHOLD_SENSITIVITY 0.0f
60 #define MEDIUM_TANK_SIGNAL_QUALITY 19.0f
61 #define MEDIUM_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION false
62 
63 // Settings for a large tank
64 #define LARGE_TANK_MEDIAN_FILTER_LENGTH 1U
65 #define LARGE_TANK_NUM_MEDIANS_TO_AVERAGE 1U
66 #define LARGE_TANK_RANGE_START_M 0.1f
67 #define LARGE_TANK_RANGE_END_M 15.0f
68 #define LARGE_TANK_MAX_STEP_LENGTH 0U // 0 means no limit
69 #define LARGE_TANK_MAX_PROFILE ACC_CONFIG_PROFILE_5
70 #define LARGE_TANK_NUM_FRAMES_REC 50U
71 #define LARGE_TANK_PEAK_SORTING ACC_DETECTOR_DISTANCE_PEAK_SORTING_STRONGEST
72 #define LARGE_TANK_REFLECTOR_SHAPE ACC_DETECTOR_DISTANCE_REFLECTOR_SHAPE_PLANAR
73 #define LARGE_TANK_THRESHOLD_METHOD ACC_DETECTOR_DISTANCE_THRESHOLD_METHOD_CFAR
74 #define LARGE_TANK_FIXED_AMPLITUDE_THRESHOLD 100.0f
75 #define LARGE_TANK_FIXED_STRENGTH_THRESHOLD 5.0f
76 #define LARGE_TANK_THRESHOLD_SENSITIVITY 0.0f
77 #define LARGE_TANK_SIGNAL_QUALITY 20.0f
78 #define LARGE_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION false
79 
80 /** \example ref_app_tank_level.c
81  * @brief This is a reference application for measuring liquid level in tanks
82  * @n
83  * This reference application executes as follows:
84  * - Retrieve HAL integration
85  * - Initialize application resources:
86  * + Create application configuration
87  * + Create distance detector configuration
88  * + Update configuration settings
89  * + Create distance detector handle
90  * + Create buffer for detector calibration data
91  * + Create buffer for sensor data
92  * - Create and calibrate the sensor
93  * - Calibrate the detector
94  * - Measure distances with the detector (loop):
95  * + Prepare sensor with the detector
96  * + Measure and wait until a read can be done
97  * + Process sensor measurement and get distance detector result
98  * + Process distance detector result and print the result
99  * + Handle "calibration_needed" indication
100  * - Cleanup:
101  * + Destroy detector configuration
102  * + Destroy detector handle
103  * + Destroy sensor data buffer
104  * + Destroy detector calibration data buffer
105  */
106 
107 typedef enum
108 {
114 
115 #define DEFAULT_PRESET_CONFIG TANK_LEVEL_PRESET_CONFIG_SMALL_TANK
116 
117 typedef struct
118 {
125 
126 typedef struct
127 {
134  uint16_t median_counter;
135  uint16_t mean_counter;
139  void *buffer;
140  uint32_t buffer_size;
144 } app_context_t;
145 
146 typedef enum
147 {
152 } peak_status_t;
153 
154 typedef struct
155 {
158  float level;
160 } app_result_t;
161 
162 static void cleanup(app_context_t *context);
163 
164 static float get_detector_start_m(acc_ref_app_tank_level_config_t *app_config);
165 
166 static float get_detector_end_m(acc_ref_app_tank_level_config_t *app_config);
167 
169 
170 static bool initialize_application_resources(app_context_t *context);
171 
172 static bool sensor_calibration(acc_sensor_t *sensor, acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size);
173 
175 
177 
179 
180 static float median(float *array, uint16_t array_length);
181 
182 static float nanmean(float *array, uint16_t array_length);
183 
184 static void process_detector_result(const acc_detector_distance_result_t *distance_result, app_result_t *app_result, app_context_t *context);
185 
186 static void print_result(const app_result_t *app_result, const acc_ref_app_tank_level_config_t *app_config);
187 
188 int acconeer_main(int argc, char *argv[]);
189 
190 int acconeer_main(int argc, char *argv[])
191 {
192  (void)argc;
193  (void)argv;
194  app_context_t context = {0};
195  acc_ref_app_tank_level_config_t app_config = {0};
196  context.app_config = &app_config;
197  context.sensor = NULL;
198 
199  printf("Acconeer software version %s\n", acc_version_get());
200 
202 
203  if (!acc_rss_hal_register(hal))
204  {
205  return EXIT_FAILURE;
206  }
207 
208  context.app_config->distance_config = acc_detector_distance_config_create();
209  if (context.app_config->distance_config == NULL)
210  {
211  printf("acc_detector_distance_config_create() failed\n");
212  cleanup(&context);
213  return EXIT_FAILURE;
214  }
215 
216  set_config(&app_config, DEFAULT_PRESET_CONFIG);
217 
218  uint32_t sleep_time_ms = (uint32_t)(1000.0f / DEFAULT_UPDATE_RATE);
219 
221 
222  if (!initialize_application_resources(&context))
223  {
224  printf("Initializing detector context failed\n");
225  cleanup(&context);
226  return EXIT_FAILURE;
227  }
228 
229  /* Turn the sensor on */
232 
234  if (context.sensor == NULL)
235  {
236  printf("acc_sensor_create() failed\n");
237  cleanup(&context);
238  return EXIT_FAILURE;
239  }
240 
241  acc_cal_result_t cal_result;
242 
243  if (!sensor_calibration(context.sensor, &cal_result, context.buffer, context.buffer_size))
244  {
245  printf("Sensor calibration failed\n");
246  cleanup(&context);
247  return EXIT_FAILURE;
248  }
249 
250  if (!full_detector_calibration(&context, &cal_result))
251  {
252  printf("Detector calibration failed\n");
253  cleanup(&context);
254  return EXIT_FAILURE;
255  }
256 
257  while (true)
258  {
259  acc_detector_distance_result_t detector_result = {0};
260  app_result_t app_result = {0};
261 
262  if (!detector_get_next(&context, &cal_result, &detector_result))
263  {
264  printf("Could not get next result\n");
265  cleanup(&context);
266  return EXIT_FAILURE;
267  }
268 
269  process_detector_result(&detector_result, &app_result, &context);
270 
271  /* If "calibration needed" is indicated, the sensor needs to be recalibrated and the detector calibration updated */
272  if (detector_result.calibration_needed)
273  {
274  printf("Sensor recalibration and detector calibration update needed ... \n");
275 
276  if (!sensor_calibration(context.sensor, &cal_result, context.buffer, context.buffer_size))
277  {
278  printf("Sensor calibration failed\n");
279  cleanup(&context);
280  return EXIT_FAILURE;
281  }
282 
283  /* Once the sensor is recalibrated, the detector calibration should be updated and measuring can continue. */
284  if (!update_detector_calibration(&context, &cal_result))
285  {
286  printf("Detector calibration update failed\n");
287  cleanup(&context);
288  return EXIT_FAILURE;
289  }
290 
291  printf("Sensor recalibration and detector calibration update done!\n");
292  }
293 
294  if (app_result.result_ready)
295  {
297 
298  print_result(&app_result, context.app_config);
299 
301 
303  }
304  }
305 
306  cleanup(&context);
307 
308  printf("Application finished OK\n");
309 
310  return EXIT_SUCCESS;
311 }
312 
313 static void cleanup(app_context_t *context)
314 {
317 
318  acc_detector_distance_config_destroy(context->app_config->distance_config);
320 
325 
326  if (context->sensor != NULL)
327  {
328  acc_sensor_destroy(context->sensor);
329  }
330 }
331 
333 {
334  // Decrease start point by 15 mm to make sure that start of the tank is fully covered
335  return app_config->tank_range_start_m - 0.015f;
336 }
337 
339 {
340  // Increase end point by 5% to make sure that the bottom of the tank is fully covered
341  return fminf(app_config->tank_range_end_m * 1.05f, 23.0f);
342 }
343 
345 {
346  switch (preset)
347  {
349  break;
355 
370  break;
376 
391  break;
397 
412  break;
413  }
414 }
415 
417 {
418  context->detector_handle = acc_detector_distance_create(context->app_config->distance_config);
419  if (context->detector_handle == NULL)
420  {
421  printf("acc_detector_distance_create() failed\n");
422  return false;
423  }
424 
426  {
427  printf("acc_detector_distance_get_sizes() failed\n");
428  return false;
429  }
430 
431  context->buffer = acc_integration_mem_alloc(context->buffer_size);
432  if (context->buffer == NULL)
433  {
434  printf("sensor buffer allocation failed\n");
435  return false;
436  }
437 
439  if (context->detector_cal_result_static == NULL)
440  {
441  printf("calibration buffer allocation failed\n");
442  return false;
443  }
444 
445  context->level_history_length = context->app_config->median_filter_length;
446 
447  context->level_history = acc_integration_mem_alloc(context->level_history_length * sizeof(*context->level_history));
448  if (context->level_history == NULL)
449  {
450  printf("level history buffer allocation failed\n");
451  return false;
452  }
453 
454  context->median_vector_length = context->app_config->num_medians_to_average;
455 
456  context->median_vector = acc_integration_mem_alloc(context->median_vector_length * sizeof(*context->median_vector));
457  if (context->median_vector == NULL)
458  {
459  printf("median vector allocation failed\n");
460  return false;
461  }
462 
463  context->median_counter = 0U;
464  context->mean_counter = 0U;
465  context->median_edge_status_count = 0U;
466  context->mean_edge_status_count = 0U;
467 
468  return true;
469 }
470 
472 {
473  bool status = false;
474  bool cal_complete = false;
475  const uint16_t calibration_retries = 1U;
476 
477  // Random disturbances may cause the calibration to fail. At failure, retry at least once.
478  for (uint16_t i = 0; !status && (i <= calibration_retries); i++)
479  {
480  // Reset sensor before calibration by disabling/enabling it
483 
484  do
485  {
486  status = acc_sensor_calibrate(sensor, &cal_complete, sensor_cal_result, buffer, buffer_size);
487 
488  if (status && !cal_complete)
489  {
491  }
492  } while (status && !cal_complete);
493  }
494 
495  if (status)
496  {
497  /* Reset sensor after calibration by disabling/enabling it */
500  }
501  else
502  {
503  printf("acc_sensor_calibrate() failed\n");
505  }
506 
507  return status;
508 }
509 
511 {
512  bool done = false;
513  bool status;
514 
515  do
516  {
517  status = acc_detector_distance_calibrate(context->sensor,
518  context->detector_handle,
520  context->buffer,
521  context->buffer_size,
524  &context->detector_cal_result_dynamic,
525  &done);
526  if (done)
527  {
528  break;
529  }
530 
531  if (status)
532  {
534  }
535  } while (status);
536 
537  return status;
538 }
539 
541 {
542  bool done = false;
543  bool status;
544 
545  do
546  {
548  context->detector_handle,
550  context->buffer,
551  context->buffer_size,
552  &context->detector_cal_result_dynamic,
553  &done);
554  if (done)
555  {
556  break;
557  }
558 
559  if (status)
560  {
562  }
563  } while (status);
564 
565  return status;
566 }
567 
569 {
570  bool result_available = false;
571 
572  do
573  {
575  context->app_config->distance_config,
576  context->sensor,
578  context->buffer,
579  context->buffer_size))
580  {
581  printf("acc_detector_distance_prepare() failed\n");
582  return false;
583  }
584 
585  if (!acc_sensor_measure(context->sensor))
586  {
587  printf("acc_sensor_measure() failed\n");
588  return false;
589  }
590 
592  {
593  printf("Sensor interrupt timeout\n");
594  return false;
595  }
596 
597  if (!acc_sensor_read(context->sensor, context->buffer, context->buffer_size))
598  {
599  printf("acc_sensor_read() failed\n");
600  return false;
601  }
602 
604  context->buffer,
606  &context->detector_cal_result_dynamic,
607  &result_available,
608  detector_result))
609  {
610  printf("acc_detector_distance_process() failed\n");
611  return false;
612  }
613  } while (!result_available);
614 
615  return true;
616 }
617 
618 static float median(float *array, uint16_t array_length)
619 {
620  bool status = true;
621  float result = 0.0f;
622 
623  for (uint16_t i = 0U; i < array_length && status; i++)
624  {
625  if (isnan(array[i]))
626  {
627  result = (float)NAN;
628  status = false;
629  }
630  }
631 
632  if (status)
633  {
634  result = acc_algorithm_median_f32(array, array_length);
635  }
636 
637  return result;
638 }
639 
640 static float nanmean(float *array, uint16_t array_length)
641 {
642  uint16_t samples = 0U;
643  float sum = 0.0f;
644 
645  for (uint16_t i = 0U; i < array_length; i++)
646  {
647  if (!isnan(array[i]))
648  {
649  samples++;
650  sum += array[i];
651  }
652  }
653 
654  return samples > 0U ? sum / (float)samples : (float)NAN;
655 }
656 
657 static void process_detector_result(const acc_detector_distance_result_t *distance_result, app_result_t *app_result, app_context_t *context)
658 {
659  app_result->peak_detected = false;
661  app_result->level = 0.0f;
662  app_result->result_ready = false;
663  float level = 0.0f;
664 
665  if (distance_result->num_distances > 0)
666  {
667  app_result->peak_detected = true;
668  level = context->app_config->tank_range_end_m - distance_result->distances[0];
669  }
670  else
671  {
672  level = (float)NAN;
673  }
674 
675  if (distance_result->near_start_edge_status)
676  {
677  context->median_edge_status_count++;
678  }
679 
680  context->level_history[context->median_counter++] = level;
681 
682  if (context->median_counter == context->level_history_length)
683  {
684  float med = median(context->level_history, context->level_history_length);
685 
686  context->median_vector[context->mean_counter++] = med;
687 
688  if (context->median_edge_status_count > context->level_history_length / 2)
689  {
690  context->mean_edge_status_count++;
691  }
692 
693  context->median_counter = 0U;
694  context->median_edge_status_count = 0U;
695  }
696 
697  if (context->mean_counter == context->median_vector_length)
698  {
699  level = nanmean(context->median_vector, context->median_vector_length);
700 
701  if (!isnan(level))
702  {
703  if (level < 0.0f)
704  {
706  }
707  else if ((level > (context->app_config->tank_range_end_m - context->app_config->tank_range_start_m)) &&
708  (context->app_config->tank_range_start_m >= CLOSE_RANGE_START))
709  {
710  app_result->peak_status = PEAK_STATUS_OVERFLOW;
711  }
712  else
713  {
714  app_result->peak_status = PEAK_STATUS_IN_RANGE;
715  app_result->level = level;
716  }
717  }
718  else if ((context->mean_edge_status_count > context->median_vector_length / 2) &&
719  (context->app_config->tank_range_start_m >= CLOSE_RANGE_START))
720  {
721  app_result->peak_status = PEAK_STATUS_OVERFLOW;
722  }
723 
724  if (app_result->peak_status == PEAK_STATUS_OVERFLOW || app_result->peak_status == PEAK_STATUS_OUT_OF_RANGE)
725  {
726  app_result->level = (float)NAN;
727  }
728 
729  app_result->result_ready = true;
730  context->mean_counter = 0U;
731  context->mean_edge_status_count = 0U;
732  }
733 }
734 
735 static void print_result(const app_result_t *app_result, const acc_ref_app_tank_level_config_t *app_config)
736 {
737  switch (app_result->peak_status)
738  {
740  printf("Level within range\n");
741  printf("Level: %" PRIfloat " cm, %" PRIfloat " %%\n",
742  ACC_LOG_FLOAT_TO_INTEGER(app_result->level * 100.0f),
743  ACC_LOG_FLOAT_TO_INTEGER((app_result->level / (app_config->tank_range_end_m - app_config->tank_range_start_m) * 100.f)));
744  break;
746  printf("No detection\n");
747  break;
749  printf("Overflow!\n");
750  break;
752  printf("Out of range\n");
753  break;
754  default:
755  break;
756  }
757 }
app_result_t::level
float level
Definition: ref_app_tank_level.c:158
SMALL_TANK_FIXED_AMPLITUDE_THRESHOLD
#define SMALL_TANK_FIXED_AMPLITUDE_THRESHOLD
Definition: ref_app_tank_level.c:40
SMALL_TANK_RANGE_START_M
#define SMALL_TANK_RANGE_START_M
Definition: ref_app_tank_level.c:32
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:510
SMALL_TANK_SIGNAL_QUALITY
#define SMALL_TANK_SIGNAL_QUALITY
Definition: ref_app_tank_level.c:43
app_context_t::median_counter
uint16_t median_counter
Definition: ref_app_tank_level.c:134
app_context_t::buffer_size
uint32_t buffer_size
Definition: ref_app_tank_level.c:140
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:104
app_context_t::level_history_length
uint16_t level_history_length
Definition: ref_app_tank_level.c:131
acc_rss_a121.h
PEAK_STATUS_IN_RANGE
@ PEAK_STATUS_IN_RANGE
Definition: ref_app_tank_level.c:148
TANK_LEVEL_PRESET_CONFIG_MEDIUM_TANK
@ TANK_LEVEL_PRESET_CONFIG_MEDIUM_TANK
Definition: ref_app_tank_level.c:111
app_context_t::detector_cal_result_dynamic
acc_detector_cal_result_dynamic_t detector_cal_result_dynamic
Definition: ref_app_tank_level.c:143
acc_detector_distance_get_sizes
bool acc_detector_distance_get_sizes(const acc_detector_distance_handle_t *handle, uint32_t *buffer_size, uint32_t *detector_cal_result_static_size)
Get the memory size needed to use the Distance Detector API functions.
MEDIUM_TANK_FIXED_STRENGTH_THRESHOLD
#define MEDIUM_TANK_FIXED_STRENGTH_THRESHOLD
Definition: ref_app_tank_level.c:58
acc_ref_app_tank_level_config_t::median_filter_length
uint16_t median_filter_length
Definition: ref_app_tank_level.c:121
app_context_t::app_config
acc_ref_app_smart_presence_config_t * app_config
Definition: ref_app_smart_presence.c:145
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 peak sorting method.
app_result_t::peak_status
peak_status_t peak_status
Definition: ref_app_tank_level.c:157
app_context_t::mean_counter
uint16_t mean_counter
Definition: ref_app_tank_level.c:135
MEDIUM_TANK_THRESHOLD_METHOD
#define MEDIUM_TANK_THRESHOLD_METHOD
Definition: ref_app_tank_level.c:56
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:99
nanmean
static float nanmean(float *array, uint16_t array_length)
Definition: ref_app_tank_level.c:640
TANK_LEVEL_PRESET_CONFIG_SMALL_TANK
@ TANK_LEVEL_PRESET_CONFIG_SMALL_TANK
Definition: ref_app_tank_level.c:110
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
MEDIUM_TANK_FIXED_AMPLITUDE_THRESHOLD
#define MEDIUM_TANK_FIXED_AMPLITUDE_THRESHOLD
Definition: ref_app_tank_level.c:57
LARGE_TANK_SIGNAL_QUALITY
#define LARGE_TANK_SIGNAL_QUALITY
Definition: ref_app_tank_level.c:77
buffer
void * buffer
Definition: i2c_example_cargo.c:40
acc_version.h
sensor_cal_result
acc_cal_result_t sensor_cal_result
Definition: i2c_example_cargo.c:36
acc_ref_app_tank_level_config_t::tank_range_end_m
float tank_range_end_m
Definition: ref_app_tank_level.c:120
SMALL_TANK_MAX_STEP_LENGTH
#define SMALL_TANK_MAX_STEP_LENGTH
Definition: ref_app_tank_level.c:34
app_context_t::buffer
void * buffer
Definition: ref_app_smart_presence.c:153
PEAK_STATUS_OVERFLOW
@ PEAK_STATUS_OVERFLOW
Definition: ref_app_tank_level.c:150
app_context_t
Definition: ref_app_smart_presence.c:143
app_result_t::peak_detected
bool peak_detected
Definition: ref_app_tank_level.c:156
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:657
SMALL_TANK_PEAK_SORTING
#define SMALL_TANK_PEAK_SORTING
Definition: ref_app_tank_level.c:37
acc_ref_app_tank_level_config_t::tank_range_start_m
float tank_range_start_m
Definition: ref_app_tank_level.c:119
acc_detector_distance_config_threshold_method_set
void acc_detector_distance_config_threshold_method_set(acc_detector_distance_config_t *config, acc_detector_distance_threshold_method_t threshold_method)
Set threshold method.
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:344
MEDIUM_TANK_MAX_STEP_LENGTH
#define MEDIUM_TANK_MAX_STEP_LENGTH
Definition: ref_app_tank_level.c:51
acc_cal_result_t
The result from a completed calibration.
Definition: acc_definitions_a121.h:30
acc_detector_distance_config_start_set
void acc_detector_distance_config_start_set(acc_detector_distance_config_t *config, float start_m)
Set start of measured interval (m)
MEDIUM_TANK_MAX_PROFILE
#define MEDIUM_TANK_MAX_PROFILE
Definition: ref_app_tank_level.c:52
acc_detector_distance_config_fixed_amplitude_threshold_value_set
void acc_detector_distance_config_fixed_amplitude_threshold_value_set(acc_detector_distance_config_t *config, float fixed_threshold_value)
Set fixed amplitude threshold value.
SMALL_TANK_RANGE_END_M
#define SMALL_TANK_RANGE_END_M
Definition: ref_app_tank_level.c:33
MEDIUM_TANK_PEAK_SORTING
#define MEDIUM_TANK_PEAK_SORTING
Definition: ref_app_tank_level.c:54
app_context_t::app_config
acc_ref_app_tank_level_config_t * app_config
Definition: ref_app_tank_level.c:128
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:338
median
static float median(float *array, uint16_t array_length)
Definition: ref_app_tank_level.c:618
app_context_t::detector_handle
acc_detector_distance_handle_t * detector_handle
Definition: ref_app_tank_level.c:138
acc_integration.h
acc_detector_distance_result_t::num_distances
uint8_t num_distances
Definition: acc_detector_distance.h:70
acc_detector_distance_create
acc_detector_distance_handle_t * acc_detector_distance_create(const acc_detector_distance_config_t *config)
Create Distance Detector handle using the provided Distance Detector configuration.
LARGE_TANK_PEAK_SORTING
#define LARGE_TANK_PEAK_SORTING
Definition: ref_app_tank_level.c:71
LARGE_TANK_FIXED_AMPLITUDE_THRESHOLD
#define LARGE_TANK_FIXED_AMPLITUDE_THRESHOLD
Definition: ref_app_tank_level.c:74
acc_detector_distance_config_destroy
void acc_detector_distance_config_destroy(acc_detector_distance_config_t *config)
Destroy Distance Detector configuration.
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:152
SMALL_TANK_NUM_FRAMES_REC
#define SMALL_TANK_NUM_FRAMES_REC
Definition: ref_app_tank_level.c:36
app_context_t::mean_edge_status_count
uint16_t mean_edge_status_count
Definition: ref_app_tank_level.c:137
MEDIUM_TANK_MEDIAN_FILTER_LENGTH
#define MEDIUM_TANK_MEDIAN_FILTER_LENGTH
Definition: ref_app_tank_level.c:47
SMALL_TANK_FIXED_STRENGTH_THRESHOLD
#define SMALL_TANK_FIXED_STRENGTH_THRESHOLD
Definition: ref_app_tank_level.c:41
TANK_LEVEL_PRESET_CONFIG_LARGE_TANK
@ TANK_LEVEL_PRESET_CONFIG_LARGE_TANK
Definition: ref_app_tank_level.c:112
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, uint8_t *detector_cal_result_static, uint32_t detector_cal_result_static_size, acc_detector_cal_result_dynamic_t *detector_cal_result_dynamic, bool *calibration_complete)
Calibrate detector.
SMALL_TANK_THRESHOLD_METHOD
#define SMALL_TANK_THRESHOLD_METHOD
Definition: ref_app_tank_level.c:39
app_context_t::detector_cal_result_static
uint8_t * detector_cal_result_static
Definition: ref_app_tank_level.c:141
PEAK_STATUS_NO_DETECTION
@ PEAK_STATUS_NO_DETECTION
Definition: ref_app_tank_level.c:149
app_result_t::result_ready
bool result_ready
Definition: ref_app_tank_level.c:159
acc_detector_distance_process
bool acc_detector_distance_process(acc_detector_distance_handle_t *handle, void *buffer, uint8_t *detector_cal_result_static, acc_detector_cal_result_dynamic_t *detector_cal_result_dynamic, bool *result_available, acc_detector_distance_result_t *result)
Process sensor data into a Detector result.
acc_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_stm32.c:592
LARGE_TANK_RANGE_START_M
#define LARGE_TANK_RANGE_START_M
Definition: ref_app_tank_level.c:66
MEDIUM_TANK_THRESHOLD_SENSITIVITY
#define MEDIUM_TANK_THRESHOLD_SENSITIVITY
Definition: ref_app_tank_level.c:59
acc_ref_app_tank_level_config_t
Definition: ref_app_tank_level.c:117
acc_hal_a121_t
Definition: acc_hal_definitions_a121.h:82
update_detector_calibration
static bool update_detector_calibration(app_context_t *context, const acc_cal_result_t *sensor_cal_result)
Definition: ref_app_tank_level.c:540
SMALL_TANK_NUM_MEDIANS_TO_AVERAGE
#define SMALL_TANK_NUM_MEDIANS_TO_AVERAGE
Definition: ref_app_tank_level.c:31
LARGE_TANK_MAX_PROFILE
#define LARGE_TANK_MAX_PROFILE
Definition: ref_app_tank_level.c:69
MEDIUM_TANK_SIGNAL_QUALITY
#define MEDIUM_TANK_SIGNAL_QUALITY
Definition: ref_app_tank_level.c:60
CLOSE_RANGE_START
#define CLOSE_RANGE_START
Definition: ref_app_tank_level.c:27
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:496
acc_sensor.h
buffer_size
uint32_t buffer_size
Definition: i2c_example_cargo.c:41
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:568
acc_detector_cal_result_dynamic_t
The result from a completed calibration update.
Definition: acc_detector_distance_definitions.h:23
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 close range leakage cancellation.
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: ref_app_tank_level.c:190
LARGE_TANK_REFLECTOR_SHAPE
#define LARGE_TANK_REFLECTOR_SHAPE
Definition: ref_app_tank_level.c:72
sensor
acc_sensor_t * sensor
Definition: i2c_example_cargo.c:33
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 maximum Profile.
app_context_t::detector_cal_result_static_size
uint32_t detector_cal_result_static_size
Definition: ref_app_tank_level.c:142
acc_detector_distance_destroy
void acc_detector_distance_destroy(acc_detector_distance_handle_t *handle)
Destroy 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:130
LARGE_TANK_THRESHOLD_METHOD
#define LARGE_TANK_THRESHOLD_METHOD
Definition: ref_app_tank_level.c:73
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 signal quality (dB)
result
cargo_result_t result
Definition: i2c_example_cargo.c:43
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:133
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:471
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:109
LARGE_TANK_NUM_FRAMES_REC
#define LARGE_TANK_NUM_FRAMES_REC
Definition: ref_app_tank_level.c:70
acc_hal_definitions_a121.h
SMALL_TANK_THRESHOLD_SENSITIVITY
#define SMALL_TANK_THRESHOLD_SENSITIVITY
Definition: ref_app_tank_level.c:42
acc_ref_app_tank_level_config_t::num_medians_to_average
uint16_t num_medians_to_average
Definition: ref_app_tank_level.c:122
TANK_LEVEL_PRESET_CONFIG_NONE
@ TANK_LEVEL_PRESET_CONFIG_NONE
Definition: ref_app_tank_level.c:109
acc_detector_distance_result_t::calibration_needed
bool calibration_needed
Definition: acc_detector_distance.h:87
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:123
acc_detector_distance_result_t
Distance Detector result returned from acc_detector_distance_process.
Definition: acc_detector_distance.h:48
SMALL_TANK_MAX_PROFILE
#define SMALL_TANK_MAX_PROFILE
Definition: ref_app_tank_level.c:35
MEDIUM_TANK_RANGE_END_M
#define MEDIUM_TANK_RANGE_END_M
Definition: ref_app_tank_level.c:50
MEDIUM_TANK_REFLECTOR_SHAPE
#define MEDIUM_TANK_REFLECTOR_SHAPE
Definition: ref_app_tank_level.c:55
MEDIUM_TANK_RANGE_START_M
#define MEDIUM_TANK_RANGE_START_M
Definition: ref_app_tank_level.c:49
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:119
acc_detector_distance_config_end_set
void acc_detector_distance_config_end_set(acc_detector_distance_config_t *config, float end_m)
Set end of measured interval (m)
app_context_t::median_edge_status_count
uint16_t median_edge_status_count
Definition: ref_app_tank_level.c:136
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:416
cleanup
static void cleanup(app_context_t *context)
Definition: ref_app_tank_level.c:313
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 number of 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:65
acc_detector_distance_handle_t
struct acc_detector_distance_handle acc_detector_distance_handle_t
Distance Detector handle.
Definition: acc_detector_distance.h:36
LARGE_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
#define LARGE_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
Definition: ref_app_tank_level.c:78
SMALL_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
#define SMALL_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
Definition: ref_app_tank_level.c:44
tank_level_preset_config_t
tank_level_preset_config_t
Definition: ref_app_tank_level.c:107
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
acc_algorithm_median_f32
float acc_algorithm_median_f32(float *data, uint16_t length)
Calculate median of input data.
Definition: acc_algorithm.c:1342
LARGE_TANK_RANGE_END_M
#define LARGE_TANK_RANGE_END_M
Definition: ref_app_tank_level.c:67
acc_sensor_status
void acc_sensor_status(const acc_sensor_t *sensor)
Check the status of the sensor.
acc_detector_distance_update_calibration
bool acc_detector_distance_update_calibration(acc_sensor_t *sensor, acc_detector_distance_handle_t *handle, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size, acc_detector_cal_result_dynamic_t *detector_cal_result_dynamic, bool *calibration_complete)
Perform a subset of the full Detector calibration.
acc_detector_distance_result_t::near_start_edge_status
bool near_start_edge_status
Definition: acc_detector_distance.h:75
acc_algorithm.h
LARGE_TANK_FIXED_STRENGTH_THRESHOLD
#define LARGE_TANK_FIXED_STRENGTH_THRESHOLD
Definition: ref_app_tank_level.c:75
acc_integration_mem_free
void acc_integration_mem_free(void *ptr)
Free dynamic memory.
Definition: acc_integration_stm32.c:602
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:53
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:552
app_context_t::sensor
acc_sensor_t * sensor
Definition: ref_app_smart_presence.c:152
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 Distance Detector configuration.
acc_detector_distance_config_fixed_strength_threshold_value_set
void acc_detector_distance_config_fixed_strength_threshold_value_set(acc_detector_distance_config_t *config, float fixed_threshold_value)
Set fixed strength threshold value.
SMALL_TANK_MEDIAN_FILTER_LENGTH
#define SMALL_TANK_MEDIAN_FILTER_LENGTH
Definition: ref_app_tank_level.c:30
app_context_t::level_history
float * level_history
Definition: ref_app_tank_level.c:130
acc_detector_distance_result_t::distances
float distances[(10U)]
Definition: acc_detector_distance.h:55
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:154
acc_detector_distance_config_t
struct acc_detector_distance_config acc_detector_distance_config_t
Distance Detector configuration.
Definition: acc_detector_distance.h:43
LARGE_TANK_THRESHOLD_SENSITIVITY
#define LARGE_TANK_THRESHOLD_SENSITIVITY
Definition: ref_app_tank_level.c:76
LARGE_TANK_MEDIAN_FILTER_LENGTH
#define LARGE_TANK_MEDIAN_FILTER_LENGTH
Definition: ref_app_tank_level.c:64
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 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:735
peak_status_t
peak_status_t
Definition: ref_app_tank_level.c:146
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:332
PEAK_STATUS_OUT_OF_RANGE
@ PEAK_STATUS_OUT_OF_RANGE
Definition: ref_app_tank_level.c:151
MEDIUM_TANK_NUM_MEDIANS_TO_AVERAGE
#define MEDIUM_TANK_NUM_MEDIANS_TO_AVERAGE
Definition: ref_app_tank_level.c:48
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:115
app_context_t::median_vector
float * median_vector
Definition: ref_app_tank_level.c:132
MEDIUM_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
#define MEDIUM_TANK_CLOSE_RANGE_LEAKAGE_CANCELLATION
Definition: ref_app_tank_level.c:61
SMALL_TANK_REFLECTOR_SHAPE
#define SMALL_TANK_REFLECTOR_SHAPE
Definition: ref_app_tank_level.c:38
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:68