example_service_calibration_caching.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2020-2023
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"
15 #include "acc_hal_definitions_a121.h"
16 #include "acc_hal_integration_a121.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 /** \example example_service_calibration_caching.c
25 * @brief This is an example on how the sensor calibration can be cached
26 * @n
27 * The example executes as follows:
28 * - Retrieve and register HAL stuct
29 * - Create config, processing, and sensor instances
30 * - Calibrate and prepare sensor
31 * - Measure, read and process radar data
32 * - Check 'calibration_needed' indication
33 * - If needed either use cached calibration or perform new calibration and store
34 * - Destroy sensor, processing, and config instances
35 */
36
37 #define SENSOR_ID (1U)
38 #define SENSOR_TIMEOUT_MS (1000U)
39 #define MAX_DATA_ENTRY_LEN (15U) // "-32000+-32000i" + zero termination
40
41 /**
42 * A sensor calibration is valid at the temperature it was done +- 15 degrees
43 *
44 * If the temperature isn't controlled during caching, which is the case in this example,
45 * the maximum amount of caches needs to be calculated from a temperature difference of 16.
46 *
47 * For example
48 * - A calibration is done at 25 degrees, which means it is valid between 10 and 40 degrees
49 * - The temperature changes to 41 degrees
50 * - A new calibration needs to be done at 41 degrees since it is above the valid range for the previous calibration
51 * - The new calibration is then valid between 26 and 56 degrees
52 *
53 * However, if the temperature is controlled, for example in a factory, the maximum amount
54 * of caches can be calculated from a temperature difference of 30.
55 *
56 * For example
57 * - A calibration is done at 25 degrees, which means it is valid between 10 and 40 degrees
58 * - The temperature is manually changed to 55 degrees
59 * - A new calibration can be done which is valid between 40 and 70 degrees
60 *
61 * The maximum temperature variation that the application will operate in is assumed to be
62 * -40 to 85 degrees in this example.
63 */
64 #define MAX_CAL_TEMP_DIFF (16)
65 #define MAX_TEMP_VARIATION (125)
66 #define MAX_CACHE_COUNT ((MAX_TEMP_VARIATION / MAX_CAL_TEMP_DIFF) + 1)
67
68 // Note that each 'acc_cal_result_t' struct should be suitably aligned for any built-in type
69 static acc_cal_result_t cal_results[MAX_CACHE_COUNT];
70 static int16_t cal_temps[MAX_CACHE_COUNT];
71 static uint16_t curr_cache_count = 0;
72
73 static void set_config(acc_config_t *config);
74
75 static bool find_cache_index(int16_t temp, uint16_t *cache_index);
76
77 static bool get_next_empty_cache_index(uint16_t *cache_index);
78
79 static bool add_cache(uint16_t cache_index, int16_t temp);
80
81 static bool calibration_caching_and_prepare(acc_sensor_t *sensor,
82 acc_config_t *config,
83 void *buffer,
84 uint32_t buffer_size,
85 bool temp_known,
86 int16_t temp);
87
88 static void cleanup(acc_config_t *config, acc_processing_t *processing, acc_sensor_t *sensor, void *buffer);
89
90 int acconeer_main(int argc, char *argv[]);
91
92 int acconeer_main(int argc, char *argv[])
93 {
94 (void)argc;
95 (void)argv;
96 acc_config_t *config = NULL;
97 acc_processing_t *processing = NULL;
98 acc_sensor_t *sensor = NULL;
99 void *buffer = NULL;
100 uint32_t buffer_size = 0;
101 acc_processing_metadata_t proc_meta;
102 acc_processing_result_t proc_result;
103
104 printf("Acconeer software version %s\n", acc_version_get());
105
106 const acc_hal_a121_t *hal = acc_hal_rss_integration_get_implementation();
107
108 if (!acc_rss_hal_register(hal))
109 {
110 return EXIT_FAILURE;
111 }
112
113 config = acc_config_create();
114 if (config == NULL)
115 {
116 printf("acc_config_create() failed\n");
117 cleanup(config, processing, sensor, buffer);
118 return EXIT_FAILURE;
119 }
120
121 set_config(config);
122
123 // Print the configuration
124 acc_config_log(config);
125
126 processing = acc_processing_create(config, &proc_meta);
127 if (processing == NULL)
128 {
129 printf("acc_processing_create() failed\n");
130 cleanup(config, processing, sensor, buffer);
131 return EXIT_FAILURE;
132 }
133
134 if (!acc_rss_get_buffer_size(config, &buffer_size))
135 {
136 printf("acc_rss_get_buffer_size() failed\n");
137 cleanup(config, processing, sensor, buffer);
138 return EXIT_FAILURE;
139 }
140
141 buffer = acc_integration_mem_alloc(buffer_size);
142 if (buffer == NULL)
143 {
144 printf("buffer allocation failed\n");
145 cleanup(config, processing, sensor, buffer);
146 return EXIT_FAILURE;
147 }
148
149 acc_hal_integration_sensor_supply_on(SENSOR_ID);
150 acc_hal_integration_sensor_enable(SENSOR_ID);
151
152 sensor = acc_sensor_create(SENSOR_ID);
153 if (sensor == NULL)
154 {
155 printf("acc_sensor_create() failed\n");
156 cleanup(config, processing, sensor, buffer);
157 return EXIT_FAILURE;
158 }
159
160 if (!calibration_caching_and_prepare(sensor, config, buffer, buffer_size, false, 0))
161 {
162 printf("calibration_caching_and_prepare() failed\n");
163 acc_sensor_status(sensor);
164 cleanup(config, processing, sensor, buffer);
165 return EXIT_FAILURE;
166 }
167
168 uint32_t update_count = 5U;
169
170 for (uint32_t i = 0U; i < update_count; i++)
171 {
172
173 if (!acc_sensor_measure(sensor))
174 {
175 printf("acc_sensor_measure failed\n");
176 acc_sensor_status(sensor);
177 cleanup(config, processing, sensor, buffer);
178 return EXIT_FAILURE;
179 }
180
181 if (!acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS))
182 {
183 printf("Sensor interrupt timeout\n");
184 acc_sensor_status(sensor);
185 cleanup(config, processing, sensor, buffer);
186 return EXIT_FAILURE;
187 }
188
189 if (!acc_sensor_read(sensor, buffer, buffer_size))
190 {
191 printf("acc_sensor_read failed\n");
192 acc_sensor_status(sensor);
193 cleanup(config, processing, sensor, buffer);
194 return EXIT_FAILURE;
195 }
196
197 acc_processing_execute(processing, buffer, &proc_result);
198
199 if (proc_result.calibration_needed)
200 {
201 printf("New calibration needed due to temperature change\n");
202
203 if (!calibration_caching_and_prepare(sensor, config, buffer, buffer_size, true, proc_result.temperature))
204 {
205 printf("calibration_caching_and_prepare() failed\n");
206 acc_sensor_status(sensor);
207 cleanup(config, processing, sensor, buffer);
208 return EXIT_FAILURE;
209 }
210 }
211 else
212 {
213 printf("IQ data retrieved\n");
214 }
215 }
216
217 cleanup(config, processing, sensor, buffer);
218
219 printf("Application finished OK\n");
220
221 return EXIT_SUCCESS;
222 }
223
224 static void set_config(acc_config_t *config)
225 {
226 // Add configuration of the sensor here
227
228 acc_config_start_point_set(config, 80);
229 acc_config_num_points_set(config, 160);
230 }
231
232 static bool find_cache_index(int16_t temp, uint16_t *cache_index)
233 {
234 bool cache_found = false;
235 uint16_t min_temp_diff = UINT16_MAX;
236
237 // If caches have overlapping temperature ranges, the cache
238 // with center temperature closest to 'temp' will be chosen
239 for (uint16_t index = 0; index < curr_cache_count; index++)
240 {
241 uint16_t temp_diff = abs(cal_temps[index] - temp);
242
243 if (temp_diff < MAX_CAL_TEMP_DIFF && temp_diff < min_temp_diff)
244 {
245 min_temp_diff = temp_diff;
246 *cache_index = index;
247 cache_found = true;
248 }
249 }
250
251 return cache_found;
252 }
253
254 static bool get_next_empty_cache_index(uint16_t *cache_index)
255 {
256 *cache_index = curr_cache_count;
257
258 return curr_cache_count < MAX_CACHE_COUNT;
259 }
260
261 static bool add_cache(uint16_t cache_index, int16_t temp)
262 {
263 bool status = false;
264
265 if ((cache_index == curr_cache_count) && (cache_index < MAX_CACHE_COUNT))
266 {
267 cal_temps[cache_index] = temp;
268 curr_cache_count++;
269 status = true;
270 }
271
272 return status;
273 }
274
275 static bool calibration_caching_and_prepare(acc_sensor_t *sensor,
276 acc_config_t *config,
277 void *buffer,
278 uint32_t buffer_size,
279 bool temp_known,
280 int16_t temp)
281 {
282 bool status = true;
283 bool use_cache = false;
284 uint16_t cache_index = 0;
285
286 // If temperature is known, there might be a cached calibration result that can be used
287 if (temp_known)
288 {
289 if (find_cache_index(temp, &cache_index))
290 {
291 use_cache = true;
292 }
293 }
294
295 // If no cached calibration can be used, a new calibration should be made
296 // and the result stored at an empty cache_index
297 if (!use_cache)
298 {
299 printf("New calibration is performed\n");
300
301 if (!get_next_empty_cache_index(&cache_index))
302 {
303 printf("No empty cache_index to store calibration result\n");
304 return false;
305 }
306
307 status = false;
308 bool cal_complete = false;
309 const uint16_t calibration_retries = 1U;
310
311 // Random disturbances may cause the calibration to fail. At failure, retry at least once.
312 for (uint16_t i = 0; !status && (i <= calibration_retries); i++)
313 {
314 // Reset sensor before calibration by disabling/enabling it
315 acc_hal_integration_sensor_disable(SENSOR_ID);
316 acc_hal_integration_sensor_enable(SENSOR_ID);
317
318 do
319 {
320 status = acc_sensor_calibrate(sensor, &cal_complete, &cal_results[cache_index], buffer, buffer_size);
321
322 if (status && !cal_complete)
323 {
324 status = acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS);
325 }
326 } while (status && !cal_complete);
327 }
328
329 if (status)
330 {
331 // Reset sensor after calibration by disabling/enabling it
332 acc_hal_integration_sensor_disable(SENSOR_ID);
333 acc_hal_integration_sensor_enable(SENSOR_ID);
334
335 acc_cal_info_t cal_info;
336 acc_sensor_get_cal_info(&cal_results[cache_index], &cal_info);
337 add_cache(cache_index, cal_info.temperature);
338 }
339 }
340 else
341 {
342 printf("Using cached calibration for %u degrees Celsius\n", cal_temps[cache_index]);
343 }
344
345 if (status)
346 {
347 status = acc_sensor_prepare(sensor, config, &cal_results[cache_index], buffer, buffer_size);
348 }
349
350 return status;
351 }
352
353 static void cleanup(acc_config_t *config, acc_processing_t *processing, acc_sensor_t *sensor, void *buffer)
354 {
355 acc_hal_integration_sensor_disable(SENSOR_ID);
356 acc_hal_integration_sensor_supply_off(SENSOR_ID);
357
358 if (sensor != NULL)
359 {
360 acc_sensor_destroy(sensor);
361 }
362
363 if (processing != NULL)
364 {
365 acc_processing_destroy(processing);
366 }
367
368 if (config != NULL)
369 {
370 acc_config_destroy(config);
371 }
372
373 if (buffer != NULL)
374 {
375 acc_integration_mem_free(buffer);
376 }
377 }
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.
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_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_stm32cube_xm.c:99
acc_cal_info_t
Information about calibration.
Definition: acc_definitions_a121.h:38
acc_processing_result_t
Result provided by the processing module.
Definition: acc_processing.h:71
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_service_calibration_caching.c:38
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_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_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:30
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.
get_next_empty_cache_index
static bool get_next_empty_cache_index(uint16_t *cache_index)
Definition: example_service_calibration_caching.c:254
acc_config_create
acc_config_t * acc_config_create(void)
Create a configuration.
acc_integration.h
config
cargo_config_t config
Definition: i2c_example_cargo.c:34
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_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
buffer_size
uint32_t buffer_size
Definition: i2c_example_cargo.c:41
sensor
acc_sensor_t * sensor
Definition: i2c_example_cargo.c:33
add_cache
static bool add_cache(uint16_t cache_index, int16_t temp)
Definition: example_service_calibration_caching.c:261
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
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_service_calibration_caching.c:92
acc_hal_integration_a121.h
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:24
printf
#define printf
Definition: printf.h:60
MAX_CACHE_COUNT
#define MAX_CACHE_COUNT
Definition: example_service_calibration_caching.c:66
acc_sensor_get_cal_info
bool acc_sensor_get_cal_info(const acc_cal_result_t *cal_result, acc_cal_info_t *cal_info)
Gets calibration information from a calibration result.
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_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
find_cache_index
static bool find_cache_index(int16_t temp, uint16_t *cache_index)
Definition: example_service_calibration_caching.c:232
cal_results
static acc_cal_result_t cal_results[(((125)/(16))+1)]
Definition: example_service_calibration_caching.c:69
cleanup
static void cleanup(acc_config_t *config, acc_processing_t *processing, acc_sensor_t *sensor, void *buffer)
Definition: example_service_calibration_caching.c:353
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_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.
calibration_caching_and_prepare
static bool calibration_caching_and_prepare(acc_sensor_t *sensor, acc_config_t *config, void *buffer, uint32_t buffer_size, bool temp_known, int16_t temp)
Definition: example_service_calibration_caching.c:275
SENSOR_ID
#define SENSOR_ID
Definition: example_service_calibration_caching.c:37
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_stm32.c:602
acc_definitions_common.h
acc_processing_result_t::temperature
int16_t temperature
Definition: acc_processing.h:89
acc_config_log
void acc_config_log(const acc_config_t *config)
Print a configuration to the log.
acc_config.h
curr_cache_count
static uint16_t curr_cache_count
Definition: example_service_calibration_caching.c:71
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.
cal_temps
static int16_t cal_temps[(((125)/(16))+1)]
Definition: example_service_calibration_caching.c:70
acc_processing_result_t::calibration_needed
bool calibration_needed
Definition: acc_processing.h:84
acc_cal_info_t::temperature
int16_t temperature
Definition: acc_definitions_a121.h:40
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.
set_config
static void set_config(acc_config_t *config)
Definition: example_service_calibration_caching.c:224
acc_processing.h
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
MAX_CAL_TEMP_DIFF
#define MAX_CAL_TEMP_DIFF
Definition: example_service_calibration_caching.c:64
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.