-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathaccel.c
More file actions
206 lines (166 loc) · 6.76 KB
/
accel.c
File metadata and controls
206 lines (166 loc) · 6.76 KB
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
/**\
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
**/
/******************************************************************************/
/*! Header Files */
#include <stdio.h>
#include "bmi270.h"
#include "common.h"
#include <math.h>
/******************************************************************************/
/*! Macro definition */
/*! Earth's gravity in m/s^2 */
#define GRAVITY_EARTH (9.80665f)
/******************************************************************************/
/*! Static Function Declaration */
/*!
* @brief This internal API is used to set configurations for accel.
*
* @param[in] bmi : Structure instance of bmi2_dev.
*
* @return Status of execution.
*/
static int8_t set_accel_config(struct bmi2_dev *bmi);
/*!
* @brief This function converts lsb to meter per second squared for 16 bit accelerometer at
* range 2G, 4G, 8G or 16G.
*
* @param[in] val : LSB from each axis.
* @param[in] g_range : Gravity range.
* @param[in] bit_width : Resolution for accel.
*
* @return Accel values in meter per second squared.
*/
static float lsb_to_mps2(int16_t val, float g_range, uint8_t bit_width);
/******************************************************************************/
/*! Functions */
/* This function starts the execution of program. */
int main(void)
{
/* Status of api are returned to this variable. */
int8_t rslt;
/* Variable to define limit to print accel data. */
uint8_t limit = 100;
/* Assign accel sensor to variable. */
uint8_t sensor_list = BMI2_ACCEL;
/* Sensor initialization configuration. */
struct bmi2_dev bmi;
/* Structure to define type of sensor and their respective data. */
struct bmi2_sens_data sens_data = { { 0 } };
uint8_t indx = 0;
float x = 0, y = 0, z = 0;
struct bmi2_sens_config config;
/* Interface reference is given as a parameter
* For I2C : BMI2_I2C_INTF
* For SPI : BMI2_SPI_INTF
*/
rslt = bmi2_interface_init(&bmi, BMI2_SPI_INTF);
bmi2_error_codes_print_result(rslt);
/* Initialize bmi270. */
rslt = bmi270_init(&bmi);
bmi2_error_codes_print_result(rslt);
if (rslt == BMI2_OK)
{
/* Accel configuration settings. */
rslt = set_accel_config(&bmi);
bmi2_error_codes_print_result(rslt);
if (rslt == BMI2_OK)
{
/* NOTE:
* Accel enable must be done after setting configurations
*/
rslt = bmi2_sensor_enable(&sensor_list, 1, &bmi);
bmi2_error_codes_print_result(rslt);
if (rslt == BMI2_OK)
{
config.type = BMI2_ACCEL;
/* Get the accel configurations. */
rslt = bmi2_get_sensor_config(&config, 1, &bmi);
bmi2_error_codes_print_result(rslt);
printf("\nData set, Range, Acc_Raw_X, Acc_Raw_Y, Acc_Raw_Z, Acc_ms2_X, Acc_ms2_Y, Acc_ms2_Z\n\n");
while (indx <= limit)
{
rslt = bmi2_get_sensor_data(&sens_data, &bmi);
bmi2_error_codes_print_result(rslt);
if ((rslt == BMI2_OK) && (sens_data.status & BMI2_DRDY_ACC))
{
/* Converting lsb to meter per second squared for 16 bit accelerometer at 2G range. */
x = lsb_to_mps2(sens_data.acc.x, (float)2, bmi.resolution);
y = lsb_to_mps2(sens_data.acc.y, (float)2, bmi.resolution);
z = lsb_to_mps2(sens_data.acc.z, (float)2, bmi.resolution);
printf("%d, %d, %d, %d, %d, %4.2f, %4.2f, %4.2f\n",
indx,
config.cfg.acc.range,
sens_data.acc.x,
sens_data.acc.y,
sens_data.acc.z,
x,
y,
z);
indx++;
}
}
}
}
}
bmi2_coines_deinit();
return rslt;
}
/*!
* @brief This internal API is used to set configurations for accel.
*/
static int8_t set_accel_config(struct bmi2_dev *bmi)
{
/* Status of api are returned to this variable. */
int8_t rslt;
/* Structure to define accelerometer configuration. */
struct bmi2_sens_config config;
/* Configure the type of feature. */
config.type = BMI2_ACCEL;
/* Get default configurations for the type of feature selected. */
rslt = bmi2_get_sensor_config(&config, 1, bmi);
bmi2_error_codes_print_result(rslt);
if (rslt == BMI2_OK)
{
/* NOTE: The user can change the following configuration parameters according to their requirement. */
/* Set Output Data Rate */
config.cfg.acc.odr = BMI2_ACC_ODR_200HZ;
/* Gravity range of the sensor (+/- 2G, 4G, 8G, 16G). */
config.cfg.acc.range = BMI2_ACC_RANGE_2G;
/* The bandwidth parameter is used to configure the number of sensor samples that are averaged
* if it is set to 2, then 2^(bandwidth parameter) samples
* are averaged, resulting in 4 averaged samples.
* Note1 : For more information, refer the datasheet.
* Note2 : A higher number of averaged samples will result in a lower noise level of the signal, but
* this has an adverse effect on the power consumed.
*/
config.cfg.acc.bwp = BMI2_ACC_NORMAL_AVG4;
/* Enable the filter performance mode where averaging of samples
* will be done based on above set bandwidth and ODR.
* There are two modes
* 0 -> Ultra low power mode
* 1 -> High performance mode(Default)
* For more info refer datasheet.
*/
config.cfg.acc.filter_perf = BMI2_PERF_OPT_MODE;
/* Set the accel configurations. */
rslt = bmi2_set_sensor_config(&config, 1, bmi);
bmi2_error_codes_print_result(rslt);
/* Map data ready interrupt to interrupt pin. */
rslt = bmi2_map_data_int(BMI2_DRDY_INT, BMI2_INT1, bmi);
bmi2_error_codes_print_result(rslt);
}
return rslt;
}
/*!
* @brief This function converts lsb to meter per second squared for 16 bit accelerometer at
* range 2G, 4G, 8G or 16G.
*/
static float lsb_to_mps2(int16_t val, float g_range, uint8_t bit_width)
{
double power = 2;
float half_scale = (float)((pow((double)power, (double)bit_width) / 2.0f));
return (GRAVITY_EARTH * val * g_range) / half_scale;
}