-
Notifications
You must be signed in to change notification settings - Fork 1
/
ITG3200.cpp
154 lines (122 loc) · 3.65 KB
/
ITG3200.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
/*
-------------------------------------------------------------------------------
ITG3200.cpp
ITG3200 Project
Initially created by Rowland O'Flaherty (rowlandoflaherty.com) on 1/12/12.
Version 1.0
-------------------------------------------------------------------------------
*/
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "ITG3200.h"
#include <Arduino.h>
#include <Wire.h>
//------------------------------------------------------------------------------
// Lifecycle
//------------------------------------------------------------------------------
// Constructors
ITG3200::ITG3200()
:
_calX(0),
_calY(0),
_calZ(0) {
}
// Destructor
ITG3200::~ITG3200() {
}
// Copy constructor
ITG3200::ITG3200(const ITG3200& srcObj) {
copyHelper(srcObj);
}
// Copy helper function
void ITG3200::copyHelper(const ITG3200& srcObj) {
_calX = srcObj._calX;
_calY = srcObj._calY;
_calZ = srcObj._calZ;
}
//------------------------------------------------------------------------------
// Overloaded Operators
//------------------------------------------------------------------------------
// Assignment operator
const ITG3200& ITG3200::operator=(const ITG3200& rhsObj) {
// Self-assignment check
if (this == &rhsObj) {
return (*this);
}
// Free old memory
// Copy new memory
copyHelper(rhsObj);
return (*this);
}
//------------------------------------------------------------------------------
// Public Member Functions
//------------------------------------------------------------------------------
void ITG3200::initialize() {
// Initialize device
Wire.begin();
//Configure device
//Set the gyroscope scale for the outputs to +/-2000 degrees per second
writeByte(_DLPF, (_DLPF2|_DLPF1|_DLPF0));
//Set the sample rate to 100 hz
writeByte(_SMPLRTDIV, 4);
// Calibrate device
ITG3200::calibrate(20);
}
int ITG3200::x() {
byte xH = readByte(_xH);
byte xL = readByte(_xL);
return (int)(xH << 8 | xL) - _calX;
}
int ITG3200::y() {
byte yH = readByte(_yH);
byte yL = readByte(_yL);
return (int)(yH << 8 | yL) - _calY;
}
int ITG3200::z() {
byte zH = readByte(_zH);
byte zL = readByte(_zL);
return (int)(zH << 8 | zL) - _calZ;
}
void ITG3200::calibrate(int nSamples) {
long sumX = 0, sumY = 0, sumZ = 0;
int tempX, tempY, tempZ;
byte xH, xL, yH, yL, zH, zL;
for (int n=1; n<=nSamples; n++) {
delay(200);
xH = readByte(_xH);
xL = readByte(_xL);
tempX = (int)(xH << 8 | xL);
yH = readByte(_yH);
yL = readByte(_yL);
tempY = (int)(yH << 8 | yL);
zH = readByte(_zH);
zL = readByte(_zL);
tempZ = (int)(zH << 8 | zL);
sumX += (long)tempX;
sumY += (long)tempY;
sumZ += (long)tempZ;
}
_calX = (int)(sumX / nSamples);
_calY = (int)(sumY / nSamples);
_calZ = (int)(sumZ / nSamples);
}
//------------------------------------------------------------------------------
// Private Member Functions
//------------------------------------------------------------------------------
byte ITG3200::readByte(byte address) {
// Read 8 bits or 1 byte out EEPROM registers at the given address
Wire.beginTransmission(_ITG3200Address);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom((int)_ITG3200Address, 1);
while(!Wire.available());
return Wire.read();
}
void ITG3200::writeByte(byte address, byte value) {
// Write 8 bits or 1 byte into E2PROM registers at the given address
Wire.beginTransmission(_ITG3200Address);
Wire.write(address);
Wire.write(value);
Wire.endTransmission();
}