Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1993 - 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
 
6
This program is free software: you can redistribute it and/or modify
7
it under the terms of the version 3 GNU General Public License as
8
published by the Free Software Foundation.
9
 
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14
 
15
You should have received a copy of the GNU General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18
 
19
// Uncomment the appropriated version of FreeIMU you are using
20
// #define FREEIMU_VER 1 // For FreeIMU v0.1
21
// #define FREEIMU_VER 2 // For FreeIMU v0.2
22
//#define FREEIMU_VER 3 // For FreeIMU v0.3 and v0.3.1
23
#define FREEIMU_VER 4 // For FreeIMU v0.3.5
24
// #define FREEIMU_VER 5 // For FreeIMU v0.3.5_MS
25
// #define FREEIMU_VER 6 // For FreeIMU v0.3.5_BMP
26
 
27
 
28
#include <Wire.h>
29
#include "WProgram.h"
30
 
31
#if FREEIMU_VER <= 3
32
  #include <ADXL345.h>
33
  // default I2C 7-bit addresses of the sensors
34
  #define FIMU_ACC_ADDR ADXL345_ADDR_ALT_LOW // SDO connected to GND
35
  //#define FIMU_ADXL345_DEF_ADDR ADXL345_ADDR_ALT_HIGH // SDO connected to GND
36
#else
37
  #include <bma180.h>
38
    #define FIMU_ACC_ADDR BMA180_ADDRESS_SDO_LOW 
39
    //#define FIMU_ACC_ADDR BMA180_ADDRESS_SDO_HIGH
40
#endif
41
#include <HMC58X3.h>
42
#include <ITG3200.h>
43
 
44
 
45
#ifndef FreeIMU_h
46
#define FreeIMU_h
47
 
48
 
49
#define FIMU_BMA180_DEF_ADDR BMA180_ADDRESS_SDO_LOW
50
#define FIMU_ITG3200_DEF_ADDR ITG3200_ADDR_AD0_LOW // AD0 connected to GND
51
// HMC5843 address is fixed so don't bother to define it
52
 
53
/*
54
// ITG3200 constants
55
#define FIMU_ITG3200_SMPLRT_DIV 0x15
56
#define FIMU_ITG3200_DLPF_FS 0x16
57
#define FIMU_ITG3200_INT_CFG 0x17
58
#define FIMU_ITG3200_PWR_MGM 0x3E
59
 
60
*/
61
 
62
#ifndef cbi
63
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
64
#endif
65
 
66
class FreeIMU
67
{
68
  public:
69
    FreeIMU();
70
    void init();
71
    void init(bool fastmode);
72
    void init(int acc_addr, int gyro_addr, bool fastmode);
73
    void getRawValues(int * raw_values);
74
    void getValues(float * values);
75
    void getQ(float * q);
76
    void getEuler(float * angles);
77
    void getYawPitchRoll(float * ypr);
78
 
79
    // we make them public so that users can interact directly with device classes
80
    #if FREEIMU_VER <= 3
81
      ADXL345 acc;
82
    #else
83
      BMA180 acc;
84
    #endif
85
 
86
    HMC58X3 magn;
87
    ITG3200 gyro;
88
 
89
  private:
90
    int* raw_acc, raw_gyro, raw_magn;
91
    void AHRSupdate(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz);
92
    float q0, q1, q2, q3; // quaternion elements representing the estimated orientation
93
    float iq0, iq1, iq2, iq3;
94
    float exInt, eyInt, ezInt;  // scaled integral error
95
    int lastUpdate, now; // sample period expressed in milliseconds
96
    float halfT; // half the sample period expressed in seconds
97
    int startLoopTime;
98
};
99
 
100
float invSqrt(float number);
101
 
102
#endif // FreeIMU_h
103