example_detector_distance_low_power_off.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2023-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_definitions_common.h"
14 #include "acc_detector_distance.h"
17 #include "acc_integration.h"
18 #include "acc_integration_log.h"
19 #include "acc_rss_a121.h"
20 #include "acc_sensor.h"
21 
22 #include "acc_version.h"
23 
24 /** \example example_detector_distance_low_power_off.c
25  * @brief This is an example on how to disable the sensor and put the system in a low power state between measurements
26  * @n
27  * The example executes as follows:
28  * - Create a distance configuration
29  * - Create a sensor instance
30  * - Enable sensor
31  * - Create a detector instance
32  * - Calibrate the sensor
33  * - Calibrate the detector
34  * - Disable sensor
35  * - Loop forever:
36  * - Enable sensor
37  * - Prepare the detector
38  * - Perform a sensor measurement and read out the data
39  * - Disable sensor
40  * - Process the measurement and get detector result
41  * - Put the system in deep sleep for a specified amount of time
42  * - Destroy the configuration
43  * - Destroy the detector instance
44  * - Destroy the sensor instance
45  */
46 
47 typedef enum
48 {
53 
54 #define SENSOR_ID (1U)
55 #define SENSOR_TIMEOUT_MS (1000U)
56 #define DEFAULT_UPDATE_RATE (1.0f)
57 
59 
60 static void cleanup(acc_detector_distance_handle_t *distance_handle,
61  acc_detector_distance_config_t *distance_config,
63  void *buffer,
65 
66 static void set_config(acc_detector_distance_config_t *detector_config, distance_preset_config_t preset);
67 
68 int acconeer_main(int argc, char *argv[]);
69 
70 int acconeer_main(int argc, char *argv[])
71 {
72  (void)argc;
73  (void)argv;
74  acc_detector_distance_config_t *distance_config = NULL;
75  acc_detector_distance_handle_t *distance_handle = NULL;
76  acc_sensor_t *sensor = NULL;
77  void *buffer = NULL;
78  uint8_t *detector_cal_result_static = NULL;
79  uint32_t buffer_size = 0U;
80  uint32_t detector_cal_result_static_size = 0U;
81  acc_detector_cal_result_dynamic_t detector_cal_result_dynamic = {0};
82 
83  printf("Acconeer software version %s\n", acc_version_get());
84 
86 
87  if (!acc_rss_hal_register(hal))
88  {
89  return EXIT_FAILURE;
90  }
91 
92  distance_config = acc_detector_distance_config_create();
93  if (distance_config == NULL)
94  {
95  printf("acc_detector_distance_config_create() failed\n");
96  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
97  return EXIT_FAILURE;
98  }
99 
101 
102  uint32_t sleep_time_ms = (uint32_t)(1000.0f / DEFAULT_UPDATE_RATE);
103 
105 
106  distance_handle = acc_detector_distance_create(distance_config);
107  if (distance_handle == NULL)
108  {
109  printf("acc_detector_distance_create() failed\n");
110  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
111  return EXIT_FAILURE;
112  }
113 
115  {
116  printf("acc_detector_distance_get_buffer_size() failed\n");
117  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
118  return EXIT_FAILURE;
119  }
120 
122  if (buffer == NULL)
123  {
124  printf("buffer allocation failed\n");
125  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
126  return EXIT_FAILURE;
127  }
128 
130  if (detector_cal_result_static == NULL)
131  {
132  printf("buffer allocation failed\n");
133  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
134  return EXIT_FAILURE;
135  }
136 
139 
141  if (sensor == NULL)
142  {
143  printf("acc_sensor_create() failed\n");
144  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
145  return EXIT_FAILURE;
146  }
147 
148  // Calibrate sensor
149  bool status;
150  bool cal_complete = false;
152 
153  do
154  {
155  status = acc_sensor_calibrate(sensor, &cal_complete, &sensor_cal_result, buffer, buffer_size);
156 
157  if (cal_complete)
158  {
159  break;
160  }
161 
162  if (status)
163  {
165  }
166  } while (status);
167 
168  if (!status)
169  {
170  printf("acc_sensor_calibrate() failed\n");
171  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
172  return EXIT_FAILURE;
173  }
174 
175  /* Reset sensor after calibration by disabling/enabling it */
178 
179  // Calibrate detector
180  bool done = false;
181 
182  do
183  {
185  distance_handle,
187  buffer,
188  buffer_size,
191  &detector_cal_result_dynamic,
192  &done);
193 
194  if (done)
195  {
196  break;
197  }
198 
199  if (status)
200  {
202  }
203  } while (status);
204 
205  if (!status)
206  {
207  printf("acc_detector_distance_calibrate() failed\n");
208  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
209  return EXIT_FAILURE;
210  }
211 
213 
214  while (true)
215  {
217 
219 
220  bool result_available = false;
221 
222  do
223  {
224  if (!acc_detector_distance_prepare(distance_handle, distance_config, sensor, &sensor_cal_result, buffer, buffer_size))
225  {
226  printf("acc_detector_distance_prepare() failed\n");
227  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
228  return EXIT_FAILURE;
229  }
230 
232  {
233  printf("acc_sensor_measure failed\n");
234  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
235  return EXIT_FAILURE;
236  }
237 
239  {
240  printf("Sensor interrupt timeout\n");
241  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
242  return EXIT_FAILURE;
243  }
244 
246  {
247  printf("acc_sensor_read failed\n");
248  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
249  return EXIT_FAILURE;
250  }
251 
252  if (!acc_detector_distance_process(distance_handle,
253  buffer,
255  &detector_cal_result_dynamic,
256  &result_available,
257  &result))
258  {
259  printf("acc_detector_distance_process failed\n");
260  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
261  return EXIT_FAILURE;
262  }
263  } while (!result_available);
264 
266 
268 
270  }
271 
272  cleanup(distance_handle, distance_config, sensor, buffer, detector_cal_result_static);
273 
274  printf("Application finished OK\n");
275 
276  return EXIT_SUCCESS;
277 }
278 
279 static void cleanup(acc_detector_distance_handle_t *distance_handle,
280  acc_detector_distance_config_t *distance_config,
282  void *buffer,
284 {
287 
288  if (distance_config != NULL)
289  {
290  acc_detector_distance_config_destroy(distance_config);
291  }
292 
293  if (distance_handle != NULL)
294  {
295  acc_detector_distance_destroy(distance_handle);
296  }
297 
298  if (sensor != NULL)
299  {
301  }
302 
303  if (buffer != NULL)
304  {
306  }
307 
308  if (detector_cal_result_static != NULL)
309  {
311  }
312 }
313 
315 {
316  // Add configuration of the detector here
317  switch (preset)
318  {
320  // Add configuration of the detector here
321  break;
322 
324  acc_detector_distance_config_start_set(detector_config, 0.25f);
325  acc_detector_distance_config_end_set(detector_config, 3.0f);
332  acc_detector_distance_config_signal_quality_set(detector_config, 15.0f);
334  break;
335 
337  acc_detector_distance_config_start_set(detector_config, 0.25f);
338  acc_detector_distance_config_end_set(detector_config, 3.0f);
345  acc_detector_distance_config_signal_quality_set(detector_config, 20.0f);
347  break;
348  }
349 }
350 
352 {
353  printf("%d detected distances", result->num_distances);
354  if (result->num_distances > 0)
355  {
356  printf(": ");
357 
358  for (uint8_t i = 0; i < result->num_distances; i++)
359  {
360  printf("%" PRIfloat "m ", ACC_LOG_FLOAT_TO_INTEGER(result->distances[i]));
361  }
362  }
363 
364  printf("\n");
365 }
DISTANCE_PRESET_CONFIG_HIGH_ACCURACY
@ DISTANCE_PRESET_CONFIG_HIGH_ACCURACY
Definition: example_detector_distance_low_power_off.c:51
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
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 memory size needed to use the Distance Detector API functions.
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.
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
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
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
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_detector_distance_low_power_off.c:55
ACC_DETECTOR_DISTANCE_PEAK_SORTING_STRONGEST
@ ACC_DETECTOR_DISTANCE_PEAK_SORTING_STRONGEST
Definition: acc_detector_distance_definitions.h:36
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_detector_distance_config_t *detector_config, distance_preset_config_t preset)
Definition: example_detector_distance_low_power_off.c:314
distance_preset_config_t
distance_preset_config_t
Definition: example_detector_distance_low_power_hibernate.c:47
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)
SENSOR_ID
#define SENSOR_ID
Definition: example_detector_distance_low_power_off.c:54
ACC_CONFIG_PROFILE_5
@ ACC_CONFIG_PROFILE_5
Definition: acc_definitions_a121.h:57
acc_integration.h
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.
print_result
static void print_result(acc_detector_distance_result_t *result)
Definition: example_detector_distance_low_power_off.c:351
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
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.
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
detector_cal_result_static
static uint8_t * detector_cal_result_static
Definition: example_detector_distance_calibration_caching.c:70
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_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
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.
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.
cleanup
static void cleanup(acc_detector_distance_handle_t *distance_handle, acc_detector_distance_config_t *distance_config, acc_sensor_t *sensor, void *buffer, uint8_t *detector_cal_result_static)
Definition: example_detector_distance_low_power_off.c:279
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
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)
DISTANCE_PRESET_CONFIG_NONE
@ DISTANCE_PRESET_CONFIG_NONE
Definition: example_detector_distance_low_power_off.c:49
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.
printf
#define printf
Definition: printf.h:60
detector_cal_result_static_size
static uint32_t detector_cal_result_static_size
Definition: example_detector_distance_calibration_caching.c:71
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
acc_hal_definitions_a121.h
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_detector_distance_result_t
Distance Detector result returned from acc_detector_distance_process.
Definition: acc_detector_distance.h:48
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_detector_distance_low_power_off.c:70
ACC_DETECTOR_DISTANCE_THRESHOLD_METHOD_CFAR
@ ACC_DETECTOR_DISTANCE_THRESHOLD_METHOD_CFAR
Definition: acc_detector_distance_definitions.h:51
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)
acc_integration_log.h
acc_detector_distance_handle_t
struct acc_detector_distance_handle acc_detector_distance_handle_t
Distance Detector handle.
Definition: acc_detector_distance.h:36
ACC_LOG_FLOAT_TO_INTEGER
#define ACC_LOG_FLOAT_TO_INTEGER(a)
Definition: acc_integration_log.h:26
distance_preset_config_t
distance_preset_config_t
Definition: example_detector_distance_low_power_off.c:47
DISTANCE_PRESET_CONFIG_BALANCED
@ DISTANCE_PRESET_CONFIG_BALANCED
Definition: example_detector_distance_low_power_off.c:50
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.
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
acc_definitions_common.h
DEFAULT_UPDATE_RATE
#define DEFAULT_UPDATE_RATE
Definition: example_detector_distance_low_power_off.c:56
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.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
acc_detector_distance_config_t
struct acc_detector_distance_config acc_detector_distance_config_t
Distance Detector configuration.
Definition: acc_detector_distance.h:43
ACC_DETECTOR_DISTANCE_REFLECTOR_SHAPE_GENERIC
@ ACC_DETECTOR_DISTANCE_REFLECTOR_SHAPE_GENERIC
Definition: acc_detector_distance_definitions.h:60
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.
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:54
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.