|
@@ -0,0 +1,226 @@
|
|
1
|
+/*
|
|
2
|
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
|
3
|
+ * Copyright (c) 2014 Intel Corporation.
|
|
4
|
+ *
|
|
5
|
+ * Based on InvenSense MPU-6050 register map document rev. 2.0, 5/19/2011 (RM-MPU-6000A-00)
|
|
6
|
+ * 8/24/2011 by Jeff Rowberg <jeff@rowberg.net>
|
|
7
|
+ *
|
|
8
|
+ * Permission is hereby granted, free of charge, to any person obtaining
|
|
9
|
+ * a copy of this software and associated documentation files (the
|
|
10
|
+ * "Software"), to deal in the Software without restriction, including
|
|
11
|
+ * without limitation the rights to use, copy, modify, merge, publish,
|
|
12
|
+ * distribute, sublicense, and/or sell copies of the Software, and to
|
|
13
|
+ * permit persons to whom the Software is furnished to do so, subject to
|
|
14
|
+ * the following conditions:
|
|
15
|
+ *
|
|
16
|
+ * The above copyright notice and this permission notice shall be
|
|
17
|
+ * included in all copies or substantial portions of the Software.
|
|
18
|
+ *
|
|
19
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
21
|
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
22
|
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
23
|
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
24
|
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
25
|
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
26
|
+ */
|
|
27
|
+
|
|
28
|
+#include <iostream>
|
|
29
|
+#include <unistd.h>
|
|
30
|
+#include <stdlib.h>
|
|
31
|
+
|
|
32
|
+#include "mpu9150.h"
|
|
33
|
+
|
|
34
|
+using namespace upm;
|
|
35
|
+
|
|
36
|
+MPU9150::MPU9150 (int bus, int devAddr) {
|
|
37
|
+ m_name = "MPU9150";
|
|
38
|
+
|
|
39
|
+ m_i2cAddr = devAddr;
|
|
40
|
+ m_bus = bus;
|
|
41
|
+
|
|
42
|
+ m_i2Ctx = maa_i2c_init(m_bus);
|
|
43
|
+
|
|
44
|
+ maa_result_t ret = maa_i2c_address(m_i2Ctx, m_i2cAddr);
|
|
45
|
+ if (ret != MAA_SUCCESS) {
|
|
46
|
+ fprintf(stderr, "Messed up i2c bus\n");
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ initSensor ();
|
|
50
|
+}
|
|
51
|
+
|
|
52
|
+MPU9150::~MPU9150() {
|
|
53
|
+ maa_i2c_stop(m_i2Ctx);
|
|
54
|
+}
|
|
55
|
+
|
|
56
|
+maa_result_t
|
|
57
|
+MPU9150::initSensor () {
|
|
58
|
+ uint8_t regData = 0x0;
|
|
59
|
+
|
|
60
|
+ // setClockSource
|
|
61
|
+ updateRegBits ( MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_CLKSEL_BIT,
|
|
62
|
+ MPU6050_PWR1_CLKSEL_LENGTH, MPU6050_CLOCK_PLL_XGYRO);
|
|
63
|
+ // setFullScaleGyroRange
|
|
64
|
+ updateRegBits ( MPU6050_RA_GYRO_CONFIG, MPU6050_GCONFIG_FS_SEL_BIT,
|
|
65
|
+ MPU6050_GCONFIG_FS_SEL_LENGTH, MPU6050_GYRO_FS_250);
|
|
66
|
+ // setFullScaleAccelRange
|
|
67
|
+ updateRegBits ( MPU6050_RA_ACCEL_CONFIG, MPU6050_ACONFIG_AFS_SEL_BIT,
|
|
68
|
+ MPU6050_ACONFIG_AFS_SEL_LENGTH, MPU6050_ACCEL_FS_2);
|
|
69
|
+ // setSleepEnabled
|
|
70
|
+ i2cReadReg_N (MPU6050_RA_PWR_MGMT_1, 0x1, ®Data);
|
|
71
|
+ regData &= ~(1 << MPU6050_PWR1_SLEEP_BIT);
|
|
72
|
+ i2cWriteReg (MPU6050_RA_PWR_MGMT_1, regData);
|
|
73
|
+
|
|
74
|
+ return MAA_SUCCESS;
|
|
75
|
+}
|
|
76
|
+
|
|
77
|
+uint8_t
|
|
78
|
+MPU9150::getDeviceID () {
|
|
79
|
+ uint8_t regData = 0x0;
|
|
80
|
+ getRegBits (MPU6050_RA_WHO_AM_I, MPU6050_WHO_AM_I_BIT, MPU6050_WHO_AM_I_LENGTH, ®Data);
|
|
81
|
+ return regData;
|
|
82
|
+}
|
|
83
|
+
|
|
84
|
+maa_result_t
|
|
85
|
+MPU9150::getData () {
|
|
86
|
+ uint8_t buffer[14];
|
|
87
|
+
|
|
88
|
+ for (int i = 0; i < SMOOTH_TIMES; i++) {
|
|
89
|
+ i2cReadReg_N (MPU6050_RA_ACCEL_XOUT_H, 14, buffer);
|
|
90
|
+ axisAcceleromter.rawData.axisX = (((int16_t)buffer[0]) << 8) | buffer[1];
|
|
91
|
+ axisAcceleromter.rawData.axisY = (((int16_t)buffer[2]) << 8) | buffer[3];
|
|
92
|
+ axisAcceleromter.rawData.axisZ = (((int16_t)buffer[4]) << 8) | buffer[5];
|
|
93
|
+ axisAcceleromter.sumData.axisX += (double) axisAcceleromter.rawData.axisX / 16384;
|
|
94
|
+ axisAcceleromter.sumData.axisY += (double) axisAcceleromter.rawData.axisY / 16384;
|
|
95
|
+ axisAcceleromter.sumData.axisZ += (double) axisAcceleromter.rawData.axisZ / 16384;
|
|
96
|
+
|
|
97
|
+ axisGyroscope.rawData.axisX = (((int16_t)buffer[8]) << 8) | buffer[9];
|
|
98
|
+ axisGyroscope.rawData.axisY = (((int16_t)buffer[10]) << 8) | buffer[11];
|
|
99
|
+ axisGyroscope.rawData.axisZ = (((int16_t)buffer[12]) << 8) | buffer[13];
|
|
100
|
+ axisGyroscope.sumData.axisX += (double) axisAcceleromter.rawData.axisX * 250 / 32768;
|
|
101
|
+ axisGyroscope.sumData.axisY += (double) axisAcceleromter.rawData.axisY * 250 / 32768;
|
|
102
|
+ axisGyroscope.sumData.axisZ += (double) axisAcceleromter.rawData.axisZ * 250 / 32768;
|
|
103
|
+
|
|
104
|
+ i2cWriteReg (MPU6050_RA_INT_PIN_CFG, 0x02);
|
|
105
|
+ usleep (10000);
|
|
106
|
+ m_i2cAddr = MPU9150_RA_MAG_ADDRESS;
|
|
107
|
+ i2cWriteReg (0x0A, 0x01);
|
|
108
|
+ usleep (10000);
|
|
109
|
+ i2cReadReg_N (MPU9150_RA_MAG_XOUT_L, 6, buffer);
|
|
110
|
+ m_i2cAddr = ADDR;
|
|
111
|
+
|
|
112
|
+ axisMagnetomer.rawData.axisX = (((int16_t)buffer[0]) << 8) | buffer[1];
|
|
113
|
+ axisMagnetomer.rawData.axisY = (((int16_t)buffer[2]) << 8) | buffer[3];
|
|
114
|
+ axisMagnetomer.rawData.axisZ = (((int16_t)buffer[4]) << 8) | buffer[5];
|
|
115
|
+ axisMagnetomer.sumData.axisX += (double) axisMagnetomer.rawData.axisX * 1200 / 4096;
|
|
116
|
+ axisMagnetomer.sumData.axisY += (double) axisMagnetomer.rawData.axisY * 1200 / 4096;
|
|
117
|
+ axisMagnetomer.sumData.axisZ += (double) axisMagnetomer.rawData.axisZ * 1200 / 4096;
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ axisAcceleromter.data.axisX = axisAcceleromter.sumData.axisX / SMOOTH_TIMES;
|
|
121
|
+ axisAcceleromter.data.axisY = axisAcceleromter.sumData.axisY / SMOOTH_TIMES;
|
|
122
|
+ axisAcceleromter.data.axisZ = axisAcceleromter.sumData.axisZ / SMOOTH_TIMES;
|
|
123
|
+
|
|
124
|
+ axisGyroscope.data.axisX = axisGyroscope.sumData.axisX / SMOOTH_TIMES;
|
|
125
|
+ axisGyroscope.data.axisY = axisGyroscope.sumData.axisY / SMOOTH_TIMES;
|
|
126
|
+ axisGyroscope.data.axisZ = axisGyroscope.sumData.axisZ / SMOOTH_TIMES;
|
|
127
|
+
|
|
128
|
+ axisMagnetomer.data.axisX = axisMagnetomer.sumData.axisX / SMOOTH_TIMES;
|
|
129
|
+ axisMagnetomer.data.axisY = axisMagnetomer.sumData.axisY / SMOOTH_TIMES;
|
|
130
|
+ axisMagnetomer.data.axisZ = axisMagnetomer.sumData.axisZ / SMOOTH_TIMES;
|
|
131
|
+}
|
|
132
|
+
|
|
133
|
+maa_result_t
|
|
134
|
+MPU9150::getAcceleromter (Vector3D * data) {
|
|
135
|
+ data->axisX = axisAcceleromter.data.axisX;
|
|
136
|
+ data->axisY = axisAcceleromter.data.axisY;
|
|
137
|
+ data->axisZ = axisAcceleromter.data.axisZ;
|
|
138
|
+
|
|
139
|
+ return MAA_SUCCESS;
|
|
140
|
+}
|
|
141
|
+
|
|
142
|
+maa_result_t
|
|
143
|
+MPU9150::getGyro (Vector3D * data) {
|
|
144
|
+ data->axisX = axisGyroscope.data.axisX;
|
|
145
|
+ data->axisY = axisGyroscope.data.axisY;
|
|
146
|
+ data->axisZ = axisGyroscope.data.axisZ;
|
|
147
|
+
|
|
148
|
+ return MAA_SUCCESS;
|
|
149
|
+}
|
|
150
|
+
|
|
151
|
+maa_result_t
|
|
152
|
+MPU9150::getMagnometer (Vector3D * data) {
|
|
153
|
+ data->axisX = axisMagnetomer.data.axisX;
|
|
154
|
+ data->axisY = axisMagnetomer.data.axisY;
|
|
155
|
+ data->axisZ = axisMagnetomer.data.axisZ;
|
|
156
|
+
|
|
157
|
+ return MAA_SUCCESS;
|
|
158
|
+}
|
|
159
|
+
|
|
160
|
+float
|
|
161
|
+MPU9150::getTemperature () {
|
|
162
|
+ uint8_t buffer[2];
|
|
163
|
+ uint16_t tempRaw = 0;
|
|
164
|
+
|
|
165
|
+ updateRegBits (MPU6050_RA_PWR_MGMT_1, MPU6050_PWR1_TEMP_DIS_BIT, 0x1, 0x0);
|
|
166
|
+ i2cReadReg_N (MPU6050_RA_TEMP_OUT_H, 2, buffer);
|
|
167
|
+ tempRaw = (((int16_t)buffer[0]) << 8) | buffer[1];
|
|
168
|
+
|
|
169
|
+ return (float)tempRaw / 340.0 + 35.0;
|
|
170
|
+}
|
|
171
|
+
|
|
172
|
+/*
|
|
173
|
+ * **************
|
|
174
|
+ * private area
|
|
175
|
+ * **************
|
|
176
|
+ */
|
|
177
|
+uint16_t
|
|
178
|
+MPU9150::i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer) {
|
|
179
|
+ int readByte = 0;
|
|
180
|
+ maa_i2c_address(m_i2Ctx, m_i2cAddr);
|
|
181
|
+ maa_i2c_write_byte(m_i2Ctx, reg);
|
|
182
|
+
|
|
183
|
+ maa_i2c_address(m_i2Ctx, m_i2cAddr);
|
|
184
|
+ readByte = maa_i2c_read(m_i2Ctx, buffer, len);
|
|
185
|
+ return readByte;
|
|
186
|
+}
|
|
187
|
+
|
|
188
|
+maa_result_t
|
|
189
|
+MPU9150::i2cWriteReg (uint8_t reg, uint8_t value) {
|
|
190
|
+ maa_result_t error = MAA_SUCCESS;
|
|
191
|
+
|
|
192
|
+ uint8_t data[2] = { reg, value };
|
|
193
|
+ error = maa_i2c_address (m_i2Ctx, m_i2cAddr);
|
|
194
|
+ error = maa_i2c_write (m_i2Ctx, data, 2);
|
|
195
|
+
|
|
196
|
+ return error;
|
|
197
|
+}
|
|
198
|
+
|
|
199
|
+int
|
|
200
|
+MPU9150::updateRegBits (uint8_t reg, uint8_t bitStart, uint8_t length, uint16_t data) {
|
|
201
|
+ uint8_t regData;
|
|
202
|
+
|
|
203
|
+ if (i2cReadReg_N (reg, 0x1, ®Data) != 0) {
|
|
204
|
+ uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
|
205
|
+ data <<= (bitStart - length + 1); // shift data into correct position
|
|
206
|
+ data &= mask; // zero all non-important bits in data
|
|
207
|
+ regData &= ~(mask); // zero all important bits in existing byte
|
|
208
|
+ regData |= data; // combine data with existing byte
|
|
209
|
+ return i2cWriteReg (reg, regData);
|
|
210
|
+ } else {
|
|
211
|
+ return 0x0;
|
|
212
|
+ }
|
|
213
|
+}
|
|
214
|
+
|
|
215
|
+uint8_t
|
|
216
|
+MPU9150::getRegBits (uint8_t reg, uint8_t bitStart, uint8_t length, uint8_t * data) {
|
|
217
|
+ uint8_t count = 0;
|
|
218
|
+ uint8_t regData;
|
|
219
|
+ if (i2cReadReg_N (reg, 0x1, ®Data) != 0) {
|
|
220
|
+ uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
|
221
|
+ regData &= mask;
|
|
222
|
+ regData >>= (bitStart - length + 1);
|
|
223
|
+ *data = regData;
|
|
224
|
+ }
|
|
225
|
+ return count;
|
|
226
|
+}
|