-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdmp.cpp
More file actions
177 lines (133 loc) · 4.48 KB
/
Copy pathdmp.cpp
File metadata and controls
177 lines (133 loc) · 4.48 KB
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
/*
---------------------------
DMP class
author : vincent jaunet
date : 10-01-2013
---------------------------
Description :
The DMP class is mainly a wrapper to the MPU6050
one from github/PiBits and Jeff Rowberg <jeff@rowberg.net>
It defines the main functions to :
-set up the I2C communication through I2Cdev
-Initialize the measurements and retrieve Offset values
-Get the attitude of the drone
Copyright (c) <2014> <Vincent Jaunet>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
//#define _DEBUG_DMP
#include "dmp.h"
#define wrap_180(x) (x < -180 ? x+360 : (x > 180 ? x - 360: x))
DMP::DMP()
{
dmpReady=true;
initialized = false;
for (int i=0;i<DIM;i++){
lastval[i]=10;
m_ypr_off[i]=0.0;
}
}
//---------------------------
// mpu setup
//---------------------------
uint8_t DMP::set_up() {
// join I2C bus (I2Cdev library doesn't do this automatically)
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
// initialize device
mpu.initialize();
#ifdef _DEBUG_DMP
Serial.print("Device ID: ");
Serial.println(mpu.getDeviceID());
#endif
// verify connection
if (!mpu.testConnection()){
return 3;
}
// load and configure the DMP
devStatus = mpu.dmpInitialize();
// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(59);
mpu.setYGyroOffset(-37);
mpu.setZGyroOffset(10);
mpu.setXAccelOffset(843);
mpu.setYAccelOffset(1577);
mpu.setZAccelOffset(4703);
// make sure it worked (returns 0 if so)
if (devStatus == 0) {
// turn on the DMP, now that it's ready
mpu.setDMPEnabled(true);
// enable Arduino interrupt detection
//Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
//attachInterrupt(0, dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();
// set our DMP Ready flag so the main loop() function knows it's okay to use it
dmpReady = true;
// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();
return 0;
} else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
// printf("DMP Initialization failed (code %d)\n", devStatus);
return devStatus;
}
}
void DMP::initialize(){
//todo
initialized = true;
}
uint8_t DMP::getAttitude()
{
if (!dmpReady) return -1;
// wait for FIFO count > 42 bits
do {
fifoCount = mpu.getFIFOCount();
}while (fifoCount<42);
if (fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
return -1;
// otherwise, check for DMP data ready interrupt
//(this should happen frequently)
} else {
//read packet from fifo
mpu.getFIFOBytes(fifoBuffer, packetSize);
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
for (int i=0;i<DIM;i++){
//offset removal
ypr[i]-=m_ypr_off[i];
//scaling for output in degrees
ypr[i]*=180/M_PI;
}
//unwrap yaw when it reaches 180
ypr[0] = wrap_180(ypr[0]);
//change sign of ROLL, MPU is attached upside down
ypr[2]*=-1.0;
mpu.dmpGetGyro(g, fifoBuffer);
//0=gyroX, 1=gyroY, 2=gyroZ
//swapped to match Yaw,Pitch,Roll
//Scaled from deg/s to get tr/s
for (int i=0;i<DIM;i++){
gyro[i] = (float)(g[DIM-i-1])/131.0/360.0;
}
return 0;
}
}