example_service_subsweeps.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2021-2025
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 #include "acc_config.h"
13 #include "acc_config_subsweep.h"
14 #include "acc_definitions_a121.h"
15 #include "acc_definitions_common.h"
16 #include "acc_hal_definitions_a121.h"
17 #include "acc_hal_integration_a121.h"
18 #include "acc_integration.h"
19 #include "acc_processing.h"
20 #include "acc_rss_a121.h"
21 #include "acc_sensor.h"
22
23 #include "acc_version.h"
24
25 /** \example example_service_subsweeps.c
26 * @brief This is an example on how to use subsweeps with the Service API
27 * @n
28 * The example executes as follows:
29 * - Create a configuration
30 * - Configure multiple subsweeps
31 * - Create a processing instance using the previously created configuration
32 * - Create a sensor instance
33 * - Prepare a sensor
34 * - Perform a sensor measurement and read out the data
35 * - Process the measurement subsweeps
36 * - Destroy the sensor instance
37 * - Destroy the processing instance
38 * - Destroy the configuration
39 */
40
41 #define SENSOR_ID (1U)
42 #define SENSOR_TIMEOUT_MS (1000U)
43 #define MAX_DATA_ENTRY_LEN 15 // "-32000+-32000i" + zero termination
44
45 static void print_data(acc_int16_complex_t *data, uint16_t data_length);
46
47 static void cleanup(acc_config_t *config, acc_processing_t *processing, acc_sensor_t *sensor, void *buffer);
48
49 int acconeer_main(int argc, char *argv[]);
50
51 int acconeer_main(int argc, char *argv[])
52 {
53 (void)argc;
54 (void)argv;
55 acc_config_t *config = NULL;
56 acc_processing_t *processing = NULL;
57 acc_sensor_t *sensor = NULL;
58 void *buffer = NULL;
59 uint32_t buffer_size = 0;
60 acc_processing_metadata_t proc_meta;
61 acc_processing_result_t proc_result;
62
63 printf("Acconeer software version %s\n", acc_version_get());
64
65 const acc_hal_a121_t *hal = acc_hal_rss_integration_get_implementation();
66
67 if (!acc_rss_hal_register(hal))
68 {
69 return EXIT_FAILURE;
70 }
71
72 config = acc_config_create();
73 if (config == NULL)
74 {
75 printf("acc_config_create() failed\n");
76 cleanup(config, processing, sensor, buffer);
77 return EXIT_FAILURE;
78 }
79
80 // Collect 16 sweeps per frame, applicable for all subsweeps
81 acc_config_sweeps_per_frame_set(config, 16);
82
83 // Setup 3 subsweeps with different gains, start points and number of points
84 acc_config_num_subsweeps_set(config, 3);
85
86 acc_config_subsweep_receiver_gain_set(config, 5, 0);
87 acc_config_subsweep_start_point_set(config, 100, 0);
88 acc_config_subsweep_num_points_set(config, 5, 0);
89
90 acc_config_subsweep_receiver_gain_set(config, 15, 1);
91 acc_config_subsweep_start_point_set(config, 300, 1);
92 acc_config_subsweep_num_points_set(config, 20, 1);
93
94 acc_config_subsweep_receiver_gain_set(config, 23, 2);
95 acc_config_subsweep_start_point_set(config, 500, 2);
96 acc_config_subsweep_num_points_set(config, 10, 2);
97
98 // Print the configuration
99 acc_config_log(config);
100
101 processing = acc_processing_create(config, &proc_meta);
102 if (processing == NULL)
103 {
104 printf("acc_processing_create() failed\n");
105 cleanup(config, processing, sensor, buffer);
106 return EXIT_FAILURE;
107 }
108
109 if (!acc_rss_get_buffer_size(config, &buffer_size))
110 {
111 printf("acc_rss_get_buffer_size() failed\n");
112 cleanup(config, processing, sensor, buffer);
113 return EXIT_FAILURE;
114 }
115
116 buffer = acc_integration_mem_alloc(buffer_size);
117 if (buffer == NULL)
118 {
119 printf("buffer allocation failed\n");
120 cleanup(config, processing, sensor, buffer);
121 return EXIT_FAILURE;
122 }
123
124 acc_hal_integration_sensor_supply_on(SENSOR_ID);
125 acc_hal_integration_sensor_enable(SENSOR_ID);
126
127 sensor = acc_sensor_create(SENSOR_ID);
128 if (sensor == NULL)
129 {
130 printf("acc_sensor_create() failed\n");
131 cleanup(config, processing, sensor, buffer);
132 return EXIT_FAILURE;
133 }
134
135 bool status;
136 bool cal_complete = false;
137 acc_cal_result_t cal_result;
138
139 do
140 {
141 status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
142
143 if (status && !cal_complete)
144 {
145 status = acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS);
146 }
147 } while (status && !cal_complete);
148
149 if (!status)
150 {
151 printf("acc_sensor_calibrate() failed\n");
152 acc_sensor_status(sensor);
153 cleanup(config, processing, sensor, buffer);
154 return EXIT_FAILURE;
155 }
156
157 // Reset sensor after calibration by disabling it
158 acc_hal_integration_sensor_disable(SENSOR_ID);
159 acc_hal_integration_sensor_enable(SENSOR_ID);
160
161 if (!acc_sensor_prepare(sensor, config, &cal_result, buffer, buffer_size))
162 {
163 printf("acc_sensor_prepare() failed\n");
164 acc_sensor_status(sensor);
165 cleanup(config, processing, sensor, buffer);
166 return EXIT_FAILURE;
167 }
168
169 for (uint32_t i = 0U; i < 5U; i++)
170 {
171 if (!acc_sensor_measure(sensor))
172 {
173 printf("acc_sensor_measure failed\n");
174 acc_sensor_status(sensor);
175 cleanup(config, processing, sensor, buffer);
176 return EXIT_FAILURE;
177 }
178
179 if (!acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS))
180 {
181 printf("Sensor interrupt timeout\n");
182 acc_sensor_status(sensor);
183 cleanup(config, processing, sensor, buffer);
184 return EXIT_FAILURE;
185 }
186
187 if (!acc_sensor_read(sensor, buffer, buffer_size))
188 {
189 printf("acc_sensor_read failed\n");
190 acc_sensor_status(sensor);
191 cleanup(config, processing, sensor, buffer);
192 return EXIT_FAILURE;
193 }
194
195 acc_processing_execute(processing, buffer, &proc_result);
196
197 // The number of sweeps can also be readout with acc_config_sweeps_per_frame_get()
198 uint16_t sweeps_per_frame = proc_meta.frame_data_length / proc_meta.sweep_data_length;
199 uint8_t number_of_subsweeps = acc_config_num_subsweeps_get(config);
200
201 for (uint16_t sweep_index = 0; sweep_index < sweeps_per_frame; sweep_index++)
202 {
203 for (uint8_t subsweep_index = 0; subsweep_index < number_of_subsweeps; subsweep_index++)
204 {
205 /* Each sweep is offsetted sweep_data_length into the frame,
206 and within the sweep the specific subsweep is offsetted
207 subsweep_data_offset. */
208 uint16_t subsweep_offset = sweep_index * proc_meta.sweep_data_length + proc_meta.subsweep_data_offset[subsweep_index];
209 uint16_t subsweep_length = proc_meta.subsweep_data_length[subsweep_index];
210 acc_int16_complex_t *subsweep_data = &proc_result.frame[subsweep_offset];
211
212 printf("Subsweep: %" PRIu16 ", sweep: %" PRIu16 ", processed data:\n", subsweep_index, sweep_index);
213 print_data(subsweep_data, subsweep_length);
214 }
215 }
216 }
217
218 cleanup(config, processing, sensor, buffer);
219
220 printf("Application finished OK\n");
221
222 return EXIT_SUCCESS;
223 }
224
225 static void cleanup(acc_config_t *config, acc_processing_t *processing, acc_sensor_t *sensor, void *buffer)
226 {
227 acc_hal_integration_sensor_disable(SENSOR_ID);
228 acc_hal_integration_sensor_supply_off(SENSOR_ID);
229
230 if (sensor != NULL)
231 {
232 acc_sensor_destroy(sensor);
233 }
234
235 if (processing != NULL)
236 {
237 acc_processing_destroy(processing);
238 }
239
240 if (config != NULL)
241 {
242 acc_config_destroy(config);
243 }
244
245 if (buffer != NULL)
246 {
247 acc_integration_mem_free(buffer);
248 }
249 }
250
251 static void print_data(acc_int16_complex_t *data, uint16_t data_length)
252 {
253 char buffer[MAX_DATA_ENTRY_LEN];
254
255 for (uint16_t i = 0; i < data_length; i++)
256 {
257 if ((i > 0) && ((i % 8) == 0))
258 {
259 printf("\n");
260 }
261
262 snprintf(buffer, sizeof(buffer), "%" PRIi16 "+%" PRIi16 "i", data[i].real, data[i].imag);
263
264 printf("%14s ", buffer);
265 }
266
267 printf("\n");
268 }
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_processing_metadata_t::subsweep_data_offset
uint16_t subsweep_data_offset[(4U)]
Definition: acc_processing.h:43
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_processing_result_t
Result provided by the processing module.
Definition: acc_processing.h:71
print_data
static void print_data(acc_int16_complex_t *data, uint16_t data_length)
Definition: example_service_subsweeps.c:251
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
buffer
void * buffer
Definition: i2c_example_cargo.c:40
acc_version.h
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_num_subsweeps_get
uint8_t acc_config_num_subsweeps_get(const acc_config_t *config)
Get the number of subsweeps to use.
acc_int16_complex_t
Data type for interger-based representation of complex numbers.
Definition: acc_definitions_common.h:40
acc_config_sweeps_per_frame_set
void acc_config_sweeps_per_frame_set(acc_config_t *config, uint16_t sweeps)
Set sweeps per frame.
acc_config_destroy
void acc_config_destroy(acc_config_t *config)
Destroy a configuration freeing any resources allocated.
acc_cal_result_t
The result from a completed calibration.
Definition: acc_definitions_a121.h:30
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_subsweep.h
acc_config_create
acc_config_t * acc_config_create(void)
Create a configuration.
acc_integration.h
cleanup
static void cleanup(acc_config_t *config, acc_processing_t *processing, acc_sensor_t *sensor, void *buffer)
Definition: example_service_subsweeps.c:225
config
cargo_config_t config
Definition: i2c_example_cargo.c:34
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:152
acc_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_stm32.c:592
acc_processing_metadata_t
Metadata that will be populated by the processing module during creation.
Definition: acc_processing.h:36
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
buffer_size
uint32_t buffer_size
Definition: i2c_example_cargo.c:41
sensor
acc_sensor_t * sensor
Definition: i2c_example_cargo.c:33
acc_processing_metadata_t::sweep_data_length
uint16_t sweep_data_length
Definition: acc_processing.h:41
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_hal_integration_a121.h
acc_processing_metadata_t::frame_data_length
uint16_t frame_data_length
Definition: acc_processing.h:39
acc_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
acc_config_t
struct acc_config acc_config_t
Definition: acc_config.h:24
printf
#define printf
Definition: printf.h:60
snprintf
#define snprintf
Definition: printf.h:84
acc_config_num_subsweeps_set
void acc_config_num_subsweeps_set(acc_config_t *config, uint8_t num_subsweeps)
Set the number of subsweeps to use.
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_config_subsweep_start_point_set
void acc_config_subsweep_start_point_set(acc_config_t *config, int32_t start_point, uint8_t index)
Set the starting point of the sweep.
acc_hal_definitions_a121.h
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_service_subsweeps.c:42
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_service_subsweeps.c:51
acc_processing_result_t::frame
acc_int16_complex_t * frame
Definition: acc_processing.h:91
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_processing_t
struct acc_processing_handle acc_processing_t
Definition: acc_processing.h:30
acc_sensor_status
void acc_sensor_status(const acc_sensor_t *sensor)
Check the status of the sensor.
acc_config_subsweep_receiver_gain_set
void acc_config_subsweep_receiver_gain_set(acc_config_t *config, uint8_t gain, uint8_t index)
Set receiver gain setting.
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
acc_definitions_common.h
acc_config_log
void acc_config_log(const acc_config_t *config)
Print a configuration to the log.
SENSOR_ID
#define SENSOR_ID
Definition: example_service_subsweeps.c:41
acc_config.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.
acc_config_subsweep_num_points_set
void acc_config_subsweep_num_points_set(acc_config_t *config, uint16_t num_points, uint8_t index)
Set the number of data points to measure.
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_processing.h
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_processing_metadata_t::subsweep_data_length
uint16_t subsweep_data_length[(4U)]
Definition: acc_processing.h:45
acc_definitions_a121.h
MAX_DATA_ENTRY_LEN
#define MAX_DATA_ENTRY_LEN
Definition: example_service_subsweeps.c:43
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.