forked from omaraflak/GY-521-Raspberry-Pi-C-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libSensor.cpp
85 lines (70 loc) · 1.78 KB
/
libSensor.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
#include <stdint.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include "libSensor.h"
#include <stdio.h>
#include <cmath>
#define MPU6050_GYRO_XOUT_H 0x43 // R
#define MPU6050_GYRO_YOUT_H 0x45 // R
#define MPU6050_GYRO_ZOUT_H 0x47 // R
#define MPU6050_ACCEL_XOUT_H 0x3B // R
#define MPU6050_ACCEL_YOUT_H 0x3D // R
#define MPU6050_ACCEL_ZOUT_H 0x3F // R
#define MPU6050_PWR_MGMT_1 0x6B // R/W
#define MPU6050_I2C_ADDRESS 0x68 // I2C
Sensor::Sensor()
{
fd = wiringPiI2CSetup(MPU6050_I2C_ADDRESS);
if (fd == -1)
return;
wiringPiI2CReadReg8 (fd, MPU6050_PWR_MGMT_1);
wiringPiI2CWriteReg16(fd, MPU6050_PWR_MGMT_1, 0);
}
int8_t Sensor::getGyroX()
{
return (int8_t) wiringPiI2CReadReg8(fd, MPU6050_GYRO_XOUT_H);
}
int8_t Sensor::getGyroY()
{
return (int8_t) wiringPiI2CReadReg8(fd, MPU6050_GYRO_YOUT_H);
}
int8_t Sensor::getGyroZ()
{
return (int8_t) wiringPiI2CReadReg8(fd, MPU6050_GYRO_ZOUT_H);
}
int8_t Sensor::getAccelX()
{
return (int8_t) wiringPiI2CReadReg8(fd, MPU6050_ACCEL_XOUT_H);
}
int8_t Sensor::getAccelY()
{
return (int8_t) wiringPiI2CReadReg8(fd, MPU6050_ACCEL_YOUT_H);
}
int8_t Sensor::getAccelZ()
{
return (int8_t) wiringPiI2CReadReg8(fd, MPU6050_ACCEL_ZOUT_H);
}
float Sensor::getAngleX()
{
int x = getAccelX();
int y = getAccelY();
int z = getAccelZ();
float ax = atan(x/(sqrt(y*y+z*z)))* 180/M_PI;
return ax;
}
float Sensor::getAngleY()
{
int x = getAccelX();
int y = getAccelY();
int z = getAccelZ();
float ay = atan(y/(sqrt(x*x+z*z)))* 180/M_PI;
return ay;
}
float Sensor::getAngleZ()
{
int x = getAccelX();
int y = getAccelY();
int z = getAccelZ();
float az = atan((sqrt(y*y+x*x))/z)* 180/M_PI;
return az;
}