Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1993 - 1
/****************************************************************************
2
* ITG3200.h - ITG-3200/I2C library v0.5 for Arduino                         *
3
* Copyright 2010-2011 Filipe Vieira & various contributors                  *
4
* http://code.google.com/p/itg-3200driver                                   *
5
* This file is part of ITG-3200 Arduino library.                            *
6
*                                                                           *
7
* This library is free software: you can redistribute it and/or modify      *
8
* it under the terms of the GNU Lesser General Public License as published  *
9
* by the Free Software Foundation, either version 3 of the License, or      *
10
* (at your option) any later version.                                       *
11
*                                                                           *
12
* This program is distributed in the hope that it will be useful,           *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
15
* GNU Lesser General Public License for more details.                       *
16
*                                                                           *
17
* You should have received a copy of the GNU Lesser General Public License  *
18
* along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
19
****************************************************************************/
20
/****************************************************************************
21
* Tested on Arduino Mega with ITG-3200 Breakout                             *
22
* SCL     -> pin 21     (no pull up resistors)                              *
23
* SDA     -> pin 20     (no pull up resistors)                              *
24
* CLK & GND -> pin GND                                                    *
25
* INT       -> not connected  (but can be used)                             *
26
* VIO & VDD -> pin 3.3V                                                     *
27
*****************************************************************************/
28
#ifndef ITG3200_h
29
#define ITG3200_h
30
 
31
#include "WProgram.h"
32
 
33
#define ITG3200_ADDR_AD0_HIGH  0x69   //AD0=1 0x69 I2C address when AD0 is connected to HIGH (VCC) - default for sparkfun breakout
34
#define ITG3200_ADDR_AD0_LOW   0x68   //AD0=0 0x68 I2C address when AD0 is connected to LOW (GND)
35
// "The LSB bit of the 7 bit address is determined by the logic level on pin 9. 
36
// This allows two ITG-3200 devices to be connected to the same I2C bus.
37
// One device should have pin9 (or bit0) LOW and the other should be HIGH." source: ITG3200 datasheet
38
// Note that pin9 (AD0 - I2C Slave Address LSB) may not be available on some breakout boards so check 
39
// the schematics of your breakout board for the correct address to use.
40
 
41
 
42
#define GYROSTART_UP_DELAY  70    // 50ms from gyro startup + 20ms register r/w startup
43
 
44
/* ---- Registers ---- */
45
#define WHO_AM_I           0x00  // RW   SETUP: I2C address   
46
#define SMPLRT_DIV         0x15  // RW   SETUP: Sample Rate Divider       
47
#define DLPF_FS            0x16  // RW   SETUP: Digital Low Pass Filter/ Full Scale range
48
#define INT_CFG            0x17  // RW   Interrupt: Configuration
49
#define INT_STATUS         0x1A  // R   Interrupt: Status
50
#define TEMP_OUT           0x1B  // R   SENSOR: Temperature 2bytes
51
#define GYRO_XOUT          0x1D  // R   SENSOR: Gyro X 2bytes  
52
#define GYRO_YOUT          0x1F  // R   SENSOR: Gyro Y 2bytes
53
#define GYRO_ZOUT          0x21  // R   SENSOR: Gyro Z 2bytes
54
#define PWR_MGM            0x3E  // RW  Power Management
55
 
56
/* ---- bit maps ---- */
57
#define DLPFFS_FS_SEL             0x18  // 00011000
58
#define DLPFFS_DLPF_CFG           0x07  // 00000111
59
#define INTCFG_ACTL               0x80  // 10000000
60
#define INTCFG_OPEN               0x40  // 01000000
61
#define INTCFG_LATCH_INT_EN       0x20  // 00100000
62
#define INTCFG_INT_ANYRD_2CLEAR   0x10  // 00010000
63
#define INTCFG_ITG_RDY_EN         0x04  // 00000100
64
#define INTCFG_RAW_RDY_EN         0x01  // 00000001
65
#define INTSTATUS_ITG_RDY         0x04  // 00000100
66
#define INTSTATUS_RAW_DATA_RDY    0x01  // 00000001
67
#define PWRMGM_HRESET             0x80  // 10000000
68
#define PWRMGM_SLEEP              0x40  // 01000000
69
#define PWRMGM_STBY_XG            0x20  // 00100000
70
#define PWRMGM_STBY_YG            0x10  // 00010000
71
#define PWRMGM_STBY_ZG            0x08  // 00001000
72
#define PWRMGM_CLK_SEL            0x07  // 00000111
73
 
