example_detector_distance.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2022-2024
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 <stdbool.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 #include "acc_definitions_a121.h"
13 #include "acc_detector_distance.h"
16 #include "acc_integration.h"
17 #include "acc_integration_log.h"
18 #include "acc_rss_a121.h"
19 #include "acc_sensor.h"
20 #include "acc_version.h"
21 
22 
23 /** \example example_detector_distance.c
24  * @brief This is an example on how the Detector Distance API can be used
25  * @n
26  * This example executes as follows:
27  * - Retrieve HAL integration
28  * - Initialize distance detector resources:
29  * + Create distance detector configuration
30  * + Update configuration settings
31  * + Create Distance detector handle
32  * + Create buffer for detector calibration data
33  * + Create buffer for sensor data
34  * - Create and calibrate the sensor
35  * - Calibrate the detector
36  * - Measure distances with the detector (loop):
37  * + Prepare sensor with the detector
38  * + Measure and wait until a read can be done
39  * + Process measurement and print the result
40  * + Handle "calibration_needed" indication
41  * - Cleanup:
42  * + Destroy detector configuration
43  * + Destroy detector handle
44  * + Destroy sensor data buffer
45  * + Destroy detector calibration data buffer
46  */
47 
48 
49 typedef enum
50 {
55 
56 
57 #define SENSOR_ID (1U)
58 // 2 seconds should be enough even for long ranges and high signal quality
59 #define SENSOR_TIMEOUT_MS (2000U)
60 
61 
62 typedef struct
63 {
67  void *buffer;
68  uint32_t buffer_size;
73 
74 
75 static void cleanup(distance_detector_resources_t *resources);
76 
77 
78 static void set_config(acc_detector_distance_config_t *detector_config, distance_preset_config_t preset);
79 
80 
82 
83 
84 static bool do_sensor_calibration(acc_sensor_t *sensor,
85  acc_cal_result_t *sensor_cal_result,
86  void *buffer,
87  uint32_t buffer_size);
88 
89 
91  const acc_cal_result_t *sensor_cal_result);
92 
93 
95  const acc_cal_result_t *sensor_cal_result);
96 
97 
99  const acc_cal_result_t *sensor_cal_result,
101 
102 
103 static void print_distance_result(const acc_detector_distance_result_t *result);
104 
105 
106 int app_main(int argc, char *argv[]);
107 
108 
109 int app_main(int argc, char *argv[])
110 {
111  (void)argc;
112  (void)argv;
113  distance_detector_resources_t resources = { 0 };
114 
115  printf("Acconeer software version %s\n", acc_version_get());
116 
118 
120  {
121  return EXIT_FAILURE;
122  }
123 
125  if (resources.config == NULL)
126  {
127  printf("acc_detector_distance_config_create() failed\n");
128  cleanup(&resources);
129  return EXIT_FAILURE;
130  }
131 
133 
134  if (!initialize_detector_resources(&resources))
135  {
136  printf("Initializing detector resources failed\n");
137  cleanup(&resources);
138  return EXIT_FAILURE;
139  }
140 
141  // Print the configuration
142  acc_detector_distance_config_log(resources.handle, resources.config);
143 
144  /* Turn the sensor on */
147 
148  resources.sensor = acc_sensor_create(SENSOR_ID);
149  if (resources.sensor == NULL)
150  {
151  printf("acc_sensor_create() failed\n");
152  cleanup(&resources);
153  return EXIT_FAILURE;
154  }
155 
156  acc_cal_result_t sensor_cal_result;
157 
158  if (!do_sensor_calibration(resources.sensor, &sensor_cal_result, resources.buffer, resources.buffer_size))
159  {
160  printf("Sensor calibration failed\n");
161  cleanup(&resources);
162  return EXIT_FAILURE;
163  }
164 
165  if (!do_full_detector_calibration(&resources, &sensor_cal_result))
166  {
167  printf("Detector calibration failed\n");
168  cleanup(&resources);
169  return EXIT_FAILURE;
170  }
171 
172  while (true)
173  {
174  acc_detector_distance_result_t result = { 0 };
175 
176  if (!do_detector_get_next(&resources, &sensor_cal_result, &result))
177  {
178  printf("Could not get next result\n");
179  cleanup(&resources);
180  return EXIT_FAILURE;
181  }
182 
183  /* If "calibration needed" is indicated, the sensor needs to be recalibrated and the detector calibration updated */
184  if (result.calibration_needed)
185  {
186  printf("Sensor recalibration and detector calibration update needed ... \n");
187 
188  if (!do_sensor_calibration(resources.sensor, &sensor_cal_result, resources.buffer, resources.buffer_size))
189  {
190  printf("Sensor calibration failed\n");
191  cleanup(&resources);
192  return EXIT_FAILURE;
193  }
194 
195  /* Once the sensor is recalibrated, the detector calibration should be updated and measuring can continue. */
196  if (!do_detector_calibration_update(&resources, &sensor_cal_result))
197  {
198  printf("Detector calibration update failed\n");
199  cleanup(&resources);
200  return EXIT_FAILURE;
201  }
202 
203  printf("Sensor recalibration and detector calibration update done!\n");
204  }
205  else
206  {
207  print_distance_result(&result);
208  }
209  }
210 
211  cleanup(&resources);
212 
213  printf("Done!\n");
214 
215  return EXIT_SUCCESS;
216 }
217 
218 
219 static void cleanup(distance_detector_resources_t *resources)
220 {
223 
226 
227  acc_integration_mem_free(resources->buffer);
229 
230  if (resources->sensor != NULL)
231  {
232  acc_sensor_destroy(resources->sensor);
233  }
234 }
235 
236 
238 {
239  // Add configuration of the detector here
240  switch (preset)
241  {
243  // Add configuration of the detector here
244  break;
245 
247  acc_detector_distance_config_start_set(detector_config, 0.25f);
248  acc_detector_distance_config_end_set(detector_config, 3.0f);
256  acc_detector_distance_config_signal_quality_set(detector_config, 15.0f);
258  break;
259 
261  acc_detector_distance_config_start_set(detector_config, 0.25f);
262  acc_detector_distance_config_end_set(detector_config, 3.0f);
270  acc_detector_distance_config_signal_quality_set(detector_config, 20.0f);
272  break;
273  }
274 }
275 
276 
278 {
279  resources->handle = acc_detector_distance_create(resources->config);
280  if (resources->handle == NULL)
281  {
282  printf("acc_detector_distance_create() failed\n");
283  return false;
284  }
285 
286  if (!acc_detector_distance_get_sizes(resources->handle, &(resources->buffer_size), &(resources->detector_cal_result_static_size)))
287  {
288  printf("acc_detector_distance_get_sizes() failed\n");
289  return false;
290  }
291 
292  resources->buffer = acc_integration_mem_alloc(resources->buffer_size);
293  if (resources->buffer == NULL)
294  {
295  printf("sensor buffer allocation failed\n");
296  return false;
297  }
298 
300  if (resources->detector_cal_result_static == NULL)
301  {
302  printf("calibration buffer allocation failed\n");
303  return false;
304  }
305 
306  return true;
307 }
308 
309 
311  acc_cal_result_t *sensor_cal_result,
312  void *buffer,
313  uint32_t buffer_size)
314 {
315  bool status = false;
316  bool cal_complete = false;
317  const uint16_t calibration_retries = 1U;
318 
319  // Random disturbances may cause the calibration to fail. At failure, retry at least once.
320  for (uint16_t i = 0; !status && (i <= calibration_retries); i++)
321  {
322  // Reset sensor before calibration by disabling/enabling it
325 
326  do
327  {
328  status = acc_sensor_calibrate(sensor, &cal_complete, sensor_cal_result, buffer, buffer_size);
329 
330  if (status && !cal_complete)
331  {
333  }
334  } while (status && !cal_complete);
335  }
336 
337  if (status)
338  {
339  /* Reset sensor after calibration by disabling/enabling it */
342  }
343 
344  return status;
345 }
346 
347 
349  const acc_cal_result_t *sensor_cal_result)
350 {
351  bool done = false;
352  bool status;
353 
354  do
355  {
356  status = acc_detector_distance_calibrate(resources->sensor,
357  resources->handle,
358  sensor_cal_result,
359  resources->buffer,
360  resources->buffer_size,
361  resources->detector_cal_result_static,
363  &resources->detector_cal_result_dynamic,
364  &done);
365 
366  if (status && !done)
367  {
369  }
370  } while (status && !done);
371 
372  return status;
373 }
374 
375 
377  const acc_cal_result_t *sensor_cal_result)
378 {
379  bool done = false;
380  bool status;
381 
382  do
383  {
385  resources->handle,
386  sensor_cal_result,
387  resources->buffer,
388  resources->buffer_size,
389  &resources->detector_cal_result_dynamic,
390  &done);
391 
392  if (status && !done)
393  {
395  }
396  } while (status && !done);
397 
398  return status;
399 }
400 
401 
403  const acc_cal_result_t *sensor_cal_result,
405 {
406  bool result_available = false;
407 
408  do
409  {
410  if (!acc_detector_distance_prepare(resources->handle, resources->config, resources->sensor, sensor_cal_result, resources->buffer,
411  resources->buffer_size))
412  {
413  printf("acc_detector_distance_prepare() failed\n");
414  return false;
415  }
416 
417  if (!acc_sensor_measure(resources->sensor))
418  {
419  printf("acc_sensor_measure() failed\n");
420  return false;
421  }
422 
424  {
425  printf("Sensor interrupt timeout\n");
426  return false;
427  }
428 
429  if (!acc_sensor_read(resources->sensor, resources->buffer, resources->buffer_size))
430  {
431  printf("acc_sensor_read() failed\n");
432  return false;
433  }
434 
435  if (!acc_detector_distance_process(resources->handle, resources->buffer, resources->detector_cal_result_static,
436  &resources->detector_cal_result_dynamic,
437  &result_available, result))
438  {
439  printf("acc_detector_distance_process() failed\n");
440  return false;
441  }
442  } while (!result_available);
443 
444  return true;
445 }
446 
447 
449 {
450  printf("%d detected distances", result->num_distances);
451  if (result->num_distances > 0)
452  {
453  printf(": ");
454 
455  for (uint8_t i = 0; i < result->num_distances; i++)
456  {
457  printf("%" PRIfloat " m ", ACC_LOG_FLOAT_TO_INTEGER(result->distances[i]));
458  }
459  }
460 
461  printf("\n");
462 }
ACC_DETECTOR_DISTANCE_REFLECTOR_SHAPE_GENERIC
@ ACC_DETECTOR_DISTANCE_REFLECTOR_SHAPE_GENERIC
Definition: acc_detector_distance_definitions.h:59
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_espidf_xe121.c:192
acc_rss_a121.h
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 sizes needed given the provided detector handle.
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.
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_espidf_xe121.c:185
distance_detector_resources_t::detector_cal_result_static_size
uint32_t detector_cal_result_static_size
Definition: example_detector_distance.c:70
distance_preset_config_t
distance_preset_config_t
Definition: example_detector_distance.c:49
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
acc_version.h
ACC_DETECTOR_DISTANCE_PEAK_SORTING_STRONGEST
@ ACC_DETECTOR_DISTANCE_PEAK_SORTING_STRONGEST
Definition: acc_detector_distance_definitions.h:33
print_distance_result
static void print_distance_result(const acc_detector_distance_result_t *result)
Definition: example_detector_distance.c:448
distance_detector_resources_t::buffer_size
uint32_t buffer_size
Definition: example_detector_distance.c:68
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 the threshold method.
acc_cal_result_t
The result from a completed calibration.
Definition: acc_definitions_a121.h:32
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.
cleanup
static void cleanup(distance_detector_resources_t *resources)
Definition: example_detector_distance.c:219
ACC_CONFIG_PROFILE_5
@ ACC_CONFIG_PROFILE_5
Definition: acc_definitions_a121.h:60
acc_integration.h
acc_detector_distance_result_t::num_distances
uint8_t num_distances
Definition: acc_detector_distance.h:63
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.
ACC_DETECTOR_DISTANCE_THRESHOLD_METHOD_CFAR
@ ACC_DETECTOR_DISTANCE_THRESHOLD_METHOD_CFAR
Definition: acc_detector_distance_definitions.h:49
acc_detector_distance_config_destroy
void acc_detector_distance_config_destroy(acc_detector_distance_config_t *config)
Destroy a configuration for a distance detector.
do_full_detector_calibration
static bool do_full_detector_calibration(distance_detector_resources_t *resources, const acc_cal_result_t *sensor_cal_result)
Definition: example_detector_distance.c:348
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_espidf_xe121.c:135
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)
Do a detector calibration.
distance_detector_resources_t::detector_cal_result_dynamic
acc_detector_cal_result_dynamic_t detector_cal_result_dynamic
Definition: example_detector_distance.c:71
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 the data according to the configuration used in acc_detector_distance_config_create.
acc_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_esp32.c:38
acc_hal_a121_t
Definition: acc_hal_definitions_a121.h:82
acc_rss_hal_register
bool acc_rss_hal_register(const acc_hal_a121_t *hal)
Register an integration.
acc_sensor.h
acc_detector_cal_result_dynamic_t
Definition: acc_detector_distance_definitions.h:19
distance_detector_resources_t::detector_cal_result_static
uint8_t * detector_cal_result_static
Definition: example_detector_distance.c:69
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.
distance_detector_resources_t
Definition: example_detector_distance.c:62
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_espidf_xe121.c:100
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.
acc_hal_integration_sensor_enable
void acc_hal_integration_sensor_enable(acc_sensor_id_t sensor_id)
Enable sensor.
Definition: acc_hal_integration_espidf_xe121.c:199
acc_hal_definitions_a121.h
DISTANCE_PRESET_CONFIG_HIGH_ACCURACY
@ DISTANCE_PRESET_CONFIG_HIGH_ACCURACY
Definition: example_detector_distance.c:53
acc_detector_distance_result_t::calibration_needed
bool calibration_needed
Definition: acc_detector_distance.h:76
SENSOR_ID
#define SENSOR_ID
Definition: example_detector_distance.c:57
acc_detector_distance_config_log
void acc_detector_distance_config_log(const acc_detector_distance_handle_t *handle, const acc_detector_distance_config_t *config)
Print a configuration to the log.
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.
app_main
int app_main(int argc, char *argv[])
Assembly test example.
Definition: example_detector_distance.c:109
acc_detector_distance_result_t
Distance detector result.
Definition: acc_detector_distance.h:50
distance_detector_resources_t::config
acc_detector_distance_config_t * config
Definition: example_detector_distance.c:65
acc_hal_integration_sensor_disable
void acc_hal_integration_sensor_disable(acc_sensor_id_t sensor_id)
Disable sensor.
Definition: acc_hal_integration_espidf_xe121.c:214
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.
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.
DISTANCE_PRESET_CONFIG_BALANCED
@ DISTANCE_PRESET_CONFIG_BALANCED
Definition: example_detector_distance.c:52
acc_integration_log.h
acc_detector_distance_handle_t
struct acc_detector_distance_handle acc_detector_distance_handle_t
Definition: acc_detector_distance.h:36
ACC_LOG_FLOAT_TO_INTEGER
#define ACC_LOG_FLOAT_TO_INTEGER(a)
Definition: acc_integration_log.h:26
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_detector_distance.c:59
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)
Update the calibration.
initialize_detector_resources
static bool initialize_detector_resources(distance_detector_resources_t *resources)
Definition: example_detector_distance.c:277
hal
static const acc_hal_a121_t hal
Definition: acc_hal_integration_espidf_xe121.c:121
acc_integration_mem_free
void acc_integration_mem_free(void *ptr)
Free dynamic memory.
Definition: acc_integration_esp32.c:57
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.
distance_detector_resources_t::sensor
acc_sensor_t * sensor
Definition: example_detector_distance.c:64
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.
DISTANCE_PRESET_CONFIG_NONE
@ DISTANCE_PRESET_CONFIG_NONE
Definition: example_detector_distance.c:51
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
distance_detector_resources_t::handle
acc_detector_distance_handle_t * handle
Definition: example_detector_distance.c:66
do_detector_get_next
static bool do_detector_get_next(distance_detector_resources_t *resources, const acc_cal_result_t *sensor_cal_result, acc_detector_distance_result_t *result)
Definition: example_detector_distance.c:402
distance_detector_resources_t::buffer
void * buffer
Definition: example_detector_distance.c:67
acc_detector_distance_config_t
struct acc_detector_distance_config acc_detector_distance_config_t
Definition: acc_detector_distance.h:44
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.
set_config
static void set_config(acc_detector_distance_config_t *detector_config, distance_preset_config_t preset)
Definition: example_detector_distance.c:237
do_detector_calibration_update
static bool do_detector_calibration_update(distance_detector_resources_t *resources, const acc_cal_result_t *sensor_cal_result)
Definition: example_detector_distance.c:376
acc_sensor_measure
bool acc_sensor_measure(acc_sensor_t *sensor)
Start a radar measurement with previously prepared configuration.
acc_sensor_t
struct acc_sensor acc_sensor_t
Definition: acc_sensor.h:31
ACC_CONFIG_PROFILE_3
@ ACC_CONFIG_PROFILE_3
Definition: acc_definitions_a121.h:57
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.
do_sensor_calibration
static bool do_sensor_calibration(acc_sensor_t *sensor, acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size)
Definition: example_detector_distance.c:310