FreeIMU Library
Open Hardware Framework for Motion Sensing
 All Data Structures Files Functions Variables Macros
FreeIMU.h
Go to the documentation of this file.
1 /*
2 FreeIMU.h - A libre and easy to use orientation sensing library for Arduino
3 Copyright (C) 2011 Fabio Varesano <fabio at varesano dot net>
4 
5 Development of this code has been supported by the Department of Computer Science,
6 Universita' degli Studi di Torino, Italy within the Piemonte Project
7 http://www.piemonte.di.unito.it/
8 
9 
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the version 3 GNU General Public License as
12 published by the Free Software Foundation.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifndef FreeIMU_h
25 #define FreeIMU_h
26 
27 // Uncomment the appropriated version of FreeIMU you are using
28 //#define FREEIMU_v01
29 //#define FREEIMU_v02
30 //#define FREEIMU_v03
31 //#define FREEIMU_v035
32 //#define FREEIMU_v035_MS
33 //#define FREEIMU_v035_BMP
34 #define FREEIMU_v04
35 
36 // 3rd party boards. Please consider donating or buying a FreeIMU board to support this library development.
37 //#define SEN_10121 //IMU Digital Combo Board - 6 Degrees of Freedom ITG3200/ADXL345 SEN-10121 http://www.sparkfun.com/products/10121
38 //#define SEN_10736 //9 Degrees of Freedom - Razor IMU SEN-10736 http://www.sparkfun.com/products/10736
39 //#define SEN_10724 //9 Degrees of Freedom - Sensor Stick SEN-10724 http://www.sparkfun.com/products/10724
40 //#define SEN_10183 //9 Degrees of Freedom - Sensor Stick SEN-10183 http://www.sparkfun.com/products/10183
41 
42 // *** No configuration needed below this line ***
43 
44 
45 #define HAS_ITG3200() (defined(FREEIMU_v01) || defined(FREEIMU_v02) || defined(FREEIMU_v03) || defined(FREEIMU_v035) || defined(FREEIMU_v035_MS) || defined(FREEIMU_v035_BMP) || defined(SEN_10121) || defined(SEN_10736) || defined(SEN_10724) || defined(SEN_10183))
46 #define HAS_ADXL345() (defined(FREEIMU_v01) || defined(FREEIMU_v02) || defined(FREEIMU_v03) || defined(SEN_10121) || defined(SEN_10736) || defined(SEN_10724) || defined(SEN_10183))
47 #define HAS_BMA180() (defined(FREEIMU_v035) || defined(FREEIMU_v035_MS) || defined(FREEIMU_v035_BMP))
48 #define HAS_MPU6050() (defined(FREEIMU_v04))
49 #define HAS_MS5611() (defined(FREEIMU_v035_MS) || defined(FREEIMU_v04))
50 #define HAS_HMC5883L() (defined(FREEIMU_v01) || defined(FREEIMU_v02) || defined(FREEIMU_v03) || defined(FREEIMU_v035) || defined(FREEIMU_v035_MS) || defined(FREEIMU_v035_BMP) || defined(FREEIMU_v04) || defined(SEN_10736) || defined(SEN_10724) || defined(SEN_10183))
51 
52 #define IS_6DOM() (defined(SEN_10121))
53 #define IS_9DOM() (defined(FREEIMU_v01) || defined(FREEIMU_v02) || defined(FREEIMU_v03) || defined(FREEIMU_v035) || defined(FREEIMU_v035_MS) || defined(FREEIMU_v035_BMP) || defined(FREEIMU_v04) || defined(SEN_10736) || defined(SEN_10724) || defined(SEN_10183))
54 #define HAS_AXIS_ALIGNED() (defined(FREEIMU_v01) || defined(FREEIMU_v02) || defined(FREEIMU_v03) || defined(FREEIMU_v035) || defined(FREEIMU_v035_MS) || defined(FREEIMU_v035_BMP) || defined(FREEIMU_v04) || defined(SEN_10121) || defined(SEN_10736))
55 
56 #include <Wire.h>
57 #include "Arduino.h"
58 
59 //#if FREEIMU_VER <= 3
60 #if HAS_ADXL345()
61  #include <ADXL345.h>
62  // default I2C 7-bit addresses of the sensors
63  #define FIMU_ACC_ADDR ADXL345_ADDR_ALT_LOW // SDO connected to GND
64  //#define FIMU_ADXL345_DEF_ADDR ADXL345_ADDR_ALT_HIGH // SDO connected to GND
65 #elif HAS_BMA180()
66  #include <bma180.h>
67  #define FIMU_ACC_ADDR BMA180_ADDRESS_SDO_LOW
68  //#define FIMU_ACC_ADDR BMA180_ADDRESS_SDO_HIGH
69 //#elif FREEIMU_VER == 7
70 #elif HAS_MPU6050()
71  #include "I2Cdev.h"
72  #include "MPU6050.h"
73  #define FIMU_ACCGYRO_ADDR MPU6050_ADDRESS_AD0_LOW
74  //#define FIMU_ACCGYRO_ADDR MPU6050_ADDRESS_AD0_HIGH
75 #endif
76 
77 #if HAS_MS5611()
78  #include <MS561101BA.h>
79  #define FIMU_BARO_ADDR MS561101BA_ADDR_CSB_LOW
80  //#define FIMU_ACC_ADDR MS561101BA_ADDR_CSB_HIGH
81 #endif
82 
83 #if HAS_ITG3200()
84  #include <ITG3200.h>
85 #endif
86 
87 #if HAS_HMC5883L()
88  #include <HMC58X3.h>
89 #endif
90 
91 
92 #define FIMU_BMA180_DEF_ADDR BMA180_ADDRESS_SDO_LOW
93 #define FIMU_ITG3200_DEF_ADDR ITG3200_ADDR_AD0_LOW // AD0 connected to GND
94 // HMC5843 address is fixed so don't bother to define it
95 
96 
97 #define twoKpDef (2.0f * 0.5f) // 2 * proportional gain
98 #define twoKiDef (2.0f * 0.1f) // 2 * integral gain
99 
100 #ifndef cbi
101 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
102 #endif
103 
104 class FreeIMU
105 {
106  public:
107  FreeIMU();
108  void init();
109  void init(bool fastmode);
110  #if HAS_ITG3200()
111  void init(int acc_addr, int gyro_addr, bool fastmode);
112  #else
113  void init(int accgyro_addr, bool fastmode);
114  #endif
115  void getRawValues(int * raw_values);
116  void getValues(float * values);
117  void getQ(float * q);
118  void getEuler(float * angles);
119  void getYawPitchRoll(float * ypr);
120  void getEulerRad(float * angles);
121  void getYawPitchRollRad(float * ypr);
122 
123  // we make them public so that users can interact directly with device classes
124  #if HAS_ADXL345()
125  ADXL345 acc;
126  #elif HAS_BMA180()
127  BMA180 acc;
128  #endif
129 
130  #if HAS_HMC5883L()
131  HMC58X3 magn;
132  #endif
133 
134 
135  #if HAS_ITG3200()
136  ITG3200 gyro;
137  #elif HAS_MPU6050()
138  MPU6050 accgyro;
139  #endif
140 
141  #if HAS_MS5611()
142  MS561101BA baro;
143  #endif
144 
145 
147 
148  private:
149  #if IS_9DOM()
150  void AHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz);
151  #elif IS_6DOM()
152  void AHRSupdate(float gx, float gy, float gz, float ax, float ay, float az);
153  #endif
154  //float q0, q1, q2, q3; // quaternion elements representing the estimated orientation
155  float iq0, iq1, iq2, iq3;
156  float exInt, eyInt, ezInt; // scaled integral error
157  volatile float twoKp; // 2 * proportional gain (Kp)
158  volatile float twoKi; // 2 * integral gain (Ki)
159  volatile float q0, q1, q2, q3; // quaternion of sensor frame relative to auxiliary frame
160  volatile float integralFBx, integralFBy, integralFBz;
161  unsigned long lastUpdate, now; // sample period expressed in milliseconds
162  float sampleFreq; // half the sample period expressed in seconds
163  int startLoopTime;
164 };
165 
166 float invSqrt(float number);
167 void arr3_rad_to_deg(float * arr);
168 
169 #endif // FreeIMU_h
170