|  | 
|  | 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 | +#define I2C_SDA p28 | 
|  | 28 | +#define I2C_SCL p27 | 
|  | 29 | + | 
|  | 30 | +// Uncomment the appropriated version of FreeIMU you are using | 
|  | 31 | +//#define FREEIMU_v01 | 
|  | 32 | +//#define FREEIMU_v02 | 
|  | 33 | +//#define FREEIMU_v03 | 
|  | 34 | +//#define FREEIMU_v035 | 
|  | 35 | +//#define FREEIMU_v035_MS | 
|  | 36 | +//#define FREEIMU_v035_BMP | 
|  | 37 | +#define FREEIMU_v04 | 
|  | 38 | + | 
|  | 39 | +// 3rd party boards. Please consider donating or buying a FreeIMU board to support this library development. | 
|  | 40 | +//#define SEN_10121 //IMU Digital Combo Board - 6 Degrees of Freedom ITG3200/ADXL345 SEN-10121 http://www.sparkfun.com/products/10121 | 
|  | 41 | +//#define SEN_10736 //9 Degrees of Freedom - Razor IMU SEN-10736 http://www.sparkfun.com/products/10736 | 
|  | 42 | +//#define SEN_10724 //9 Degrees of Freedom - Sensor Stick SEN-10724 http://www.sparkfun.com/products/10724 | 
|  | 43 | +//#define SEN_10183 //9 Degrees of Freedom - Sensor Stick  SEN-10183 http://www.sparkfun.com/products/10183 | 
|  | 44 | +//#define ARDUIMU_v3 //  DIYDrones ArduIMU+ V3 http://store.diydrones.com/ArduIMU_V3_p/kt-arduimu-30.htm or https://www.sparkfun.com/products/11055 | 
|  | 45 | +#define GEN_MPU6050 // Generic MPU6050 breakout board. Compatible with GY-521, SEN-11028 and other MPU6050 wich have the MPU6050 AD0 pin connected to GND. | 
|  | 46 | + | 
|  | 47 | +// *** No configuration needed below this line *** | 
|  | 48 | + | 
|  | 49 | + | 
|  | 50 | +#define FREEIMU_LIB_VERSION "20121122" | 
|  | 51 | + | 
|  | 52 | +#define FREEIMU_DEVELOPER "Fabio Varesano - varesano.net" | 
|  | 53 | + | 
|  | 54 | +#if F_CPU == 16000000L | 
|  | 55 | +  #define FREEIMU_FREQ "16 MHz" | 
|  | 56 | +#elif F_CPU == 8000000L | 
|  | 57 | +  #define FREEIMU_FREQ "8 MHz" | 
|  | 58 | +#endif | 
|  | 59 | + | 
|  | 60 | + | 
|  | 61 | +// board IDs | 
|  | 62 | + | 
|  | 63 | +#if defined(FREEIMU_v04) | 
|  | 64 | +  #define FREEIMU_ID "FreeIMU v0.4" | 
|  | 65 | +#endif | 
|  | 66 | + | 
|  | 67 | + | 
|  | 68 | +#define HAS_MPU6050() (defined(FREEIMU_v04) || defined(GEN_MPU6050)) | 
|  | 69 | + | 
|  | 70 | +#define IS_6DOM() (defined(SEN_10121) || defined(GEN_MPU6050)) | 
|  | 71 | +#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)) | 
|  | 72 | + | 
|  | 73 | + | 
|  | 74 | + | 
|  | 75 | +//#include <Wire.h> | 
|  | 76 | + | 
|  | 77 | +#include "mbed.h" | 
|  | 78 | +#include "calibration.h" | 
|  | 79 | +/* | 
|  | 80 | +#ifndef CALIBRATION_H | 
|  | 81 | +#include <EEPROM.h> | 
|  | 82 | +#endif | 
|  | 83 | +
 | 
|  | 84 | +#define FREEIMU_EEPROM_BASE 0x0A | 
|  | 85 | +#define FREEIMU_EEPROM_SIGNATURE 0x19 | 
|  | 86 | +*/ | 
|  | 87 | +//#if FREEIMU_VER <= 3 | 
|  | 88 | + | 
|  | 89 | +#if HAS_MPU6050() | 
|  | 90 | + // #include <Wire.h> | 
|  | 91 | +  #include "I2Cdev.h" | 
|  | 92 | +  #include "MPU6050.h" | 
|  | 93 | +  #define FIMU_ACCGYRO_ADDR MPU6050_DEFAULT_ADDRESS | 
|  | 94 | + | 
|  | 95 | +#endif | 
|  | 96 | + | 
|  | 97 | +#include <HMC58X3.h> | 
|  | 98 | +#include <MS561101BA.h> | 
|  | 99 | + | 
|  | 100 | + | 
|  | 101 | +#define FIMU_BARO_ADDR MS561101BA_ADDR_CSB_LOW | 
|  | 102 | + | 
|  | 103 | + | 
|  | 104 | +#define FIMU_BMA180_DEF_ADDR BMA180_ADDRESS_SDO_LOW | 
|  | 105 | +#define FIMU_ITG3200_DEF_ADDR ITG3200_ADDR_AD0_LOW // AD0 connected to GND | 
|  | 106 | +// HMC5843 address is fixed so don't bother to define it | 
|  | 107 | + | 
|  | 108 | + | 
|  | 109 | +#define twoKpDef  (2.0f * 1.0f) // 2 * proportional gain | 
|  | 110 | +#define twoKiDef  (2.0f * 0.6f) // 2 * integral gain | 
|  | 111 | + | 
|  | 112 | +#ifndef cbi | 
|  | 113 | +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) | 
|  | 114 | +#endif | 
|  | 115 | + | 
|  | 116 | +class FreeIMU | 
|  | 117 | +{ | 
|  | 118 | +  public: | 
|  | 119 | +    FreeIMU(); | 
|  | 120 | +    void init(); | 
|  | 121 | +    void init(bool fastmode); | 
|  | 122 | +     | 
|  | 123 | +    void init(int accgyro_addr, bool fastmode); | 
|  | 124 | +     | 
|  | 125 | +    #ifndef CALIBRATION_H | 
|  | 126 | +    void calLoad(); | 
|  | 127 | +    #endif | 
|  | 128 | +    void zeroGyro(); | 
|  | 129 | +    void getRawValues(int16_t * raw_values); | 
|  | 130 | +    void getValues(float * values); | 
|  | 131 | +    void getQ(float * q); | 
|  | 132 | +    void getEuler(float * angles); | 
|  | 133 | +    void getYawPitchRoll(float * ypr); | 
|  | 134 | +    void getEulerRad(float * angles); | 
|  | 135 | +    void getYawPitchRollRad(float * ypr); | 
|  | 136 | +    void gravityCompensateAcc(float * acc, float * q); | 
|  | 137 | +    float getRawPressure(); | 
|  | 138 | +     | 
|  | 139 | +    float getBaroAlt(); | 
|  | 140 | +    float getBaroAlt(float sea_press); | 
|  | 141 | +     | 
|  | 142 | +    void getQ_simple(float* q); | 
|  | 143 | +     | 
|  | 144 | +    // we make them public so that users can interact directly with device classes | 
|  | 145 | + | 
|  | 146 | +      MPU6050 *accgyro; | 
|  | 147 | +      MS561101BA *baro; | 
|  | 148 | +      HMC58X3 *magn; | 
|  | 149 | +     | 
|  | 150 | +    int* raw_acc, raw_gyro, raw_magn; | 
|  | 151 | +    // calibration parameters | 
|  | 152 | +    int16_t gyro_off_x, gyro_off_y, gyro_off_z; | 
|  | 153 | +    int16_t acc_off_x, acc_off_y, acc_off_z, magn_off_x, magn_off_y, magn_off_z; | 
|  | 154 | +    float acc_scale_x, acc_scale_y, acc_scale_z, magn_scale_x, magn_scale_y, magn_scale_z; | 
|  | 155 | +     | 
|  | 156 | +  private: | 
|  | 157 | + | 
|  | 158 | +    void AHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz, bool _magn_valid); | 
|  | 159 | + | 
|  | 160 | +    //float q0, q1, q2, q3; // quaternion elements representing the estimated orientation | 
|  | 161 | +    float iq0, iq1, iq2, iq3; | 
|  | 162 | +    float exInt, eyInt, ezInt;  // scaled integral error | 
|  | 163 | +    volatile float twoKp;      // 2 * proportional gain (Kp) | 
|  | 164 | +    volatile float twoKi;      // 2 * integral gain (Ki) | 
|  | 165 | +    volatile float twoKiz, twoKpz; | 
|  | 166 | +    volatile float q0, q1, q2, q3; // quaternion of sensor frame relative to auxiliary frame | 
|  | 167 | +    volatile float integralFBx,  integralFBy, integralFBz; | 
|  | 168 | +    Timer update; | 
|  | 169 | +    int dt_us; | 
|  | 170 | +    //unsigned long lastUpdate, now; // sample period expressed in milliseconds | 
|  | 171 | +    float sampleFreq; // half the sample period expressed in seconds | 
|  | 172 | + | 
|  | 173 | +}; | 
|  | 174 | + | 
|  | 175 | +float invSqrt(float number); | 
|  | 176 | +void arr3_rad_to_deg(float * arr); | 
|  | 177 | + | 
|  | 178 | + | 
|  | 179 | + | 
|  | 180 | +#endif // FreeIMU_h | 
|  | 181 | + | 
0 commit comments