example_detector_presence_multiple_configurations.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_definitions_common.h"
14 #include "acc_detector_presence.h"
17 #include "acc_integration.h"
18 #include "acc_rss_a121.h"
19 #include "acc_sensor.h"
20 
21 #include "acc_version.h"
22 
23 /** \example example_detector_presence_multiple_configurations.c
24  * @brief This is an example on how the Detector Presence API can be used
25  * @n
26  * The example executes as follows:
27  * - Create a first presence configuration
28  * - Create a second presence configuration
29  * - Create a sensor instance
30  * - Calibrate the sensor
31  * - Loop forever:
32  * - Loop configurations (i):
33  * - Create a detector instance with the i:th configuration
34  * - Prepare the detector with the i:th configuration
35  * - Loop nbr iterations per configuration:
36  * - Perform a sensor measurement and read out the data
37  * - Process the measurement and get detector result
38  * - Destroy the detector instance
39  * - Destroy the configuration
40  * - Destroy the sensor instance
41  */
42 
43 #define SENSOR_ID (1U)
44 #define SENSOR_TIMEOUT_MS (1000U)
45 #define NBR_CONFIGS (2U)
46 #define NBR_ITERATIONS_PER_CONFIG (20U)
47 
48 
49 typedef struct example_config
50 {
51  char *name;
52  float start;
53  float end;
54  float frame_rate;
57 
58 
59 static void print_result(acc_detector_presence_result_t result, const char *config_name);
60 
61 
62 static void cleanup(acc_detector_presence_handle_t **presence_handle,
63  acc_detector_presence_config_t **presence_config,
64  acc_sensor_t *sensor,
65  void *buffer,
66  uint32_t nbr_configs);
67 
68 
69 int app_main(int argc, char *argv[]);
70 
71 
72 int app_main(int argc, char *argv[])
73 {
74  (void)argc;
75  (void)argv;
76  acc_detector_presence_config_t *presence_config[NBR_CONFIGS] = { NULL };
77  acc_detector_presence_handle_t *presence_handle[NBR_CONFIGS] = { NULL };
79  acc_sensor_t *sensor = NULL;
80  void *buffer = NULL;
81  uint32_t buffer_size = 0U;
82 
83  example_config_t example_configurations[] =
84  {
85  {
86  .name = "zone1",
87  .start = 1.0f,
88  .end = 2.0f,
89  .frame_rate = 5.0f,
90  /**
91  * Detector filters and states are reset on prepare (reconfiguration) to avoid risk of false detections
92  * when switching back to a zone.
93  */
94  .reset_filters_on_prepare = true,
95  },
96  {
97  .name = "zone2",
98  .start = 0.2f,
99  .end = 1.0f,
100  .frame_rate = 10.0f,
101  /**
102  * Detector filters and states are reset on prepare (reconfiguration) to avoid risk of false detections
103  * when switching back to a zone.
104  */
105  .reset_filters_on_prepare = true,
106  },
107  };
108 
109  printf("Acconeer software version %s\n", acc_version_get());
110 
112 
114  {
115  return EXIT_FAILURE;
116  }
117 
118  for (uint32_t cfg = 0U; cfg < NBR_CONFIGS; cfg++)
119  {
120  presence_config[cfg] = acc_detector_presence_config_create();
121  if (presence_config[cfg] == NULL)
122  {
123  printf("acc_detector_presence_config_create() failed\n");
124  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
125  return EXIT_FAILURE;
126  }
127 
128  acc_detector_presence_config_start_set(presence_config[cfg], example_configurations[cfg].start);
129  acc_detector_presence_config_end_set(presence_config[cfg], example_configurations[cfg].end);
130  acc_detector_presence_config_frame_rate_set(presence_config[cfg], example_configurations[cfg].frame_rate);
131  acc_detector_presence_config_reset_filters_on_prepare_set(presence_config[cfg], example_configurations[cfg].reset_filters_on_prepare);
132 
133  uint32_t current_buffer_size = 0;
134 
135  presence_handle[cfg] = acc_detector_presence_create(presence_config[cfg], &metadata[cfg]);
136  if (presence_handle[cfg] == NULL)
137  {
138  printf("acc_detector_presence_create() failed\n");
139  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
140  return EXIT_FAILURE;
141  }
142 
143  if (!acc_detector_presence_get_buffer_size(presence_handle[cfg], &current_buffer_size))
144  {
145  printf("acc_detector_presence_get_buffer_size() failed\n");
146  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
147  return EXIT_FAILURE;
148  }
149 
150  if (buffer_size < current_buffer_size)
151  {
152  buffer_size = current_buffer_size;
153  }
154  }
155 
156  buffer = acc_integration_mem_alloc(buffer_size);
157  if (buffer == NULL)
158  {
159  printf("buffer allocation failed\n");
160  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
161  return EXIT_FAILURE;
162  }
163 
166 
167  sensor = acc_sensor_create(SENSOR_ID);
168  if (sensor == NULL)
169  {
170  printf("acc_sensor_create() failed\n");
171  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
172  return EXIT_FAILURE;
173  }
174 
175  bool status;
176  bool cal_complete = false;
177  acc_cal_result_t cal_result;
178 
179  do
180  {
181  status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
182 
183  if (status && !cal_complete)
184  {
186  }
187  } while (status && !cal_complete);
188 
189  if (!status)
190  {
191  printf("acc_sensor_calibrate() failed\n");
192  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
193  return EXIT_FAILURE;
194  }
195 
196  // Reset sensor after calibration by disabling it
199 
200  while (true)
201  {
202  for (uint32_t cfg = 0U; cfg < NBR_CONFIGS; cfg++)
203  {
205 
206  if (!acc_detector_presence_prepare(presence_handle[cfg], presence_config[cfg], sensor,
207  &cal_result, buffer, buffer_size))
208  {
209  printf("acc_detector_presence_prepare() failed\n");
210  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
211  return EXIT_FAILURE;
212  }
213 
214  for (uint32_t i = 0U; i < NBR_ITERATIONS_PER_CONFIG; i++)
215  {
216  if (!acc_sensor_measure(sensor))
217  {
218  printf("acc_sensor_measure failed\n");
219  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
220  return EXIT_FAILURE;
221  }
222 
224  {
225  printf("Sensor interrupt timeout\n");
226  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
227  return EXIT_FAILURE;
228  }
229 
230  if (!acc_sensor_read(sensor, buffer, buffer_size))
231  {
232  printf("acc_sensor_read failed\n");
233  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
234  return EXIT_FAILURE;
235  }
236 
237  if (!acc_detector_presence_process(presence_handle[cfg], buffer, &result))
238  {
239  printf("acc_detector_presence_process failed\n");
240  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
241  return EXIT_FAILURE;
242  }
243 
244  /* The depthwise inter and intra score arrays that are part of the 'result',
245  * points to memory inside the 'buffer'. So they need to be handled before
246  * the 'buffer' is used again (for example in @ref acc_sensor_read or
247  * @ref acc_detector_presence_process).
248  */
249  print_result(result, example_configurations[cfg].name);
250  }
251  }
252  }
253 
254  cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
255 
256  printf("Application finished OK\n");
257 
258  return EXIT_SUCCESS;
259 }
260 
261 
262 static void cleanup(acc_detector_presence_handle_t **presence_handle,
263  acc_detector_presence_config_t **presence_config,
264  acc_sensor_t *sensor,
265  void *buffer,
266  uint32_t nbr_configs)
267 {
270 
271  for (uint32_t cfg = 0U; cfg < nbr_configs; cfg++)
272  {
273  if (presence_config[cfg] != NULL)
274  {
275  acc_detector_presence_config_destroy(presence_config[cfg]);
276  presence_config[cfg] = NULL;
277  }
278 
279  if (presence_handle[cfg] != NULL)
280  {
281  acc_detector_presence_destroy(presence_handle[cfg]);
282  presence_handle[cfg] = NULL;
283  }
284  }
285 
286  if (sensor != NULL)
287  {
288  acc_sensor_destroy(sensor);
289  }
290 
291  if (buffer != NULL)
292  {
293  acc_integration_mem_free(buffer);
294  }
295 }
296 
297 
298 static void print_result(acc_detector_presence_result_t result, const char *config_name)
299 {
300  if (result.presence_detected)
301  {
302  printf("Motion\n");
303  }
304  else
305  {
306  printf("No motion\n");
307  }
308 
309  // Score and distance are multiplied by 1000 to avoid printing floats
310  printf("%s - Intra presence score: %d, Inter presence score: %d, Distance (mm): %d\n",
311  config_name,
312  (int)(result.intra_presence_score * 1000.0f),
313  (int)(result.inter_presence_score * 1000.0f),
314  (int)(result.presence_distance * 1000.0f));
315 
316  printf("%s - Depthwise Intra Presence Scores: \t", config_name);
317  for (uint16_t i = 0; i < result.depthwise_presence_scores_length; i++)
318  {
319  // Score is multiplied by 1000 to avoid printing floats
320  printf("%5i", (int)(result.depthwise_intra_presence_scores[i] * 1000));
321  }
322 
323  printf("\n");
324  printf("%s - Depthwise Inter Presence Scores: \t", config_name);
325  for (uint16_t i = 0; i < result.depthwise_presence_scores_length; i++)
326  {
327  // Score is multiplied by 1000 to avoid printing floats
328  printf("%5i", (int)(result.depthwise_inter_presence_scores[i] * 1000));
329  }
330 
331  printf("\n");
332 }
acc_detector_presence_result_t::inter_presence_score
float inter_presence_score
Definition: acc_detector_presence.h:63
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_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
acc_detector_presence_destroy
void acc_detector_presence_destroy(acc_detector_presence_handle_t *presence_handle)
Destroy a presence detector identified with the provided handle.
NBR_CONFIGS
#define NBR_CONFIGS
Definition: example_detector_presence_multiple_configurations.c:45
example_config
Definition: example_detector_presence_multiple_configurations.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_detector_presence_result_t::depthwise_presence_scores_length
uint32_t depthwise_presence_scores_length
Definition: acc_detector_presence.h:81
acc_version.h
NBR_ITERATIONS_PER_CONFIG
#define NBR_ITERATIONS_PER_CONFIG
Definition: example_detector_presence_multiple_configurations.c:46
acc_cal_result_t
The result from a completed calibration.
Definition: acc_definitions_a121.h:32
example_config::reset_filters_on_prepare
bool reset_filters_on_prepare
Definition: example_detector_presence_multiple_configurations.c:55
acc_detector_presence_config_frame_rate_set
void acc_detector_presence_config_frame_rate_set(acc_detector_presence_config_t *presence_config, float frame_rate)
Set the frame rate.
acc_integration.h
acc_detector_presence_get_buffer_size
bool acc_detector_presence_get_buffer_size(const acc_detector_presence_handle_t *presence_handle, uint32_t *buffer_size)
Get the buffer size needed for the provided presence detector handle.
app_main
int app_main(int argc, char *argv[])
Assembly test example.
Definition: example_detector_presence_multiple_configurations.c:72
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_presence_result_t::presence_detected
bool presence_detected
Definition: acc_detector_presence.h:55
acc_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_esp32.c:38
print_result
static void print_result(acc_detector_presence_result_t result, const char *config_name)
Definition: example_detector_presence_multiple_configurations.c:298
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_presence_result_t
Presence detector results container.
Definition: acc_detector_presence.h:50
acc_detector_presence_config_destroy
void acc_detector_presence_config_destroy(acc_detector_presence_config_t *presence_config)
Destroy a presence detector configuration.
acc_detector_presence_metadata_t
Definition: acc_detector_presence.h:93
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_presence_config_end_set
void acc_detector_presence_config_end_set(acc_detector_presence_config_t *presence_config, float end)
Set the end point of measurement interval in meters.
acc_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
example_config::name
char * name
Definition: example_detector_presence_multiple_configurations.c:51
acc_detector_presence_result_t::intra_presence_score
float intra_presence_score
Definition: acc_detector_presence.h:59
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_detector_presence_result_t::depthwise_intra_presence_scores
float * depthwise_intra_presence_scores
Definition: acc_detector_presence.h:72
acc_hal_definitions_a121.h
acc_detector_presence_process
bool acc_detector_presence_process(acc_detector_presence_handle_t *presence_handle, void *buffer, acc_detector_presence_result_t *result)
Process the data according to the configuration used in acc_detector_presence_config_create.
example_config::frame_rate
float frame_rate
Definition: example_detector_presence_multiple_configurations.c:54
acc_detector_presence_config_reset_filters_on_prepare_set
void acc_detector_presence_config_reset_filters_on_prepare_set(acc_detector_presence_config_t *presence_config, bool enable)
Set if the presence filters should reset on prepare.
acc_detector_presence_prepare
bool acc_detector_presence_prepare(const acc_detector_presence_handle_t *presence_handle, acc_detector_presence_config_t *presence_config, acc_sensor_t *sensor, const acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size)
Prepare the detector to do a measurement.
acc_detector_presence_result_t::depthwise_inter_presence_scores
float * depthwise_inter_presence_scores
Definition: acc_detector_presence.h:77
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_presence_config_t
struct acc_detector_presence_config acc_detector_presence_config_t
Definition: acc_detector_presence.h:44
example_config_t
struct example_config example_config_t
cleanup
static void cleanup(acc_detector_presence_handle_t **presence_handle, acc_detector_presence_config_t **presence_config, acc_sensor_t *sensor, void *buffer, uint32_t nbr_configs)
Definition: example_detector_presence_multiple_configurations.c:262
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_detector_presence_multiple_configurations.c:44
example_config::end
float end
Definition: example_detector_presence_multiple_configurations.c:53
hal
static const acc_hal_a121_t hal
Definition: acc_hal_integration_espidf_xe121.c:121
SENSOR_ID
#define SENSOR_ID
Definition: example_detector_presence_multiple_configurations.c:43
example_config::start
float start
Definition: example_detector_presence_multiple_configurations.c:52
acc_detector_presence_result_t::presence_distance
float presence_distance
Definition: acc_detector_presence.h:67
acc_integration_mem_free
void acc_integration_mem_free(void *ptr)
Free dynamic memory.
Definition: acc_integration_esp32.c:57
acc_definitions_common.h
acc_detector_presence_create
acc_detector_presence_handle_t * acc_detector_presence_create(acc_detector_presence_config_t *presence_config, acc_detector_presence_metadata_t *metadata)
Create a presence detector with the provided configuration.
acc_detector_presence_handle_t
struct acc_detector_presence_handle acc_detector_presence_handle_t
Definition: acc_detector_presence.h:36
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.
acc_detector_presence_config_create
acc_detector_presence_config_t * acc_detector_presence_config_create(void)
Create a configuration for a presence detector.
acc_detector_presence_config_start_set
void acc_detector_presence_config_start_set(acc_detector_presence_config_t *presence_config, float start)
Set the start point of measurement interval in meters.
acc_detector_presence.h
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_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.