/
calib_profile_example.c
393 lines (326 loc) · 14.4 KB
/
calib_profile_example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*!
* Copyright (C) 2015 - 2016 Bosch Sensortec GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of the
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
* OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*
* The information provided is believed to be accurate and reliable.
* The copyright holder assumes no responsibility
* for the consequences of use
* of such information nor for any infringement of patents or
* other rights of third parties which may result from its use.
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
*/
/********************************************************************************/
/* HEADER FILES */
/********************************************************************************/
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include "asf.h"
#include "task.h"
#include "arm_math.h"
#include "demo-tasks.h"
#include "bhy_support.h"
#include "bhy_uc_driver.h"
#include ".\firmware\Bosch_PCB_7183_di03_BMI160_BMM150-7183_di03.2.1.11696_170103.h"
/********************************************************************************/
/* MACROS */
/********************************************************************************/
/* should be greater or equal to 69 bytes, page size (50) + maximum packet size(18) + 1 */
#define FIFO_SIZE 300
#define ROTATION_VECTOR_SAMPLE_RATE 100
#define MAX_PACKET_LENGTH 18
#define OUT_BUFFER_SIZE 60
#define BHY_PARAM_OFFSET_ACC_GET 6
#define BHY_PARAM_OFFSET_MAG_GET 7
#define BHY_PARAM_OFFSET_GYRO_GET 8
#define BHY_PARAM_OFFSET_ACC_SET 15
#define BHY_PARAM_OFFSET_MAG_SET 16
#define BHY_PARAM_OFFSET_GYRO_SET 17
/*!
* @brief Sensor configuration
* This structure holds calib profile informations
*/
typedef struct
{
s16 x_offset; /** 3-axis offset */
s16 y_offset; /** 3-axis offset */
s16 z_offset; /** 3-axis offset */
s16 radius; /** radius */
}ts_calibparam;
/*!
* struct ts_calibparam
* brief calibration parameters
*/
typedef struct
{
ts_calibparam calibParam;
uint8_t accuracy;
}ts_calibprofile;
/********************************************************************************/
/* GLOBAL VARIABLES */
/********************************************************************************/
/********************************************************************************/
/* STATIC VARIABLES */
/********************************************************************************/
char out_buffer[OUT_BUFFER_SIZE] = " W: 0.999 X: 0.999 Y: 0.999 Z: 0.999 \r";
uint8_t fifo[FIFO_SIZE];
ts_calibprofile calib_profile[3];
/********************************************************************************/
/* FUNCTIONS */
/********************************************************************************/
/*!
* @brief This function is callback function for acquring sensor datas
*
* @param[in] sensor_data
* @param[in] sensor_id
*/
static void sensors_callback_calib_profile(bhy_data_generic_t * sensor_data, bhy_virtual_sensor_t sensor_id)
{
float temp;
uint8_t index;
temp = sensor_data->data_quaternion.w / 16384.0f; /* change the data unit by dividing 16384 */
out_buffer[3] = temp < 0 ? '-' : ' ';
temp = temp < 0 ? -temp : temp;
out_buffer[4] = floor(temp) + '0';
for (index = 6; index <= 8; index++)
{
temp = (temp - floor(temp)) * 10;
out_buffer[index] = floor(temp) + '0';
}
temp = sensor_data->data_quaternion.x / 16384.0f;
out_buffer[13] = temp < 0 ? '-' : ' ';
temp = temp < 0 ? -temp : temp;
out_buffer[14] = floor(temp) + '0';
for (index = 16; index <= 18; index++)
{
temp = (temp - floor(temp)) * 10;
out_buffer[index] = floor(temp) + '0';
}
temp = sensor_data->data_quaternion.y / 16384.0f;
out_buffer[23] = temp < 0 ? '-' : ' ';
temp = temp < 0 ? -temp : temp;
out_buffer[24] = floor(temp) + '0';
for (index = 26; index <= 28; index++)
{
temp = (temp - floor(temp)) * 10;
out_buffer[index] = floor(temp) + '0';
}
temp = sensor_data->data_quaternion.z / 16384.0f;
out_buffer[33] = temp < 0 ? '-' : ' ';
temp = temp < 0 ? -temp : temp;
out_buffer[34] = floor(temp) + '0';
for (index = 36; index <= 38; index++)
{
temp = (temp - floor(temp)) * 10;
out_buffer[index] = floor(temp) + '0';
}
DEBUG("x=%d, y=%d, z=%d, w=%d\n",
sensor_data->data_quaternion.x,
sensor_data->data_quaternion.y,
sensor_data->data_quaternion.z,
sensor_data->data_quaternion.w
);
}
/*!
* @brief This function is used to run bhy hub
*/
void demo_sensor(void)
{
int8_t ret;
/* BHY Variable*/
uint8_t *fifoptr = NULL;
uint8_t bytes_left_in_fifo = 0;
uint16_t bytes_remaining = 0;
uint16_t bytes_read = 0;
bhy_data_generic_t fifo_packet;
bhy_data_type_t packet_type;
BHY_RETURN_FUNCTION_TYPE result;
uint32_t count = 0;
uint32_t i;
/* the remapping matrix for BHA or BHI here should be configured according to its placement on customer's PCB. */
/* for details, please check 'Application Notes Axes remapping of BHA250(B)/BHI160(B)' document. */
int8_t bhy_mapping_matrix_config[3*3] = {0,1,0,-1,0,0,0,0,1};
/* the remapping matrix for Magnetometer should be configured according to its placement on customer's PCB. */
/* for details, please check 'Application Notes Axes remapping of BHA250(B)/BHI160(B)' document. */
int8_t mag_mapping_matrix_config[3*3] = {0,1,0,1,0,0,0,0,-1};
/* To get the customized version number in firmware, it is necessary to read Parameter Page 2, index 125 */
/* to get this information. This feature is only supported for customized firmware. To get this customized */
/* firmware, you need to contact your local FAE of Bosch Sensortec. */
//struct cus_version_t bhy_cus_version;
/* init the bhy chip */
if(bhy_driver_init(&bhy1_fw))
{
DEBUG("Fail to init bhy\n");
}
/* wait for the bhy trigger the interrupt pin go down and up again */
while (ioport_get_pin_level(BHY_INT));
while (!ioport_get_pin_level(BHY_INT));
/* To get the customized version number in firmware, it is necessary to read Parameter Page 2, index 125 */
/* to get this information. This feature is only supported for customized firmware. To get this customized */
/* firmware, you need to contact your local FAE of Bosch Sensortec. */
//bhy_read_parameter_page(BHY_PAGE_2, PAGE2_CUS_FIRMWARE_VERSION, (uint8_t*)&bhy_cus_version, sizeof(struct cus_version_t));
//DEBUG("cus version base:%d major:%d minor:%d\n", bhy_cus_version.base, bhy_cus_version.major, bhy_cus_version.minor);
/* the remapping matrix for BHI and Magmetometer should be configured here to make sure rotation vector is */
/* calculated in a correct coordinates system. */
bhy_mapping_matrix_set(PHYSICAL_SENSOR_INDEX_ACC, bhy_mapping_matrix_config);
bhy_mapping_matrix_set(PHYSICAL_SENSOR_INDEX_MAG, mag_mapping_matrix_config);
bhy_mapping_matrix_set(PHYSICAL_SENSOR_INDEX_GYRO, bhy_mapping_matrix_config);
/* install the callback function for parse fifo data */
if(bhy_install_sensor_callback(VS_TYPE_ROTATION_VECTOR, VS_WAKEUP, sensors_callback_calib_profile))
{
DEBUG("Fail to install sensor callback\n");
}
do_calibrate:
/* first time run rotation vector, do calibrate actions for a while */
DEBUG("Presss button1 and do calibrate with BSX running...\n");
while(gpio_pin_is_high(GPIO_PUSH_BUTTON_1));
/* enables the virtual sensor */
if(bhy_enable_virtual_sensor(VS_TYPE_ROTATION_VECTOR, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0))
{
DEBUG("Fail to enable sensor id=%d\n", VS_TYPE_ROTATION_VECTOR);
}
while(count++ < 1500)
{
/* wait until the interrupt fires */
/* unless we already know there are bytes remaining in the fifo */
while (!ioport_get_pin_level(BHY_INT) && !bytes_remaining)
{
}
bhy_read_fifo(fifo + bytes_left_in_fifo, FIFO_SIZE - bytes_left_in_fifo, &bytes_read, &bytes_remaining);
bytes_read += bytes_left_in_fifo;
fifoptr = fifo;
packet_type = BHY_DATA_TYPE_PADDING;
do
{
/* this function will call callbacks that are registered */
result = bhy_parse_next_fifo_packet(&fifoptr, &bytes_read, &fifo_packet, &packet_type);
/* prints all the debug packets */
if (packet_type == BHY_DATA_TYPE_DEBUG)
{
bhy_print_debug_packet(&fifo_packet.data_debug, bhy_printf);
}
/* the logic here is that if doing a partial parsing of the fifo, then we should not parse */
/* the last 18 bytes (max length of a packet) so that we don't try to parse an incomplete */
/* packet */
} while ((result == BHY_SUCCESS) && (bytes_read > (bytes_remaining ? MAX_PACKET_LENGTH : 0)));
bytes_left_in_fifo = 0;
if (bytes_remaining)
{
/* shifts the remaining bytes to the beginning of the buffer */
while (bytes_left_in_fifo < bytes_read)
{
fifo[bytes_left_in_fifo++] = *(fifoptr++);
}
}
}
count = 0;
/* disable the rotation vector sensor */
if(bhy_disable_virtual_sensor(VS_TYPE_ROTATION_VECTOR, VS_WAKEUP))
{
DEBUG("Fail to disable sensor\n");
}
/* get the calibrate profile(acc mag gyro) from bhy and save to the host side. */
for(i = 0; i < 3; i++)
{
if(bhy_read_parameter_page(BHY_PAGE_2, BHY_PARAM_OFFSET_ACC_GET+i, &calib_profile[i], sizeof(ts_calibprofile)))
{
DEBUG("calibrate profile %d loaded. 0x%d, x=0x%x, y=0x%x, z=0x%x,r=0x%d\n",
i,
calib_profile[i].accuracy,
calib_profile[i].calibParam.x_offset,
calib_profile[i].calibParam.y_offset,
calib_profile[i].calibParam.z_offset,
calib_profile[i].calibParam.radius);
}
else
{
DEBUG("calibrate read failed on %d, re-do calibrate..\n", i);
goto do_calibrate;
}
/* set the valid profile(accuracy=3) to bhy */
if(calib_profile[i].accuracy != 3)
{
DEBUG("calibrate accuracy failed on %d, re-do calibrate..\n", i);
goto do_calibrate;
}
}
DEBUG("profile save to host side success\n");
DEBUG("enable the rotation vector sensor again with set calibrate profile(acc mag gyro) in advance\n");
for(i = 0; i < 3; i++)
{
if(calib_profile[i].accuracy != 3
|| bhy_write_parameter_page(BHY_PAGE_2, BHY_PARAM_OFFSET_ACC_SET+i, &calib_profile[i].calibParam, sizeof(ts_calibparam)))
{
DEBUG("calibrate set failed on %d ...\n", i);
}
}
if(bhy_enable_virtual_sensor(VS_TYPE_ROTATION_VECTOR, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0))
{
DEBUG("Fail to enable sensor\n");
}
while(1)
{
/* wait until the interrupt fires */
/* unless we already know there are bytes remaining in the fifo */
while (!ioport_get_pin_level(BHY_INT) && !bytes_remaining)
{
}
bhy_read_fifo(fifo + bytes_left_in_fifo, FIFO_SIZE - bytes_left_in_fifo, &bytes_read, &bytes_remaining);
bytes_read += bytes_left_in_fifo;
fifoptr = fifo;
packet_type = BHY_DATA_TYPE_PADDING;
do
{
/* this function will call callbacks that are registered */
result = bhy_parse_next_fifo_packet(&fifoptr, &bytes_read, &fifo_packet, &packet_type);
/* prints all the debug packets */
if (packet_type == BHY_DATA_TYPE_DEBUG)
{
bhy_print_debug_packet(&fifo_packet.data_debug, bhy_printf);
}
/* the logic here is that if doing a partial parsing of the fifo, then we should not parse */
/* the last 18 bytes (max length of a packet) so that we don't try to parse an incomplete */
/* packet */
} while ((result == BHY_SUCCESS) && (bytes_read > (bytes_remaining ? MAX_PACKET_LENGTH : 0)));
bytes_left_in_fifo = 0;
if (bytes_remaining)
{
/* shifts the remaining bytes to the beginning of the buffer */
while (bytes_left_in_fifo < bytes_read)
{
fifo[bytes_left_in_fifo++] = *(fifoptr++);
}
}
}
}
/** @}*/