74
/************************************/
75
/*    REGISTERS PARAMETERS    */
76
/************************************/
77
// Sample Rate Divider
78
#define NOSRDIVIDER         0 // default    FsampleHz=SampleRateHz/(divider+1)
79
// Gyro Full Scale Range
80
#define RANGE2000           3   // default
81
// Digital Low Pass Filter BandWidth and SampleRate
82
#define BW256_SR8           0   // default    256Khz BW and 8Khz SR
83
#define BW188_SR1           1
84
#define BW098_SR1           2
85
#define BW042_SR1           3
86
#define BW020_SR1           4
87
#define BW010_SR1           5
88
#define BW005_SR1           6
89
// Interrupt Active logic lvl
90
#define ACTIVE_ONHIGH       0 // default
91
#define ACTIVE_ONLOW        1
92
// Interrupt drive type
93
#define PUSH_PULL           0 // default
94
#define OPEN_DRAIN          1
95
// Interrupt Latch mode
96
#define PULSE_50US          0 // default
97
#define UNTIL_INT_CLEARED   1
98
// Interrupt Latch clear method
99
#define READ_STATUSREG      0 // default
100
#define READ_ANYREG         1
101
// Power management
102
#define NORMAL              0 // default
103
#define STANDBY             1
104
// Clock Source - user parameters
105
#define INTERNALOSC         0   // default
106
#define PLL_XGYRO_REF       1
107
#define PLL_YGYRO_REF       2
108
#define PLL_ZGYRO_REF       3
109
#define PLL_EXTERNAL32      4   // 32.768 kHz
110
#define PLL_EXTERNAL19      5   // 19.2 Mhz
111
 
112
class ITG3200 {
113
 
114
public:
115
  float gains[3];
116
  int offsets[3];
117
  float polarities[3];
118
 
119
  ITG3200();
120
 
121
  // Gyro initialization
122
  void init(unsigned int address);
123
  void init(unsigned int address, byte _SRateDiv, byte _Range, byte _filterBW, byte _ClockSrc, bool _ITGReady, bool _INTRawDataReady);      
124
 
125
  // Who Am I
126
  byte getDevAddr();
127
  void setDevAddr(unsigned int _addr);
128
  // Sample Rate Divider
129
  byte getSampleRateDiv();          
130
  void setSampleRateDiv(byte _SampleRate);
131
  // Digital Low Pass Filter BandWidth and SampleRate 
132
  byte getFSRange();
133
  void setFSRange(byte _Range); // RANGE2000
134
  byte getFilterBW();
135
  void setFilterBW(byte _BW); // see register parameters above
136
  // Interrupt Configuration
137
  bool isINTActiveOnLow();
138
  void setINTLogiclvl(bool _State); //ACTIVE_ONHIGH, ACTIVE_ONLOW
139
  // Interrupt drive type
140
  bool isINTOpenDrain();
141
  void setINTDriveType(bool _State); //OPEN_DRAIN, PUSH_PULL
142
  // Interrupt Latch mode
143
  bool isLatchUntilCleared();
144
  void setLatchMode(bool _State); //UNTIL_INT_CLEARED, PULSE_50US
145
  // Interrupt Latch clear method
146
  bool isAnyRegClrMode();
147
  void setLatchClearMode(bool _State); //READ_ANYREG, READ_STATUSREG
148
  // INT pin triggers
149
  bool isITGReadyOn();          
150
  void setITGReady(bool _State);
151
  bool isRawDataReadyOn();
152
  void setRawDataReady(bool _State);      
153
  // Trigger Status
154
  bool isITGReady();
155
  bool isRawDataReady();
156
  // Gyro Sensors
157
  void readTemp(float *_Temp);
158
  void readGyroRaw(int  *_GyroXYZ);
159
  void readGyroRaw(int *_GyroX, int *_GyroY, int *_GyroZ);
160
  void setRevPolarity(bool _Xpol, bool _Ypol, bool _Zpol);      // true = Reversed  false = default
161
  void setGains(float _Xgain, float _Ygain, float _Zgain);
162
  void setOffsets(int _Xoffset, int _Yoffset, int _Zoffset);
163
  void zeroCalibrate(unsigned int totSamples, unsigned int sampleDelayMS);      // assuming gyroscope is stationary (updates XYZ offsets for zero)
164
  void readGyroRawCal(int *_GyroX, int *_GyroY, int *_GyroZ);
165
  void readGyroRawCal(int *_GyroXYZ);
166
  void readGyro(float *_GyroXYZ); // includes gain and offset
167
  void readGyro(float *_GyroX, float *_GyroY, float *_GyroZ); // includes gain and offset    
168
  // Power management
169
  void reset(); // after reset all registers have default values
170
  bool isLowPower();
171
  void setPowerMode(bool _State); // NORMAL, STANDBY
172
  bool isXgyroStandby();            
173
  bool isYgyroStandby();
174
  bool isZgyroStandby();
175
  void setXgyroStandby(bool _Status); // NORMAL, STANDBY
176
  void setYgyroStandby(bool _Status);
177
  void setZgyroStandby(bool _Status);
178
  byte getClockSource();
179
  void setClockSource(byte _CLKsource); // see register parameters above
180
 
181
  void writemem(uint8_t _addr, uint8_t _val);
182
  void readmem(uint8_t _addr, uint8_t _nbytes, uint8_t __buff[]);
183
 
184
private:
185
 
186
  uint8_t _dev_address;
187
  uint8_t _buff[6];      
188
};
189
#endif