-
Notifications
You must be signed in to change notification settings - Fork 1
/
L3G.cpp
238 lines (204 loc) · 6.17 KB
/
L3G.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <L3G.h>
#include <Wire.h>
#include <math.h>
// Defines ////////////////////////////////////////////////////////////////
// The Arduino two-wire interface uses a 7-bit number for the address,
// and sets the last bit correctly based on reads and writes
#define D20_SA0_HIGH_ADDRESS 0b1101011 // also applies to D20H
#define D20_SA0_LOW_ADDRESS 0b1101010 // also applies to D20H
#define L3G4200D_SA0_HIGH_ADDRESS 0b1101001
#define L3G4200D_SA0_LOW_ADDRESS 0b1101000
#define TEST_REG_ERROR -1
#define D20H_WHO_ID 0xD7
#define D20_WHO_ID 0xD4
#define L3G4200D_WHO_ID 0xD3
// Constructors ////////////////////////////////////////////////////////////////
L3G::L3G(void)
{
_device = device_auto;
io_timeout = 0; // 0 = no timeout
did_timeout = false;
}
// Public Methods //////////////////////////////////////////////////////////////
// Did a timeout occur in read() since the last call to timeoutOccurred()?
bool L3G::timeoutOccurred()
{
bool tmp = did_timeout;
did_timeout = false;
return tmp;
}
void L3G::setTimeout(unsigned int timeout)
{
io_timeout = timeout;
}
unsigned int L3G::getTimeout()
{
return io_timeout;
}
bool L3G::init(deviceType device, sa0State sa0)
{
int id;
// perform auto-detection unless device type and SA0 state were both specified
if (device == device_auto || sa0 == sa0_auto)
{
// check for L3GD20H, D20 if device is unidentified or was specified to be one of these types
if (device == device_auto || device == device_D20H || device == device_D20)
{
// check SA0 high address unless SA0 was specified to be low
if (sa0 != sa0_low && (id = testReg(D20_SA0_HIGH_ADDRESS, WHO_AM_I)) != TEST_REG_ERROR)
{
// device responds to address 1101011; it's a D20H or D20 with SA0 high
sa0 = sa0_high;
if (device == device_auto)
{
// use ID from WHO_AM_I register to determine device type
device = (id == D20H_WHO_ID) ? device_D20H : device_D20;
}
}
// check SA0 low address unless SA0 was specified to be high
else if (sa0 != sa0_high && (id = testReg(D20_SA0_LOW_ADDRESS, WHO_AM_I)) != TEST_REG_ERROR)
{
// device responds to address 1101010; it's a D20H or D20 with SA0 low
sa0 = sa0_low;
if (device == device_auto)
{
// use ID from WHO_AM_I register to determine device type
device = (id == D20H_WHO_ID) ? device_D20H : device_D20;
}
}
}
// check for L3G4200D if device is still unidentified or was specified to be this type
if (device == device_auto || device == device_4200D)
{
if (sa0 != sa0_low && testReg(L3G4200D_SA0_HIGH_ADDRESS, WHO_AM_I) == L3G4200D_WHO_ID)
{
// device responds to address 1101001; it's a 4200D with SA0 high
device = device_4200D;
sa0 = sa0_high;
}
else if (sa0 != sa0_high && testReg(L3G4200D_SA0_LOW_ADDRESS, WHO_AM_I) == L3G4200D_WHO_ID)
{
// device responds to address 1101000; it's a 4200D with SA0 low
device = device_4200D;
sa0 = sa0_low;
}
}
// make sure device and SA0 were successfully detected; otherwise, indicate failure
if (device == device_auto || sa0 == sa0_auto)
{
return false;
}
}
_device = device;
// set device address
switch (device)
{
case device_D20H:
case device_D20:
address = (sa0 == sa0_high) ? D20_SA0_HIGH_ADDRESS : D20_SA0_LOW_ADDRESS;
break;
case device_4200D:
address = (sa0 == sa0_high) ? L3G4200D_SA0_HIGH_ADDRESS : L3G4200D_SA0_LOW_ADDRESS;
break;
}
return true;
}
/*
Enables the L3G's gyro. Also:
- Sets gyro full scale (gain) to default power-on value of +/- 250 dps
(specified as +/- 245 dps for L3GD20H).
- Selects 200 Hz ODR (output data rate). (Exact rate is specified as 189.4 Hz
for L3GD20H and 190 Hz for L3GD20.)
Note that this function will also reset other settings controlled by
the registers it writes to.
*/
void L3G::enableDefault(void)
{
if (_device == device_D20H)
{
// 0x00 = 0b00000000
// Low_ODR = 0 (low speed ODR disabled)
writeReg(LOW_ODR, 0x00);
}
// 0x00 = 0b00000000
// FS = 00 (+/- 250 dps full scale)
writeReg(CTRL_REG4, 0x00);
// 0x6F = 0b01101111
// DR = 01 (200 Hz ODR); BW = 10 (50 Hz bandwidth); PD = 1 (normal mode); Zen = Yen = Xen = 1 (all axes enabled)
writeReg(CTRL_REG1, 0x6F);
}
// Writes a gyro register
void L3G::writeReg(byte reg, byte value)
{
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(value);
last_status = Wire.endTransmission();
}
// Reads a gyro register
byte L3G::readReg(byte reg)
{
byte value;
Wire.beginTransmission(address);
Wire.write(reg);
last_status = Wire.endTransmission();
Wire.requestFrom(address, (byte)1);
value = Wire.read();
Wire.endTransmission();
return value;
}
// Reads the 3 gyro channels and stores them in vector g
void L3G::read()
{
Wire.beginTransmission(address);
// assert the MSB of the address to get the gyro
// to do slave-transmit subaddress updating.
Wire.write(OUT_X_L | (1 << 7));
Wire.endTransmission();
Wire.requestFrom(address, (byte)6);
unsigned int millis_start = millis();
while (Wire.available() < 6)
{
if (io_timeout > 0 && ((unsigned int)millis() - millis_start) > io_timeout)
{
did_timeout = true;
return;
}
}
uint8_t xlg = Wire.read();
uint8_t xhg = Wire.read();
uint8_t ylg = Wire.read();
uint8_t yhg = Wire.read();
uint8_t zlg = Wire.read();
uint8_t zhg = Wire.read();
// combine high and low bytes
g.x = (int16_t)(xhg << 8 | xlg);
g.y = (int16_t)(yhg << 8 | ylg);
g.z = (int16_t)(zhg << 8 | zlg);
}
void L3G::vector_normalize(vector<float> *a)
{
float mag = sqrt(vector_dot(a,a));
a->x /= mag;
a->y /= mag;
a->z /= mag;
}
// Private Methods //////////////////////////////////////////////////////////////
int L3G::testReg(byte address, regAddr reg)
{
Wire.beginTransmission(address);
Wire.write((byte)reg);
if (Wire.endTransmission() != 0)
{
return TEST_REG_ERROR;
}
Wire.requestFrom(address, (byte)1);
if (Wire.available())
{
return Wire.read();
}
else
{
return TEST_REG_ERROR;
}
}