example_detector_presence.c

This is an example on how the Detector Presence API can be used
The example executes as follows:

// Copyright (c) Acconeer AB, 2022-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 <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "acc_rss_a121.h"
#include "acc_sensor.h"
#include "acc_version.h"
/** \example example_detector_presence.c
* @brief This is an example on how the Detector Presence API can be used
* @n
* The example executes as follows:
* - Create a presence configuration
* - Create a sensor instance
* - Create a detector instance
* - Calibrate the sensor
* - Prepare the detector
* - Perform a sensor measurement and read out the data
* - Process the measurement and get detector result
* - Destroy the configuration
* - Destroy the detector instance
* - Destroy the sensor instance
*/
typedef enum
{
#define SENSOR_ID (1U)
#define SENSOR_TIMEOUT_MS (2000U)
#define DEFAULT_PRESET_CONFIG PRESENCE_PRESET_CONFIG_MEDIUM_RANGE
static bool do_sensor_calibration(acc_sensor_t *sensor, acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size);
static void cleanup(acc_detector_presence_handle_t *presence_handle,
acc_sensor_t *sensor,
void *buffer);
int app_main(int argc, char *argv[]);
int app_main(int argc, char *argv[])
{
(void)argc;
(void)argv;
acc_detector_presence_config_t *presence_config = NULL;
acc_detector_presence_handle_t *presence_handle = NULL;
acc_sensor_t *sensor = NULL;
void *buffer = NULL;
uint32_t buffer_size = 0U;
printf("Acconeer software version %s\n", acc_version_get());
{
return EXIT_FAILURE;
}
if (presence_config == NULL)
{
printf("acc_detector_presence_config_create() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
set_config(presence_config, DEFAULT_PRESET_CONFIG);
// Print the configuration
presence_handle = acc_detector_presence_create(presence_config, &metadata);
if (presence_handle == NULL)
{
printf("acc_detector_presence_create() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
if (!acc_detector_presence_get_buffer_size(presence_handle, &buffer_size))
{
printf("acc_detector_presence_get_buffer_size() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
buffer = acc_integration_mem_alloc(buffer_size);
if (buffer == NULL)
{
printf("buffer allocation failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
if (sensor == NULL)
{
printf("acc_sensor_create() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
acc_cal_result_t cal_result;
if (!do_sensor_calibration(sensor, &cal_result, buffer, buffer_size))
{
printf("do_sensor_calibration() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
if (!acc_detector_presence_prepare(presence_handle, presence_config, sensor, &cal_result,
buffer, buffer_size))
{
printf("acc_detector_presence_prepare() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
while (true)
{
if (!acc_sensor_measure(sensor))
{
printf("acc_sensor_measure failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
{
printf("Sensor interrupt timeout\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
if (!acc_sensor_read(sensor, buffer, buffer_size))
{
printf("acc_sensor_read failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
if (!acc_detector_presence_process(presence_handle, buffer, &result))
{
printf("acc_detector_presence_process failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
print_result(result);
{
printf("Data saturated. The detector result is not reliable.\n");
}
{
printf("Frame delayed. Could not read data fast enough.\n");
printf("Try lowering the frame rate or call 'acc_sensor_read' more frequently.\n");
}
/* If "calibration_needed" is indicated, the sensor needs to be recalibrated. */
{
printf("Sensor recalibration needed ... \n");
if (!do_sensor_calibration(sensor, &cal_result, buffer, buffer_size))
{
printf("do_sensor_calibration() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
printf("Sensor recalibration done!\n");
/* Before measuring again, the sensor needs to be prepared through the detector. */
if (!acc_detector_presence_prepare(presence_handle, presence_config, sensor, &cal_result,
buffer, buffer_size))
{
printf("acc_detector_presence_prepare() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer);
return EXIT_FAILURE;
}
}
}
cleanup(presence_handle, presence_config, sensor, buffer);
printf("Application finished OK\n");
return EXIT_SUCCESS;
}
static void cleanup(acc_detector_presence_handle_t *presence_handle,
acc_sensor_t *sensor,
void *buffer)
{
if (presence_config != NULL)
{
}
if (presence_handle != NULL)
{
}
if (sensor != NULL)
{
}
if (buffer != NULL)
{
}
}
static bool do_sensor_calibration(acc_sensor_t *sensor, acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size)
{
bool status = false;
bool cal_complete = false;
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 */
}
return status;
}
{
if (result.presence_detected)
{
printf("Motion\n");
}
else
{
printf("No motion\n");
}
// Score and distance are multiplied by 1000 to avoid printing floats
printf("Intra presence score: %d, Inter presence score: %d, Distance (mm): %d\n",
(int)(result.intra_presence_score * 1000.0f),
(int)(result.inter_presence_score * 1000.0f),
(int)(result.presence_distance * 1000.0f));
}
{
switch (preset)
{
// Add configuration of the detector here
break;
acc_detector_presence_config_start_set(presence_config, 0.06f);
acc_detector_presence_config_end_set(presence_config, 1.0f);
break;
acc_detector_presence_config_end_set(presence_config, 2.5f);
break;
acc_detector_presence_config_end_set(presence_config, 7.5f);
break;
acc_detector_presence_config_start_set(presence_config, 0.38f);
acc_detector_presence_config_end_set(presence_config, 0.67f);
break;
}
}
acc_detector_presence_result_t::inter_presence_score
float inter_presence_score
Definition: acc_detector_presence.h:63
set_config
static void set_config(acc_detector_presence_config_t *presence_config, presence_preset_config_t preset)
Definition: example_detector_presence.c:319
acc_detector_presence_config_inter_detection_set
void acc_detector_presence_config_inter_detection_set(acc_detector_presence_config_t *presence_config, bool enable)
Set inter-frame presence detection.
acc_detector_presence_config_sweeps_per_frame_set
void acc_detector_presence_config_sweeps_per_frame_set(acc_detector_presence_config_t *presence_config, uint16_t sweeps_per_frame)
Set the number of sweeps per frame.
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_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_detector_presence_destroy
void acc_detector_presence_destroy(acc_detector_presence_handle_t *presence_handle)
Destroy a presence detector identified with the provided handle.
acc_detector_presence_config_hwaas_set
void acc_detector_presence_config_hwaas_set(acc_detector_presence_config_t *presence_config, uint16_t hwaas)
Set the hardware accelerated average samples (HWAAS)
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_detector_presence.c:51
acc_detector_presence_config_inter_phase_boost_set
void acc_detector_presence_config_inter_phase_boost_set(acc_detector_presence_config_t *presence_config, bool enable)
Set inter-frame phase boost.
acc_version.h
acc_detector_presence_config_inter_frame_deviation_time_const_set
void acc_detector_presence_config_inter_frame_deviation_time_const_set(acc_detector_presence_config_t *presence_config, float inter_frame_deviation_time_const)
Set the time constant of the low pass filter for the inter-frame deviation between fast and slow.
acc_detector_presence_config_auto_profile_set
void acc_detector_presence_config_auto_profile_set(acc_detector_presence_config_t *presence_config, bool enable)
Enable automatic selection of profile based on start point of measurement.
acc_detector_presence_result_t::processing_result
acc_processing_result_t processing_result
Definition: acc_detector_presence.h:86
print_result
static void print_result(acc_detector_presence_result_t result)
Definition: example_detector_presence.c:300
acc_cal_result_t
The result from a completed calibration.
Definition: acc_definitions_a121.h:32
acc_detector_presence_config_frame_rate_set
void acc_detector_presence_config_frame_rate_set(acc_detector_presence_config_t *presence_config, float frame_rate)
Set the frame rate.
ACC_CONFIG_IDLE_STATE_DEEP_SLEEP
@ ACC_CONFIG_IDLE_STATE_DEEP_SLEEP
Definition: acc_definitions_a121.h:77
ACC_CONFIG_PROFILE_5
@ ACC_CONFIG_PROFILE_5
Definition: acc_definitions_a121.h:60
PRESENCE_PRESET_CONFIG_LONG_RANGE
@ PRESENCE_PRESET_CONFIG_LONG_RANGE
Definition: example_detector_presence.c:45
acc_integration.h
acc_detector_presence_get_buffer_size
bool acc_detector_presence_get_buffer_size(const acc_detector_presence_handle_t *presence_handle, uint32_t *buffer_size)
Get the buffer size needed for the provided presence detector handle.
app_main
int app_main(int argc, char *argv[])
Assembly test example.
Definition: example_detector_presence.c:73
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_detector_presence_result_t::presence_detected
bool presence_detected
Definition: acc_detector_presence.h:55
acc_detector_presence_config_intra_detection_threshold_set
void acc_detector_presence_config_intra_detection_threshold_set(acc_detector_presence_config_t *presence_config, float intra_detection_threshold)
Set the detection threshold for the intra-frame presence detection.
acc_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_esp32.c:38
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
acc_detector_presence_result_t
Presence detector results container.
Definition: acc_detector_presence.h:50
acc_detector_presence_config_inter_frame_presence_timeout_set
void acc_detector_presence_config_inter_frame_presence_timeout_set(acc_detector_presence_config_t *presence_config, uint16_t inter_frame_presence_timeout)
Set the inter-frame presence timeout in seconds.
DEFAULT_PRESET_CONFIG
#define DEFAULT_PRESET_CONFIG
Definition: example_detector_presence.c:53
acc_detector_presence_config_destroy
void acc_detector_presence_config_destroy(acc_detector_presence_config_t *presence_config)
Destroy a presence detector configuration.
acc_detector_presence_metadata_t
Definition: acc_detector_presence.h:93
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
acc_detector_presence_config_end_set
void acc_detector_presence_config_end_set(acc_detector_presence_config_t *presence_config, float end)
Set the end point of measurement interval in meters.
acc_detector_presence_config_intra_output_time_const_set
void acc_detector_presence_config_intra_output_time_const_set(acc_detector_presence_config_t *presence_config, float intra_output_time_const)
Set the time constant for the output in the intra-frame part.
acc_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
PRESENCE_PRESET_CONFIG_SHORT_RANGE
@ PRESENCE_PRESET_CONFIG_SHORT_RANGE
Definition: example_detector_presence.c:43
acc_detector_presence_result_t::intra_presence_score
float intra_presence_score
Definition: acc_detector_presence.h:59
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_detector_presence_config_inter_frame_idle_state_set
void acc_detector_presence_config_inter_frame_idle_state_set(acc_detector_presence_config_t *presence_config, acc_config_idle_state_t idle_state)
Set inter frame idle state.
acc_hal_definitions_a121.h
acc_detector_presence_process
bool acc_detector_presence_process(acc_detector_presence_handle_t *presence_handle, void *buffer, acc_detector_presence_result_t *result)
Process the data according to the configuration used in acc_detector_presence_config_create.
acc_detector_presence_config_inter_output_time_const_set
void acc_detector_presence_config_inter_output_time_const_set(acc_detector_presence_config_t *presence_config, float inter_output_time_const)
Set the time constant for the output in the inter-frame part.
PRESENCE_PRESET_CONFIG_NONE
@ PRESENCE_PRESET_CONFIG_NONE
Definition: example_detector_presence.c:42
acc_detector_presence_config_reset_filters_on_prepare_set
void acc_detector_presence_config_reset_filters_on_prepare_set(acc_detector_presence_config_t *presence_config, bool enable)
Set if the presence filters should reset on prepare.
acc_detector_presence_prepare
bool acc_detector_presence_prepare(const acc_detector_presence_handle_t *presence_handle, acc_detector_presence_config_t *presence_config, acc_sensor_t *sensor, const acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size)
Prepare the detector to do a measurement.
acc_detector_presence_config_intra_detection_set
void acc_detector_presence_config_intra_detection_set(acc_detector_presence_config_t *presence_config, bool enable)
Set intra-frame presence detection.
acc_processing_result_t::frame_delayed
bool frame_delayed
Definition: acc_processing.h:80
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_detector_presence_config_t
struct acc_detector_presence_config acc_detector_presence_config_t
Definition: acc_detector_presence.h:44
do_sensor_calibration
static bool do_sensor_calibration(acc_sensor_t *sensor, acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size)
Definition: example_detector_presence.c:265
acc_detector_presence_config_inter_frame_slow_cutoff_set
void acc_detector_presence_config_inter_frame_slow_cutoff_set(acc_detector_presence_config_t *presence_config, float inter_frame_slow_cutoff)
Set the cutoff frequency of the low pass filter for the slow filtered absolute sweep mean.
acc_detector_presence_config_log
void acc_detector_presence_config_log(acc_detector_presence_config_t *presence_config)
Print a configuration to the log.
hal
static const acc_hal_a121_t hal
Definition: acc_hal_integration_espidf_xe121.c:121
PRESENCE_PRESET_CONFIG_MEDIUM_RANGE
@ PRESENCE_PRESET_CONFIG_MEDIUM_RANGE
Definition: example_detector_presence.c:44
acc_detector_presence_config_profile_set
void acc_detector_presence_config_profile_set(acc_detector_presence_config_t *presence_config, acc_config_profile_t profile)
Set a profile.
acc_detector_presence_result_t::presence_distance
float presence_distance
Definition: acc_detector_presence.h:67
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_detector_presence_create
acc_detector_presence_handle_t * acc_detector_presence_create(acc_detector_presence_config_t *presence_config, acc_detector_presence_metadata_t *metadata)
Create a presence detector with the provided configuration.
SENSOR_ID
#define SENSOR_ID
Definition: example_detector_presence.c:50
acc_processing_result_t::data_saturated
bool data_saturated
Definition: acc_processing.h:76
PRESENCE_PRESET_CONFIG_LOW_POWER_WAKEUP
@ PRESENCE_PRESET_CONFIG_LOW_POWER_WAKEUP
Definition: example_detector_presence.c:46
acc_detector_presence_handle_t
struct acc_detector_presence_handle acc_detector_presence_handle_t
Definition: acc_detector_presence.h:36
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_detector_presence_config_create
acc_detector_presence_config_t * acc_detector_presence_config_create(void)
Create a configuration for a presence detector.
acc_detector_presence_config_signal_quality_set
void acc_detector_presence_config_signal_quality_set(acc_detector_presence_config_t *presence_config, float signal_quality)
Set signal quality.
acc_detector_presence_config_start_set
void acc_detector_presence_config_start_set(acc_detector_presence_config_t *presence_config, float start)
Set the start point of measurement interval in meters.
acc_processing_result_t::calibration_needed
bool calibration_needed
Definition: acc_processing.h:84
acc_detector_presence.h
acc_detector_presence_config_inter_detection_threshold_set
void acc_detector_presence_config_inter_detection_threshold_set(acc_detector_presence_config_t *presence_config, float inter_detection_threshold)
Set the detection threshold for the inter-frame presence detection.
acc_sensor_measure
bool acc_sensor_measure(acc_sensor_t *sensor)
Start a radar measurement with previously prepared configuration.
acc_detector_presence_config_auto_step_length_set
void acc_detector_presence_config_auto_step_length_set(acc_detector_presence_config_t *presence_config, bool enable)
Enable automatic selection of step length based on the profile.
acc_sensor_t
struct acc_sensor acc_sensor_t
Definition: acc_sensor.h:31
acc_detector_presence_config_intra_frame_time_const_set
void acc_detector_presence_config_intra_frame_time_const_set(acc_detector_presence_config_t *presence_config, float intra_frame_time_const)
Set the time constant for the depthwise filtering in the intra-frame part.
acc_detector_presence_config_automatic_subsweeps_set
void acc_detector_presence_config_automatic_subsweeps_set(acc_detector_presence_config_t *presence_config, bool automatic_subsweeps)
Set if automatic subsweeps should be used.
acc_sensor_destroy
void acc_sensor_destroy(acc_sensor_t *sensor)
Destroy a sensor instance freeing any resources allocated.
cleanup
static void cleanup(acc_detector_presence_handle_t *presence_handle, acc_detector_presence_config_t *presence_config, acc_sensor_t *sensor, void *buffer)
Definition: example_detector_presence.c:235
acc_definitions_a121.h
acc_detector_presence_config_frame_rate_app_driven_set
void acc_detector_presence_config_frame_rate_app_driven_set(acc_detector_presence_config_t *presence_config, bool enable)
Set if the application should maintain the requested frame rate.
acc_detector_presence_config_inter_frame_fast_cutoff_set
void acc_detector_presence_config_inter_frame_fast_cutoff_set(acc_detector_presence_config_t *presence_config, float inter_frame_fast_cutoff)
Set the cutoff frequency of the low pass filter for the fast filtered absolute sweep mean.
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.
presence_preset_config_t
presence_preset_config_t
Definition: example_detector_presence.c:40