Sille Van Landschoot / ITG3200

Dependents:   m3Dpi

Fork of ITG3200 by Aaron Berk

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ITG3200.h Source File

ITG3200.h

00001 /**
00002  * @author Aaron Berk
00003  *
00004  * @section LICENSE
00005  *
00006  * Copyright (c) 2010 ARM Limited
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  * @section DESCRIPTION
00027  *
00028  * ITG-3200 triple axis, digital interface, gyroscope.
00029  *
00030  * Datasheet:
00031  *
00032  * http://invensense.com/mems/gyro/documents/PS-ITG-3200-00-01.4.pdf
00033  */
00034 
00035 #ifndef ITG3200_H
00036 #define ITG3200_H
00037 
00038 /**
00039  * Includes
00040  */
00041 #include "mbed.h"
00042 
00043 /**
00044  * Defines
00045  */
00046 #define ITG3200_I2C_ADDRESS 0x68 //7-bit address.
00047 
00048 //-----------
00049 // Registers
00050 //-----------
00051 #define WHO_AM_I_REG    0x00
00052 #define SMPLRT_DIV_REG  0x15
00053 #define DLPF_FS_REG     0x16
00054 #define INT_CFG_REG     0x17
00055 #define INT_STATUS      0x1A
00056 #define TEMP_OUT_H_REG  0x1B
00057 #define TEMP_OUT_L_REG  0x1C
00058 #define GYRO_XOUT_H_REG 0x1D
00059 #define GYRO_XOUT_L_REG 0x1E
00060 #define GYRO_YOUT_H_REG 0x1F
00061 #define GYRO_YOUT_L_REG 0x20
00062 #define GYRO_ZOUT_H_REG 0x21
00063 #define GYRO_ZOUT_L_REG 0x22
00064 #define PWR_MGM_REG     0x3E
00065 
00066 //----------------------------
00067 // Low Pass Filter Bandwidths
00068 //----------------------------
00069 #define LPFBW_256HZ 0x00
00070 #define LPFBW_188HZ 0x01
00071 #define LPFBW_98HZ  0x02
00072 #define LPFBW_42HZ  0x03
00073 #define LPFBW_20HZ  0x04
00074 #define LPFBW_10HZ  0x05
00075 #define LPFBW_5HZ   0x06
00076 
00077 /**
00078  * ITG-3200 triple axis digital gyroscope.
00079  */
00080 class ITG3200 {
00081 
00082 public:
00083 
00084     ITG3200(I2C &i2c);
00085     
00086     /**
00087      * Constructor.
00088      *
00089      * Sets FS_SEL to 0x03 for proper opertaion.
00090      *
00091      * @param sda - mbed pin to use for the SDA I2C line.
00092      * @param scl - mbed pin to use for the SCL I2C line.
00093      */
00094     ITG3200(PinName sda, PinName scl);
00095 
00096     /**
00097      * Get the identity of the device.
00098      *
00099      * @return The contents of the Who Am I register which contains the I2C
00100      *         address of the device.
00101      */
00102     char getWhoAmI(void);
00103 
00104     /**
00105      * Set the address of the device.
00106      *
00107      * @param address The I2C slave address to write to the Who Am I register
00108      *        on the device.
00109      */
00110     void setWhoAmI(char address);
00111 
00112     /**
00113      * Get the sample rate divider.
00114      *
00115      * @return The sample rate divider as a number from 0-255.
00116      */
00117     char getSampleRateDivider(void);
00118 
00119     /**
00120      * Set the sample rate divider.
00121      *
00122      * Fsample = Finternal / (divider + 1), where Finternal = 1kHz or 8kHz,
00123      * as decidied by the DLPF_FS register.
00124      *
00125      * @param The sample rate divider as a number from 0-255.
00126      */
00127     void setSampleRateDivider(char divider);
00128 
00129     /**
00130      * Get the internal sample rate.
00131      *
00132      * @return The internal sample rate in kHz - either 1 or 8.
00133      */
00134     int getInternalSampleRate(void);
00135 
00136     /**
00137      * Set the low pass filter bandwidth.
00138      *
00139      * Also used to set the internal sample rate.
00140      * Pass the #define bandwidth codes as a parameter.
00141      *
00142      * 256Hz -> 8kHz internal sample rate.
00143      * Everything else -> 1kHz internal rate.
00144      *
00145      * @param bandwidth Low pass filter bandwidth code
00146      */
00147     void setLpBandwidth(char bandwidth);
00148 
00149     /**
00150      * Get the interrupt configuration.
00151      *
00152      * See datasheet for register contents details.
00153      *
00154      *    7      6           5                 4
00155      * +------+------+--------------+------------------+
00156      * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR |
00157      * +------+------+--------------+------------------+
00158      *
00159      *   3        2            1       0
00160      * +---+------------+------------+---+
00161      * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 |
00162      * +---+------------+------------+---+
00163      *
00164      * ACTL Logic level for INT output pin; 1 = active low, 0 = active high.
00165      * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull.
00166      * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared,
00167      *                          0 = 50us pulse.
00168      * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read,
00169      *                                      0 = status register read only.
00170      * ITG_RDY_EN Enable interrupt when device is ready,
00171      *            (PLL ready after changing clock source).
00172      * RAW_RDY_EN Enable interrupt when data is available.
00173      * 0 Bits 1 and 3 of the INT_CFG register should be zero.
00174      *
00175      * @return the contents of the INT_CFG register.
00176      */
00177     char getInterruptConfiguration(void);
00178 
00179     /**
00180      * Set the interrupt configuration.
00181      *
00182      * See datasheet for configuration byte details.
00183      *
00184      *    7      6           5                 4
00185      * +------+------+--------------+------------------+
00186      * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR |
00187      * +------+------+--------------+------------------+
00188      *
00189      *   3        2            1       0
00190      * +---+------------+------------+---+
00191      * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 |
00192      * +---+------------+------------+---+
00193      *
00194      * ACTL Logic level for INT output pin; 1 = active low, 0 = active high.
00195      * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull.
00196      * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared,
00197      *                          0 = 50us pulse.
00198      * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read,
00199      *                                      0 = status register read only.
00200      * ITG_RDY_EN Enable interrupt when device is ready,
00201      *            (PLL ready after changing clock source).
00202      * RAW_RDY_EN Enable interrupt when data is available.
00203      * 0 Bits 1 and 3 of the INT_CFG register should be zero.
00204      *
00205      * @param config Configuration byte to write to INT_CFG register.
00206      */
00207     void setInterruptConfiguration(char config);
00208 
00209     /**
00210      * Check the ITG_RDY bit of the INT_STATUS register.
00211      *
00212      * @return True if the ITG_RDY bit is set, corresponding to PLL ready,
00213      *         false if the ITG_RDY bit is not set, corresponding to PLL not
00214      *         ready.
00215      */
00216     bool isPllReady(void);
00217 
00218     /**
00219      * Check the RAW_DATA_RDY bit of the INT_STATUS register.
00220      *
00221      * @return True if the RAW_DATA_RDY bit is set, corresponding to new data
00222      *         in the sensor registers, false if the RAW_DATA_RDY bit is not
00223      *         set, corresponding to no new data yet in the sensor registers.
00224      */
00225     bool isRawDataReady(void);
00226 
00227     /**
00228      * Get the temperature of the device.
00229      *
00230      * @return The temperature in degrees celsius.
00231      */
00232     float getTemperature(void);
00233 
00234     /**
00235      * Get the output for the x-axis gyroscope.
00236      *
00237      * Typical sensitivity is 14.375 LSB/(degrees/sec).
00238      *
00239      * @return The output on the x-axis in raw ADC counts.
00240      */
00241     int getGyroX(void);
00242 
00243     /**
00244      * Get the output for the y-axis gyroscope.
00245      *
00246      * Typical sensitivity is 14.375 LSB/(degrees/sec).
00247      *
00248      * @return The output on the y-axis in raw ADC counts.
00249      */
00250     int getGyroY(void);
00251 
00252     /**
00253      * Get the output on the z-axis gyroscope.
00254      *
00255      * Typical sensitivity is 14.375 LSB/(degrees/sec).
00256      * 
00257      * @return The output on the z-axis in raw ADC counts.
00258      */
00259     int getGyroZ(void);
00260 
00261     /**
00262      * Get the power management configuration.
00263      *
00264      * See the datasheet for register contents details.
00265      *
00266      *     7        6        5         4
00267      * +---------+-------+---------+---------+
00268      * | H_RESET | SLEEP | STBY_XG | STBY_YG |
00269      * +---------+-------+---------+---------+
00270      *
00271      *      3          2         1          0
00272      * +---------+----------+----------+----------+
00273      * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
00274      * +---------+----------+----------+----------+
00275      *
00276      * H_RESET Reset device and internal registers to the power-up-default settings.
00277      * SLEEP Enable low power sleep mode.
00278      * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
00279      * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
00280      * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
00281      * CLK_SEL Select device clock source:
00282      *
00283      * CLK_SEL | Clock Source
00284      * --------+--------------
00285      *    0      Internal oscillator
00286      *    1      PLL with X Gyro reference
00287      *    2      PLL with Y Gyro reference
00288      *    3      PLL with Z Gyro reference
00289      *    4      PLL with external 32.768kHz reference
00290      *    5      PLL with external 19.2MHz reference
00291      *    6      Reserved
00292      *    7      Reserved
00293      *
00294      * @return The contents of the PWR_MGM register.
00295      */
00296     char getPowerManagement(void);
00297 
00298     /**
00299      * Set power management configuration.
00300      *
00301      * See the datasheet for configuration byte details
00302      *
00303      *      7        6        5         4
00304      * +---------+-------+---------+---------+
00305      * | H_RESET | SLEEP | STBY_XG | STBY_YG |
00306      * +---------+-------+---------+---------+
00307      *
00308      *      3          2         1          0
00309      * +---------+----------+----------+----------+
00310      * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
00311      * +---------+----------+----------+----------+
00312      *
00313      * H_RESET Reset device and internal registers to the power-up-default settings.
00314      * SLEEP Enable low power sleep mode.
00315      * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
00316      * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
00317      * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
00318      * CLK_SEL Select device clock source:
00319      *
00320      * CLK_SEL | Clock Source
00321      * --------+--------------
00322      *    0      Internal oscillator
00323      *    1      PLL with X Gyro reference
00324      *    2      PLL with Y Gyro reference
00325      *    3      PLL with Z Gyro reference
00326      *    4      PLL with external 32.768kHz reference
00327      *    5      PLL with external 19.2MHz reference
00328      *    6      Reserved
00329      *    7      Reserved
00330      *
00331      * @param config The configuration byte to write to the PWR_MGM register.
00332      */
00333     void setPowerManagement(char config);
00334 
00335 private:
00336 
00337     void initialize();
00338     I2C i2c_;
00339 
00340 };
00341 
00342 #endif /* ITG3200_H */