acc_control_helper.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2022
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 <stddef.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 #include "acc_control_helper.h"
13 #include "acc_hal_integration_a121.h"
14 #include "acc_integration.h"
15 #include "acc_rss_a121.h"
16
17
18 #define SENSOR_CALIBRATION_TIMEOUT_MS 500
19 #define SENSOR_MEASURE_TIMEOUT_MS 1000
20
21
22 static bool acc_control_helper_calibrate(acc_control_helper_t *radar)
23 {
24 bool cal_status;
25 bool cal_complete = false;
26
27 do
28 {
29 cal_status = acc_sensor_calibrate(radar->sensor, &cal_complete, &radar->cal_result, radar->buffer, radar->buffer_size);
30
31 if (cal_status && !cal_complete)
32 {
33 cal_status = acc_hal_integration_wait_for_sensor_interrupt(radar->sensor_id, SENSOR_CALIBRATION_TIMEOUT_MS);
34 }
35 } while (cal_status && !cal_complete);
36
37 return cal_status;
38 }
39
40
41 bool acc_control_helper_create(acc_control_helper_t *radar, acc_sensor_id_t sensor_id)
42 {
43 memset(radar, 0, sizeof(acc_control_helper_t));
44 radar->sensor_id = sensor_id;
45 radar->config = acc_config_create();
46 return radar->config != NULL ? true : false;
47 }
48
49
50 void acc_control_helper_destroy(acc_control_helper_t *radar)
51 {
52 if (radar->sensor != NULL)
53 {
54 acc_sensor_destroy(radar->sensor);
55 radar->sensor = NULL;
56 }
57
58 if (radar->buffer != NULL)
59 {
60 acc_integration_mem_free(radar->buffer);
61 radar->buffer = NULL;
62 }
63
64 if (radar->config != NULL)
65 {
66 acc_config_destroy(radar->config);
67 radar->config = NULL;
68 }
69
70 if (radar->processing != NULL)
71 {
72 acc_processing_destroy(radar->processing);
73 radar->processing = NULL;
74 }
75
76 acc_hal_integration_sensor_disable(radar->sensor_id);
77 acc_hal_integration_sensor_supply_off(radar->sensor_id);
78 }
79
80
81 bool acc_control_helper_activate(acc_control_helper_t *radar)
82 {
83 radar->processing = acc_processing_create(radar->config, &radar->proc_meta);
84 if (radar->processing == NULL)
85 {
86 fprintf(stderr, "Failed to create processing\n");
87 goto fail;
88 }
89
90 if (!acc_rss_get_buffer_size(radar->config, &radar->buffer_size))
91 {
92 fprintf(stderr, "acc_rss_get_buffer_size() failed\n");
93 goto fail;
94 }
95
96 radar->buffer = acc_integration_mem_alloc(radar->buffer_size);
97 if (radar->buffer == NULL)
98 {
99 fprintf(stderr, "acc_rss_get_buffer_size() failed\n");
100 goto fail;
101 }
102
103 acc_hal_integration_sensor_supply_on(radar->sensor_id);
104 acc_hal_integration_sensor_enable(radar->sensor_id);
105
106 radar->sensor = acc_sensor_create(radar->sensor_id);
107 if (radar->sensor == NULL)
108 {
109 fprintf(stderr, "Failed to create sensor\n");
110 goto fail;
111 }
112
113 if (!acc_control_helper_calibrate(radar))
114 {
115 fprintf(stderr, "calibration failed\n");
116 goto fail;
117 }
118
119 acc_hal_integration_sensor_disable(radar->sensor_id);
120 acc_hal_integration_sensor_enable(radar->sensor_id);
121
122 if (!acc_sensor_prepare(radar->sensor, radar->config, &radar->cal_result, radar->buffer, radar->buffer_size))
123 {
124 fprintf(stderr, "acc_sensor_prepare failed\n");
125 acc_sensor_status(radar->sensor);
126 goto fail;
127 }
128
129 return true;
130
131 fail:
132 acc_hal_integration_sensor_disable(radar->sensor_id);
133 acc_hal_integration_sensor_supply_off(radar->sensor_id);
134
135 if (radar->sensor != NULL)
136 {
137 acc_sensor_destroy(radar->sensor);
138 radar->sensor = NULL;
139 }
140
141 if (radar->buffer != NULL)
142 {
143 acc_integration_mem_free(radar->buffer);
144 radar->buffer = NULL;
145 }
146
147 if (radar->processing != NULL)
148 {
149 acc_processing_destroy(radar->processing);
150 radar->processing = NULL;
151 }
152
153 return false;
154 }
155
156
157 bool acc_control_helper_get_next(acc_control_helper_t *radar)
158 {
159 if (!acc_sensor_measure(radar->sensor))
160 {
161 fprintf(stderr, "acc_sensor_measure failed\n");
162 goto fail;
163 }
164
165 if (!acc_hal_integration_wait_for_sensor_interrupt(radar->sensor_id, SENSOR_MEASURE_TIMEOUT_MS))
166 {
167 fprintf(stderr, "Sensor interrupt timeout\n");
168 goto fail;
169 }
170
171 if (!acc_sensor_read(radar->sensor, radar->buffer, radar->buffer_size))
172 {
173 fprintf(stderr, "acc_sensor_read failed\n");
174 goto fail;
175 }
176
177 acc_processing_execute(radar->processing, radar->buffer, &radar->proc_result);
178
179 return true;
180
181 fail:
182 acc_sensor_status(radar->sensor);
183 return false;
184 }
acc_control_helper_get_next
bool acc_control_helper_get_next(acc_control_helper_t *radar)
Perform a radar measurement and wait for the result.
Definition: acc_control_helper.c:157
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_control_helper_t::sensor_id
acc_sensor_id_t sensor_id
Definition: acc_control_helper.h:27
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_control_helper_t::processing
acc_processing_t * processing
Definition: acc_control_helper.h:28
acc_control_helper_t::config
acc_config_t * config
Definition: acc_control_helper.h:25
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
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_config_destroy
void acc_config_destroy(acc_config_t *config)
Destroy a configuration freeing any resources allocated.
SENSOR_MEASURE_TIMEOUT_MS
#define SENSOR_MEASURE_TIMEOUT_MS
Definition: acc_control_helper.c:19
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.
acc_config_create
acc_config_t * acc_config_create(void)
Create a configuration.
acc_integration.h
acc_control_helper_t::buffer_size
uint32_t buffer_size
Definition: acc_control_helper.h:30
acc_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_stm32.c:592
acc_control_helper_t::proc_meta
acc_processing_metadata_t proc_meta
Definition: acc_control_helper.h:32
acc_control_helper.h
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_control_helper_calibrate
static bool acc_control_helper_calibrate(acc_control_helper_t *radar)
Definition: acc_control_helper.c:22
acc_hal_integration_a121.h
acc_control_helper_destroy
void acc_control_helper_destroy(acc_control_helper_t *radar)
Destroy a helper instance.
Definition: acc_control_helper.c:50
acc_control_helper_t::sensor
acc_sensor_t * sensor
Definition: acc_control_helper.h:26
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_control_helper_t::buffer
void * buffer
Definition: acc_control_helper.h:29
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_control_helper_t::cal_result
acc_cal_result_t cal_result
Definition: acc_control_helper.h:31
acc_sensor_id_t
uint32_t acc_sensor_id_t
Type representing a sensor ID.
Definition: acc_definitions_common.h:13
acc_sensor_status
void acc_sensor_status(const acc_sensor_t *sensor)
Check the status of the sensor.
acc_control_helper_t
Definition: acc_control_helper.h:23
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
SENSOR_CALIBRATION_TIMEOUT_MS
#define SENSOR_CALIBRATION_TIMEOUT_MS
Definition: acc_control_helper.c:18
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_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_control_helper_t::proc_result
acc_processing_result_t proc_result
Definition: acc_control_helper.h:33
acc_control_helper_activate
bool acc_control_helper_activate(acc_control_helper_t *radar)
Activate the sensor.
Definition: acc_control_helper.c:81
acc_sensor_destroy
void acc_sensor_destroy(acc_sensor_t *sensor)
Destroy a sensor instance freeing any resources allocated.
acc_control_helper_create
bool acc_control_helper_create(acc_control_helper_t *radar, acc_sensor_id_t sensor_id)
Create a helper instance.
Definition: acc_control_helper.c:41
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.