example_detector_presence_low_power_off.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2022-2023
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 <stdbool.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 
13 #include "acc_definitions_a121.h"
14 #include "acc_definitions_common.h"
15 #include "acc_detector_presence.h"
18 #include "acc_integration.h"
19 #include "acc_rss_a121.h"
20 #include "acc_sensor.h"
21 
22 #include "acc_version.h"
23 
24 
25 /** \example example_detector_presence_low_power_off.c
26  * @brief This is an example on how to disable the sensor and put the system in a low power state between measurements
27  * @n
28  * The example executes as follows:
29  * - Create a presence configuration
30  * - Create a sensor instance
31  * - Enable sensor
32  * - Create a detector instance
33  * - Calibrate the sensor
34  * - Prepare the detector
35  * - Loop forever:
36  * - Enable sensor
37  * - Prepare a sensor
38  * - Perform a sensor measurement and read out the data
39  * - Disable sensor
40  * - Process the measurement and get detector result
41  * - Put the system in deep sleep for a specified amount of time
42  * - Destroy the configuration
43  * - Destroy the detector instance
44  * - Destroy the sensor instance
45  */
46 
47 
48 typedef enum
49 {
56 
57 
58 #define SENSOR_ID (1U)
59 #define SENSOR_TIMEOUT_MS (1000U)
60 
61 #define DEFAULT_PRESET_CONFIG PRESENCE_PRESET_CONFIG_MEDIUM_RANGE
62 
63 
65 
66 
67 static void cleanup(acc_detector_presence_handle_t *presence_handle,
68  acc_detector_presence_config_t *presence_config,
69  acc_sensor_t *sensor,
70  void *buffer);
71 
72 
73 static void set_config(acc_detector_presence_config_t *presence_config, presence_preset_config_t preset);
74 
75 
76 int acconeer_main(int argc, char *argv[]);
77 
78 
79 int acconeer_main(int argc, char *argv[])
80 {
81  (void)argc;
82  (void)argv;
83  acc_detector_presence_config_t *presence_config = NULL;
84  acc_detector_presence_handle_t *presence_handle = NULL;
86  acc_sensor_t *sensor = NULL;
87  void *buffer = NULL;
88  uint32_t buffer_size = 0U;
89 
90  printf("Acconeer software version %s\n", acc_version_get());
91 
93 
94  if (!acc_rss_hal_register(hal))
95  {
96  return EXIT_FAILURE;
97  }
98 
99  presence_config = acc_detector_presence_config_create();
100  if (presence_config == NULL)
101  {
102  printf("acc_detector_presence_config_create() failed\n");
103  cleanup(presence_handle, presence_config, sensor, buffer);
104  return EXIT_FAILURE;
105  }
106 
107  set_config(presence_config, DEFAULT_PRESET_CONFIG);
108 
109  uint32_t sleep_time_ms = (uint32_t)(1000.0f / acc_detector_presence_config_frame_rate_get(presence_config));
110 
112 
113  presence_handle = acc_detector_presence_create(presence_config, &metadata);
114  if (presence_handle == NULL)
115  {
116  printf("acc_detector_presence_create() failed\n");
117  cleanup(presence_handle, presence_config, sensor, buffer);
118  return EXIT_FAILURE;
119  }
120 
121  if (!acc_detector_presence_get_buffer_size(presence_handle, &buffer_size))
122  {
123  printf("acc_detector_presence_get_buffer_size() failed\n");
124  cleanup(presence_handle, presence_config, sensor, buffer);
125  return EXIT_FAILURE;
126  }
127 
128  buffer = acc_integration_mem_alloc(buffer_size);
129  if (buffer == NULL)
130  {
131  printf("buffer allocation failed\n");
132  cleanup(presence_handle, presence_config, sensor, buffer);
133  return EXIT_FAILURE;
134  }
135 
138 
139  sensor = acc_sensor_create(SENSOR_ID);
140  if (sensor == NULL)
141  {
142  printf("acc_sensor_create() failed\n");
143  cleanup(presence_handle, presence_config, sensor, buffer);
144  return EXIT_FAILURE;
145  }
146 
147  bool status;
148  bool cal_complete = false;
149  acc_cal_result_t cal_result;
150 
151  do
152  {
153  status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
154 
155  if (status && !cal_complete)
156  {
158  }
159  } while (status && !cal_complete);
160 
161  if (!status)
162  {
163  printf("acc_sensor_calibrate() failed\n");
164  cleanup(presence_handle, presence_config, sensor, buffer);
165  return EXIT_FAILURE;
166  }
167 
168  // Reset sensor after calibration by disabling it
170 
171  while (true)
172  {
174 
176  if (!acc_detector_presence_prepare(presence_handle, presence_config, sensor, &cal_result, buffer, buffer_size))
177  {
178  printf("acc_detector_presence_prepare() failed\n");
179  cleanup(presence_handle, presence_config, sensor, buffer);
180  return EXIT_FAILURE;
181  }
182 
183  if (!acc_sensor_measure(sensor))
184  {
185  printf("acc_sensor_measure failed\n");
186  cleanup(presence_handle, presence_config, sensor, buffer);
187  return EXIT_FAILURE;
188  }
189 
191  {
192  printf("Sensor interrupt timeout\n");
193  cleanup(presence_handle, presence_config, sensor, buffer);
194  return EXIT_FAILURE;
195  }
196 
197  if (!acc_sensor_read(sensor, buffer, buffer_size))
198  {
199  printf("acc_sensor_read failed\n");
200  cleanup(presence_handle, presence_config, sensor, buffer);
201  return EXIT_FAILURE;
202  }
203 
205 
206  if (!acc_detector_presence_process(presence_handle, buffer, &result))
207  {
208  printf("acc_detector_presence_process failed\n");
209  cleanup(presence_handle, presence_config, sensor, buffer);
210  return EXIT_FAILURE;
211  }
212 
213  print_result(result);
214 
216  }
217 
218  cleanup(presence_handle, presence_config, sensor, buffer);
219 
220  printf("Application finished OK\n");
221 
222  return EXIT_SUCCESS;
223 }
224 
225 
226 static void cleanup(acc_detector_presence_handle_t *presence_handle,
227  acc_detector_presence_config_t *presence_config,
228  acc_sensor_t *sensor,
229  void *buffer)
230 {
233 
234  if (presence_config != NULL)
235  {
236  acc_detector_presence_config_destroy(presence_config);
237  }
238 
239  if (presence_handle != NULL)
240  {
241  acc_detector_presence_destroy(presence_handle);
242  }
243 
244  if (sensor != NULL)
245  {
246  acc_sensor_destroy(sensor);
247  }
248 
249  if (buffer != NULL)
250  {
251  acc_integration_mem_free(buffer);
252  }
253 }
254 
255 
257 {
258  switch (preset)
259  {
262  // Add configuration of the detector here
263  break;
264 
266  acc_detector_presence_config_start_set(presence_config, 0.06f);
267  acc_detector_presence_config_end_set(presence_config, 1.0f);
269  acc_detector_presence_config_auto_profile_set(presence_config, true);
271  acc_detector_presence_config_hwaas_set(presence_config, 16);
273  acc_detector_presence_config_frame_rate_set(presence_config, 10.0f);
288 
289  break;
290 
292  acc_detector_presence_config_start_set(presence_config, 0.3f);
293  acc_detector_presence_config_end_set(presence_config, 2.5f);
295  acc_detector_presence_config_auto_profile_set(presence_config, true);
297  acc_detector_presence_config_hwaas_set(presence_config, 32);
299  acc_detector_presence_config_frame_rate_set(presence_config, 12.0f);
314 
315  break;
316 
318  acc_detector_presence_config_start_set(presence_config, 5.0f);
319  acc_detector_presence_config_end_set(presence_config, 7.5f);
321  acc_detector_presence_config_auto_profile_set(presence_config, true);
323  acc_detector_presence_config_hwaas_set(presence_config, 128);
325  acc_detector_presence_config_frame_rate_set(presence_config, 12.0f);
340 
341  break;
342 
344  acc_detector_presence_config_start_set(presence_config, 0.725f);
345  acc_detector_presence_config_end_set(presence_config, 1.505f);
347  acc_detector_presence_config_auto_profile_set(presence_config, true);
349  acc_detector_presence_config_hwaas_set(presence_config, 8);
351  acc_detector_presence_config_frame_rate_set(presence_config, 1.0f);
366 
367  break;
368  }
369 }
370 
371 
373 {
374  if (result.presence_detected)
375  {
376  printf("Motion\n");
377  }
378  else
379  {
380  printf("No motion\n");
381  }
382 
383  // Score and distance is multiplied by 1000 to avoid printing floats
384  printf("Intra presence score: %d, Inter presence score: %d, Distance (mm): %d\n",
385  (int)(result.intra_presence_score * 1000.0f),
386  (int)(result.inter_presence_score * 1000.0f),
387  (int)(result.presence_distance * 1000.0f));
388 }
acc_detector_presence_result_t::inter_presence_score
float inter_presence_score
Definition: acc_detector_presence.h:63
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.
PRESENCE_PRESET_CONFIG_NONE
@ PRESENCE_PRESET_CONFIG_NONE
Definition: example_detector_presence_low_power_off.c:50
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.
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_low_power_off.c:226
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
SENSOR_ID
#define SENSOR_ID
Definition: example_detector_presence_low_power_off.c:58
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.
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_cal_result_t
Definition: acc_definitions_a121.h:19
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.
PRESENCE_PRESET_CONFIG_MEDIUM_RANGE
@ PRESENCE_PRESET_CONFIG_MEDIUM_RANGE
Definition: example_detector_presence_low_power_off.c:52
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_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_stm32.c:632
PRESENCE_PRESET_CONFIG_LONG_RANGE
@ PRESENCE_PRESET_CONFIG_LONG_RANGE
Definition: example_detector_presence_low_power_off.c:53
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.
presence_preset_config_t
presence_preset_config_t
Definition: example_detector_presence_low_power_off.c:48
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
set_config
static void set_config(acc_detector_presence_config_t *presence_config, presence_preset_config_t preset)
Definition: example_detector_presence_low_power_off.c:256
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.
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_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.
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_detector_presence_low_power_off.c:79
acc_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
printf
#define printf
Definition: printf.h:60
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_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.
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.
PRESENCE_PRESET_CONFIG_SHORT_RANGE
@ PRESENCE_PRESET_CONFIG_SHORT_RANGE
Definition: example_detector_presence_low_power_off.c:51
ACC_CONFIG_IDLE_STATE_DEEP_SLEEP
@ ACC_CONFIG_IDLE_STATE_DEEP_SLEEP
Definition: acc_definitions_a121.h:64
DEFAULT_PRESET_CONFIG
#define DEFAULT_PRESET_CONFIG
Definition: example_detector_presence_low_power_off.c:61
PRESENCE_PRESET_CONFIG_LOW_POWER_WAKEUP
@ PRESENCE_PRESET_CONFIG_LOW_POWER_WAKEUP
Definition: example_detector_presence_low_power_off.c:54
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_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
presence_preset_config_t
presence_preset_config_t
Definition: example_detector_presence_low_power_hibernate.c:48
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_frame_rate_get
float acc_detector_presence_config_frame_rate_get(const acc_detector_presence_config_t *presence_config)
Get the frame rate.
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_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
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_detector_presence_low_power_off.c:59
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.
print_result
static void print_result(acc_detector_presence_result_t result)
Definition: example_detector_presence_low_power_off.c:372
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_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_sensor_destroy
void acc_sensor_destroy(acc_sensor_t *sensor)
Destroy a sensor instance freeing any resources allocated.
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.