-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPU6050_getdata.cpp
83 lines (78 loc) · 2.26 KB
/
MPU6050_getdata.cpp
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
/*
* @Author: ELEGOO
* @Date: 2019-10-22 11:59:09
* @LastEditTime: 2020-06-30 10:34:30
* @LastEditors: Changhua
* @Description: MPU6050 Data solution
* @FilePath:
*/
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
#include "MPU6050_getdata.h"
#include <stdio.h>
#include <math.h>
MPU6050 accelgyro;
MPU6050_getdata MPU6050Getdata;
// static void MsTimer2_MPU6050getdata(void)
// {
// sei();
// int16_t ax, ay, az, gx, gy, gz;
// accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); //Read the raw values of the six axes
// float gyroz = -(gz - MPU6050Getdata.gzo) / 131 * 0.005f;
// MPU6050Getdata.yaw += gyroz;
// }
bool MPU6050_getdata::MPU6050_dveInit(void)
{
Wire.begin();
uint8_t chip_id = 0x00;
uint8_t cout;
do
{
chip_id = accelgyro.getDeviceID();
Serial.print("MPU6050_chip_id: ");
Serial.println(chip_id);
delay(10);
cout += 1;
if (cout > 10)
{
return true;
}
} while (chip_id == 0X00 || chip_id == 0XFF); //Ensure that the slave device is online(Wait forcibly to get the ID)
accelgyro.initialize();
// unsigned short times = 100; //Sampling times
// for (int i = 0; i < times; i++)
// {
// gz = accelgyro.getRotationZ();
// gzo += gz;
// }
// gzo /= times; //Calculate gyroscope offset
return false;
}
bool MPU6050_getdata::MPU6050_calibration(void)
{
unsigned short times = 100; //Sampling times
for (int i = 0; i < times; i++)
{
gz = accelgyro.getRotationZ();
gzo += gz;
}
gzo /= times; //Calculate gyroscope offset
// gzo = accelgyro.getRotationZ();
return false;
}
bool MPU6050_getdata::MPU6050_dveGetEulerAngles(float *Yaw)
{
unsigned long now = millis(); //Record the current time(ms)
dt = (now - lastTime) / 1000.0; //Caculate the derivative time(s)
lastTime = now; //Record the last sampling time(ms)
gz = accelgyro.getRotationZ(); //Read the raw values of the six axes
float gyroz = -(gz - gzo) / 131.0 * dt; //z-axis angular velocity
if (fabs(gyroz) < 0.05) //Clear instant zero drift signal
{
gyroz = 0.00;
}
agz += gyroz; //z-axis angular velocity integral
*Yaw = agz;
return false;
}