example_detector_distance_low_power_off.c

This is an example on how to disable the sensor and put the system in a low power state between measurements
The example executes as follows:

- Create a distance configuration
- Create a sensor instance
- Enable sensor
- Create a detector instance
- Calibrate the sensor
- Calibrate the detector
  - Disable sensor
- Loop forever:
  - Enable sensor
  - Prepare the detector
  - Perform a sensor measurement and read out the data
  - Disable sensor
  - Process the measurement and get detector result
  - Put the system in deep sleep for a specified amount of time
- Destroy the configuration
- Destroy the detector instance
- Destroy the sensor instance

// Copyright (c) Acconeer AB, 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_distance.h"
#include "acc_hal_definitions_a121.h"
#include "acc_hal_integration_a121.h"
#include "acc_integration.h"
#include "acc_integration_log.h"
#include "acc_rss_a121.h"
#include "acc_sensor.h"
#include "acc_version.h"
/** \example example_detector_distance_low_power_off.c
* @brief This is an example on how to disable the sensor and put the system in a low power state between measurements
* @n
* The example executes as follows:
* - Create a distance configuration
* - Create a sensor instance
* - Enable sensor
* - Create a detector instance
* - Calibrate the sensor
* - Calibrate the detector
* - Disable sensor
* - Loop forever:
* - Enable sensor
* - Prepare the detector
* - Perform a sensor measurement and read out the data
* - Disable sensor
* - Process the measurement and get detector result
* - Put the system in deep sleep for a specified amount of time
* - Destroy the configuration
* - Destroy the detector instance
* - Destroy the sensor instance
*/
#define SENSOR_ID (1U)
#define SENSOR_TIMEOUT_MS (1000U)
#define DEFAULT_UPDATE_RATE (1.0f)
static void print_result(acc_detector_distance_result_t *result);
static void cleanup(acc_detector_distance_handle_t *distance_handle,
acc_detector_distance_config_t *distance_config,
acc_sensor_t *sensor,
void *buffer,
void *calibration_buffer);
static void set_config(acc_detector_distance_config_t *distance_config);
int acconeer_main(int argc, char *argv[]);
int acconeer_main(int argc, char *argv[])
{
(void)argc;
(void)argv;
acc_detector_distance_config_t *distance_config = NULL;
acc_detector_distance_handle_t *distance_handle = NULL;
acc_sensor_t *sensor = NULL;
void *buffer = NULL;
void *calibration_buffer = NULL;
uint32_t buffer_size = 0U;
uint32_t calibration_buffer_size = 0U;
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;
}
distance_config = acc_detector_distance_config_create();
if (distance_config == NULL)
{
printf("acc_detector_distance_config_create() failed\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
set_config(distance_config);
uint32_t sleep_time_ms = (uint32_t)(1000.0f / DEFAULT_UPDATE_RATE);
acc_integration_set_periodic_wakeup(sleep_time_ms);
distance_handle = acc_detector_distance_create(distance_config);
if (distance_handle == NULL)
{
printf("acc_detector_distance_create() failed\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
if (!acc_detector_distance_get_buffer_sizes(distance_handle, &buffer_size, &calibration_buffer_size))
{
printf("acc_detector_distance_get_buffer_size() failed\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
buffer = acc_integration_mem_alloc(buffer_size);
if (buffer == NULL)
{
printf("buffer allocation failed\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
calibration_buffer = acc_integration_mem_alloc(calibration_buffer_size);
if (calibration_buffer == NULL)
{
printf("buffer allocation failed\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
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(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
// Calibrate sensor
bool status;
bool cal_complete = false;
acc_cal_result_t sensor_cal_result;
do
{
status = acc_sensor_calibrate(sensor, &cal_complete, &sensor_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(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
/* Reset sensor after calibration by disabling/enabling it */
acc_hal_integration_sensor_disable(SENSOR_ID);
acc_hal_integration_sensor_enable(SENSOR_ID);
// Calibrate detector
bool done = false;
do
{
status = acc_detector_distance_calibrate(sensor,
distance_handle,
&sensor_cal_result,
buffer,
buffer_size,
calibration_buffer,
calibration_buffer_size,
&done);
} while (status && !done && acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS));
if (!status)
{
printf("acc_detector_distance_calibrate() failed\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
acc_hal_integration_sensor_disable(SENSOR_ID);
while (true)
{
acc_detector_distance_result_t result;
acc_hal_integration_sensor_enable(SENSOR_ID);
bool result_available = false;
do
{
if (!acc_detector_distance_prepare(distance_handle, distance_config, sensor, &sensor_cal_result, buffer,
buffer_size))
{
printf("acc_detector_distance_prepare() failed\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
if (!acc_sensor_measure(sensor))
{
printf("acc_sensor_measure failed\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
if (!acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS))
{
printf("Sensor interrupt timeout\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
if (!acc_sensor_read(sensor, buffer, buffer_size))
{
printf("acc_sensor_read failed\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
if (!acc_detector_distance_process(distance_handle, buffer, calibration_buffer, &result_available, &result))
{
printf("acc_detector_distance_process failed\n");
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
return EXIT_FAILURE;
}
} while (!result_available);
acc_hal_integration_sensor_disable(SENSOR_ID);
print_result(&result);
acc_integration_sleep_until_periodic_wakeup();
}
cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
printf("Application finished OK\n");
return EXIT_SUCCESS;
}
static void cleanup(acc_detector_distance_handle_t *distance_handle,
acc_detector_distance_config_t *distance_config,
acc_sensor_t *sensor,
void *buffer,
void *calibration_buffer)
{
acc_hal_integration_sensor_disable(SENSOR_ID);
acc_hal_integration_sensor_supply_off(SENSOR_ID);
if (distance_config != NULL)
{
acc_detector_distance_config_destroy(distance_config);
}
if (distance_handle != NULL)
{
acc_detector_distance_destroy(distance_handle);
}
if (sensor != NULL)
{
acc_sensor_destroy(sensor);
}
if (buffer != NULL)
{
acc_integration_mem_free(buffer);
}
if (calibration_buffer != NULL)
{
acc_integration_mem_free(calibration_buffer);
}
}
static void set_config(acc_detector_distance_config_t *distance_config)
{
// Add configuration of the detector here
acc_detector_distance_config_start_set(distance_config, 0.25f);
acc_detector_distance_config_end_set(distance_config, 3.0f);
}
static void print_result(acc_detector_distance_result_t *result)
{
printf("%d detected distances", result->num_distances);
if (result->num_distances > 0)
{
printf(": ");
for (uint8_t i = 0; i < result->num_distances; i++)
{
printf("%" PRIfloat "m ", ACC_LOG_FLOAT_TO_INTEGER(result->distances[i]));
}
}
printf("\n");
}
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_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_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
acc_version.h
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_detector_distance_low_power_off.c:50
acc_cal_result_t
Definition: acc_definitions_a121.h:19
acc_detector_distance_config_start_set
void acc_detector_distance_config_start_set(acc_detector_distance_config_t *config, float start_m)
Set the start of measured interval in meters.
SENSOR_ID
#define SENSOR_ID
Definition: example_detector_distance_low_power_off.c:49
set_config
static void set_config(acc_detector_distance_config_t *distance_config)
Definition: example_detector_distance_low_power_off.c:295
acc_detector_distance_get_buffer_sizes
bool acc_detector_distance_get_buffer_sizes(const acc_detector_distance_handle_t *handle, uint32_t *buffer_size, uint32_t *calibration_buffer_size)
Get the buffer sizes needed given the provided detector handle.
acc_integration.h
acc_detector_distance_result_t::num_distances
uint8_t num_distances
Definition: acc_detector_distance.h:62
acc_detector_distance_create
acc_detector_distance_handle_t * acc_detector_distance_create(const acc_detector_distance_config_t *config)
Create a distance detector with the provided configuration.
print_result
static void print_result(acc_detector_distance_result_t *result)
Definition: example_detector_distance_low_power_off.c:304
cleanup
static void cleanup(acc_detector_distance_handle_t *distance_handle, acc_detector_distance_config_t *distance_config, acc_sensor_t *sensor, void *buffer, void *calibration_buffer)
Definition: example_detector_distance_low_power_off.c:259
acc_detector_distance_config_destroy
void acc_detector_distance_config_destroy(acc_detector_distance_config_t *config)
Destroy a configuration for a distance detector.
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_distance_process
bool acc_detector_distance_process(acc_detector_distance_handle_t *handle, void *buffer, void *calibration_buffer, bool *result_available, acc_detector_distance_result_t *result)
Process the data according to the configuration used in acc_detector_distance_config_create.
acc_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_stm32.c:632
acc_hal_a121_t
Definition: acc_hal_definitions_a121.h:82
acc_detector_distance_calibrate
bool acc_detector_distance_calibrate(acc_sensor_t *sensor, acc_detector_distance_handle_t *handle, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size, void *calibration_buffer, uint32_t calibration_buffer_size, bool *calibration_complete)
Do a detector calibration.
acc_rss_hal_register
bool acc_rss_hal_register(const acc_hal_a121_t *hal)
Register an integration.
acc_integration_set_periodic_wakeup
void acc_integration_set_periodic_wakeup(uint32_t time_msec)
Set up a periodic timer used to wake up the system from sleep.
Definition: acc_integration_stm32.c:531
acc_sensor.h
acc_detector_distance_destroy
void acc_detector_distance_destroy(acc_detector_distance_handle_t *handle)
Destroy the distance detector handle, freeing its resources.
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_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
printf
#define printf
Definition: printf.h:60
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_hal_definitions_a121.h
acc_detector_distance_prepare
bool acc_detector_distance_prepare(const acc_detector_distance_handle_t *handle, const acc_detector_distance_config_t *config, acc_sensor_t *sensor, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size)
Prepare the detector for measurements.
acc_detector_distance_result_t
Distance detector result.
Definition: acc_detector_distance.h:49
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_detector_distance_low_power_off.c:70
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_distance_config_end_set
void acc_detector_distance_config_end_set(acc_detector_distance_config_t *config, float end_m)
Set the end of measured interval in meters.
acc_integration_log.h
acc_detector_distance_handle_t
struct acc_detector_distance_handle acc_detector_distance_handle_t
Definition: acc_detector_distance.h:35
ACC_LOG_FLOAT_TO_INTEGER
#define ACC_LOG_FLOAT_TO_INTEGER(a)
Definition: acc_integration_log.h:26
acc_integration_mem_free
void acc_integration_mem_free(void *ptr)
Free dynamic memory.
Definition: acc_integration_stm32.c:644
acc_integration_sleep_until_periodic_wakeup
void acc_integration_sleep_until_periodic_wakeup(void)
Put the system in sleep until the periodic timer triggers.
Definition: acc_integration_stm32.c:590
acc_definitions_common.h
DEFAULT_UPDATE_RATE
#define DEFAULT_UPDATE_RATE
Definition: example_detector_distance_low_power_off.c:51
acc_detector_distance_config_create
acc_detector_distance_config_t * acc_detector_distance_config_create(void)
Create a configuration for a distance detector.
acc_detector_distance_result_t::distances
float distances[(10U)]
Definition: acc_detector_distance.h:54
acc_detector_distance.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_detector_distance_config_t
struct acc_detector_distance_config acc_detector_distance_config_t
Definition: acc_detector_distance.h:43
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.