example_cargo_main.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 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 <stdio.h>
9 #include <stdlib.h>
10 
11 #include "acc_config.h"
14 #include "acc_integration.h"
15 #include "acc_integration_log.h"
16 #include "acc_rss_a121.h"
17 #include "acc_sensor.h"
18 #include "acc_version.h"
19 #include "example_cargo.h"
20 #include "example_cargo_main.h"
21 
22 #define SENSOR_ID (1U)
23 #define SENSOR_TIMEOUT_MS (1000U)
24 
25 /**
26  * @brief Frees any allocated resources
27  */
29 
30 /**
31  * @brief Performs sensor calibration (with retry)
32  */
33 static bool do_sensor_calibration(acc_sensor_t *sensor, void *buffer, uint32_t buffer_size, acc_cal_result_t *cal_result);
34 
35 /**
36  * @brief Performs cargo calibration (with retry)
37  */
40  void *buffer,
41  uint32_t buffer_size,
46 
47 /**
48  * @brief Print a processor result in a human-readable format
49  */
50 static void print_cargo_result(const cargo_result_t *result, bool result_available);
51 
52 int acconeer_main(int argc, char *argv[])
53 {
54  (void)argc;
55  (void)argv;
56  acc_sensor_t *sensor = NULL;
58 
59  void *buffer = NULL;
60  uint32_t buffer_size = 0;
61 
62  uint32_t distance_cal_result_static_size = 0U;
63  uint8_t *distance_cal_result_static = NULL;
65 
67  cargo_result_t cargo_result = {0};
68 
69  cargo_config_t cargo_config = {0};
70 
71  printf("Acconeer software version %s\n", acc_version_get());
72 
74 
75  if (!acc_rss_hal_register(hal))
76  {
77  return EXIT_FAILURE;
78  }
79 
81 
82  cargo_config_log(&cargo_config);
83 
84  cargo_handle = cargo_handle_create(&cargo_config);
85  if (cargo_handle == NULL)
86  {
87  printf("cargo_handle_create() failed\n");
89  return EXIT_FAILURE;
90  }
91 
92  acc_detector_presence_metadata_t presence_metadata = {0};
93  if (cargo_presence_metadata_get(cargo_handle, &presence_metadata))
94  {
95  printf("Presence metadata\n");
96  printf("presence_metadata.start_m: %" PRIfloat "\n", ACC_LOG_FLOAT_TO_INTEGER(presence_metadata.start_m));
97  printf("presence_metadata.end_m: %" PRIfloat "\n", ACC_LOG_FLOAT_TO_INTEGER(presence_metadata.end_m));
98  printf("presence_metadata.step_length_m: %" PRIfloat "\n", ACC_LOG_FLOAT_TO_INTEGER(presence_metadata.step_length_m));
99  printf("presence_metadata.num_points: %" PRIu16 "\n", presence_metadata.num_points);
100  }
101  else
102  {
103  printf("Cargo is not configured to have presence activated.\n");
104  }
105 
107  {
108  printf("cargo_get_buffer_size() failed\n");
110  return EXIT_FAILURE;
111  }
112 
114  {
115  printf("cargo_get_distance_cal_result_static_size() failed\n");
117  return EXIT_FAILURE;
118  }
119 
121  if (buffer == NULL)
122  {
123  printf("buffer allocation failed\n");
125  return EXIT_FAILURE;
126  }
127 
129  {
131  if (distance_cal_result_static == NULL)
132  {
133  printf("distance_cal_result_static buffer allocation failed\n");
135  return EXIT_FAILURE;
136  }
137  }
138 
141 
143  if (sensor == NULL)
144  {
145  printf("acc_sensor_create() failed\n");
147  return EXIT_FAILURE;
148  }
149 
151  {
152  printf("do_sensor_calibration() failed\n");
155  return EXIT_FAILURE;
156  }
157 
158  // Reset sensor after calibration by disabling/enabling it
161 
163  cargo_handle,
164  buffer,
165  buffer_size,
170  {
171  printf("do_cargo_calibration() failed\n");
174  return EXIT_FAILURE;
175  }
176 
177  while (true)
178  {
179  if (cargo_config.activate_utilization_level)
180  {
182  {
183  printf("cargo_prepare_utilization() failed\n");
186  return EXIT_FAILURE;
187  }
188 
189  do
190  {
192  {
193  printf("acc_sensor_measure() failed\n");
196  return EXIT_FAILURE;
197  }
198 
200  {
201  printf("Sensor interrupt timeout\n");
204  return EXIT_FAILURE;
205  }
206 
208  {
209  printf("acc_sensor_read() failed\n");
212  return EXIT_FAILURE;
213  }
214 
215  bool result_available = false;
219  buffer,
220  &cargo_result,
221  &result_available,
222  NULL))
223  {
224  printf("cargo_process_utilization() failed\n");
227  return EXIT_FAILURE;
228  }
229 
230  print_cargo_result(&cargo_result, result_available);
231 
233  }
234  if (cargo_config.activate_presence)
235  {
237  {
238  printf("cargo_prepare_presence() failed\n");
241  return EXIT_FAILURE;
242  }
243 
244  do
245  {
247  {
248  printf("acc_sensor_measure() failed\n");
251  return EXIT_FAILURE;
252  }
253 
255  {
256  printf("Sensor interrupt timeout\n");
259  return EXIT_FAILURE;
260  }
261 
263  {
264  printf("acc_sensor_read() failed\n");
267  return EXIT_FAILURE;
268  }
269 
270  if (!cargo_process_presence(cargo_handle, buffer, &cargo_result, NULL))
271  {
272  printf("cargo_process_presence() failed\n");
275  return EXIT_FAILURE;
276  }
277 
278  print_cargo_result(&cargo_result, true);
279 
281  }
282  }
283 
285 
286  printf("Application finished OK\n");
287 
288  return EXIT_SUCCESS;
289 }
290 
292 {
295 
296  if (sensor != NULL)
297  {
299  }
300 
301  if (buffer != NULL)
302  {
304  }
305 
306  if (distance_cal_result_static != NULL)
307  {
309  }
310 
311  if (handle != NULL)
312  {
314  }
315 }
316 
317 static bool do_sensor_calibration(acc_sensor_t *sensor, void *buffer, uint32_t buffer_size, acc_cal_result_t *cal_result)
318 {
319  bool status = false;
320  bool cal_complete = false;
321  const uint16_t calibration_retries = 1U;
322 
323  // Random disturbances may cause the calibration to fail. At failure, retry at least once.
324  for (uint16_t i = 0; !status && (i <= calibration_retries); i++)
325  {
326  // Reset sensor before calibration by disabling/enabling it
329 
330  do
331  {
332  status = acc_sensor_calibrate(sensor, &cal_complete, cal_result, buffer, buffer_size);
333 
334  if (status && !cal_complete)
335  {
337  }
338  } while (status && !cal_complete);
339  }
340 
341  return status;
342 }
343 
346  void *buffer,
347  uint32_t buffer_size,
352 {
353  bool status = true;
354  bool calibration_done = false;
355 
356  while (status && !calibration_done)
357  {
358  status = cargo_calibrate(cargo_handle,
359  sensor,
361  buffer,
362  buffer_size,
366  &calibration_done);
367 
368  if (status && !calibration_done)
369  {
371  }
372  }
373 
374  return status;
375 }
376 
377 static void print_cargo_result(const cargo_result_t *result, bool result_available)
378 {
379  static uint32_t frameno = 0U;
380  frameno++;
381 
382  printf("frameno %" PRIu32 "\n", frameno);
383 
384  if (result_available)
385  {
387  {
388  printf("distance: %" PRIfloat "\n", ACC_LOG_FLOAT_TO_INTEGER(result->distance));
389  printf("level_m: %" PRIfloat "\n", ACC_LOG_FLOAT_TO_INTEGER(result->level_m));
390  printf("level_percent: %" PRIfloat "\n", ACC_LOG_FLOAT_TO_INTEGER(result->level_percent));
391  }
392 
393  if (result->presence_valid)
394  {
395  printf("presence_detected: %s\n", result->presence_detected ? "true" : "false");
396  printf("inter_presence_score: %" PRIfloat "\n", ACC_LOG_FLOAT_TO_INTEGER(result->inter_presence_score));
397  printf("intra_presence_score: %" PRIfloat "\n", ACC_LOG_FLOAT_TO_INTEGER(result->intra_presence_score));
398  }
399  }
400 }
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
do_cargo_calibration
static bool do_cargo_calibration(acc_sensor_t *sensor, cargo_handle_t *cargo_handle, void *buffer, uint32_t buffer_size, acc_cal_result_t *sensor_cal_result, uint8_t *distance_cal_result_static, uint32_t distance_cal_result_static_size, acc_detector_cal_result_dynamic_t *distance_cal_result_dynamic)
Performs cargo calibration (with retry)
Definition: example_cargo_main.c:344
cargo_handle_create
cargo_handle_t * cargo_handle_create(const cargo_config_t *cargo_config)
Create a cargo handle.
Definition: example_cargo.c:164
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
distance_cal_result_static_size
uint32_t distance_cal_result_static_size
Definition: i2c_example_cargo.c:39
cargo_process_presence
bool cargo_process_presence(cargo_handle_t *handle, void *buffer, cargo_result_t *cargo_result, acc_detector_presence_result_t *nullable_presence_result)
Process Sparse IQ data into a Cargo result.
Definition: example_cargo.c:558
CARGO_MODE_UTILIZATION
@ CARGO_MODE_UTILIZATION
Definition: example_cargo.h:156
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_cargo_main.c:23
cargo_prepare_presence
bool cargo_prepare_presence(cargo_handle_t *handle, acc_sensor_t *sensor, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size)
Prepare Cargo presence for measurements.
Definition: example_cargo.c:423
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
acc_detector_presence_metadata_t::step_length_m
float step_length_m
Definition: acc_detector_presence.h:108
sensor_cal_result
acc_cal_result_t sensor_cal_result
Definition: i2c_example_cargo.c:36
cargo_presence_metadata_get
bool cargo_presence_metadata_get(cargo_handle_t *handle, acc_detector_presence_metadata_t *presence_metadata)
Get presence detector metadata.
Definition: example_cargo.c:634
cargo_result_t::distance
float distance
Definition: example_cargo.h:118
acc_cal_result_t
The result from a completed calibration.
Definition: acc_definitions_a121.h:30
distance_cal_result_static
uint8_t * distance_cal_result_static
Definition: i2c_example_cargo.c:38
acc_detector_presence_metadata_t::start_m
float start_m
Definition: acc_detector_presence.h:95
example_cargo_main.h
cargo_result_t::level_m
float level_m
Definition: example_cargo.h:123
acc_integration.h
cargo_config_initialize
void cargo_config_initialize(cargo_config_t *cargo_config, cargo_preset_t preset)
Initialize cargo config struct with a preset.
Definition: example_cargo.c:80
cargo_config_t
Configuration struct for cargo.
Definition: example_cargo.h:43
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
cargo_result_t::level_percent
float level_percent
Definition: example_cargo.h:128
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
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
sensor
acc_sensor_t * sensor
Definition: i2c_example_cargo.c:33
acc_detector_presence_metadata_t::num_points
uint16_t num_points
Definition: acc_detector_presence.h:115
cargo_calibrate
bool cargo_calibrate(cargo_handle_t *handle, acc_sensor_t *sensor, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size, uint8_t *distance_cal_result_static, uint32_t distance_cal_result_static_size, acc_detector_cal_result_dynamic_t *distance_cal_result_dynamic, bool *calibration_complete)
Calibrate Cargo.
Definition: example_cargo.c:318
example_cargo.h
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
cargo_result_t::utilization_valid
bool utilization_valid
Definition: example_cargo.h:115
do_sensor_calibration
static bool do_sensor_calibration(acc_sensor_t *sensor, void *buffer, uint32_t buffer_size, acc_cal_result_t *cal_result)
Performs sensor calibration (with retry)
Definition: example_cargo_main.c:317
acc_hal_integration_a121.h
handle
cargo_handle_t * handle
Definition: i2c_example_cargo.c:35
cargo_result_t::presence_valid
bool presence_valid
Definition: example_cargo.h:131
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
acc_detector_presence_metadata_t::end_m
float end_m
Definition: acc_detector_presence.h:101
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
cargo_get_buffer_size
bool cargo_get_buffer_size(const cargo_handle_t *handle, uint32_t *buffer_size)
Get the required buffer sensor data buffer size.
Definition: example_cargo.c:272
cargo_get_distance_cal_result_static_size
bool cargo_get_distance_cal_result_static_size(const cargo_handle_t *handle, uint32_t *distance_cal_result_static_size)
Get the required buffer for static distance detector calibration.
Definition: example_cargo.c:298
cargo_config_t::activate_utilization_level
bool activate_utilization_level
Definition: example_cargo.h:49
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
SENSOR_ID
#define SENSOR_ID
Definition: example_cargo_main.c:22
acc_integration_log.h
ACC_LOG_FLOAT_TO_INTEGER
#define ACC_LOG_FLOAT_TO_INTEGER(a)
Definition: acc_integration_log.h:26
cargo_config_log
void cargo_config_log(const cargo_config_t *cargo_config)
Log cargo config to debug uart.
Definition: example_cargo.c:135
cargo_handle_destroy
void cargo_handle_destroy(cargo_handle_t *handle)
Destroy a cargo handle.
Definition: example_cargo.c:240
acc_sensor_status
void acc_sensor_status(const acc_sensor_t *sensor)
Check the status of the sensor.
acc_integration_mem_free
void acc_integration_mem_free(void *ptr)
Free dynamic memory.
Definition: acc_integration_stm32.c:602
cleanup
static void cleanup(acc_sensor_t *sensor, void *buffer, uint8_t *distance_cal_result_static, cargo_handle_t *handle)
Frees any allocated resources.
Definition: example_cargo_main.c:291
cargo_process_utilization
bool cargo_process_utilization(cargo_handle_t *handle, uint8_t *distance_cal_result_static, acc_detector_cal_result_dynamic_t *distance_cal_result_dynamic, void *buffer, cargo_result_t *cargo_result, bool *result_available, acc_detector_distance_result_t *nullable_distance_result)
Process Sparse IQ data into a Cargo result.
Definition: example_cargo.c:462
acc_config.h
distance_cal_result_dynamic
acc_detector_cal_result_dynamic_t distance_cal_result_dynamic
Definition: i2c_example_cargo.c:37
cargo_current_mode_get
cargo_mode_t cargo_current_mode_get(const cargo_handle_t *handle)
Get current mode of the cargo handle.
Definition: example_cargo.c:622
cargo_handle
Cargo handle.
Definition: example_cargo.c:27
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
cargo_result_t
Result type.
Definition: example_cargo.h:109
cargo_result_t::presence_detected
bool presence_detected
Definition: example_cargo.h:134
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_cargo_main.c:52
print_cargo_result
static void print_cargo_result(const cargo_result_t *result, bool result_available)
Print a processor result in a human-readable format.
Definition: example_cargo_main.c:377
CARGO_PRESET_NO_LENS
@ CARGO_PRESET_NO_LENS
Definition: example_cargo.h:27
acc_sensor_measure
bool acc_sensor_measure(acc_sensor_t *sensor)
Start a radar measurement with previously prepared configuration.
cargo_prepare_utilization
bool cargo_prepare_utilization(cargo_handle_t *handle, acc_sensor_t *sensor, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size)
Prepare Cargo utilization for measurements.
Definition: example_cargo.c:384
acc_sensor_t
struct acc_sensor acc_sensor_t
Definition: acc_sensor.h:31
cargo_config_t::activate_presence
bool activate_presence
Definition: example_cargo.h:68
acc_sensor_destroy
void acc_sensor_destroy(acc_sensor_t *sensor)
Destroy a sensor instance freeing any resources allocated.
CARGO_MODE_PRESENCE
@ CARGO_MODE_PRESENCE
Definition: example_cargo.h:157
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.