example_detector_presence_multiple_configurations.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2022-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 <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"
15 #include "acc_hal_definitions_a121.h"
16 #include "acc_hal_integration_a121.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 typedef struct example_config
49 {
50 char *name;
51 float start;
52 float end;
53 float frame_rate;
54 bool reset_filters_on_prepare;
55 } example_config_t;
56
57 static void print_result(acc_detector_presence_result_t *result, const char *config_name);
58
59 static void cleanup(acc_detector_presence_handle_t **presence_handle,
60 acc_detector_presence_config_t **presence_config,
61 acc_sensor_t *sensor,
62 void *buffer,
63 uint32_t nbr_configs);
64
65 int acconeer_main(int argc, char *argv[]);
66
67 int acconeer_main(int argc, char *argv[])
68 {
69 (void)argc;
70 (void)argv;
71 acc_detector_presence_config_t *presence_config[NBR_CONFIGS] = {NULL};
72 acc_detector_presence_handle_t *presence_handle[NBR_CONFIGS] = {NULL};
73 acc_detector_presence_metadata_t metadata[NBR_CONFIGS];
74 acc_sensor_t *sensor = NULL;
75 void *buffer = NULL;
76 uint32_t buffer_size = 0U;
77
78 example_config_t example_configurations[] = {
79 {
80 .name = "zone1",
81 .start = 1.0f,
82 .end = 2.0f,
83 .frame_rate = 5.0f,
84 /**
85 * Detector filters and states are reset on prepare (reconfiguration) to avoid risk of false detections
86 * when switching back to a zone.
87 */
88 .reset_filters_on_prepare = true,
89 },
90 {
91 .name = "zone2",
92 .start = 0.2f,
93 .end = 1.0f,
94 .frame_rate = 10.0f,
95 /**
96 * Detector filters and states are reset on prepare (reconfiguration) to avoid risk of false detections
97 * when switching back to a zone.
98 */
99 .reset_filters_on_prepare = true,
100 },
101 };
102
103 printf("Acconeer software version %s\n", acc_version_get());
104
105 const acc_hal_a121_t *hal = acc_hal_rss_integration_get_implementation();
106
107 if (!acc_rss_hal_register(hal))
108 {
109 return EXIT_FAILURE;
110 }
111
112 for (uint32_t cfg = 0U; cfg < NBR_CONFIGS; cfg++)
113 {
114 presence_config[cfg] = acc_detector_presence_config_create();
115 if (presence_config[cfg] == NULL)
116 {
117 printf("acc_detector_presence_config_create() failed\n");
118 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
119 return EXIT_FAILURE;
120 }
121
122 acc_detector_presence_config_start_set(presence_config[cfg], example_configurations[cfg].start);
123 acc_detector_presence_config_end_set(presence_config[cfg], example_configurations[cfg].end);
124 acc_detector_presence_config_frame_rate_set(presence_config[cfg], example_configurations[cfg].frame_rate);
125 acc_detector_presence_config_reset_filters_on_prepare_set(presence_config[cfg], example_configurations[cfg].reset_filters_on_prepare);
126
127 uint32_t current_buffer_size = 0;
128
129 presence_handle[cfg] = acc_detector_presence_create(presence_config[cfg], &metadata[cfg]);
130 if (presence_handle[cfg] == NULL)
131 {
132 printf("acc_detector_presence_create() failed\n");
133 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
134 return EXIT_FAILURE;
135 }
136
137 if (!acc_detector_presence_get_buffer_size(presence_handle[cfg], &current_buffer_size))
138 {
139 printf("acc_detector_presence_get_buffer_size() failed\n");
140 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
141 return EXIT_FAILURE;
142 }
143
144 if (buffer_size < current_buffer_size)
145 {
146 buffer_size = current_buffer_size;
147 }
148 }
149
150 buffer = acc_integration_mem_alloc(buffer_size);
151 if (buffer == NULL)
152 {
153 printf("buffer allocation failed\n");
154 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
155 return EXIT_FAILURE;
156 }
157
158 acc_hal_integration_sensor_supply_on(SENSOR_ID);
159 acc_hal_integration_sensor_enable(SENSOR_ID);
160
161 sensor = acc_sensor_create(SENSOR_ID);
162 if (sensor == NULL)
163 {
164 printf("acc_sensor_create() failed\n");
165 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
166 return EXIT_FAILURE;
167 }
168
169 bool status;
170 bool cal_complete = false;
171 acc_cal_result_t cal_result;
172
173 do
174 {
175 status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
176
177 if (status && !cal_complete)
178 {
179 status = acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS);
180 }
181 } while (status && !cal_complete);
182
183 if (!status)
184 {
185 printf("acc_sensor_calibrate() failed\n");
186 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
187 return EXIT_FAILURE;
188 }
189
190 // Reset sensor after calibration by disabling it
191 acc_hal_integration_sensor_disable(SENSOR_ID);
192 acc_hal_integration_sensor_enable(SENSOR_ID);
193
194 while (true)
195 {
196 for (uint32_t cfg = 0U; cfg < NBR_CONFIGS; cfg++)
197 {
198 acc_detector_presence_result_t result;
199
200 if (!acc_detector_presence_prepare(presence_handle[cfg], presence_config[cfg], sensor, &cal_result, buffer, buffer_size))
201 {
202 printf("acc_detector_presence_prepare() failed\n");
203 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
204 return EXIT_FAILURE;
205 }
206
207 for (uint32_t i = 0U; i < NBR_ITERATIONS_PER_CONFIG; i++)
208 {
209 if (!acc_sensor_measure(sensor))
210 {
211 printf("acc_sensor_measure failed\n");
212 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
213 return EXIT_FAILURE;
214 }
215
216 if (!acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS))
217 {
218 printf("Sensor interrupt timeout\n");
219 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
220 return EXIT_FAILURE;
221 }
222
223 if (!acc_sensor_read(sensor, buffer, buffer_size))
224 {
225 printf("acc_sensor_read failed\n");
226 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
227 return EXIT_FAILURE;
228 }
229
230 if (!acc_detector_presence_process(presence_handle[cfg], buffer, &result))
231 {
232 printf("acc_detector_presence_process failed\n");
233 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
234 return EXIT_FAILURE;
235 }
236
237 /* The depthwise inter and intra score arrays that are part of the 'result',
238 * points to memory inside the 'buffer'. So they need to be handled before
239 * the 'buffer' is used again (for example in @ref acc_sensor_read or
240 * @ref acc_detector_presence_process).
241 */
242 print_result(&result, example_configurations[cfg].name);
243 }
244 }
245 }
246
247 cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
248
249 printf("Application finished OK\n");
250
251 return EXIT_SUCCESS;
252 }
253
254 static void cleanup(acc_detector_presence_handle_t **presence_handle,
255 acc_detector_presence_config_t **presence_config,
256 acc_sensor_t *sensor,
257 void *buffer,
258 uint32_t nbr_configs)
259 {
260 acc_hal_integration_sensor_disable(SENSOR_ID);
261 acc_hal_integration_sensor_supply_off(SENSOR_ID);
262
263 for (uint32_t cfg = 0U; cfg < nbr_configs; cfg++)
264 {
265 if (presence_config[cfg] != NULL)
266 {
267 acc_detector_presence_config_destroy(presence_config[cfg]);
268 presence_config[cfg] = NULL;
269 }
270
271 if (presence_handle[cfg] != NULL)
272 {
273 acc_detector_presence_destroy(presence_handle[cfg]);
274 presence_handle[cfg] = NULL;
275 }
276 }
277
278 if (sensor != NULL)
279 {
280 acc_sensor_destroy(sensor);
281 }
282
283 if (buffer != NULL)
284 {
285 acc_integration_mem_free(buffer);
286 }
287 }
288
289 static void print_result(acc_detector_presence_result_t *result, const char *config_name)
290 {
291 if (result->presence_detected)
292 {
293 printf("Motion\n");
294 }
295 else
296 {
297 printf("No motion\n");
298 }
299
300 // Score and distance are multiplied by 1000 to avoid printing floats
301 printf("%s - Intra presence score: %d, Inter presence score: %d, Distance (mm): %d\n",
302 config_name,
303 (int)(result->intra_presence_score * 1000.0f),
304 (int)(result->inter_presence_score * 1000.0f),
305 (int)(result->presence_distance * 1000.0f));
306
307 printf("%s - Depthwise Intra Presence Scores: \t", config_name);
308 for (uint16_t i = 0; i < result->depthwise_presence_scores_length; i++)
309 {
310 // Score is multiplied by 1000 to avoid printing floats
311 printf("%5i", (int)(result->depthwise_intra_presence_scores[i] * 1000));
312 }
313
314 printf("\n");
315 printf("%s - Depthwise Inter Presence Scores: \t", config_name);
316 for (uint16_t i = 0; i < result->depthwise_presence_scores_length; i++)
317 {
318 // Score is multiplied by 1000 to avoid printing floats
319 printf("%5i", (int)(result->depthwise_inter_presence_scores[i] * 1000));
320 }
321
322 printf("\n");
323 }
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_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_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:48
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
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:30
example_config::reset_filters_on_prepare
bool reset_filters_on_prepare
Definition: example_detector_presence_multiple_configurations.c:54
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.
cargo_result_t::inter_presence_score
float inter_presence_score
Definition: example_cargo.h:137
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_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_stm32.c:592
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:46
buffer_size
uint32_t buffer_size
Definition: i2c_example_cargo.c:41
sensor
acc_sensor_t * sensor
Definition: i2c_example_cargo.c:33
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_detector_presence_multiple_configurations.c:67
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:88
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_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.
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
example_config::name
char * name
Definition: example_detector_presence_multiple_configurations.c:50
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_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:53
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.
cargo_result_t::intra_presence_score
float intra_presence_score
Definition: example_cargo.h:140
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_presence_config_t
struct acc_detector_presence_config acc_detector_presence_config_t
Definition: acc_detector_presence.h:41
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:254
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:52
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:51
acc_integration_mem_free
void acc_integration_mem_free(void *ptr)
Free dynamic memory.
Definition: acc_integration_stm32.c:602
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:34
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.
cargo_result_t::presence_detected
bool presence_detected
Definition: example_cargo.h:134
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.
print_result
static void print_result(acc_detector_presence_result_t *result, const char *config_name)
Definition: example_detector_presence_multiple_configurations.c:289
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.