forked from monkeylover1234/mesacmsrobotics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPU6050_getdata.cpp
84 lines (78 loc) · 2.06 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
84
/*
* @Author: ELEGOO
* @Date: 2019-10-22 11:59:09
* @LastEditTime: 2020-06-19 11:57:50
* @LastEditors: Changhua
* @Description: conqueror robot tank
* @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); //读取六轴原始数值
// 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 count = 0;
do
{
chip_id = accelgyro.getDeviceID();
Serial.print("MPU6050_chip_id: ");
Serial.println(chip_id);
delay(10);
count += 1;
if (count > 10)
{
return true;
}
} while (chip_id == 0X00 || chip_id == 0XFF); //确保从机设备在线(强行等待 获取 ID )
accelgyro.initialize();
// unsigned short times = 100; //采样次数
// for (int i = 0; i < times; i++)
// {
// gz = accelgyro.getRotationZ();
// gzo += gz;
// }
// gzo /= times; //计算陀螺仪偏移
return false;
}
bool MPU6050_getdata::MPU6050_calibration(void)
{
unsigned short times = 100; //采样次数
for (unsigned int i = 0; i < times; i++)
{
gz = accelgyro.getRotationZ();
gzo += gz;
}
gzo /= times; //计算陀螺仪偏移
// gzo = accelgyro.getRotationZ();
return false;
}
bool MPU6050_getdata::MPU6050_dveGetEulerAngles(float *Yaw)
{
unsigned long now = millis(); //当前时间(ms)
dt = (now - lastTime) / 1000.0; //微分时间(s)
lastTime = now; //上一次采样时间(ms)
gz = accelgyro.getRotationZ();
float gyroz = -(gz - gzo) / 131.0 * dt; //z轴角速度
if (fabs(gyroz) < 0.05)
{
gyroz = 0.00;
}
agz += gyroz; //z轴角速度积分
*Yaw = agz;
return false;
}