-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGY521.cpp
600 lines (498 loc) · 12.7 KB
/
GY521.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//
// FILE: GY521.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.6.1
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521
#include "GY521.h"
// keep register names in sync with BIG MPU6050 lib
#include "GY521_registers.h"
// COMMANDS
#define GY521_WAKEUP 0x00
/////////////////////////////////////////////////////
//
// PUBLIC
//
GY521::GY521(uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
// initialize errors
// don't do it in reset, as users might want to keep them
axe = aye = aze = 0;
reset();
}
bool GY521::begin()
{
if (isConnected())
{
return wakeup();
}
reset();
return false;
}
bool GY521::isConnected()
{
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
}
uint8_t GY521::getAddress()
{
return _address;
}
void GY521::reset()
{
setThrottleTime(GY521_THROTTLE_TIME);
_ax = _ay = _az = 0;
_aax = _aay = _aaz = 0;
_gx = _gy = _gz = 0;
_gax = _gay = _gaz = 0;
_pitch = 0;
_roll = 0;
_yaw = 0;
}
bool GY521::calibrate(uint16_t times, float angleX, float angleY, bool inverted)
{
// disable throttling / caching of read values.
bool oldThrottle = _throttle;
_throttle = false;
// set all errors to zero, to get the raw reads.
axe = aye = aze = 0;
gxe = gye = gze = 0;
// use local error sums, to calculate the average error.
float _axe = 0, _aye = 0, _aze = 0;
float _gxe = 0, _gye = 0, _gze = 0;
// adjust times if zero.
if (times == 0) times = 1;
// sum (6x) the measurements.
uint16_t samples = 0;
for (uint16_t i = 0; i < times; i++)
{
if (_readRaw() == GY521_OK)
{
_axe -= getAccelX();
_aye -= getAccelY();
_aze -= getAccelZ();
_gxe -= getGyroX();
_gye -= getGyroY();
_gze -= getGyroZ();
samples++;
}
}
if (samples == 0) return false;
// scale gyro calibration errors so read() should get all zero's on average.
float factor = _raw2dps / samples;
gxe = _gxe * factor;
gye = _gye * factor;
gze = _gze * factor;
// scale accelerometer calibration errors so read() should get all zero's on average.
factor = _raw2g / samples;
axe = _axe * factor;
aye = _aye * factor;
aze = _aze * factor;
// remove expected gravity from error
angleX *= GY521_DEGREES2RAD;
angleY *= GY521_DEGREES2RAD;
float _gravx = -sin(angleY) * cos(angleX);
float _gravy = sin(angleX);
float _gravz = cos(angleY) * cos(angleX);
axe -= _gravx;
aye -= _gravy;
aze += inverted ? -_gravz : _gravz;
// restore throttle state.
_throttle = oldThrottle;
return true;
}
bool GY521::wakeup()
{
_wire->beginTransmission(_address);
_wire->write(GY521_PWR_MGMT_1);
_wire->write(GY521_WAKEUP);
return (_wire->endTransmission() == 0);
}
int16_t GY521::read()
{
uint32_t now = millis();
if (_throttle)
{
if ((now - _lastTime) < _throttleTime)
{
// not an error.
return GY521_THROTTLED;
}
}
_lastTime = now;
int16_t rv = _readRaw();
if (rv != GY521_OK)
{
return rv;
}
// duration interval
now = micros();
float duration = (now - _lastMicros) * 1e-6; // duration in seconds.
_lastMicros = now;
// next lines might be merged per axis. (performance)
// Convert raw acceleration to g's
_ax *= _raw2g;
_ay *= _raw2g;
_az *= _raw2g;
// Error correct raw acceleration (in g) measurements // #18 kudos to Merkxic
_ax += axe;
_ay += aye;
_az += aze;
// prepare for Pitch Roll Yaw
float _ax2 = _ax * _ax;
float _ay2 = _ay * _ay;
float _az2 = _az * _az;
// calculate angle
_aax = atan( _ay / sqrt(_ax2 + _az2)) * GY521_RAD2DEGREES;
_aay = atan(-1.0 * _ax / sqrt(_ay2 + _az2)) * GY521_RAD2DEGREES;
_aaz = atan( _az / sqrt(_ax2 + _ay2)) * GY521_RAD2DEGREES;
// optimize #22
// _aax = atan(_ay / hypot(_ax, _az)) * GY521_RAD2DEGREES;
// _aay = atan(-1.0 * _ax / hypot(_ay, _az)) * GY521_RAD2DEGREES;
// _aaz = atan(_az / hypot(_ax, _ay)) * GY521_RAD2DEGREES;
// Convert to Celsius
_temperature = _temperature * 0.00294117647 + 36.53; // == /340.0 + 36.53;
// Convert raw Gyro to degrees/seconds
_gx *= _raw2dps;
_gy *= _raw2dps;
_gz *= _raw2dps;
// Error correct raw gyro measurements.
_gx += gxe;
_gy += gye;
_gz += gze;
// integrate gyroscope data
_gax += _gx * duration;
_gay += _gy * duration;
_gaz += _gz * duration;
// experimental below this line.
// Pitch Roll Yaw are work in progress
// normalize
// _gax etc might loose precision after many iterations #36
if (_normalize)
{
// correction at 375 due to the factor 0.96 in roll
if (_gax >= 375) _gax -= 375;
else if (_gax < 0) _gax += 375;
// correction at 375 due to the factor 0.96 in pitch
if (_gay >= 375) _gay -= 375;
else if (_gay < 0) _gay += 375;
// correction at 360
if (_gaz >= 360) _gaz -= 360;
else if (_gaz < 0) _gaz += 360;
}
// Calculate Pitch Roll Yaw
_yaw = _gaz;
_roll = 0.96 * _gax + 0.04 * _aax;
_pitch = 0.96 * _gay + 0.04 * _aay;
if (_normalize)
{
if (_pitch >= 360) _pitch -= 360;
else if (_pitch < 0) _pitch += 360;
if (_roll >= 360) _roll -= 360;
else if (_roll < 0) _roll += 360;
if (_yaw >= 360) _yaw -= 360;
else if (_yaw < 0) _yaw += 360;
}
return GY521_OK;
}
int16_t GY521::readAccel()
{
uint32_t now = millis();
if (_throttle)
{
if ((now - _lastTime) < _throttleTime)
{
// not an error.
return GY521_THROTTLED;
}
}
_lastTime = now;
// Connected ?
_wire->beginTransmission(_address);
_wire->write(GY521_ACCEL_XOUT_H);
if (_wire->endTransmission() != 0)
{
_error = GY521_ERROR_WRITE;
return _error;
}
// Get the data
int8_t n = _wire->requestFrom(_address, (uint8_t)6);
if (n != 6)
{
_error = GY521_ERROR_READ;
return _error;
}
// ACCELEROMETER
_ax = _WireRead2(); // ACCEL_XOUT_H ACCEL_XOUT_L
_ay = _WireRead2(); // ACCEL_YOUT_H ACCEL_YOUT_L
_az = _WireRead2(); // ACCEL_ZOUT_H ACCEL_ZOUT_L
// next lines might be merged per axis.
// Convert raw acceleration to g's
_ax *= _raw2g;
_ay *= _raw2g;
_az *= _raw2g;
// Error correct raw acceleration (in g) measurements // #18 kudos to Merkxic
_ax += axe;
_ay += aye;
_az += aze;
// prepare for Pitch Roll Yaw
float _ax2 = _ax * _ax;
float _ay2 = _ay * _ay;
float _az2 = _az * _az;
// calculate angles.
_aax = atan( _ay / sqrt(_ax2 + _az2)) * GY521_RAD2DEGREES;
_aay = atan(-1.0 * _ax / sqrt(_ay2 + _az2)) * GY521_RAD2DEGREES;
_aaz = atan( _az / sqrt(_ax2 + _ay2)) * GY521_RAD2DEGREES;
// optimize #22
// _aax = atan(_ay / hypot(_ax, _az)) * GY521_RAD2DEGREES;
// _aay = atan(-1.0 * _ax / hypot(_ay, _az)) * GY521_RAD2DEGREES;
// _aaz = atan(_az / hypot(_ax, _ay)) * GY521_RAD2DEGREES;
return GY521_OK;
}
int16_t GY521::readGyro()
{
uint32_t now = millis();
if (_throttle)
{
if ((now - _lastTime) < _throttleTime)
{
// not an error.
return GY521_THROTTLED;
}
}
_lastTime = now;
// Connected ?
_wire->beginTransmission(_address);
_wire->write(GY521_GYRO_XOUT_H);
if (_wire->endTransmission() != 0)
{
_error = GY521_ERROR_WRITE;
return _error;
}
// Get the data
int8_t n = _wire->requestFrom(_address, (uint8_t)6);
if (n != 6)
{
_error = GY521_ERROR_READ;
return _error;
}
// GYROSCOPE
_gx = _WireRead2(); // GYRO_XOUT_H GYRO_XOUT_L
_gy = _WireRead2(); // GYRO_YOUT_H GYRO_YOUT_L
_gz = _WireRead2(); // GYRO_ZOUT_H GYRO_ZOUT_L
// duration interval
now = micros();
float duration = (now - _lastMicros) * 1e-6; // duration in seconds.
_lastMicros = now;
// next lines might be merged per axis.
// Convert raw Gyro to degrees/seconds
_gx *= _raw2dps;
_gy *= _raw2dps;
_gz *= _raw2dps;
// Error correct raw gyro measurements.
_gx += gxe;
_gy += gye;
_gz += gze;
_gax += _gx * duration;
_gay += _gy * duration;
_gaz += _gz * duration;
// experimental below this line.
// normalize
// _gax etc might loose precision after many iterations #36
if (_normalize)
{
// correction at 375 due to the factor 0.96 in roll
if (_gax >= 375) _gax -= 375;
else if (_gax < 0) _gax += 375;
// correction at 375 due to the factor 0.96 in pitch
if (_gay >= 375) _gay -= 375;
else if (_gay < 0) _gay += 375;
// correction at 360
if (_gaz >= 360) _gaz -= 360;
else if (_gaz < 0) _gaz += 360;
}
return GY521_OK;
}
int16_t GY521::readTemperature()
{
// DO NOT THROTTLE
_wire->beginTransmission(_address);
_wire->write(GY521_TEMP_OUT_H);
if (_wire->endTransmission() != 0)
{
_error = GY521_ERROR_WRITE;
return _error;
}
// Get the data
int8_t n = _wire->requestFrom(_address, (uint8_t)2);
if (n != 2)
{
_error = GY521_ERROR_READ;
return _error;
}
// TEMPERATURE
_temperature = _WireRead2(); // TEMP_OUT_H TEMP_OUT_L
return GY521_OK;
}
bool GY521::setAccelSensitivity(uint8_t as)
{
_afs = as;
if (_afs > 3) _afs = 3;
uint8_t val = getRegister(GY521_ACCEL_CONFIG);
if (_error != 0)
{
return false;
}
// no need to write same value
if (((val >> 3) & 3) != _afs)
{
val &= 0xE7;
val |= (_afs << 3);
if (setRegister(GY521_ACCEL_CONFIG, val) != GY521_OK)
{
return false;
}
}
// calculate conversion factor. // 4 possible values => lookup table?
_raw2g = (1 << _afs) * GY521_RAW2G;
return true;
}
uint8_t GY521::getAccelSensitivity()
{
uint8_t val = getRegister(GY521_ACCEL_CONFIG);
if (_error != GY521_OK)
{
return _error; // return and propagate error (best thing to do)
}
_afs = (val >> 3) & 3;
return _afs;
}
bool GY521::setGyroSensitivity(uint8_t gs)
{
_gfs = gs;
if (_gfs > 3) _gfs = 3;
uint8_t val = getRegister(GY521_GYRO_CONFIG);
if (_error != 0)
{
return false;
}
// no need to write same value
if (((val >> 3) & 3) != _gfs)
{
val &= 0xE7;
val |= (_gfs << 3);
if (setRegister(GY521_GYRO_CONFIG, val) != GY521_OK)
{
return false;
}
}
// calculate conversion factor..
// 4 possible values => lookup table?
_raw2dps = (1 << _gfs) * GY521_RAW2DPS;
return true;
}
uint8_t GY521::getGyroSensitivity()
{
uint8_t val = getRegister(GY521_GYRO_CONFIG);
if (_error != GY521_OK)
{
return _error; // return and propagate error (best thing to do)
}
_gfs = (val >> 3) & 3;
return _gfs;
}
// CONFIGURATION
// Digital Low Pass Filter datasheet P13-reg26
bool GY521::setDLPFMode(uint8_t mode)
{
if (mode > 6)
{
_error = GY521_ERROR_PARAMETER;
return false;
}
uint8_t value = getRegister(GY521_CONFIG);
value &= 0xF8;
value |= mode;
return (setRegister(GY521_CONFIG, value) == GY521_OK);
}
uint8_t GY521::getDLPFMode()
{
uint8_t val = getRegister(GY521_CONFIG);
return val & 0x07;
}
// GENERIC
uint8_t GY521::setRegister(uint8_t reg, uint8_t value)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value);
// no need to do anything if not connected.
if (_wire->endTransmission() != 0)
{
_error = GY521_ERROR_WRITE;
return _error;
}
return GY521_OK;
}
uint8_t GY521::getRegister(uint8_t reg)
{
_wire->beginTransmission(_address);
_wire->write(reg);
if (_wire->endTransmission() != 0)
{
_error = GY521_ERROR_WRITE;
return _error;
}
uint8_t n = _wire->requestFrom(_address, (uint8_t) 1);
if (n != 1)
{
_error = GY521_ERROR_READ;
return _error;
}
uint8_t val = _wire->read();
return val;
}
///////////////////////////////////////////////////////////////////
//
// PRIVATE
//
int16_t GY521::_readRaw()
{
// Connected ?
_wire->beginTransmission(_address);
_wire->write(GY521_ACCEL_XOUT_H);
if (_wire->endTransmission() != 0)
{
_error = GY521_ERROR_WRITE;
return _error;
}
// Get the data
int8_t n = _wire->requestFrom(_address, (uint8_t)14);
if (n != 14)
{
_error = GY521_ERROR_READ;
return _error;
}
// ACCELEROMETER
_ax = _WireRead2(); // ACCEL_XOUT_H ACCEL_XOUT_L
_ay = _WireRead2(); // ACCEL_YOUT_H ACCEL_YOUT_L
_az = _WireRead2(); // ACCEL_ZOUT_H ACCEL_ZOUT_L
// TEMPERATURE
_temperature = _WireRead2(); // TEMP_OUT_H TEMP_OUT_L
// GYROSCOPE
_gx = _WireRead2(); // GYRO_XOUT_H GYRO_XOUT_L
_gy = _WireRead2(); // GYRO_YOUT_H GYRO_YOUT_L
_gz = _WireRead2(); // GYRO_ZOUT_H GYRO_ZOUT_L
return GY521_OK;
}
// to read register of 2 bytes.
int16_t GY521::_WireRead2()
{
int16_t tmp = _wire->read();
tmp <<= 8;
tmp |= _wire->read();
return tmp;
}
// -- END OF FILE --