example_vibration_main.c

This is an example on how the Service API can be used to measure vibrations
The example executes as follows:

// Copyright (c) Acconeer AB, 2023-2024
// All rights reserved
// This file is subject to the terms and conditions defined in the file
// 'LICENSES/license_acconeer.txt', (BSD 3-Clause License) which is part
// of this source code package.
#include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "acc_algorithm.h"
#include "acc_config.h"
#include "acc_processing.h"
#include "acc_rss_a121.h"
#include "acc_sensor.h"
#include "acc_version.h"
/** \example example_vibration_main.c
* @brief This is an example on how the Service API can be used to measure
* vibrations
* @n
* The example executes as follows:
* - Set vibration configuration
* - Create vibration processing handle using config from previous step
* - Create a processing instance using an internally translated config from previous step
* - Allocate sensor buffer
* - Create a sensor instance
* - Calibrate & prepare the sensor
* - Loop:
* - Perform a sensor measurement and read out the data
* - Check & handle the 'calibration_needed' indication
* - Process the measurement and print vibration result
* - Destroy the sensor instance
* - Destroy the processing instance
* - Destroy the configuration
*/
#define SENSOR_ID (1U)
#define SENSOR_TIMEOUT_MS (1000U)
#define DISPLACEMENT_HISTORY_COLUMN_WIDTH (16U)
static bool do_sensor_calibration_and_prepare(acc_sensor_t *sensor, void *buffer, uint32_t buffer_size, const acc_config_t *sensor_config);
int acconeer_main(int argc, char *argv[]);
int acconeer_main(int argc, char *argv[])
{
(void)argc;
(void)argv;
void *buffer = NULL;
uint32_t buffer_size = 0U;
acc_processing_t *processing = NULL;
acc_processing_metadata_t proc_meta = {0};
acc_processing_result_t proc_result = {0};
/** Vibration example configuration **/
// Use a preset as a starting point. The call below will also initialize the config struct.
// If needed, adjust configuration parameters before creating a vibration handle.
// Refer to the config struct definition in 'example_vibration.h' to see what parameters are available.
printf("Acconeer software version %s\n", acc_version_get());
{
return EXIT_FAILURE;
}
/*
* The handle will use the config passed to it here
* for its lifetime. Make sure any changes to
* 'config' happens before this point.
*/
if (handle == NULL)
{
printf("acc_vibration_handle_create() failed\n");
cleanup(sensor, processing, buffer, handle);
return EXIT_FAILURE;
}
if (processing == NULL)
{
printf("acc_processing_create() failed\n");
return EXIT_FAILURE;
}
{
printf("acc_rss_get_buffer_size() failed\n");
cleanup(sensor, processing, buffer, handle);
return EXIT_FAILURE;
}
if (buffer == NULL)
{
printf("buffer allocation failed\n");
cleanup(sensor, processing, buffer, handle);
return EXIT_FAILURE;
}
if (sensor == NULL)
{
printf("acc_sensor_create() failed\n");
return EXIT_FAILURE;
}
{
printf("do_sensor_calibration_and_prepare() failed\n");
cleanup(sensor, processing, buffer, handle);
return EXIT_FAILURE;
}
while (true)
{
{
printf("acc_sensor_measure failed\n");
cleanup(sensor, processing, buffer, handle);
return EXIT_FAILURE;
}
{
printf("Sensor interrupt timeout\n");
cleanup(sensor, processing, buffer, handle);
return EXIT_FAILURE;
}
{
printf("acc_sensor_read failed\n");
cleanup(sensor, processing, buffer, handle);
return EXIT_FAILURE;
}
acc_processing_execute(processing, buffer, &proc_result);
if (proc_result.calibration_needed)
{
printf("The current calibration is not valid for the current temperature.\n");
printf("The sensor needs to be re-calibrated.\n");
{
printf("do_sensor_calibration_and_prepare() failed\n");
cleanup(sensor, processing, buffer, handle);
return EXIT_FAILURE;
}
printf("The sensor was successfully re-calibrated.\n");
}
else
{
}
}
cleanup(sensor, processing, buffer, handle);
printf("Application finished OK\n");
return EXIT_SUCCESS;
}
static bool do_sensor_calibration_and_prepare(acc_sensor_t *sensor, void *buffer, uint32_t buffer_size, const acc_config_t *sensor_config)
{
bool status = false;
bool cal_complete = false;
acc_cal_result_t cal_result = {0};
const uint16_t calibration_retries = 1U;
// Random disturbances may cause the calibration to fail. At failure, retry at least once.
for (uint16_t i = 0; !status && (i <= calibration_retries); i++)
{
// Reset sensor before calibration by disabling/enabling it
do
{
status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
if (status && !cal_complete)
{
}
} while (status && !cal_complete);
}
if (status)
{
// Reset sensor after calibration by disabling/enabling it
status = acc_sensor_prepare(sensor, sensor_config, &cal_result, buffer, buffer_size);
}
return status;
}
{
char buf[80] = "";
size_t buf_size = sizeof(buf);
if (result->max_displacement != FLT_MAX)
{
snprintf(buf,
buf_size,
", max_displacement=%" PRIfloat " um, max_displacement_freq=%" PRIfloat " Hz",
ACC_LOG_FLOAT_TO_INTEGER(result->max_displacement),
ACC_LOG_FLOAT_TO_INTEGER(result->max_displacement_freq));
}
printf("Vibration: max_sweep_amplitude=%" PRIfloat " um%s\n", ACC_LOG_FLOAT_TO_INTEGER(result->max_sweep_amplitude), buf);
bool continuous_data_acquisition = true;
if (acc_vibration_handle_continuous_data_acquisition_get(handle, &continuous_data_acquisition) && continuous_data_acquisition)
{
return;
}
uint16_t num_elems = 0U;
const float *displacement_history = acc_vibration_handle_displacement_history_get(handle, &num_elems);
if (displacement_history != NULL)
{
printf("\nDisplacement history:\n");
for (uint16_t i = 0; i < num_elems; i++)
{
printf("%" PRIfloat " ", ACC_LOG_FLOAT_TO_INTEGER(displacement_history[i]));
{
printf("\n");
}
}
printf("\n\n");
}
}
{
if (sensor != NULL)
{
}
if (processing != NULL)
{
}
if (buffer != NULL)
{
}
if (handle != NULL)
{
}
}
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.