User | Revision | Line number | New contents of line |
SED9008 |
0:13307e4676f9
|
1
|
/**
|
SED9008 |
0:13307e4676f9
|
2
|
* Copyright (c) 2011 Pololu Corporation. For more information, see
|
SED9008 |
0:13307e4676f9
|
3
|
*
|
SED9008 |
0:13307e4676f9
|
4
|
* http://www.pololu.com/
|
SED9008 |
0:13307e4676f9
|
5
|
* http://forum.pololu.com/
|
SED9008 |
0:13307e4676f9
|
6
|
*
|
SED9008 |
0:13307e4676f9
|
7
|
* Permission is hereby granted, free of charge, to any person
|
SED9008 |
0:13307e4676f9
|
8
|
* obtaining a copy of this software and associated documentation
|
SED9008 |
0:13307e4676f9
|
9
|
* files (the "Software"), to deal in the Software without
|
SED9008 |
0:13307e4676f9
|
10
|
* restriction, including without limitation the rights to use,
|
SED9008 |
0:13307e4676f9
|
11
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
SED9008 |
0:13307e4676f9
|
12
|
* copies of the Software, and to permit persons to whom the
|
SED9008 |
0:13307e4676f9
|
13
|
* Software is furnished to do so, subject to the following
|
SED9008 |
0:13307e4676f9
|
14
|
* conditions:
|
SED9008 |
0:13307e4676f9
|
15
|
*
|
SED9008 |
0:13307e4676f9
|
16
|
* The above copyright notice and this permission notice shall be
|
SED9008 |
0:13307e4676f9
|
17
|
* included in all copies or substantial portions of the Software.
|
SED9008 |
0:13307e4676f9
|
18
|
*
|
SED9008 |
0:13307e4676f9
|
19
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
SED9008 |
0:13307e4676f9
|
20
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
SED9008 |
0:13307e4676f9
|
21
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
SED9008 |
0:13307e4676f9
|
22
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
SED9008 |
0:13307e4676f9
|
23
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
SED9008 |
0:13307e4676f9
|
24
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
SED9008 |
0:13307e4676f9
|
25
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
SED9008 |
0:13307e4676f9
|
26
|
* OTHER DEALINGS IN THE SOFTWARE.
|
SED9008 |
0:13307e4676f9
|
27
|
*/
|
SED9008 |
0:13307e4676f9
|
28
|
|
SED9008 |
0:13307e4676f9
|
29
|
#include "mbed.h"
|
SED9008 |
0:13307e4676f9
|
30
|
#include <L3G4200D.h>
|
SED9008 |
0:13307e4676f9
|
31
|
#include <math.h>
|
SED9008 |
0:13307e4676f9
|
32
|
|
SED9008 |
0:13307e4676f9
|
33
|
// Defines ////////////////////////////////////////////////////////////////
|
SED9008 |
0:13307e4676f9
|
34
|
|
SED9008 |
0:13307e4676f9
|
35
|
// The Arduino two-wire interface uses a 7-bit number for the address,
|
SED9008 |
0:13307e4676f9
|
36
|
// and sets the last bit correctly based on reads and writes
|
SED9008 |
0:13307e4676f9
|
37
|
// mbed I2C libraries take the 7-bit address shifted left 1 bit
|
SED9008 |
0:13307e4676f9
|
38
|
// #define GYR_ADDRESS (0xD2 >> 1)
|
SED9008 |
0:13307e4676f9
|
39
|
#define GYR_ADDRESS 0xD1
|
SED9008 |
0:13307e4676f9
|
40
|
|
SED9008 |
0:13307e4676f9
|
41
|
// Public Methods //////////////////////////////////////////////////////////////
|
SED9008 |
0:13307e4676f9
|
42
|
|
SED9008 |
0:13307e4676f9
|
43
|
// Constructor
|
SED9008 |
0:13307e4676f9
|
44
|
L3G4200D::L3G4200D(PinName sda, PinName scl):
|
SED9008 |
0:13307e4676f9
|
45
|
_device(sda, scl)
|
SED9008 |
0:13307e4676f9
|
46
|
{
|
SED9008 |
0:13307e4676f9
|
47
|
_device.frequency(400000);
|
SED9008 |
0:13307e4676f9
|
48
|
// Turns on the L3G4200D's gyro and places it in normal mode.
|
SED9008 |
0:13307e4676f9
|
49
|
// 0x0F = 0b00001111
|
SED9008 |
0:13307e4676f9
|
50
|
// Normal power mode, all axes enabled
|
SED9008 |
0:13307e4676f9
|
51
|
writeReg(L3G4200D_CTRL_REG1, 0x0F);
|
SED9008 |
0:13307e4676f9
|
52
|
writeReg(L3G4200D_CTRL_REG4, 0x20); // 2000 dps full scale
|
SED9008 |
0:13307e4676f9
|
53
|
|
SED9008 |
0:13307e4676f9
|
54
|
}
|
SED9008 |
0:13307e4676f9
|
55
|
|
SED9008 |
0:13307e4676f9
|
56
|
// Writes a gyro register
|
SED9008 |
0:13307e4676f9
|
57
|
void L3G4200D::writeReg(byte reg, byte value)
|
SED9008 |
0:13307e4676f9
|
58
|
{
|
SED9008 |
0:13307e4676f9
|
59
|
data[0] = reg;
|
SED9008 |
0:13307e4676f9
|
60
|
data[1] = value;
|
SED9008 |
0:13307e4676f9
|
61
|
|
SED9008 |
0:13307e4676f9
|
62
|
_device.write(GYR_ADDRESS, data, 2);
|
SED9008 |
0:13307e4676f9
|
63
|
}
|
SED9008 |
0:13307e4676f9
|
64
|
|
SED9008 |
0:13307e4676f9
|
65
|
// Reads a gyro register
|
SED9008 |
0:13307e4676f9
|
66
|
byte L3G4200D::readReg(byte reg)
|
SED9008 |
0:13307e4676f9
|
67
|
{
|
SED9008 |
0:13307e4676f9
|
68
|
byte value = 0;
|
SED9008 |
0:13307e4676f9
|
69
|
|
SED9008 |
0:13307e4676f9
|
70
|
_device.write(GYR_ADDRESS, ®, 1);
|
SED9008 |
0:13307e4676f9
|
71
|
_device.read(GYR_ADDRESS, &value, 1);
|
SED9008 |
0:13307e4676f9
|
72
|
|
SED9008 |
0:13307e4676f9
|
73
|
return value;
|
SED9008 |
0:13307e4676f9
|
74
|
}
|
SED9008 |
0:13307e4676f9
|
75
|
|
SED9008 |
0:13307e4676f9
|
76
|
// Reads the 3 gyro channels and stores them in vector g
|
SED9008 |
0:13307e4676f9
|
77
|
void L3G4200D::read(int g[3])
|
SED9008 |
0:13307e4676f9
|
78
|
{
|
SED9008 |
0:13307e4676f9
|
79
|
// assert the MSB of the address to get the gyro
|
SED9008 |
0:13307e4676f9
|
80
|
// to do slave-transmit subaddress updating.
|
SED9008 |
0:13307e4676f9
|
81
|
data[0] = L3G4200D_OUT_X_L | (1 << 7);
|
SED9008 |
0:13307e4676f9
|
82
|
_device.write(GYR_ADDRESS, data, 1);
|
SED9008 |
0:13307e4676f9
|
83
|
|
SED9008 |
0:13307e4676f9
|
84
|
// Wire.requestFrom(GYR_ADDRESS, 6);
|
SED9008 |
0:13307e4676f9
|
85
|
// while (Wire.available() < 6);
|
SED9008 |
0:13307e4676f9
|
86
|
|
SED9008 |
0:13307e4676f9
|
87
|
_device.read(GYR_ADDRESS, data, 6);
|
SED9008 |
0:13307e4676f9
|
88
|
|
SED9008 |
0:13307e4676f9
|
89
|
uint8_t xla = data[0];
|
SED9008 |
0:13307e4676f9
|
90
|
uint8_t xha = data[1];
|
SED9008 |
0:13307e4676f9
|
91
|
uint8_t yla = data[2];
|
SED9008 |
0:13307e4676f9
|
92
|
uint8_t yha = data[3];
|
SED9008 |
0:13307e4676f9
|
93
|
uint8_t zla = data[4];
|
SED9008 |
0:13307e4676f9
|
94
|
uint8_t zha = data[5];
|
SED9008 |
0:13307e4676f9
|
95
|
|
SED9008 |
0:13307e4676f9
|
96
|
g[0] = (short) (xha << 8 | xla);
|
SED9008 |
0:13307e4676f9
|
97
|
g[1] = (short) (yha << 8 | yla);
|
SED9008 |
0:13307e4676f9
|
98
|
g[2] = (short) (zha << 8 | zla);
|
SED9008 |
0:13307e4676f9
|
99
|
} |