forked from amperka/Troyka-IMU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l3g4200d.cpp
102 lines (86 loc) · 2.21 KB
/
l3g4200d.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
94
95
96
97
98
99
100
101
102
#include <Wire.h>
#include <Arduino.h>
#include "l3g4200d.h"
#define ADR_FS_250 0x00
#define ADR_FS_500 0x10
#define ADR_FS_2000 0x20
#define SENS_FS_250 0.00875
#define SENS_FS_500 0.0175
#define SENS_FS_2000 0.07
L3G4200D_TWI::L3G4200D_TWI(uint8_t addr) : AxisHw(addr) {
}
void L3G4200D_TWI::setRange(uint8_t range) {
switch (range) {
case RANGE_250DPS: {
_ctrlReg4 = ADR_FS_250;
_mult = SENS_FS_250;
break;
}
case RANGE_500DPS: {
_ctrlReg4 = ADR_FS_500;
_mult = SENS_FS_500;
break;
}
case RANGE_2000DPS: {
_ctrlReg4 = ADR_FS_2000;
_mult = SENS_FS_2000;
break;
}
default: {
_mult = SENS_FS_250;
}
break;
}
writeCtrlReg4();
}
void L3G4200D_TWI::begin() {
// подключаемся к шине I²C
Wire.begin();
// включаем координаты x, y, z
_ctrlReg1 |= (1 << 0);
_ctrlReg1 |= (1 << 1);
_ctrlReg1 |= (1 << 2);
// включаем гироскоп
_ctrlReg1 |= (1 << 3);
// устанавливаем чувствительность
setRange(RANGE_250DPS);
writeCtrlReg1();
}
void L3G4200D_TWI::sleep(bool enable) {
if (enable)
_ctrlReg1 &= ~(1 << 3);
else
_ctrlReg1 |= (1 << 3);
writeCtrlReg1();
}
float L3G4200D_TWI::readDegPerSecX() {
return readX() * _mult;
}
float L3G4200D_TWI::readDegPerSecY() {
return readY() * _mult;
}
float L3G4200D_TWI::readDegPerSecZ() {
return readZ() * _mult;
}
float L3G4200D_TWI::readRadPerSecX() {
return readDegPerSecX() * DEG_TO_RAD;
}
float L3G4200D_TWI::readRadPerSecY() {
return readDegPerSecY() * DEG_TO_RAD;
}
float L3G4200D_TWI::readRadPerSecZ() {
return readDegPerSecZ() * DEG_TO_RAD;
}
void L3G4200D_TWI::readDegPerSecXYZ(float *gx, float *gy, float *gz) {
int16_t x, y, z;
readXYZ(&x, &y, &z);
*gx = x * _mult;
*gy = y * _mult;
*gz = z * _mult;
}
void L3G4200D_TWI::readRadPerSecXYZ(float *gx, float *gy, float *gz) {
readDegPerSecXYZ(gx, gy, gz);
(*gx) *= DEG_TO_RAD;
(*gy) *= DEG_TO_RAD;
(*gz) *= DEG_TO_RAD;
}