forked from eyllanesc/minimu9-ahrs-gy85
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGY85.cpp
93 lines (82 loc) · 2.29 KB
/
GY85.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
85
86
87
88
89
90
91
92
93
#include "GY85.h"
GY85::GY85(const char * i2cDeviceName) :
i2c_hmc5883l(i2cDeviceName), i2c_adxl345(i2cDeviceName), i2c_itg3205(i2cDeviceName)
{
I2CBus i2c(i2cDeviceName);
i2c_hmc5883l.addressSet(hmc5883l_ADDRESS);
i2c_adxl345.addressSet(adxl345_ADDRESS);
i2c_itg3205.addressSet(itg3205_ADDRESS);
}
//Read
uint8_t GY85::readMagReg(uint8_t reg)
{
return i2c_hmc5883l.readByte(reg);
}
uint8_t GY85::readAccReg(uint8_t reg)
{
return i2c_adxl345.readByte(reg);
}
uint8_t GY85::readGyroReg(uint8_t reg)
{
return i2c_itg3205.readByte(reg);
}
//Write
void GY85::writeMagReg(uint8_t reg, uint8_t value)
{
i2c_hmc5883l.writeByte(reg, value);
}
void GY85::writeAccReg(uint8_t reg, uint8_t value)
{
i2c_adxl345.writeByte(reg, value);
}
void GY85::writeGyroReg(uint8_t reg, uint8_t value)
{
i2c_itg3205.writeByte(reg, value);
}
// Turns on the GY_85's magnetometers,accelerometer and gyroscopes and places them in normal
// mode.
void GY85::enable(void)
{
//init hmc5883l
writeMagReg(hmc5883l_Mode_Reg, 0x00);
writeMagReg(hmc5883l_Config_RegA, 0x18);
//init adxl345
writeAccReg(adxl345_reg_PowerCtl, 0x08);
writeAccReg(adxl345_reg_DataFormat, 0x08);
writeAccReg(adxl345_reg_BwRate, 0x09);
//init itg_3205
writeGyroReg(itg_3205_reg_PwrMgm, 0x00);
writeGyroReg(itg_3205_reg_SmplRtDiv, 0x07);
writeGyroReg(itg_3205_reg_DLPFFS, 0x1E);
writeGyroReg(itg_3205_reg_IntCfg, 0x00);
}
void GY85::readMag(void)
{
uint8_t block[6];
i2c_hmc5883l.readBlock(0x80 | hmc5883l_AxisXDataReg_MSB, sizeof(block), block);
m[0] = (int16_t)(block[0] << 8 | block[1]);
m[2] = (int16_t)(block[2] << 8 | block[3]);
m[1] = (int16_t)(block[4] << 8 | block[5]);
}
void GY85::readAcc(void)
{
uint8_t block[6];
i2c_adxl345.readBlock(0x80 | adxl345_reg_DataX0, sizeof(block), block);
a[0] = (int16_t)(block[1] << 8 | block[0]);
a[1] = (int16_t)(block[3] << 8 | block[2]);
a[2] = (int16_t)(block[5] << 8 | block[4]);
}
void GY85::readGyro(void)
{
uint8_t block[6];
i2c_itg3205.readBlock(0x80 | itg_3205_reg_GyroXOutH, sizeof(block), block);
g[0] = (int16_t)(block[0] << 8 | block[1]);
g[1] = (int16_t)(block[2] << 8 | block[3]);
g[2] = (int16_t)(block[4] << 8 | block[5]);
}
void GY85::read(void)
{
readMag();
readAcc();
readGyro();
}