-
Notifications
You must be signed in to change notification settings - Fork 2
/
mpu6050.c
executable file
·279 lines (239 loc) · 8.17 KB
/
mpu6050.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
/* Name: mpu6050_low.c
* Author: Zhyhariev Mikhail
* License: MIT
*/
#include <math.h>
#include <stdlib.h>
#include "mpu6050.h"
/**
* Move pointer to register
* @param reg register address [hex]
*/
void _MPU6050_moveToReg(u8 reg) {
I2C_start();
I2C_send(MPU6050_W);
I2C_send(reg);
I2C_stop();
}
/**
* Getting the value from a register
* @param reg register address [hex]
* @param len number of bytes of the register
* @return register value
*/
s32 _MPU6050_getRegValue(u8 reg, u8 len) {
_MPU6050_moveToReg(reg);
I2C_start();
I2C_send(MPU6050_R);
s32 value = 0;
for (u8 i = 0; i < len - 1; i++) {
value += I2C_get(0) << 8;
}
value += I2C_get(1);
I2C_stop();
return value;
}
/**
* Writing value to the register
* @param reg register address [hex]
* @param value value to write
*/
void _MPU6050_writeToReg(u8 reg, u8 value) {
I2C_start();
I2C_send(MPU6050_W);
I2C_send(reg);
I2C_send(value);
I2C_stop();
}
/**
* Getting an array of registers values
* @param reg - register address [hex]
* @param len - number of bytes of the register
* @return the array of values
*/
s32* _MPU6050_getArrValues(u8 reg, u8 len) {
// Create array
s32 *arr = (s32 *)malloc(len * sizeof(s32));
// Getting values of registers
for (u8 i = 0; i < len; i++) {
arr[i] = _MPU6050_getRegValue(reg + i * 2, 2);
}
return arr;
}
/**
* Initialize and set the settings MPU6050
*/
void MPU6050_Init(void) {
// I2C Initialize
I2C_init();
//Sets sample rate to 8000/1+7 = 1000Hz
_MPU6050_writeToReg(SMPLRT_DIV, 0x07);
//Disable FSync, 256Hz DLPF
_MPU6050_writeToReg(CONFIG, 0x00);
//Disable gyro self tests, scale of 2000 degrees/s
_MPU6050_writeToReg(GYRO_CONFIG, 0b00011000);
//Disable accel self tests, scale of +-2g, no DHPF
_MPU6050_writeToReg(ACCEL_CONFIG, 0x00);
//Freefall threshold of |0mg|
_MPU6050_writeToReg(FF_THR, 0x00);
//Freefall duration limit of 0
_MPU6050_writeToReg(FF_DUR, 0x00);
//Motion threshold of 0mg
_MPU6050_writeToReg(MOT_THR, 0x00);
//Motion duration of 0s
_MPU6050_writeToReg(MOT_DUR, 0x00);
//Zero motion threshold
_MPU6050_writeToReg(ZRMOT_THR, 0x00);
//Zero motion duration threshold
_MPU6050_writeToReg(ZRMOT_DUR, 0x00);
//Disable sensor output to FIFO buffer
_MPU6050_writeToReg(FIFO_EN, 0x00);
//AUX I2C setup
//Sets AUX I2C to single master control, plus other config
_MPU6050_writeToReg(I2C_MST_CTRL, 0x00);
//Setup AUX I2C slaves
_MPU6050_writeToReg(I2C_SLV0_ADDR, 0x00);
_MPU6050_writeToReg(I2C_SLV0_REG, 0x00);
_MPU6050_writeToReg(I2C_SLV0_CTRL, 0x00);
_MPU6050_writeToReg(I2C_SLV1_ADDR, 0x00);
_MPU6050_writeToReg(I2C_SLV1_REG, 0x00);
_MPU6050_writeToReg(I2C_SLV1_CTRL, 0x00);
_MPU6050_writeToReg(I2C_SLV2_ADDR, 0x00);
_MPU6050_writeToReg(I2C_SLV2_REG, 0x00);
_MPU6050_writeToReg(I2C_SLV2_CTRL, 0x00);
_MPU6050_writeToReg(I2C_SLV3_ADDR, 0x00);
_MPU6050_writeToReg(I2C_SLV3_REG, 0x00);
_MPU6050_writeToReg(I2C_SLV3_CTRL, 0x00);
_MPU6050_writeToReg(I2C_SLV4_ADDR, 0x00);
_MPU6050_writeToReg(I2C_SLV4_REG, 0x00);
_MPU6050_writeToReg(I2C_SLV4_DO, 0x00);
_MPU6050_writeToReg(I2C_SLV4_CTRL, 0x00);
_MPU6050_writeToReg(I2C_SLV4_DI, 0x00);
//I2C_MST_STATUS //Read-only
//Setup INT pin and AUX I2C pass through
_MPU6050_writeToReg(INT_PIN_CFG, 0x00);
//Enable data ready interrupt
_MPU6050_writeToReg(INT_ENABLE, 0x00);
//Slave out, dont care
_MPU6050_writeToReg(I2C_SLV0_DO, 0x00);
_MPU6050_writeToReg(I2C_SLV1_DO, 0x00);
_MPU6050_writeToReg(I2C_SLV2_DO, 0x00);
_MPU6050_writeToReg(I2C_SLV3_DO, 0x00);
//More slave config
_MPU6050_writeToReg(I2C_MST_DELAY_CTRL, 0x00);
//Reset sensor signal paths
_MPU6050_writeToReg(SIGNAL_PATH_RESET, 0x00);
//Motion detection control
_MPU6050_writeToReg(MOT_DETECT_CTRL, 0x00);
//Disables FIFO, AUX I2C, FIFO and I2C reset bits to 0
_MPU6050_writeToReg(USER_CTRL, 0x00);
//Sets clock source to gyro reference w/ PLL
_MPU6050_writeToReg(PWR_MGMT_1, 0b00000010);
//Controls frequency of wakeups in accel low power mode plus the sensor standby modes
_MPU6050_writeToReg(PWR_MGMT_2, 0x00);
}
/**
* Returning a value of "whoAmI" register MPU6050
* @return "whoAmI" register value
*/
u8 MPU6050_whoAmI(void) {
// Getting value of "whoAmI" register
return _MPU6050_getRegValue(WHO_AM_I, 1);
}
/**
* Getting a value of temperature registers MPU6050
* @return temperature register value
*/
s32 MPU6050_getTemp(void) {
// Getting value of temperature register
return _MPU6050_getRegValue(TEMP_OUT_H, 2);
}
/**
* Getting a value of accelerometer registers MPU6050
* @return the array of accelerometer registers values
*/
s32* MPU6050_getAccel(void) {
// Getting value of accelerometer registers for X, Y and Z axises
return _MPU6050_getArrValues(ACCEL_XOUT_H, 3);
}
/**
* Getting a value of gyroscope registers MPU6050
* @return the array of gyroscope registers values
*/
s32* MPU6050_getGyro(void) {
// Getting value of gyroscope registers for X, Y and Z axises
return _MPU6050_getArrValues(GYRO_XOUT_H, 3);
}
/**
* Computing the temperature in degrees Celsius
* @return temperature in degrees Celsius
*/
float MPU6050_countTemp(void) {
// Getting the values of temp_high and temp_l registers
s32 temp_reg = MPU6050_getTemp();
// Computing the temperature in degrees Celsius
return temp_reg / 340 + 36.53;
}
/**
* Counts the deviation angle of the MPU6050 module from the accelerometer data
* @param a - accelerometer data on the axes x, y, z
* @param b - accelerometer data on the axes x, y, z
* @param c - accelerometer data on the axes x, y, z
* @return calculated angle
*/
float _MPU6050_countAccelAngle(float a, float b, float c) {
return 57.295 * atan(a / sqrt(pow(b, 2) + pow(c,2)));
}
/**
* [_MPU6050_countGyroAngle description]
* @param previous_data - angles that has benn counted on previous iteration
* @param data - gyroscope data
* @param delta - the time that passed between the measurements
* @return calculated angle
*/
float _MPU6050_countGyroAngle(float previous_data, s32 data, s32 delta) {
return previous_data + data * delta * 0.001;
}
/**
* Counts the deviation angles of the MPU6050 module from the accelerometer data on the axes x, y, z
* @return an array of the calculated angles
*/
float* MPU6050_getAccelAngles(void) {
s32* accel = MPU6050_getAccel();
float *accel_angle = (float *)malloc(sizeof(float) * 3);
accel_angle[0] = _MPU6050_countAccelAngle(accel[0], accel[1], accel[2]);
accel_angle[1] = _MPU6050_countAccelAngle(accel[1], accel[0], accel[2]);
accel_angle[2] = _MPU6050_countAccelAngle(accel[2], accel[0], accel[1]);
return accel_angle;
}
/**
* Counts the deviation angles of the MPU6050 module from the accelerometer data on the axes x, y, z
* @param previous_data - angles that has benn counted on previous iteration
* @param delta - the time that passed between the measurements
* @return an array of the calculated angles
*/
float* MPU6050_getGyroAngles(float* previous_data, s32 delta) {
s32* gyro = MPU6050_getGyro();
float *gyro_angle = (float *)malloc(sizeof(float) * 3);
for (u8 i = 0; i < 3; i++) {
gyro_angle[i] = _MPU6050_countGyroAngle(previous_data[i], gyro[i], delta);
}
return gyro_angle;
}
/**
* Carries out the filtration of calculated angles
* @param previous_data - a previous values array of data
* @param filter_func - a function that filters a data
* @return an array of filtered data
*/
float* MPU6050_getFilteredAngles(
float *previous_data,
float* (* filter_func)(float* accel, float* gyro, float* previous_data, u8 len)
) {
// Obtain the angles calculated from the accelerometer data
float* accel = MPU6050_getAccelAngles();
// Obtain the angles calculated from the gyroscope data
float* gyro = MPU6050_getGyroAngles(previous_data, 200);
// Use callback function and return an array of filtered data
return filter_func(accel, gyro, previous_data, 3);
}