9f5c77fbace60e08a412a4766ea10910be245eb5
[contrib/upm.git] / src / mpu9150 / mpu9150.h
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 #pragma once
28
29 #include <string>
30 #include <maa/i2c.h>
31
32 #define MPU6050_ADDRESS_AD0_LOW             0x68 // address pin low (GND), default for InvenSense evaluation board
33 #define MPU6050_ADDRESS_AD0_HIGH            0x69 // address pin high (VCC)
34 #define ADDR                                MPU6050_ADDRESS_AD0_LOW // device address
35
36 // registers address
37 #define MPU6050_CLOCK_PLL_XGYRO             0x01
38 #define MPU6050_GYRO_FS_250                 0x00
39 #define MPU6050_ACCEL_FS_2                  0x00
40 #define MPU6050_RA_INT_PIN_CFG              0x37
41
42 #define MPU6050_RA_ACCEL_XOUT_H             0x3B
43 #define MPU6050_RA_ACCEL_XOUT_L             0x3C
44 #define MPU6050_RA_ACCEL_YOUT_H             0x3D
45 #define MPU6050_RA_ACCEL_YOUT_L             0x3E
46 #define MPU6050_RA_ACCEL_ZOUT_H             0x3F
47 #define MPU6050_RA_ACCEL_ZOUT_L             0x40
48 #define MPU6050_RA_TEMP_OUT_H               0x41
49 #define MPU6050_RA_TEMP_OUT_L               0x42
50 #define MPU6050_RA_GYRO_XOUT_H              0x43
51 #define MPU6050_RA_GYRO_XOUT_L              0x44
52 #define MPU6050_RA_GYRO_YOUT_H              0x45
53 #define MPU6050_RA_GYRO_YOUT_L              0x46
54 #define MPU6050_RA_GYRO_ZOUT_H              0x47
55 #define MPU6050_RA_GYRO_ZOUT_L              0x48
56
57 #define MPU6050_RA_CONFIG                   0x1A
58 #define MPU6050_CFG_DLPF_CFG_BIT            2
59 #define MPU6050_CFG_DLPF_CFG_LENGTH         3
60
61 #define MPU6050_RA_GYRO_CONFIG              0x1B
62 #define MPU6050_GCONFIG_FS_SEL_BIT          4
63 #define MPU6050_GCONFIG_FS_SEL_LENGTH       2
64
65 #define MPU6050_RA_ACCEL_CONFIG             0x1C
66 #define MPU6050_ACONFIG_AFS_SEL_BIT         4
67 #define MPU6050_ACONFIG_AFS_SEL_LENGTH      2
68
69 // magnotometer
70 #define MPU9150_RA_MAG_ADDRESS              0x0C
71 #define MPU9150_RA_MAG_XOUT_L               0x03
72
73 #define MPU6050_RA_PWR_MGMT_1               0x6B
74 #define MPU6050_PWR1_CLKSEL_BIT             2
75 #define MPU6050_PWR1_CLKSEL_LENGTH          3
76 #define MPU6050_PWR1_SLEEP_BIT              6
77
78 #define MPU6050_RA_INT_PIN_CFG              0x37
79
80 // temperature
81 #define MPU6050_PWR1_TEMP_DIS_BIT           3
82 #define MPU6050_RA_WHO_AM_I                 0x75
83 #define MPU6050_WHO_AM_I_BIT                6
84 #define MPU6050_WHO_AM_I_LENGTH             6
85
86 #define SMOOTH_TIMES                        10.0
87
88 #define HIGH               1
89 #define LOW                0
90
91 namespace upm {
92
93 struct Vector3DRaw {
94     uint16_t axisX;
95     uint16_t axisY;
96     uint16_t axisZ;
97 };
98
99 struct Vector3D {
100     double axisX;
101     double axisY;
102     double axisZ;
103 };
104
105 struct AxisData {
106     Vector3DRaw rawData;
107     Vector3D    sumData;
108     Vector3D    data;
109 };
110
111 /**
112  * @brief C++ API for MPU9150 chip (Accelrometer, Gyro and Magnometer Sensor)
113  *
114  * This file defines the MPU9150 C++ interface for libmpu9150
115  *
116  * @snippet mpu9150-example.cxx Interesting
117  */
118 class MPU9150 {
119     public:
120         /**
121          * Instanciates a MPU9150 object
122          *
123          * @param bus number of used bus
124          * @param devAddr addres of used i2c device
125          */
126         MPU9150 (int bus, int devAddr);
127
128         /**
129          * MPU9150 object destructor, basicaly it close i2c connection.
130          */
131         ~MPU9150 ();
132
133         /**
134          * Initiate MPU9150 chips
135          */
136         maa_result_t initSensor ();
137
138         /**
139          * Get identity of the device
140          */
141         uint8_t getDeviceID ();
142
143         /**
144          * Get the Accelerometer, Gyro and Compass data from the chip and
145          * save it in private section.
146          */
147         maa_result_t getData ();
148
149         /**
150          * @param data structure with 3 axis (x,y,z)
151          */
152         maa_result_t getAcceleromter (Vector3D * data);
153
154         /**
155          * @param data structure with 3 axis (x,y,z)
156          */
157         maa_result_t getGyro (Vector3D * data);
158
159         /**
160          * @param data structure with 3 axis (x,y,z)
161          */
162         maa_result_t getMagnometer (Vector3D * data);
163
164         /**
165          * Read on die temperature from the chip
166          */
167         float getTemperature ();
168
169         /**
170          * Return name of the component
171          */
172         std::string name()
173         {
174             return m_name;
175         }
176
177     private:
178         std::string m_name;
179
180         int m_i2cAddr;
181         int m_bus;
182         maa_i2c_context m_i2Ctx;
183
184         AxisData axisMagnetomer;
185         AxisData axisAcceleromter;
186         AxisData axisGyroscope;
187
188         uint16_t i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer);
189         maa_result_t i2cWriteReg (uint8_t reg, uint8_t value);
190         int updateRegBits (uint8_t reg, uint8_t bitStart,
191                                     uint8_t length, uint16_t data);
192         uint8_t getRegBits (uint8_t reg, uint8_t bitStart,
193                                     uint8_t length, uint8_t * data);
194 };
195
196 }