upm  0.1.8
Sensor/Actuator repository for libmraa (v0.4.5)
 All Data Structures Files Functions Variables Macros Pages
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 <mraa/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 
118 class MPU9150 {
119  public:
126  MPU9150 (int bus, int devAddr);
127 
131  ~MPU9150 ();
132 
136  mraa_result_t initSensor ();
137 
141  uint8_t getDeviceID ();
142 
147  mraa_result_t getData ();
148 
152  mraa_result_t getAcceleromter (Vector3D * data);
153 
157  mraa_result_t getGyro (Vector3D * data);
158 
162  mraa_result_t getMagnometer (Vector3D * data);
163 
167  float getTemperature ();
168 
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  mraa_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  mraa_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 }
Definition: mpu9150.h:99
mraa_result_t initSensor()
Definition: mpu9150.cxx:57
~MPU9150()
Definition: mpu9150.cxx:52
uint8_t getDeviceID()
Definition: mpu9150.cxx:78
MPU9150(int bus, int devAddr)
Definition: mpu9150.cxx:36
Definition: mpu9150.h:105
mraa_result_t getData()
Definition: mpu9150.cxx:85
std::string name()
Definition: mpu9150.h:172
mraa_result_t getAcceleromter(Vector3D *data)
Definition: mpu9150.cxx:134
mraa_result_t getMagnometer(Vector3D *data)
Definition: mpu9150.cxx:152
Definition: mpu9150.h:93
mraa_result_t getGyro(Vector3D *data)
Definition: mpu9150.cxx:143
float getTemperature()
Definition: mpu9150.cxx:161
C++ API for MPU9150 chip (Accelrometer, Gyro and Magnometer Sensor)
Definition: mpu9150.h:118