example_detector_presence_multiple_configurations.c

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

- Create a first presence configuration
- Create a second presence configuration
- Create a sensor instance
- Calibrate the sensor
- Loop forever:
  - Loop configurations (i):
    - Create a detector instance with the i:th configuration
    - Prepare the detector with the i:th configuration
    - Loop nbr iterations per configuration:
      - Perform a sensor measurement and read out the data
      - Process the measurement and get detector result
    - Destroy the detector instance
- Destroy the configuration
- Destroy the sensor instance

// Copyright (c) Acconeer AB, 2022-2023
// 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_definitions_a121.h"
#include "acc_definitions_common.h"
#include "acc_detector_presence.h"
#include "acc_hal_definitions_a121.h"
#include "acc_hal_integration_a121.h"
#include "acc_integration.h"
#include "acc_rss_a121.h"
#include "acc_sensor.h"
#include "acc_version.h"
/** \example example_detector_presence_multiple_configurations.c
* @brief This is an example on how the Detector Presence API can be used
* @n
* The example executes as follows:
* - Create a first presence configuration
* - Create a second presence configuration
* - Create a sensor instance
* - Calibrate the sensor
* - Loop forever:
* - Loop configurations (i):
* - Create a detector instance with the i:th configuration
* - Prepare the detector with the i:th configuration
* - Loop nbr iterations per configuration:
* - Perform a sensor measurement and read out the data
* - Process the measurement and get detector result
* - Destroy the detector instance
* - Destroy the configuration
* - Destroy the sensor instance
*/
#define SENSOR_ID (1U)
#define SENSOR_TIMEOUT_MS (1000U)
#define NBR_CONFIGS (2U)
#define NBR_ITERATIONS_PER_CONFIG (20U)
typedef struct example_config
{
char *name;
float start;
float end;
float frame_rate;
bool reset_filters_on_prepare;
} example_config_t;
static void print_result(acc_detector_presence_result_t result, const char *config_name);
static void cleanup(acc_detector_presence_handle_t **presence_handle,
acc_detector_presence_config_t **presence_config,
acc_sensor_t *sensor,
void *buffer,
uint32_t nbr_configs);
int acconeer_main(int argc, char *argv[]);
int acconeer_main(int argc, char *argv[])
{
(void)argc;
(void)argv;
acc_detector_presence_config_t *presence_config[NBR_CONFIGS] = { NULL };
acc_detector_presence_handle_t *presence_handle[NBR_CONFIGS] = { NULL };
acc_detector_presence_metadata_t metadata[NBR_CONFIGS];
acc_sensor_t *sensor = NULL;
void *buffer = NULL;
uint32_t buffer_size = 0U;
example_config_t example_configurations[] =
{
{
.name = "zone1",
.start = 1.0f,
.end = 2.0f,
.frame_rate = 5.0f,
/**
* Detector filters and states are reset on prepare (reconfiguration) to avoid risk of false detections
* when switching back to a zone.
*/
.reset_filters_on_prepare = true,
},
{
.name = "zone2",
.start = 0.2f,
.end = 1.0f,
.frame_rate = 10.0f,
/**
* Detector filters and states are reset on prepare (reconfiguration) to avoid risk of false detections
* when switching back to a zone.
*/
.reset_filters_on_prepare = true,
},
};
printf("Acconeer software version %s\n", acc_version_get());
const acc_hal_a121_t *hal = acc_hal_rss_integration_get_implementation();
if (!acc_rss_hal_register(hal))
{
return EXIT_FAILURE;
}
for (uint32_t cfg = 0U; cfg < NBR_CONFIGS; cfg++)
{
presence_config[cfg] = acc_detector_presence_config_create();
if (presence_config[cfg] == NULL)
{
printf("acc_detector_presence_config_create() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
acc_detector_presence_config_start_set(presence_config[cfg], example_configurations[cfg].start);
acc_detector_presence_config_end_set(presence_config[cfg], example_configurations[cfg].end);
acc_detector_presence_config_frame_rate_set(presence_config[cfg], example_configurations[cfg].frame_rate);
acc_detector_presence_config_reset_filters_on_prepare_set(presence_config[cfg], example_configurations[cfg].reset_filters_on_prepare);
uint32_t current_buffer_size = 0;
presence_handle[cfg] = acc_detector_presence_create(presence_config[cfg], &metadata[cfg]);
if (presence_handle[cfg] == NULL)
{
printf("acc_detector_presence_create() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
if (!acc_detector_presence_get_buffer_size(presence_handle[cfg], &current_buffer_size))
{
printf("acc_detector_presence_get_buffer_size() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
if (buffer_size < current_buffer_size)
{
buffer_size = current_buffer_size;
}
}
buffer = acc_integration_mem_alloc(buffer_size);
if (buffer == NULL)
{
printf("buffer allocation failed\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
acc_hal_integration_sensor_supply_on(SENSOR_ID);
acc_hal_integration_sensor_enable(SENSOR_ID);
sensor = acc_sensor_create(SENSOR_ID);
if (sensor == NULL)
{
printf("acc_sensor_create() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
bool status;
bool cal_complete = false;
acc_cal_result_t cal_result;
do
{
status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
if (status && !cal_complete)
{
status = acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS);
}
} while (status && !cal_complete);
if (!status)
{
printf("acc_sensor_calibrate() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
// Reset sensor after calibration by disabling it
acc_hal_integration_sensor_disable(SENSOR_ID);
acc_hal_integration_sensor_enable(SENSOR_ID);
while (true)
{
for (uint32_t cfg = 0U; cfg < NBR_CONFIGS; cfg++)
{
acc_detector_presence_result_t result;
if (!acc_detector_presence_prepare(presence_handle[cfg], presence_config[cfg], sensor,
&cal_result, buffer, buffer_size))
{
printf("acc_detector_presence_prepare() failed\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
for (uint32_t i = 0U; i < NBR_ITERATIONS_PER_CONFIG; i++)
{
if (!acc_sensor_measure(sensor))
{
printf("acc_sensor_measure failed\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
if (!acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS))
{
printf("Sensor interrupt timeout\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
if (!acc_sensor_read(sensor, buffer, buffer_size))
{
printf("acc_sensor_read failed\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
if (!acc_detector_presence_process(presence_handle[cfg], buffer, &result))
{
printf("acc_detector_presence_process failed\n");
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
return EXIT_FAILURE;
}
/* The depthwise inter and intra score arrays that are part of the 'result',
* points to memory inside the 'buffer'. So they need to be handled before
* the 'buffer' is used again (for example in @ref acc_sensor_read or
* @ref acc_detector_presence_process).
*/
print_result(result, example_configurations[cfg].name);
}
}
}
cleanup(presence_handle, presence_config, sensor, buffer, NBR_CONFIGS);
printf("Application finished OK\n");
return EXIT_SUCCESS;
}
static void cleanup(acc_detector_presence_handle_t **presence_handle,
acc_detector_presence_config_t **presence_config,
acc_sensor_t *sensor,
void *buffer,
uint32_t nbr_configs)
{
acc_hal_integration_sensor_disable(SENSOR_ID);
acc_hal_integration_sensor_supply_off(SENSOR_ID);
for (uint32_t cfg = 0U; cfg < nbr_configs; cfg++)
{
if (presence_config[cfg] != NULL)
{
acc_detector_presence_config_destroy(presence_config[cfg]);
presence_config[cfg] = NULL;
}
if (presence_handle[cfg] != NULL)
{
acc_detector_presence_destroy(presence_handle[cfg]);
presence_handle[cfg] = NULL;
}
}
if (sensor != NULL)
{
acc_sensor_destroy(sensor);
}
if (buffer != NULL)
{
acc_integration_mem_free(buffer);
}
}
static void print_result(acc_detector_presence_result_t result, const char *config_name)
{
if (result.presence_detected)
{
printf("Motion\n");
}
else
{
printf("No motion\n");
}
// Score and distance are multiplied by 1000 to avoid printing floats
printf("%s - Intra presence score: %d, Inter presence score: %d, Distance (mm): %d\n",
config_name,
(int)(result.intra_presence_score * 1000.0f),
(int)(result.inter_presence_score * 1000.0f),
(int)(result.presence_distance * 1000.0f));
printf("%s - Depthwise Intra Presence Scores: \t", config_name);
for (uint16_t i = 0; i < result.depthwise_presence_scores_length; i++)
{
// Score is multiplied by 1000 to avoid printing floats
printf("%5i", (int)(result.depthwise_intra_presence_scores[i] * 1000));
}
printf("\n");
printf("%s - Depthwise Inter Presence Scores: \t", config_name);
for (uint16_t i = 0; i < result.depthwise_presence_scores_length; i++)
{
// Score is multiplied by 1000 to avoid printing floats
printf("%5i", (int)(result.depthwise_inter_presence_scores[i] * 1000));
}
printf("\n");
}
acc_detector_presence_result_t::inter_presence_score
float inter_presence_score
Definition: acc_detector_presence.h:63
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:113
acc_rss_a121.h
acc_detector_presence_prepare
bool acc_detector_presence_prepare(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_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:107
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.
NBR_CONFIGS
#define NBR_CONFIGS
Definition: example_detector_presence_multiple_configurations.c:45
example_config
Definition: example_detector_presence_multiple_configurations.c:49
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
acc_detector_presence_result_t::depthwise_presence_scores_length
uint32_t depthwise_presence_scores_length
Definition: acc_detector_presence.h:81
acc_version.h
NBR_ITERATIONS_PER_CONFIG
#define NBR_ITERATIONS_PER_CONFIG
Definition: example_detector_presence_multiple_configurations.c:46
acc_cal_result_t
Definition: acc_definitions_a121.h:19
example_config::reset_filters_on_prepare
bool reset_filters_on_prepare
Definition: example_detector_presence_multiple_configurations.c:55
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_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.
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:166
acc_detector_presence_result_t::presence_detected
bool presence_detected
Definition: acc_detector_presence.h:55
acc_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_stm32.c:632
print_result
static void print_result(acc_detector_presence_result_t result, const char *config_name)
Definition: example_detector_presence_multiple_configurations.c:298
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
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_detector_presence_multiple_configurations.c:72
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_stm32cube_xm.c:142
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_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
printf
#define printf
Definition: printf.h:60
example_config::name
char * name
Definition: example_detector_presence_multiple_configurations.c:51
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_stm32cube_xm.c:119
acc_detector_presence_result_t::depthwise_intra_presence_scores
float * depthwise_intra_presence_scores
Definition: acc_detector_presence.h:72
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.
example_config::frame_rate
float frame_rate
Definition: example_detector_presence_multiple_configurations.c:54
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_result_t::depthwise_inter_presence_scores
float * depthwise_inter_presence_scores
Definition: acc_detector_presence.h:77
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:130
acc_detector_presence_config_t
struct acc_detector_presence_config acc_detector_presence_config_t
Definition: acc_detector_presence.h:44
example_config_t
struct example_config example_config_t
cleanup
static void cleanup(acc_detector_presence_handle_t **presence_handle, acc_detector_presence_config_t **presence_config, acc_sensor_t *sensor, void *buffer, uint32_t nbr_configs)
Definition: example_detector_presence_multiple_configurations.c:262
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_detector_presence_multiple_configurations.c:44
example_config::end
float end
Definition: example_detector_presence_multiple_configurations.c:53
SENSOR_ID
#define SENSOR_ID
Definition: example_detector_presence_multiple_configurations.c:43
example_config::start
float start
Definition: example_detector_presence_multiple_configurations.c:52
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_stm32.c:644
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.
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_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_detector_presence.h
acc_sensor_measure
bool acc_sensor_measure(acc_sensor_t *sensor)
Start a radar measurement with previously prepared configuration.
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
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.