-
Notifications
You must be signed in to change notification settings - Fork 18
/
Example.cpp
47 lines (34 loc) · 1.46 KB
/
Example.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
//-------------------------------MPU6050 Accelerometer and Gyroscope C++ library-----------------------------
//Copyright (c) 2019, Alex Mous
//Licensed under the CC BY-NC SA 4.0
//Example code
#include <MPU6050.h>
MPU6050 device(0x68);
int main() {
float ax, ay, az, gr, gp, gy; //Variables to store the accel, gyro and angle values
sleep(1); //Wait for the MPU6050 to stabilize
/*
//Calculate the offsets
std::cout << "Calculating the offsets...\n Please keep the accelerometer level and still\n This could take a couple of minutes...";
device.getOffsets(&ax, &ay, &az, &gr, &gp, &gy);
std::cout << "Gyroscope R,P,Y: " << gr << "," << gp << "," << gy << "\nAccelerometer X,Y,Z: " << ax << "," << ay << "," << az << "\n";
*/
//Read the current yaw angle
device.calc_yaw = true;
for (int i = 0; i < 40; i++) {
device.getAngle(0, &gr);
device.getAngle(1, &gp);
device.getAngle(2, &gy);
std::cout << "Current angle around the roll axis: " << gr << "\n";
std::cout << "Current angle around the pitch axis: " << gp << "\n";
std::cout << "Current angle around the yaw axis: " << gy << "\n";
usleep(250000); //0.25sec
}
//Get the current accelerometer values
device.getAccel(&ax, &ay, &az);
std::cout << "Accelerometer Readings: X: " << ax << ", Y: " << ay << ", Z: " << az << "\n";
//Get the current gyroscope values
device.getGyro(&gr, &gp, &gy);
std::cout << "Gyroscope Readings: X: " << gr << ", Y: " << gp << ", Z: " << gy << "\n";
return 0;
}