example_vibration_main.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2023-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 <float.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <string.h>
12 
13 #include "acc_alg_basic_utils.h"
14 #include "acc_algorithm.h"
15 #include "acc_config.h"
16 #include "acc_definitions_a121.h"
17 #include "acc_definitions_common.h"
20 #include "acc_integration.h"
21 #include "acc_integration_log.h"
22 #include "acc_processing.h"
23 #include "acc_rss_a121.h"
24 #include "acc_sensor.h"
25 #include "acc_version.h"
26 
27 #include "example_vibration.h"
28 
29 /** \example example_vibration_main.c
30  * @brief This is an example on how the Service API can be used to measure
31  * vibrations
32  * @n
33  * The example executes as follows:
34  * - Set vibration configuration
35  * - Create vibration processing handle using config from previous step
36  * - Create a processing instance using an internally translated config from previous step
37  * - Allocate sensor buffer
38  * - Create a sensor instance
39  * - Calibrate & prepare the sensor
40  * - Loop:
41  * - Perform a sensor measurement and read out the data
42  * - Check & handle the 'calibration_needed' indication
43  * - Process the measurement and print vibration result
44  * - Destroy the sensor instance
45  * - Destroy the processing instance
46  * - Destroy the configuration
47  */
48 
49 #define SENSOR_ID (1U)
50 #define SENSOR_TIMEOUT_MS (1000U)
51 
52 #define DISPLACEMENT_HISTORY_COLUMN_WIDTH (16U)
53 
54 static bool do_sensor_calibration_and_prepare(acc_sensor_t *sensor, void *buffer, uint32_t buffer_size, const acc_config_t *sensor_config);
55 
57 
59 
60 int acconeer_main(int argc, char *argv[]);
61 
62 int acconeer_main(int argc, char *argv[])
63 {
64  (void)argc;
65  (void)argv;
66  acc_sensor_t *sensor = NULL;
67  void *buffer = NULL;
68  uint32_t buffer_size = 0U;
69  acc_processing_t *processing = NULL;
70  acc_processing_metadata_t proc_meta = {0};
71  acc_processing_result_t proc_result = {0};
72 
76 
77  /** Vibration example configuration **/
78  // Use a preset as a starting point. The call below will also initialize the config struct.
80 
81  // If needed, adjust configuration parameters before creating a vibration handle.
82  // Refer to the config struct definition in 'example_vibration.h' to see what parameters are available.
83 
84  printf("Acconeer software version %s\n", acc_version_get());
85 
87 
88  if (!acc_rss_hal_register(hal))
89  {
90  return EXIT_FAILURE;
91  }
92 
94 
95  /*
96  * The handle will use the config passed to it here
97  * for its lifetime. Make sure any changes to
98  * 'config' happens before this point.
99  */
101  if (handle == NULL)
102  {
103  printf("acc_vibration_handle_create() failed\n");
104  cleanup(sensor, processing, buffer, handle);
105  return EXIT_FAILURE;
106  }
107 
109 
111  if (processing == NULL)
112  {
113  printf("acc_processing_create() failed\n");
114  return EXIT_FAILURE;
115  }
116 
118  {
119  printf("acc_rss_get_buffer_size() failed\n");
120  cleanup(sensor, processing, buffer, handle);
121  return EXIT_FAILURE;
122  }
123 
125  if (buffer == NULL)
126  {
127  printf("buffer allocation failed\n");
128  cleanup(sensor, processing, buffer, handle);
129  return EXIT_FAILURE;
130  }
131 
134 
136  if (sensor == NULL)
137  {
138  printf("acc_sensor_create() failed\n");
139  return EXIT_FAILURE;
140  }
141 
143  {
144  printf("do_sensor_calibration_and_prepare() failed\n");
146  cleanup(sensor, processing, buffer, handle);
147  return EXIT_FAILURE;
148  }
149 
150  while (true)
151  {
153  {
154  printf("acc_sensor_measure failed\n");
156  cleanup(sensor, processing, buffer, handle);
157  return EXIT_FAILURE;
158  }
159 
161  {
162  printf("Sensor interrupt timeout\n");
164  cleanup(sensor, processing, buffer, handle);
165  return EXIT_FAILURE;
166  }
167 
169  {
170  printf("acc_sensor_read failed\n");
172  cleanup(sensor, processing, buffer, handle);
173  return EXIT_FAILURE;
174  }
175 
176  acc_processing_execute(processing, buffer, &proc_result);
177 
178  if (proc_result.calibration_needed)
179  {
180  printf("The current calibration is not valid for the current temperature.\n");
181  printf("The sensor needs to be re-calibrated.\n");
182 
184  {
185  printf("do_sensor_calibration_and_prepare() failed\n");
187  cleanup(sensor, processing, buffer, handle);
188  return EXIT_FAILURE;
189  }
190 
191  printf("The sensor was successfully re-calibrated.\n");
192  }
193  else
194  {
195  acc_vibration_process(&proc_result, handle, &config, &result);
197  }
198  }
199 
200  cleanup(sensor, processing, buffer, handle);
201 
202  printf("Application finished OK\n");
203 
204  return EXIT_SUCCESS;
205 }
206 
207 static bool do_sensor_calibration_and_prepare(acc_sensor_t *sensor, void *buffer, uint32_t buffer_size, const acc_config_t *sensor_config)
208 {
209  bool status = false;
210  bool cal_complete = false;
211  acc_cal_result_t cal_result = {0};
212 
213  const uint16_t calibration_retries = 1U;
214 
215  // Random disturbances may cause the calibration to fail. At failure, retry at least once.
216  for (uint16_t i = 0; !status && (i <= calibration_retries); i++)
217  {
218  // Reset sensor before calibration by disabling/enabling it
221 
222  do
223  {
224  status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
225 
226  if (status && !cal_complete)
227  {
229  }
230  } while (status && !cal_complete);
231  }
232 
233  if (status)
234  {
235  // Reset sensor after calibration by disabling/enabling it
238 
239  status = acc_sensor_prepare(sensor, sensor_config, &cal_result, buffer, buffer_size);
240  }
241 
242  return status;
243 }
244 
246 {
247  char buf[80] = "";
248  size_t buf_size = sizeof(buf);
249 
250  if (result->max_displacement != FLT_MAX)
251  {
252  snprintf(buf,
253  buf_size,
254  ", max_displacement=%" PRIfloat " um, max_displacement_freq=%" PRIfloat " Hz",
255  ACC_LOG_FLOAT_TO_INTEGER(result->max_displacement),
256  ACC_LOG_FLOAT_TO_INTEGER(result->max_displacement_freq));
257  }
258 
259  printf("Vibration: max_sweep_amplitude=%" PRIfloat " um%s\n", ACC_LOG_FLOAT_TO_INTEGER(result->max_sweep_amplitude), buf);
260 
261  bool continuous_data_acquisition = true;
262  if (acc_vibration_handle_continuous_data_acquisition_get(handle, &continuous_data_acquisition) && continuous_data_acquisition)
263  {
264  return;
265  }
266 
267  uint16_t num_elems = 0U;
268  const float *displacement_history = acc_vibration_handle_displacement_history_get(handle, &num_elems);
269 
270  if (displacement_history != NULL)
271  {
272  printf("\nDisplacement history:\n");
273 
274  for (uint16_t i = 0; i < num_elems; i++)
275  {
276  printf("%" PRIfloat " ", ACC_LOG_FLOAT_TO_INTEGER(displacement_history[i]));
277  if ((i % DISPLACEMENT_HISTORY_COLUMN_WIDTH) == 0)
278  {
279  printf("\n");
280  }
281  }
282 
283  printf("\n\n");
284  }
285 }
286 
288 {
291 
292  if (sensor != NULL)
293  {
295  }
296 
297  if (processing != NULL)
298  {
299  acc_processing_destroy(processing);
300  }
301 
302  if (buffer != NULL)
303  {
305  }
306 
307  if (handle != NULL)
308  {
310  }
311 }
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_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.
buffer
void * buffer
Definition: i2c_example_cargo.c:40
acc_version.h
acc_vibration_preset_set
void acc_vibration_preset_set(acc_vibration_config_t *config, acc_vibration_preset_t preset)
Definition: example_vibration.c:115
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_vibration_config_log
void acc_vibration_config_log(const acc_vibration_config_t *config)
Definition: example_vibration.c:170
acc_alg_basic_utils.h
acc_cal_result_t
The result from a completed calibration.
Definition: acc_definitions_a121.h:30
example_vibration.h
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_vibration_main.c:50
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.
SENSOR_ID
#define SENSOR_ID
Definition: example_vibration_main.c:49
DISPLACEMENT_HISTORY_COLUMN_WIDTH
#define DISPLACEMENT_HISTORY_COLUMN_WIDTH
Definition: example_vibration_main.c:52
acc_vibration_config_t
Vibration config container.
Definition: example_vibration.h:37
acc_vibration_handle
Definition: example_vibration.c:58
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_vibration_handle_create
acc_vibration_handle_t * acc_vibration_handle_create(const acc_vibration_config_t *config)
Definition: example_vibration.c:233
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_vibration_handle_displacement_history_get
const float * acc_vibration_handle_displacement_history_get(acc_vibration_handle_t *handle, uint16_t *num_elem)
Definition: example_vibration.c:355
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
acc_vibration_handle_sensor_config_get
const acc_config_t * acc_vibration_handle_sensor_config_get(acc_vibration_handle_t *handle)
Definition: example_vibration.c:350
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
handle
cargo_handle_t * handle
Definition: i2c_example_cargo.c:35
print_result
static void print_result(acc_vibration_handle_t *handle, acc_vibration_result_t *result)
Definition: example_vibration_main.c:245
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.
acc_config_t
struct acc_config acc_config_t
Definition: acc_config.h:24
printf
#define printf
Definition: printf.h:60
ACC_VIBRATION_PRESET_LOW_FREQUENCY
@ ACC_VIBRATION_PRESET_LOW_FREQUENCY
Definition: example_vibration.h:20
snprintf
#define snprintf
Definition: printf.h:84
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_vibration_process
void acc_vibration_process(acc_processing_result_t *proc_result, acc_vibration_handle_t *handle, acc_vibration_config_t *config, acc_vibration_result_t *result)
Definition: example_vibration.c:437
acc_hal_definitions_a121.h
acc_vibration_handle_destroy
void acc_vibration_handle_destroy(acc_vibration_handle_t *handle)
Definition: example_vibration.c:381
acc_vibration_result_t
Vibration processing result.
Definition: example_vibration.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_stm32cube_xm.c:119
acc_vibration_handle_continuous_data_acquisition_get
bool acc_vibration_handle_continuous_data_acquisition_get(acc_vibration_handle_t *handle, bool *continuous_data_acquisition)
Definition: example_vibration.c:369
do_sensor_calibration_and_prepare
static bool do_sensor_calibration_and_prepare(acc_sensor_t *sensor, void *buffer, uint32_t buffer_size, const acc_config_t *sensor_config)
Definition: example_vibration_main.c:207
acc_processing_t
struct acc_processing_handle acc_processing_t
Definition: acc_processing.h:30
acc_integration_log.h
ACC_LOG_FLOAT_TO_INTEGER
#define ACC_LOG_FLOAT_TO_INTEGER(a)
Definition: acc_integration_log.h:26
acc_sensor_status
void acc_sensor_status(const acc_sensor_t *sensor)
Check the status of the sensor.
acc_algorithm.h
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_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.
PRIfloat
#define PRIfloat
Specifier for printing float type using integers.
Definition: acc_integration_log.h:31
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
cleanup
static void cleanup(acc_sensor_t *sensor, acc_processing_t *processing, void *buffer, acc_vibration_handle_t *handle)
Definition: example_vibration_main.c:287
acc_sensor_destroy
void acc_sensor_destroy(acc_sensor_t *sensor)
Destroy a sensor instance freeing any resources allocated.
acc_definitions_a121.h
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_vibration_main.c:62
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.