forked from blaisejarrett/Arduino-Lib.MPU3050
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPU3050lib.cpp
281 lines (205 loc) · 5.48 KB
/
MPU3050lib.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Code by Blaise Jarrett @ OSEPP http://blaisejarrett.com
// Released to the public domain! Enjoy!
#include "MPU3050lib.h"
#include <stdio.h>
#include <math.h>
#include <Wire.h>
#define WIRE Wire
#if (ARDUINO >= 100)
#include <Arduino.h> // capital A so it is error prone on case-sensitive filesystems
#else
#include <WProgram.h>
#endif
Gyroscope::Gyroscope() : i2cAddr_(0) {}
int8_t Gyroscope::begin(uint8_t i2cAddr)
{
uint8_t id;
i2cAddr_ = i2cAddr;
// Join the I2C bus as master
WIRE.begin();
// perform a simple check to see if we're talking to the right device
// read the who am I register
if (i2cReadBytes(MPU3050_REG_WHOAMI, &id, 1) != 0)
return MPU3050_ERROR_I2CREAD;
// compare the who am I register
if ((id & 0x7E) != 0x68)
return MPU3050_ERROR_WRONG_ID;
// set defaults
uint8_t ret = setDLPFReg(MPU3050_LOWPASS_188HZ, MPU3050_RANGE_PM250);
if (ret != 0)
return ret;
return 0;
}
int8_t Gyroscope::readRaw(int16_t * x, int16_t * y, int16_t * z)
{
if (i2cAddr_ == 0)
return MPU3050_ERROR_NOT_INITIALIZED;
uint8_t data[6];
if (i2cReadBytes(MPU3050_REG_DATAXYZ, data, 6) != 0)
return MPU3050_ERROR_I2CREAD;
conv2Byte2Signed16(data[1], data[0], x);
conv2Byte2Signed16(data[3], data[2], y);
conv2Byte2Signed16(data[5], data[4], z);
return 0;
}
int8_t Gyroscope::readDegPerSecond(double * x, double * y, double * z)
{
int16_t x_r, y_r, z_r;
int8_t ret = readRaw(&x_r, &y_r, &z_r);
// pass the error
if (ret != 0)
return ret;
// perform the float conversion
double divisor;
switch (range_)
{
case MPU3050_RANGE_PM250:
divisor = 131;
break;
case MPU3050_RANGE_PM500:
divisor = 65.5;
break;
case MPU3050_RANGE_PM1000:
divisor = 32.8;
break;
case MPU3050_RANGE_PM2000:
divisor = 16.4;
break;
default:
return MPU3050_ERROR_RANGEOUTOFRANGE;
}
*x = (double)x_r / divisor;
*y = (double)y_r / divisor;
*z = (double)z_r / divisor;
return 0;
}
int8_t Gyroscope::readRawTemp(int16_t * temp)
{
uint8_t data[2];
if (i2cReadBytes(MPU3050_REG_TEMP, data, 2) != 0)
return MPU3050_ERROR_I2CREAD;
conv2Byte2Signed16(data[1], data[0], temp);
return 0;
}
int8_t Gyroscope::readTempDegrees(double * temp)
{
int16_t temp_r;
int8_t ret = readRawTemp(&temp_r);
// pass the error
if (ret != 0)
return ret;
int16_t tmp;
// convert it to degrees
// subtract room temperature offset
tmp = temp_r + 13200;
// convert to degrees
*temp = (double)tmp / 280;
// add room offset
*temp = *temp + 35;
return 0;
}
int8_t Gyroscope::setLowPassFilterBw(uint8_t hz)
{
return setDLPFReg(hz, range_);
}
int8_t Gyroscope::setRange(uint8_t range)
{
return setDLPFReg(lowpass_, range);
}
int8_t Gyroscope::setDLPFReg(uint8_t lowpass, uint8_t range)
{
uint8_t byte, tmp;
if (lowpass > 6)
return MPU3050_ERROR_LOWPASSOUTOFRANGE;
if (range > 3)
return MPU3050_ERROR_RANGEOUTOFRANGE;
lowpass_ = lowpass;
range_ = range;
byte = lowpass_;
tmp = range_ << 3;
byte = byte | tmp;
byte = byte & 0x1F;
if (i2cWriteByte(MPU3050_REG_DLPF, byte) != 0)
return MPU3050_ERROR_I2CWRITE;
// make a reading to apply
int16_t x, y, z;
readRaw(&x, &y, &z);
return 0;
}
int8_t Gyroscope::setOffsets(int16_t x, int16_t y, int16_t z)
{
int8_t ret;
ret = writei2c2s(MPU3050_REG_XOFFH, MPU3050_REG_XOFFL, x);
if (ret != 0)
return ret;
ret = writei2c2s(MPU3050_REG_YOFFH, MPU3050_REG_YOFFL, y);
if (ret != 0)
return ret;
ret = writei2c2s(MPU3050_REG_ZOFFH, MPU3050_REG_ZOFFL, z);
if (ret != 0)
return ret;
return 0;
}
int8_t Gyroscope::writei2c2s(uint8_t regmsb, uint8_t reglsb, int16_t data)
{
uint8_t msb_data, lsb_data;
uint16_t tmp;
tmp = (uint16_t)data;
tmp &= 0x00FF;
lsb_data = tmp;
tmp = (uint16_t)data;
tmp = tmp >> 8;
tmp &= 0x00FF;
msb_data = tmp;
int8_t ret;
ret = i2cWriteByte(regmsb, msb_data);
if (ret != 0)
return ret;
ret = i2cWriteByte(reglsb, lsb_data);
if (ret != 0)
return ret;
return 0;
}
void Gyroscope::conv2Byte2Signed16(uint8_t LSB, uint8_t MSB, int16_t * dest)
{
*dest = 0;
*dest = (int16_t)LSB;
*dest |= ((int16_t)MSB << 8);
}
int8_t Gyroscope::i2cReadBytes(uint8_t reg, uint8_t *data, uint8_t len)
{
if (i2cAddr_ == 0)
return MPU3050_ERROR_NOT_INITIALIZED;
WIRE.beginTransmission(i2cAddr_);
// request a read from this address
WIRE.write(reg);
// end to allow the response
WIRE.endTransmission();
// Read a byte from the device
WIRE.requestFrom(i2cAddr_, len);
uint8_t i = 0;
while (WIRE.available())
{
data[i] = WIRE.read();
++i;
if (i == len)
break;
}
if (i != len)
return -1;
return 0;
}
int8_t Gyroscope::i2cWriteByte(uint8_t reg, uint8_t data)
{
if (i2cAddr_ == 0)
return MPU3050_ERROR_NOT_INITIALIZED;
// Begin the write sequence
WIRE.beginTransmission(i2cAddr_);
// First byte is to set the register pointer
WIRE.write(reg);
// Write the data byte
WIRE.write(data);
// End the write sequence; bytes are actually transmitted now
WIRE.endTransmission();
return 0;
}