Forked from Aaron Berk's ITG3200 driver class library, customized for my specific application using 9DoF-Stick by Sparkfun.
Fork of ITG3200 by
ITG-3200 is triple axis, digital interface, gyro sensor.
This library is forked from Aaron Berk's work.
This library is for specific application using 9DoF-Stick.
Datasheet:
http://invensense.com/mems/gyro/documents/PS-ITG-3200-00-01.4.pdf
This library has a feature to correct thermal drift of the device. For details, see Thermal Drift.
ITG-3200は3軸のデジタルインターフェースを備えたジャイロセンサです。
このライブラリは 9DoF-Stick を使用した特定の企画のために保守しています。
mbed IDEが日本語をサポートするまでは英語でコメントを書いていきますが、サポートした後もきっと英語で書いていくでしょう。
このライブラリはデバイスの熱ドリフトを補正する機能を持っています。詳しくは Thermal Drift。
ITG3200.h@9:05396b551a9a, 2012-10-02 (annotated)
- Committer:
- gltest26
- Date:
- Tue Oct 02 18:04:13 2012 +0000
- Revision:
- 9:05396b551a9a
- Parent:
- 8:ac0365ab3cef
Added support for calibration curve on chip temperature.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gltest26 | 1:9bef044f45ad | 1 | /** |
gltest26 | 3:eea9733ca427 | 2 | * @file ITG3200.h |
gltest26 | 1:9bef044f45ad | 3 | * @author Aaron Berk |
gltest26 | 1:9bef044f45ad | 4 | * |
gltest26 | 1:9bef044f45ad | 5 | * @section LICENSE |
gltest26 | 1:9bef044f45ad | 6 | * |
gltest26 | 1:9bef044f45ad | 7 | * Copyright (c) 2010 ARM Limited |
gltest26 | 1:9bef044f45ad | 8 | * |
gltest26 | 1:9bef044f45ad | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
gltest26 | 1:9bef044f45ad | 10 | * of this software and associated documentation files (the "Software"), to deal |
gltest26 | 1:9bef044f45ad | 11 | * in the Software without restriction, including without limitation the rights |
gltest26 | 1:9bef044f45ad | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
gltest26 | 1:9bef044f45ad | 13 | * copies of the Software, and to permit persons to whom the Software is |
gltest26 | 1:9bef044f45ad | 14 | * furnished to do so, subject to the following conditions: |
gltest26 | 1:9bef044f45ad | 15 | * |
gltest26 | 1:9bef044f45ad | 16 | * The above copyright notice and this permission notice shall be included in |
gltest26 | 1:9bef044f45ad | 17 | * all copies or substantial portions of the Software. |
gltest26 | 1:9bef044f45ad | 18 | * |
gltest26 | 1:9bef044f45ad | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
gltest26 | 1:9bef044f45ad | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
gltest26 | 1:9bef044f45ad | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
gltest26 | 1:9bef044f45ad | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
gltest26 | 1:9bef044f45ad | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gltest26 | 1:9bef044f45ad | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
gltest26 | 1:9bef044f45ad | 25 | * THE SOFTWARE. |
gltest26 | 1:9bef044f45ad | 26 | * |
gltest26 | 1:9bef044f45ad | 27 | * @section DESCRIPTION |
gltest26 | 1:9bef044f45ad | 28 | * |
gltest26 | 1:9bef044f45ad | 29 | * ITG-3200 triple axis, digital interface, gyroscope. |
gltest26 | 3:eea9733ca427 | 30 | * Forked from Aaron Berk's work. |
gltest26 | 1:9bef044f45ad | 31 | * |
gltest26 | 1:9bef044f45ad | 32 | * Datasheet: |
gltest26 | 1:9bef044f45ad | 33 | * |
gltest26 | 1:9bef044f45ad | 34 | * http://invensense.com/mems/gyro/documents/PS-ITG-3200-00-01.4.pdf |
gltest26 | 1:9bef044f45ad | 35 | */ |
gltest26 | 1:9bef044f45ad | 36 | |
gltest26 | 1:9bef044f45ad | 37 | #ifndef ITG3200_H |
gltest26 | 1:9bef044f45ad | 38 | #define ITG3200_H |
gltest26 | 1:9bef044f45ad | 39 | |
gltest26 | 3:eea9733ca427 | 40 | /* |
gltest26 | 1:9bef044f45ad | 41 | * Includes |
gltest26 | 1:9bef044f45ad | 42 | */ |
gltest26 | 1:9bef044f45ad | 43 | #include "mbed.h" |
gltest26 | 1:9bef044f45ad | 44 | |
gltest26 | 3:eea9733ca427 | 45 | /* |
gltest26 | 1:9bef044f45ad | 46 | * Defines |
gltest26 | 1:9bef044f45ad | 47 | */ |
gltest26 | 1:9bef044f45ad | 48 | #define ITG3200_I2C_ADDRESS 0x68 //7-bit address. |
gltest26 | 1:9bef044f45ad | 49 | |
gltest26 | 1:9bef044f45ad | 50 | //----------- |
gltest26 | 1:9bef044f45ad | 51 | // Registers |
gltest26 | 1:9bef044f45ad | 52 | //----------- |
gltest26 | 1:9bef044f45ad | 53 | #define WHO_AM_I_REG 0x00 |
gltest26 | 1:9bef044f45ad | 54 | #define SMPLRT_DIV_REG 0x15 |
gltest26 | 1:9bef044f45ad | 55 | #define DLPF_FS_REG 0x16 |
gltest26 | 1:9bef044f45ad | 56 | #define INT_CFG_REG 0x17 |
gltest26 | 1:9bef044f45ad | 57 | #define INT_STATUS 0x1A |
gltest26 | 1:9bef044f45ad | 58 | #define TEMP_OUT_H_REG 0x1B |
gltest26 | 1:9bef044f45ad | 59 | #define TEMP_OUT_L_REG 0x1C |
gltest26 | 1:9bef044f45ad | 60 | #define GYRO_XOUT_H_REG 0x1D |
gltest26 | 1:9bef044f45ad | 61 | #define GYRO_XOUT_L_REG 0x1E |
gltest26 | 1:9bef044f45ad | 62 | #define GYRO_YOUT_H_REG 0x1F |
gltest26 | 1:9bef044f45ad | 63 | #define GYRO_YOUT_L_REG 0x20 |
gltest26 | 1:9bef044f45ad | 64 | #define GYRO_ZOUT_H_REG 0x21 |
gltest26 | 1:9bef044f45ad | 65 | #define GYRO_ZOUT_L_REG 0x22 |
gltest26 | 1:9bef044f45ad | 66 | #define PWR_MGM_REG 0x3E |
gltest26 | 1:9bef044f45ad | 67 | |
gltest26 | 1:9bef044f45ad | 68 | //---------------------------- |
gltest26 | 1:9bef044f45ad | 69 | // Low Pass Filter Bandwidths |
gltest26 | 1:9bef044f45ad | 70 | //---------------------------- |
gltest26 | 1:9bef044f45ad | 71 | #define LPFBW_256HZ 0x00 |
gltest26 | 1:9bef044f45ad | 72 | #define LPFBW_188HZ 0x01 |
gltest26 | 1:9bef044f45ad | 73 | #define LPFBW_98HZ 0x02 |
gltest26 | 1:9bef044f45ad | 74 | #define LPFBW_42HZ 0x03 |
gltest26 | 1:9bef044f45ad | 75 | #define LPFBW_20HZ 0x04 |
gltest26 | 1:9bef044f45ad | 76 | #define LPFBW_10HZ 0x05 |
gltest26 | 1:9bef044f45ad | 77 | #define LPFBW_5HZ 0x06 |
gltest26 | 1:9bef044f45ad | 78 | |
gltest26 | 1:9bef044f45ad | 79 | // Utility |
gltest26 | 1:9bef044f45ad | 80 | #ifndef M_PI |
gltest26 | 1:9bef044f45ad | 81 | #define M_PI 3.1415926535897932384626433832795 |
gltest26 | 1:9bef044f45ad | 82 | #endif |
gltest26 | 1:9bef044f45ad | 83 | |
gltest26 | 1:9bef044f45ad | 84 | /** |
gltest26 | 1:9bef044f45ad | 85 | * ITG-3200 triple axis digital gyroscope. |
gltest26 | 1:9bef044f45ad | 86 | */ |
gltest26 | 1:9bef044f45ad | 87 | class ITG3200 { |
gltest26 | 1:9bef044f45ad | 88 | |
gltest26 | 1:9bef044f45ad | 89 | public: |
gltest26 | 1:9bef044f45ad | 90 | |
gltest26 | 3:eea9733ca427 | 91 | /** |
gltest26 | 3:eea9733ca427 | 92 | * The I2C address that can be passed directly to i2c object (it's already shifted 1 bit left). |
gltest26 | 3:eea9733ca427 | 93 | * |
gltest26 | 3:eea9733ca427 | 94 | * You don't need to manually set or clear the LSB when calling I2C::read() or I2C::write(), |
gltest26 | 3:eea9733ca427 | 95 | * the library takes care of it. We just always clear the LSB. |
gltest26 | 3:eea9733ca427 | 96 | */ |
gltest26 | 1:9bef044f45ad | 97 | static const int I2C_ADDRESS = 0xD0; |
gltest26 | 9:05396b551a9a | 98 | |
gltest26 | 9:05396b551a9a | 99 | /** |
gltest26 | 9:05396b551a9a | 100 | * @brief Zero offset correction mode that can be specified when calling getGyroXYZ(). |
gltest26 | 9:05396b551a9a | 101 | * |
gltest26 | 9:05396b551a9a | 102 | * The device has a major drift in readings depending on ambient temperature. |
gltest26 | 9:05396b551a9a | 103 | * You can measure the temperature with built-in thermometer to correct it, but you |
gltest26 | 9:05396b551a9a | 104 | * must have calibration curves for each axes to do so. |
gltest26 | 9:05396b551a9a | 105 | * Here are the options on how to correct the drift. |
gltest26 | 9:05396b551a9a | 106 | */ |
gltest26 | 9:05396b551a9a | 107 | enum Correction{ |
gltest26 | 9:05396b551a9a | 108 | NoCorrection, ///< Do not correct zero offset at all; You would have trouble integrating the values to obtain rotation. |
gltest26 | 9:05396b551a9a | 109 | OffsetCorrection, ///< Correct the outputs with single-point zero adjust. |
gltest26 | 9:05396b551a9a | 110 | Calibration, ///< Use calibration curve (actually lines) to correct the outputs. You must provide coefficients with setCalibrationCurve(). |
gltest26 | 9:05396b551a9a | 111 | Num_Correction |
gltest26 | 9:05396b551a9a | 112 | }; |
gltest26 | 1:9bef044f45ad | 113 | |
gltest26 | 1:9bef044f45ad | 114 | /** |
gltest26 | 1:9bef044f45ad | 115 | * Constructor. |
gltest26 | 1:9bef044f45ad | 116 | * |
gltest26 | 1:9bef044f45ad | 117 | * Sets FS_SEL to 0x03 for proper opertaion. |
gltest26 | 1:9bef044f45ad | 118 | * |
gltest26 | 1:9bef044f45ad | 119 | * @param sda - mbed pin to use for the SDA I2C line. |
gltest26 | 1:9bef044f45ad | 120 | * @param scl - mbed pin to use for the SCL I2C line. |
gltest26 | 7:43b936a53b64 | 121 | * @param fastmode Sets the internal I2C interface to use 400kHz clock. |
gltest26 | 1:9bef044f45ad | 122 | */ |
gltest26 | 7:43b936a53b64 | 123 | ITG3200(PinName sda, PinName scl, bool fastmode = false); |
gltest26 | 8:ac0365ab3cef | 124 | |
gltest26 | 8:ac0365ab3cef | 125 | /** |
gltest26 | 8:ac0365ab3cef | 126 | * Constructor that accepts external i2c interface object. |
gltest26 | 8:ac0365ab3cef | 127 | * |
gltest26 | 8:ac0365ab3cef | 128 | * @param i2c The I2C interface object to use. |
gltest26 | 8:ac0365ab3cef | 129 | */ |
gltest26 | 8:ac0365ab3cef | 130 | ITG3200(I2C &i2c) : i2c_(i2c){ |
gltest26 | 8:ac0365ab3cef | 131 | init(); |
gltest26 | 8:ac0365ab3cef | 132 | } |
gltest26 | 8:ac0365ab3cef | 133 | |
gltest26 | 8:ac0365ab3cef | 134 | ~ITG3200(); |
gltest26 | 8:ac0365ab3cef | 135 | |
gltest26 | 8:ac0365ab3cef | 136 | void init(); |
gltest26 | 9:05396b551a9a | 137 | |
gltest26 | 9:05396b551a9a | 138 | /** |
gltest26 | 9:05396b551a9a | 139 | * Sets calibration curve parameters. |
gltest26 | 9:05396b551a9a | 140 | * |
gltest26 | 9:05396b551a9a | 141 | * @param offset An array holding calibration curve offsets (0th-order coefficient) for each axis, must have 3 elements. |
gltest26 | 9:05396b551a9a | 142 | * @param slope An array holding calibration curve slopes (1st-order coefficient) for each axis, must have 3 elements. |
gltest26 | 9:05396b551a9a | 143 | */ |
gltest26 | 9:05396b551a9a | 144 | void setCalibrationCurve(const float offset[3], const float slope[3]); |
gltest26 | 1:9bef044f45ad | 145 | |
gltest26 | 1:9bef044f45ad | 146 | /** |
gltest26 | 1:9bef044f45ad | 147 | * Get the identity of the device. |
gltest26 | 1:9bef044f45ad | 148 | * |
gltest26 | 1:9bef044f45ad | 149 | * @return The contents of the Who Am I register which contains the I2C |
gltest26 | 1:9bef044f45ad | 150 | * address of the device. |
gltest26 | 1:9bef044f45ad | 151 | */ |
gltest26 | 1:9bef044f45ad | 152 | char getWhoAmI(void); |
gltest26 | 1:9bef044f45ad | 153 | |
gltest26 | 1:9bef044f45ad | 154 | /** |
gltest26 | 1:9bef044f45ad | 155 | * Set the address of the device. |
gltest26 | 1:9bef044f45ad | 156 | * |
gltest26 | 1:9bef044f45ad | 157 | * @param address The I2C slave address to write to the Who Am I register |
gltest26 | 1:9bef044f45ad | 158 | * on the device. |
gltest26 | 1:9bef044f45ad | 159 | */ |
gltest26 | 1:9bef044f45ad | 160 | void setWhoAmI(char address); |
gltest26 | 1:9bef044f45ad | 161 | |
gltest26 | 1:9bef044f45ad | 162 | /** |
gltest26 | 1:9bef044f45ad | 163 | * Get the sample rate divider. |
gltest26 | 1:9bef044f45ad | 164 | * |
gltest26 | 1:9bef044f45ad | 165 | * @return The sample rate divider as a number from 0-255. |
gltest26 | 1:9bef044f45ad | 166 | */ |
gltest26 | 1:9bef044f45ad | 167 | char getSampleRateDivider(void); |
gltest26 | 1:9bef044f45ad | 168 | |
gltest26 | 1:9bef044f45ad | 169 | /** |
gltest26 | 1:9bef044f45ad | 170 | * Set the sample rate divider. |
gltest26 | 1:9bef044f45ad | 171 | * |
gltest26 | 1:9bef044f45ad | 172 | * Fsample = Finternal / (divider + 1), where Finternal = 1kHz or 8kHz, |
gltest26 | 1:9bef044f45ad | 173 | * as decidied by the DLPF_FS register. |
gltest26 | 1:9bef044f45ad | 174 | * |
gltest26 | 1:9bef044f45ad | 175 | * @param The sample rate divider as a number from 0-255. |
gltest26 | 1:9bef044f45ad | 176 | */ |
gltest26 | 1:9bef044f45ad | 177 | void setSampleRateDivider(char divider); |
gltest26 | 1:9bef044f45ad | 178 | |
gltest26 | 1:9bef044f45ad | 179 | /** |
gltest26 | 1:9bef044f45ad | 180 | * Get the internal sample rate. |
gltest26 | 1:9bef044f45ad | 181 | * |
gltest26 | 1:9bef044f45ad | 182 | * @return The internal sample rate in kHz - either 1 or 8. |
gltest26 | 1:9bef044f45ad | 183 | */ |
gltest26 | 1:9bef044f45ad | 184 | int getInternalSampleRate(void); |
gltest26 | 1:9bef044f45ad | 185 | |
gltest26 | 1:9bef044f45ad | 186 | /** |
gltest26 | 1:9bef044f45ad | 187 | * Set the low pass filter bandwidth. |
gltest26 | 1:9bef044f45ad | 188 | * |
gltest26 | 1:9bef044f45ad | 189 | * Also used to set the internal sample rate. |
gltest26 | 1:9bef044f45ad | 190 | * Pass the #define bandwidth codes as a parameter. |
gltest26 | 1:9bef044f45ad | 191 | * |
gltest26 | 1:9bef044f45ad | 192 | * 256Hz -> 8kHz internal sample rate. |
gltest26 | 1:9bef044f45ad | 193 | * Everything else -> 1kHz internal rate. |
gltest26 | 1:9bef044f45ad | 194 | * |
gltest26 | 1:9bef044f45ad | 195 | * @param bandwidth Low pass filter bandwidth code |
gltest26 | 1:9bef044f45ad | 196 | */ |
gltest26 | 1:9bef044f45ad | 197 | void setLpBandwidth(char bandwidth); |
gltest26 | 1:9bef044f45ad | 198 | |
gltest26 | 1:9bef044f45ad | 199 | /** |
gltest26 | 1:9bef044f45ad | 200 | * Get the interrupt configuration. |
gltest26 | 1:9bef044f45ad | 201 | * |
gltest26 | 1:9bef044f45ad | 202 | * See datasheet for register contents details. |
gltest26 | 1:9bef044f45ad | 203 | * |
gltest26 | 1:9bef044f45ad | 204 | * 7 6 5 4 |
gltest26 | 1:9bef044f45ad | 205 | * +------+------+--------------+------------------+ |
gltest26 | 1:9bef044f45ad | 206 | * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR | |
gltest26 | 1:9bef044f45ad | 207 | * +------+------+--------------+------------------+ |
gltest26 | 1:9bef044f45ad | 208 | * |
gltest26 | 1:9bef044f45ad | 209 | * 3 2 1 0 |
gltest26 | 1:9bef044f45ad | 210 | * +---+------------+------------+---+ |
gltest26 | 1:9bef044f45ad | 211 | * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 | |
gltest26 | 1:9bef044f45ad | 212 | * +---+------------+------------+---+ |
gltest26 | 1:9bef044f45ad | 213 | * |
gltest26 | 1:9bef044f45ad | 214 | * ACTL Logic level for INT output pin; 1 = active low, 0 = active high. |
gltest26 | 1:9bef044f45ad | 215 | * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull. |
gltest26 | 1:9bef044f45ad | 216 | * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared, |
gltest26 | 1:9bef044f45ad | 217 | * 0 = 50us pulse. |
gltest26 | 1:9bef044f45ad | 218 | * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read, |
gltest26 | 1:9bef044f45ad | 219 | * 0 = status register read only. |
gltest26 | 1:9bef044f45ad | 220 | * ITG_RDY_EN Enable interrupt when device is ready, |
gltest26 | 1:9bef044f45ad | 221 | * (PLL ready after changing clock source). |
gltest26 | 1:9bef044f45ad | 222 | * RAW_RDY_EN Enable interrupt when data is available. |
gltest26 | 1:9bef044f45ad | 223 | * 0 Bits 1 and 3 of the INT_CFG register should be zero. |
gltest26 | 1:9bef044f45ad | 224 | * |
gltest26 | 1:9bef044f45ad | 225 | * @return the contents of the INT_CFG register. |
gltest26 | 1:9bef044f45ad | 226 | */ |
gltest26 | 1:9bef044f45ad | 227 | char getInterruptConfiguration(void); |
gltest26 | 1:9bef044f45ad | 228 | |
gltest26 | 1:9bef044f45ad | 229 | /** |
gltest26 | 1:9bef044f45ad | 230 | * Set the interrupt configuration. |
gltest26 | 1:9bef044f45ad | 231 | * |
gltest26 | 1:9bef044f45ad | 232 | * See datasheet for configuration byte details. |
gltest26 | 1:9bef044f45ad | 233 | * |
gltest26 | 1:9bef044f45ad | 234 | * 7 6 5 4 |
gltest26 | 1:9bef044f45ad | 235 | * +------+------+--------------+------------------+ |
gltest26 | 1:9bef044f45ad | 236 | * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR | |
gltest26 | 1:9bef044f45ad | 237 | * +------+------+--------------+------------------+ |
gltest26 | 1:9bef044f45ad | 238 | * |
gltest26 | 1:9bef044f45ad | 239 | * 3 2 1 0 |
gltest26 | 1:9bef044f45ad | 240 | * +---+------------+------------+---+ |
gltest26 | 1:9bef044f45ad | 241 | * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 | |
gltest26 | 1:9bef044f45ad | 242 | * +---+------------+------------+---+ |
gltest26 | 1:9bef044f45ad | 243 | * |
gltest26 | 1:9bef044f45ad | 244 | * ACTL Logic level for INT output pin; 1 = active low, 0 = active high. |
gltest26 | 1:9bef044f45ad | 245 | * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull. |
gltest26 | 1:9bef044f45ad | 246 | * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared, |
gltest26 | 1:9bef044f45ad | 247 | * 0 = 50us pulse. |
gltest26 | 1:9bef044f45ad | 248 | * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read, |
gltest26 | 1:9bef044f45ad | 249 | * 0 = status register read only. |
gltest26 | 1:9bef044f45ad | 250 | * ITG_RDY_EN Enable interrupt when device is ready, |
gltest26 | 1:9bef044f45ad | 251 | * (PLL ready after changing clock source). |
gltest26 | 1:9bef044f45ad | 252 | * RAW_RDY_EN Enable interrupt when data is available. |
gltest26 | 1:9bef044f45ad | 253 | * 0 Bits 1 and 3 of the INT_CFG register should be zero. |
gltest26 | 1:9bef044f45ad | 254 | * |
gltest26 | 1:9bef044f45ad | 255 | * @param config Configuration byte to write to INT_CFG register. |
gltest26 | 1:9bef044f45ad | 256 | */ |
gltest26 | 1:9bef044f45ad | 257 | void setInterruptConfiguration(char config); |
gltest26 | 1:9bef044f45ad | 258 | |
gltest26 | 1:9bef044f45ad | 259 | /** |
gltest26 | 1:9bef044f45ad | 260 | * Check the ITG_RDY bit of the INT_STATUS register. |
gltest26 | 1:9bef044f45ad | 261 | * |
gltest26 | 1:9bef044f45ad | 262 | * @return True if the ITG_RDY bit is set, corresponding to PLL ready, |
gltest26 | 1:9bef044f45ad | 263 | * false if the ITG_RDY bit is not set, corresponding to PLL not |
gltest26 | 1:9bef044f45ad | 264 | * ready. |
gltest26 | 1:9bef044f45ad | 265 | */ |
gltest26 | 1:9bef044f45ad | 266 | bool isPllReady(void); |
gltest26 | 1:9bef044f45ad | 267 | |
gltest26 | 1:9bef044f45ad | 268 | /** |
gltest26 | 1:9bef044f45ad | 269 | * Check the RAW_DATA_RDY bit of the INT_STATUS register. |
gltest26 | 1:9bef044f45ad | 270 | * |
gltest26 | 1:9bef044f45ad | 271 | * @return True if the RAW_DATA_RDY bit is set, corresponding to new data |
gltest26 | 1:9bef044f45ad | 272 | * in the sensor registers, false if the RAW_DATA_RDY bit is not |
gltest26 | 1:9bef044f45ad | 273 | * set, corresponding to no new data yet in the sensor registers. |
gltest26 | 1:9bef044f45ad | 274 | */ |
gltest26 | 1:9bef044f45ad | 275 | bool isRawDataReady(void); |
gltest26 | 1:9bef044f45ad | 276 | |
gltest26 | 1:9bef044f45ad | 277 | /** |
gltest26 | 1:9bef044f45ad | 278 | * Get the temperature in raw format. |
gltest26 | 1:9bef044f45ad | 279 | * |
gltest26 | 1:9bef044f45ad | 280 | * @return The temperature in raw 16bit integer. |
gltest26 | 1:9bef044f45ad | 281 | */ |
gltest26 | 4:155c44407af5 | 282 | int getRawTemperature(void){ return getWord(TEMP_OUT_H_REG); } |
gltest26 | 1:9bef044f45ad | 283 | |
gltest26 | 1:9bef044f45ad | 284 | /** |
gltest26 | 1:9bef044f45ad | 285 | * Get the temperature of the device. |
gltest26 | 1:9bef044f45ad | 286 | * |
gltest26 | 1:9bef044f45ad | 287 | * @return The temperature in degrees celsius. |
gltest26 | 1:9bef044f45ad | 288 | */ |
gltest26 | 1:9bef044f45ad | 289 | float getTemperature(void); |
gltest26 | 1:9bef044f45ad | 290 | |
gltest26 | 1:9bef044f45ad | 291 | /** |
gltest26 | 1:9bef044f45ad | 292 | * Get the output for the x-axis gyroscope. |
gltest26 | 1:9bef044f45ad | 293 | * |
gltest26 | 1:9bef044f45ad | 294 | * Typical sensitivity is 14.375 LSB/(degrees/sec). |
gltest26 | 1:9bef044f45ad | 295 | * |
gltest26 | 1:9bef044f45ad | 296 | * @return The output on the x-axis in raw ADC counts. |
gltest26 | 1:9bef044f45ad | 297 | */ |
gltest26 | 4:155c44407af5 | 298 | int getGyroX(void){ return getWord(GYRO_XOUT_H_REG); } |
gltest26 | 1:9bef044f45ad | 299 | |
gltest26 | 1:9bef044f45ad | 300 | /** |
gltest26 | 1:9bef044f45ad | 301 | * Get the output for the y-axis gyroscope. |
gltest26 | 1:9bef044f45ad | 302 | * |
gltest26 | 1:9bef044f45ad | 303 | * Typical sensitivity is 14.375 LSB/(degrees/sec). |
gltest26 | 1:9bef044f45ad | 304 | * |
gltest26 | 1:9bef044f45ad | 305 | * @return The output on the y-axis in raw ADC counts. |
gltest26 | 1:9bef044f45ad | 306 | */ |
gltest26 | 4:155c44407af5 | 307 | int getGyroY(void){ return getWord(GYRO_YOUT_H_REG); } |
gltest26 | 1:9bef044f45ad | 308 | |
gltest26 | 1:9bef044f45ad | 309 | /** |
gltest26 | 1:9bef044f45ad | 310 | * Get the output on the z-axis gyroscope. |
gltest26 | 1:9bef044f45ad | 311 | * |
gltest26 | 1:9bef044f45ad | 312 | * Typical sensitivity is 14.375 LSB/(degrees/sec). |
gltest26 | 1:9bef044f45ad | 313 | * |
gltest26 | 1:9bef044f45ad | 314 | * @return The output on the z-axis in raw ADC counts. |
gltest26 | 1:9bef044f45ad | 315 | */ |
gltest26 | 4:155c44407af5 | 316 | int getGyroZ(void){ return getWord(GYRO_ZOUT_H_REG); } |
gltest26 | 1:9bef044f45ad | 317 | |
gltest26 | 1:9bef044f45ad | 318 | /** |
gltest26 | 1:9bef044f45ad | 319 | * Burst read the outputs on the x,y,z-axis gyroscope. |
gltest26 | 1:9bef044f45ad | 320 | * |
gltest26 | 1:9bef044f45ad | 321 | * Typical sensitivity is 14.375 LSB/(degrees/sec). |
gltest26 | 1:9bef044f45ad | 322 | * |
gltest26 | 1:9bef044f45ad | 323 | * @param readings The output buffer array that has at least 3 length. |
gltest26 | 9:05396b551a9a | 324 | * @param corr Correction method for returned values. |
gltest26 | 1:9bef044f45ad | 325 | */ |
gltest26 | 9:05396b551a9a | 326 | void getGyroXYZ(int readings[3], Correction corr = OffsetCorrection); |
gltest26 | 1:9bef044f45ad | 327 | |
gltest26 | 1:9bef044f45ad | 328 | /** |
gltest26 | 1:9bef044f45ad | 329 | * Burst read the outputs on the x,y,z-axis gyroscope and convert them into degrees per second. |
gltest26 | 1:9bef044f45ad | 330 | * |
gltest26 | 1:9bef044f45ad | 331 | * @param readings The output buffer array that has at least 3 length. |
gltest26 | 9:05396b551a9a | 332 | * @param corr Correction method for returned values. |
gltest26 | 1:9bef044f45ad | 333 | */ |
gltest26 | 9:05396b551a9a | 334 | void getGyroXYZDegrees(double readings[3], Correction corr = OffsetCorrection); |
gltest26 | 1:9bef044f45ad | 335 | |
gltest26 | 1:9bef044f45ad | 336 | /** |
gltest26 | 1:9bef044f45ad | 337 | * Burst read the outputs on the x,y,z-axis gyroscope and convert them into degrees per second. |
gltest26 | 1:9bef044f45ad | 338 | * |
gltest26 | 1:9bef044f45ad | 339 | * @param readings The output buffer array that has at least 3 length. |
gltest26 | 9:05396b551a9a | 340 | * @param corr Correction method for returned values. |
gltest26 | 1:9bef044f45ad | 341 | */ |
gltest26 | 9:05396b551a9a | 342 | void getGyroXYZRadians(double readings[3], Correction corr = OffsetCorrection); |
gltest26 | 1:9bef044f45ad | 343 | |
gltest26 | 1:9bef044f45ad | 344 | /** |
gltest26 | 1:9bef044f45ad | 345 | * Get the power management configuration. |
gltest26 | 1:9bef044f45ad | 346 | * |
gltest26 | 1:9bef044f45ad | 347 | * See the datasheet for register contents details. |
gltest26 | 1:9bef044f45ad | 348 | * |
gltest26 | 1:9bef044f45ad | 349 | * 7 6 5 4 |
gltest26 | 1:9bef044f45ad | 350 | * +---------+-------+---------+---------+ |
gltest26 | 1:9bef044f45ad | 351 | * | H_RESET | SLEEP | STBY_XG | STBY_YG | |
gltest26 | 1:9bef044f45ad | 352 | * +---------+-------+---------+---------+ |
gltest26 | 1:9bef044f45ad | 353 | * |
gltest26 | 1:9bef044f45ad | 354 | * 3 2 1 0 |
gltest26 | 1:9bef044f45ad | 355 | * +---------+----------+----------+----------+ |
gltest26 | 1:9bef044f45ad | 356 | * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 | |
gltest26 | 1:9bef044f45ad | 357 | * +---------+----------+----------+----------+ |
gltest26 | 1:9bef044f45ad | 358 | * |
gltest26 | 1:9bef044f45ad | 359 | * H_RESET Reset device and internal registers to the power-up-default settings. |
gltest26 | 1:9bef044f45ad | 360 | * SLEEP Enable low power sleep mode. |
gltest26 | 1:9bef044f45ad | 361 | * STBY_XG Put gyro X in standby mode (1=standby, 0=normal). |
gltest26 | 1:9bef044f45ad | 362 | * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal). |
gltest26 | 1:9bef044f45ad | 363 | * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal). |
gltest26 | 1:9bef044f45ad | 364 | * CLK_SEL Select device clock source: |
gltest26 | 1:9bef044f45ad | 365 | * |
gltest26 | 1:9bef044f45ad | 366 | * CLK_SEL | Clock Source |
gltest26 | 1:9bef044f45ad | 367 | * --------+-------------- |
gltest26 | 1:9bef044f45ad | 368 | * 0 Internal oscillator |
gltest26 | 1:9bef044f45ad | 369 | * 1 PLL with X Gyro reference |
gltest26 | 1:9bef044f45ad | 370 | * 2 PLL with Y Gyro reference |
gltest26 | 1:9bef044f45ad | 371 | * 3 PLL with Z Gyro reference |
gltest26 | 1:9bef044f45ad | 372 | * 4 PLL with external 32.768kHz reference |
gltest26 | 1:9bef044f45ad | 373 | * 5 PLL with external 19.2MHz reference |
gltest26 | 1:9bef044f45ad | 374 | * 6 Reserved |
gltest26 | 1:9bef044f45ad | 375 | * 7 Reserved |
gltest26 | 1:9bef044f45ad | 376 | * |
gltest26 | 1:9bef044f45ad | 377 | * @return The contents of the PWR_MGM register. |
gltest26 | 1:9bef044f45ad | 378 | */ |
gltest26 | 1:9bef044f45ad | 379 | char getPowerManagement(void); |
gltest26 | 1:9bef044f45ad | 380 | |
gltest26 | 1:9bef044f45ad | 381 | /** |
gltest26 | 1:9bef044f45ad | 382 | * Set power management configuration. |
gltest26 | 1:9bef044f45ad | 383 | * |
gltest26 | 1:9bef044f45ad | 384 | * See the datasheet for configuration byte details |
gltest26 | 1:9bef044f45ad | 385 | * |
gltest26 | 1:9bef044f45ad | 386 | * 7 6 5 4 |
gltest26 | 1:9bef044f45ad | 387 | * +---------+-------+---------+---------+ |
gltest26 | 1:9bef044f45ad | 388 | * | H_RESET | SLEEP | STBY_XG | STBY_YG | |
gltest26 | 1:9bef044f45ad | 389 | * +---------+-------+---------+---------+ |
gltest26 | 1:9bef044f45ad | 390 | * |
gltest26 | 1:9bef044f45ad | 391 | * 3 2 1 0 |
gltest26 | 1:9bef044f45ad | 392 | * +---------+----------+----------+----------+ |
gltest26 | 1:9bef044f45ad | 393 | * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 | |
gltest26 | 1:9bef044f45ad | 394 | * +---------+----------+----------+----------+ |
gltest26 | 1:9bef044f45ad | 395 | * |
gltest26 | 1:9bef044f45ad | 396 | * H_RESET Reset device and internal registers to the power-up-default settings. |
gltest26 | 1:9bef044f45ad | 397 | * SLEEP Enable low power sleep mode. |
gltest26 | 1:9bef044f45ad | 398 | * STBY_XG Put gyro X in standby mode (1=standby, 0=normal). |
gltest26 | 1:9bef044f45ad | 399 | * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal). |
gltest26 | 1:9bef044f45ad | 400 | * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal). |
gltest26 | 1:9bef044f45ad | 401 | * CLK_SEL Select device clock source: |
gltest26 | 1:9bef044f45ad | 402 | * |
gltest26 | 1:9bef044f45ad | 403 | * CLK_SEL | Clock Source |
gltest26 | 1:9bef044f45ad | 404 | * --------+-------------- |
gltest26 | 1:9bef044f45ad | 405 | * 0 Internal oscillator |
gltest26 | 1:9bef044f45ad | 406 | * 1 PLL with X Gyro reference |
gltest26 | 1:9bef044f45ad | 407 | * 2 PLL with Y Gyro reference |
gltest26 | 1:9bef044f45ad | 408 | * 3 PLL with Z Gyro reference |
gltest26 | 1:9bef044f45ad | 409 | * 4 PLL with external 32.768kHz reference |
gltest26 | 1:9bef044f45ad | 410 | * 5 PLL with external 19.2MHz reference |
gltest26 | 1:9bef044f45ad | 411 | * 6 Reserved |
gltest26 | 1:9bef044f45ad | 412 | * 7 Reserved |
gltest26 | 1:9bef044f45ad | 413 | * |
gltest26 | 1:9bef044f45ad | 414 | * @param config The configuration byte to write to the PWR_MGM register. |
gltest26 | 1:9bef044f45ad | 415 | */ |
gltest26 | 1:9bef044f45ad | 416 | void setPowerManagement(char config); |
gltest26 | 3:eea9733ca427 | 417 | |
gltest26 | 3:eea9733ca427 | 418 | /** |
gltest26 | 3:eea9733ca427 | 419 | * Calibrate the sensor drift by sampling zero offset. |
gltest26 | 3:eea9733ca427 | 420 | * |
gltest26 | 3:eea9733ca427 | 421 | * Be sure to keep the sensor stationary while sampling offset. |
gltest26 | 3:eea9733ca427 | 422 | * |
gltest26 | 3:eea9733ca427 | 423 | * Once this function is invoked, following getGyroXYZ*() functions return |
gltest26 | 3:eea9733ca427 | 424 | * corrected values. |
gltest26 | 3:eea9733ca427 | 425 | * |
gltest26 | 3:eea9733ca427 | 426 | * If the drift value changes over time, you can call this function once in a while |
gltest26 | 3:eea9733ca427 | 427 | * to follow it. But don't forget to fix the sensor while calibrating! |
gltest26 | 3:eea9733ca427 | 428 | * |
gltest26 | 3:eea9733ca427 | 429 | * @param time The time span to sample and average offset. |
gltest26 | 3:eea9733ca427 | 430 | * Sampling rate is limited, so giving long time to calibrate will improve |
gltest26 | 3:eea9733ca427 | 431 | * correction quality. |
gltest26 | 3:eea9733ca427 | 432 | */ |
gltest26 | 3:eea9733ca427 | 433 | void calibrate(double time); |
gltest26 | 6:a7ad6046824c | 434 | |
gltest26 | 6:a7ad6046824c | 435 | long getCalibrationSamples()const{ |
gltest26 | 6:a7ad6046824c | 436 | return calibSamples; |
gltest26 | 6:a7ad6046824c | 437 | } |
gltest26 | 3:eea9733ca427 | 438 | |
gltest26 | 7:43b936a53b64 | 439 | /** |
gltest26 | 7:43b936a53b64 | 440 | * Returns the I2C object that this object is using for communication. |
gltest26 | 7:43b936a53b64 | 441 | */ |
gltest26 | 7:43b936a53b64 | 442 | I2C &getI2C(){ |
gltest26 | 7:43b936a53b64 | 443 | return i2c_; |
gltest26 | 7:43b936a53b64 | 444 | } |
gltest26 | 7:43b936a53b64 | 445 | |
gltest26 | 7:43b936a53b64 | 446 | /** |
gltest26 | 7:43b936a53b64 | 447 | * Returns internal offset values for zero adjusting. Returned pointer is pointing an array of 3 elements. |
gltest26 | 7:43b936a53b64 | 448 | */ |
gltest26 | 7:43b936a53b64 | 449 | const int *getOffset()const{ |
gltest26 | 7:43b936a53b64 | 450 | return offset; |
gltest26 | 7:43b936a53b64 | 451 | } |
gltest26 | 7:43b936a53b64 | 452 | |
gltest26 | 3:eea9733ca427 | 453 | protected: |
gltest26 | 4:155c44407af5 | 454 | |
gltest26 | 4:155c44407af5 | 455 | /** |
gltest26 | 4:155c44407af5 | 456 | * Reads a word (2 bytes) from the sensor via I2C bus. |
gltest26 | 4:155c44407af5 | 457 | * |
gltest26 | 4:155c44407af5 | 458 | * The queried value is assumed big-endian, 2's complement value. |
gltest26 | 4:155c44407af5 | 459 | * |
gltest26 | 4:155c44407af5 | 460 | * This protected function is added because we shouldn't write getGyroX(), getGyroY() and getGyroZ() |
gltest26 | 4:155c44407af5 | 461 | * independently, but collect common codes. |
gltest26 | 4:155c44407af5 | 462 | * |
gltest26 | 4:155c44407af5 | 463 | * @param regi Register address to be read. |
gltest26 | 4:155c44407af5 | 464 | */ |
gltest26 | 4:155c44407af5 | 465 | int getWord(int regi); |
gltest26 | 4:155c44407af5 | 466 | |
gltest26 | 3:eea9733ca427 | 467 | /** |
gltest26 | 3:eea9733ca427 | 468 | * An internal method to acquire gyro sensor readings before calibration correction. |
gltest26 | 3:eea9733ca427 | 469 | * |
gltest26 | 3:eea9733ca427 | 470 | * Protected for the time being, although there could be cases that raw values are |
gltest26 | 3:eea9733ca427 | 471 | * appreciated by the user. |
gltest26 | 3:eea9733ca427 | 472 | */ |
gltest26 | 3:eea9733ca427 | 473 | void getRawGyroXYZ(int readings[3]); |
gltest26 | 3:eea9733ca427 | 474 | |
gltest26 | 3:eea9733ca427 | 475 | /** |
gltest26 | 3:eea9733ca427 | 476 | * Offset values that will be subtracted from output. |
gltest26 | 3:eea9733ca427 | 477 | * |
gltest26 | 3:eea9733ca427 | 478 | * TODO: temperature drift calibration |
gltest26 | 3:eea9733ca427 | 479 | */ |
gltest26 | 3:eea9733ca427 | 480 | int offset[3]; |
gltest26 | 6:a7ad6046824c | 481 | |
gltest26 | 9:05396b551a9a | 482 | float foffset[3]; |
gltest26 | 9:05396b551a9a | 483 | float slope[3]; |
gltest26 | 9:05396b551a9a | 484 | |
gltest26 | 6:a7ad6046824c | 485 | long calibSamples; |
gltest26 | 7:43b936a53b64 | 486 | |
gltest26 | 1:9bef044f45ad | 487 | private: |
gltest26 | 1:9bef044f45ad | 488 | |
gltest26 | 8:ac0365ab3cef | 489 | I2C &i2c_; |
gltest26 | 8:ac0365ab3cef | 490 | |
gltest26 | 8:ac0365ab3cef | 491 | /** |
gltest26 | 8:ac0365ab3cef | 492 | * The raw buffer for allocating I2C object in its own without heap memory. |
gltest26 | 8:ac0365ab3cef | 493 | */ |
gltest26 | 8:ac0365ab3cef | 494 | char i2cRaw[sizeof(I2C)]; |
gltest26 | 1:9bef044f45ad | 495 | |
gltest26 | 5:0a0315f0f34e | 496 | /** |
gltest26 | 5:0a0315f0f34e | 497 | * Converts big-endian 2's complement byte pair to native byte order of |
gltest26 | 5:0a0315f0f34e | 498 | * the CPU and then sign extend it to the CPU's register size. |
gltest26 | 5:0a0315f0f34e | 499 | * |
gltest26 | 5:0a0315f0f34e | 500 | * Implemented here to make the compiler inline expand it. |
gltest26 | 5:0a0315f0f34e | 501 | */ |
gltest26 | 5:0a0315f0f34e | 502 | int swapExtend(const char rx[2]){ |
gltest26 | 5:0a0315f0f34e | 503 | // Readings are expressed in 16bit 2's complement, so we must first |
gltest26 | 5:0a0315f0f34e | 504 | // concatenate two bytes to make a word and sign extend it to obtain |
gltest26 | 5:0a0315f0f34e | 505 | // correct negative values. |
gltest26 | 5:0a0315f0f34e | 506 | // ARMCC compiles char as unsigned, which means no sign extension is |
gltest26 | 5:0a0315f0f34e | 507 | // performed during bitwise operations to chars. But we should make sure |
gltest26 | 5:0a0315f0f34e | 508 | // that lower byte won't extend its sign past upper byte for other |
gltest26 | 5:0a0315f0f34e | 509 | // compilers if we want to keep it portable. |
gltest26 | 5:0a0315f0f34e | 510 | return int16_t(((unsigned char)rx[0] << 8) | (unsigned char)rx[1]); |
gltest26 | 5:0a0315f0f34e | 511 | } |
gltest26 | 1:9bef044f45ad | 512 | }; |
gltest26 | 1:9bef044f45ad | 513 | |
gltest26 | 1:9bef044f45ad | 514 | |
gltest26 | 9:05396b551a9a | 515 | inline void ITG3200::getGyroXYZDegrees(double readings[3], Correction corr){ |
gltest26 | 1:9bef044f45ad | 516 | int intData[3]; |
gltest26 | 9:05396b551a9a | 517 | getGyroXYZ(intData, corr); |
gltest26 | 1:9bef044f45ad | 518 | for(int i = 0; i < 3; i++) |
gltest26 | 1:9bef044f45ad | 519 | readings[i] = intData[i] * 2000. / 32767.; |
gltest26 | 1:9bef044f45ad | 520 | } |
gltest26 | 1:9bef044f45ad | 521 | |
gltest26 | 9:05396b551a9a | 522 | inline void ITG3200::getGyroXYZRadians(double readings[3], Correction corr){ |
gltest26 | 1:9bef044f45ad | 523 | int intData[3]; |
gltest26 | 9:05396b551a9a | 524 | getGyroXYZ(intData, corr); |
gltest26 | 1:9bef044f45ad | 525 | for(int i = 0; i < 3; i++) |
gltest26 | 1:9bef044f45ad | 526 | readings[i] = intData[i] * 2000. / 32767. * 2. * M_PI / 360.; |
gltest26 | 1:9bef044f45ad | 527 | } |
gltest26 | 1:9bef044f45ad | 528 | |
gltest26 | 1:9bef044f45ad | 529 | #endif /* ITG3200_H */ |