example_service.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2020-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_config.h"
13 #include "acc_definitions_a121.h"
14 #include "acc_definitions_common.h"
17 #include "acc_integration.h"
18 #include "acc_processing.h"
19 #include "acc_rss_a121.h"
20 #include "acc_sensor.h"
21 
22 #include "acc_version.h"
23 
24 
25 /** \example example_service.c
26  * @brief This is an example on how the Service API can be used
27  * @n
28  * The example executes as follows:
29  * - Create a configuration
30  * - Create a processing instance using the previously created configuration
31  * - Create a sensor instance
32  * - Prepare a sensor
33  * - Perform a sensor measurement and read out the data
34  * - Process the measurement
35  * - Check 'calibration_needed' indication
36  * - Destroy the sensor instance
37  * - Destroy the processing instance
38  * - Destroy the configuration
39  */
40 
41 #define SENSOR_ID (1U)
42 #define SENSOR_TIMEOUT_MS (1000U)
43 #define MAX_DATA_ENTRY_LEN (15U) // "-32000+-32000i" + zero termination
44 
45 static void set_config(acc_config_t *config);
46 
47 
48 static bool do_sensor_calibration_and_prepare(acc_sensor_t *sensor, acc_config_t *config, void *buffer, uint32_t buffer_size);
49 
50 
51 static void print_data(acc_int16_complex_t *data, uint16_t data_length);
52 
53 
54 static void cleanup(acc_config_t *config, acc_processing_t *processing,
55  acc_sensor_t *sensor, void *buffer);
56 
57 
58 int app_main(int argc, char *argv[]);
59 
60 
61 int app_main(int argc, char *argv[])
62 {
63  (void)argc;
64  (void)argv;
65  acc_config_t *config = NULL;
66  acc_processing_t *processing = NULL;
67  acc_sensor_t *sensor = NULL;
68  void *buffer = NULL;
69  uint32_t buffer_size = 0;
70  acc_processing_metadata_t proc_meta;
71  acc_processing_result_t proc_result;
72 
73  printf("Acconeer software version %s\n", acc_version_get());
74 
76 
78  {
79  return EXIT_FAILURE;
80  }
81 
82  config = acc_config_create();
83  if (config == NULL)
84  {
85  printf("acc_config_create() failed\n");
86  cleanup(config, processing, sensor, buffer);
87  return EXIT_FAILURE;
88  }
89 
90  set_config(config);
91 
92  // Print the configuration
93  acc_config_log(config);
94 
95  processing = acc_processing_create(config, &proc_meta);
96  if (processing == NULL)
97  {
98  printf("acc_processing_create() failed\n");
99  cleanup(config, processing, sensor, buffer);
100  return EXIT_FAILURE;
101  }
102 
103  if (!acc_rss_get_buffer_size(config, &buffer_size))
104  {
105  printf("acc_rss_get_buffer_size() failed\n");
106  cleanup(config, processing, sensor, buffer);
107  return EXIT_FAILURE;
108  }
109 
110  buffer = acc_integration_mem_alloc(buffer_size);
111  if (buffer == NULL)
112  {
113  printf("buffer allocation failed\n");
114  cleanup(config, processing, sensor, buffer);
115  return EXIT_FAILURE;
116  }
117 
120 
121  sensor = acc_sensor_create(SENSOR_ID);
122  if (sensor == NULL)
123  {
124  printf("acc_sensor_create() failed\n");
125  cleanup(config, processing, sensor, buffer);
126  return EXIT_FAILURE;
127  }
128 
129  if (!do_sensor_calibration_and_prepare(sensor, config, buffer, buffer_size))
130  {
131  printf("do_sensor_calibration_and_prepare() failed\n");
132  acc_sensor_status(sensor);
133  cleanup(config, processing, sensor, buffer);
134  return EXIT_FAILURE;
135  }
136 
137  const uint32_t update_count = 5U;
138 
139  for (uint32_t i = 0U; i < update_count; i++)
140  {
141  if (!acc_sensor_measure(sensor))
142  {
143  printf("acc_sensor_measure failed\n");
144  acc_sensor_status(sensor);
145  cleanup(config, processing, sensor, buffer);
146  return EXIT_FAILURE;
147  }
148 
150  {
151  printf("Sensor interrupt timeout\n");
152  acc_sensor_status(sensor);
153  cleanup(config, processing, sensor, buffer);
154  return EXIT_FAILURE;
155  }
156 
157  if (!acc_sensor_read(sensor, buffer, buffer_size))
158  {
159  printf("acc_sensor_read failed\n");
160  acc_sensor_status(sensor);
161  cleanup(config, processing, sensor, buffer);
162  return EXIT_FAILURE;
163  }
164 
165  acc_processing_execute(processing, buffer, &proc_result);
166 
167  if (proc_result.calibration_needed)
168  {
169  printf("The current calibration is not valid for the current temperature.\n");
170  printf("The sensor needs to be re-calibrated.\n");
171 
172  if (!do_sensor_calibration_and_prepare(sensor, config, buffer, buffer_size))
173  {
174  printf("do_sensor_calibration_and_prepare() failed\n");
175  acc_sensor_status(sensor);
176  cleanup(config, processing, sensor, buffer);
177  return EXIT_FAILURE;
178  }
179 
180  printf("The sensor was successfully re-calibrated.\n");
181  }
182  else
183  {
184  print_data(proc_result.frame, proc_meta.frame_data_length);
185  }
186  }
187 
188  cleanup(config, processing, sensor, buffer);
189 
190  printf("Application finished OK\n");
191 
192  return EXIT_SUCCESS;
193 }
194 
195 
196 static void set_config(acc_config_t *config)
197 {
198  // Add configuration of the sensor here
199 
200  acc_config_start_point_set(config, 80);
201  acc_config_num_points_set(config, 160);
202 }
203 
204 
205 static bool do_sensor_calibration_and_prepare(acc_sensor_t *sensor, acc_config_t *config, void *buffer, uint32_t buffer_size)
206 {
207  bool status = false;
208  bool cal_complete = false;
209  acc_cal_result_t cal_result;
210  const uint16_t calibration_retries = 1U;
211 
212  // Random disturbances may cause the calibration to fail. At failure, retry at least once.
213  for (uint16_t i = 0; !status && (i <= calibration_retries); i++)
214  {
215  // Reset sensor before calibration by disabling/enabling it
218 
219  do
220  {
221  status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
222 
223  if (status && !cal_complete)
224  {
226  }
227  } while (status && !cal_complete);
228  }
229 
230  if (status)
231  {
232  // Reset sensor after calibration by disabling/enabling it
235 
236  status = acc_sensor_prepare(sensor, config, &cal_result, buffer, buffer_size);
237  }
238 
239  return status;
240 }
241 
242 
243 static void print_data(acc_int16_complex_t *data, uint16_t data_length)
244 {
245  printf("Processed data:\n");
246  char buffer[MAX_DATA_ENTRY_LEN];
247 
248  for (uint16_t i = 0; i < data_length; i++)
249  {
250  if ((i > 0) && ((i % 8) == 0))
251  {
252  printf("\n");
253  }
254 
255  snprintf(buffer, sizeof(buffer), "%" PRIi16 "+%" PRIi16 "i", data[i].real, data[i].imag);
256 
257  printf("%14s ", buffer);
258  }
259 
260  printf("\n");
261 }
262 
263 
264 static void cleanup(acc_config_t *config, acc_processing_t *processing,
265  acc_sensor_t *sensor, void *buffer)
266 {
269 
270  if (sensor != NULL)
271  {
272  acc_sensor_destroy(sensor);
273  }
274 
275  if (processing != NULL)
276  {
277  acc_processing_destroy(processing);
278  }
279 
280  if (config != NULL)
281  {
282  acc_config_destroy(config);
283  }
284 
285  if (buffer != NULL)
286  {
287  acc_integration_mem_free(buffer);
288  }
289 }
acc_config_start_point_set
void acc_config_start_point_set(acc_config_t *config, int32_t start_point)
Set the starting point of the sweep.
MAX_DATA_ENTRY_LEN
#define MAX_DATA_ENTRY_LEN
Definition: example_service.c:43
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_processing_destroy
void acc_processing_destroy(acc_processing_t *handle)
Destroy a processing instance identified with the provided processing handle.
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_processing_result_t
Result provided by the processing module.
Definition: acc_processing.h:71
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
set_config
static void set_config(acc_config_t *config)
Definition: example_service.c:196
acc_version.h
acc_rss_get_buffer_size
bool acc_rss_get_buffer_size(const acc_config_t *config, uint32_t *buffer_size)
Get the buffer size needed for the specified config.
acc_int16_complex_t
Data type for interger-based representation of complex numbers.
Definition: acc_definitions_common.h:43
acc_config_destroy
void acc_config_destroy(acc_config_t *config)
Destroy a configuration freeing any resources allocated.
acc_cal_result_t
The result from a completed calibration.
Definition: acc_definitions_a121.h:32
acc_processing_execute
void acc_processing_execute(acc_processing_t *handle, void *buffer, acc_processing_result_t *result)
Process the data according to the configuration used in create.
acc_config_create
acc_config_t * acc_config_create(void)
Create a configuration.
acc_integration.h
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_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_esp32.c:38
acc_processing_metadata_t
Metadata that will be populated by the processing module during creation.
Definition: acc_processing.h:36
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
cleanup
static void cleanup(acc_config_t *config, acc_processing_t *processing, acc_sensor_t *sensor, void *buffer)
Definition: example_service.c:264
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
app_main
int app_main(int argc, char *argv[])
Assembly test example.
Definition: example_service.c:61
acc_processing_metadata_t::frame_data_length
uint16_t frame_data_length
Definition: acc_processing.h:39
acc_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
acc_config_t
struct acc_config acc_config_t
Definition: acc_config.h:26
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_config_num_points_set
void acc_config_num_points_set(acc_config_t *config, uint16_t num_points)
Set the number of data points to measure.
acc_hal_definitions_a121.h
acc_processing_result_t::frame
acc_int16_complex_t * frame
Definition: acc_processing.h:91
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_processing_t
struct acc_processing_handle acc_processing_t
Definition: acc_processing.h:30
acc_sensor_status
void acc_sensor_status(const acc_sensor_t *sensor)
Check the status of the sensor.
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_service.c:42
print_data
static void print_data(acc_int16_complex_t *data, uint16_t data_length)
Definition: example_service.c:243
hal
static const acc_hal_a121_t hal
Definition: acc_hal_integration_espidf_xe121.c:121
acc_sensor_prepare
bool acc_sensor_prepare(acc_sensor_t *sensor, const acc_config_t *config, const acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size)
Prepare a sensor to do a measurement.
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_config_log
void acc_config_log(const acc_config_t *config)
Print a configuration to the log.
acc_config.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.
acc_processing_result_t::calibration_needed
bool calibration_needed
Definition: acc_processing.h:84
acc_sensor_measure
bool acc_sensor_measure(acc_sensor_t *sensor)
Start a radar measurement with previously prepared configuration.
acc_processing_create
acc_processing_t * acc_processing_create(const acc_config_t *config, acc_processing_metadata_t *processing_metadata)
Create a processing instance with the provided configuration.
acc_processing.h
acc_sensor_t
struct acc_sensor acc_sensor_t
Definition: acc_sensor.h:31
SENSOR_ID
#define SENSOR_ID
Definition: example_service.c:41
acc_sensor_destroy
void acc_sensor_destroy(acc_sensor_t *sensor)
Destroy a sensor instance freeing any resources allocated.
acc_definitions_a121.h
do_sensor_calibration_and_prepare
static bool do_sensor_calibration_and_prepare(acc_sensor_t *sensor, acc_config_t *config, void *buffer, uint32_t buffer_size)
Definition: example_service.c:205
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.