Forked from Aaron Berk's ITG3200 driver class library, customized for my specific application using 9DoF-Stick by Sparkfun.

Dependents:   HARP

Fork of ITG3200 by Aaron Berk

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

Committer:
gltest26
Date:
Tue Oct 02 17:09:20 2012 +0000
Revision:
8:ac0365ab3cef
Parent:
7:43b936a53b64
Child:
9:05396b551a9a
Added a new constructor to accept an external I2C interface object.

Who changed what in which revision?

UserRevisionLine numberNew 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 1:9bef044f45ad 98
gltest26 1:9bef044f45ad 99 /**
gltest26 1:9bef044f45ad 100 * Constructor.
gltest26 1:9bef044f45ad 101 *
gltest26 1:9bef044f45ad 102 * Sets FS_SEL to 0x03 for proper opertaion.
gltest26 1:9bef044f45ad 103 *
gltest26 1:9bef044f45ad 104 * @param sda - mbed pin to use for the SDA I2C line.
gltest26 1:9bef044f45ad 105 * @param scl - mbed pin to use for the SCL I2C line.
gltest26 7:43b936a53b64 106 * @param fastmode Sets the internal I2C interface to use 400kHz clock.
gltest26 1:9bef044f45ad 107 */
gltest26 7:43b936a53b64 108 ITG3200(PinName sda, PinName scl, bool fastmode = false);
gltest26 8:ac0365ab3cef 109
gltest26 8:ac0365ab3cef 110 /**
gltest26 8:ac0365ab3cef 111 * Constructor that accepts external i2c interface object.
gltest26 8:ac0365ab3cef 112 *
gltest26 8:ac0365ab3cef 113 * @param i2c The I2C interface object to use.
gltest26 8:ac0365ab3cef 114 */
gltest26 8:ac0365ab3cef 115 ITG3200(I2C &i2c) : i2c_(i2c){
gltest26 8:ac0365ab3cef 116 init();
gltest26 8:ac0365ab3cef 117 }
gltest26 8:ac0365ab3cef 118
gltest26 8:ac0365ab3cef 119 ~ITG3200();
gltest26 8:ac0365ab3cef 120
gltest26 8:ac0365ab3cef 121 void init();
gltest26 1:9bef044f45ad 122
gltest26 1:9bef044f45ad 123 /**
gltest26 1:9bef044f45ad 124 * Get the identity of the device.
gltest26 1:9bef044f45ad 125 *
gltest26 1:9bef044f45ad 126 * @return The contents of the Who Am I register which contains the I2C
gltest26 1:9bef044f45ad 127 * address of the device.
gltest26 1:9bef044f45ad 128 */
gltest26 1:9bef044f45ad 129 char getWhoAmI(void);
gltest26 1:9bef044f45ad 130
gltest26 1:9bef044f45ad 131 /**
gltest26 1:9bef044f45ad 132 * Set the address of the device.
gltest26 1:9bef044f45ad 133 *
gltest26 1:9bef044f45ad 134 * @param address The I2C slave address to write to the Who Am I register
gltest26 1:9bef044f45ad 135 * on the device.
gltest26 1:9bef044f45ad 136 */
gltest26 1:9bef044f45ad 137 void setWhoAmI(char address);
gltest26 1:9bef044f45ad 138
gltest26 1:9bef044f45ad 139 /**
gltest26 1:9bef044f45ad 140 * Get the sample rate divider.
gltest26 1:9bef044f45ad 141 *
gltest26 1:9bef044f45ad 142 * @return The sample rate divider as a number from 0-255.
gltest26 1:9bef044f45ad 143 */
gltest26 1:9bef044f45ad 144 char getSampleRateDivider(void);
gltest26 1:9bef044f45ad 145
gltest26 1:9bef044f45ad 146 /**
gltest26 1:9bef044f45ad 147 * Set the sample rate divider.
gltest26 1:9bef044f45ad 148 *
gltest26 1:9bef044f45ad 149 * Fsample = Finternal / (divider + 1), where Finternal = 1kHz or 8kHz,
gltest26 1:9bef044f45ad 150 * as decidied by the DLPF_FS register.
gltest26 1:9bef044f45ad 151 *
gltest26 1:9bef044f45ad 152 * @param The sample rate divider as a number from 0-255.
gltest26 1:9bef044f45ad 153 */
gltest26 1:9bef044f45ad 154 void setSampleRateDivider(char divider);
gltest26 1:9bef044f45ad 155
gltest26 1:9bef044f45ad 156 /**
gltest26 1:9bef044f45ad 157 * Get the internal sample rate.
gltest26 1:9bef044f45ad 158 *
gltest26 1:9bef044f45ad 159 * @return The internal sample rate in kHz - either 1 or 8.
gltest26 1:9bef044f45ad 160 */
gltest26 1:9bef044f45ad 161 int getInternalSampleRate(void);
gltest26 1:9bef044f45ad 162
gltest26 1:9bef044f45ad 163 /**
gltest26 1:9bef044f45ad 164 * Set the low pass filter bandwidth.
gltest26 1:9bef044f45ad 165 *
gltest26 1:9bef044f45ad 166 * Also used to set the internal sample rate.
gltest26 1:9bef044f45ad 167 * Pass the #define bandwidth codes as a parameter.
gltest26 1:9bef044f45ad 168 *
gltest26 1:9bef044f45ad 169 * 256Hz -> 8kHz internal sample rate.
gltest26 1:9bef044f45ad 170 * Everything else -> 1kHz internal rate.
gltest26 1:9bef044f45ad 171 *
gltest26 1:9bef044f45ad 172 * @param bandwidth Low pass filter bandwidth code
gltest26 1:9bef044f45ad 173 */
gltest26 1:9bef044f45ad 174 void setLpBandwidth(char bandwidth);
gltest26 1:9bef044f45ad 175
gltest26 1:9bef044f45ad 176 /**
gltest26 1:9bef044f45ad 177 * Get the interrupt configuration.
gltest26 1:9bef044f45ad 178 *
gltest26 1:9bef044f45ad 179 * See datasheet for register contents details.
gltest26 1:9bef044f45ad 180 *
gltest26 1:9bef044f45ad 181 * 7 6 5 4
gltest26 1:9bef044f45ad 182 * +------+------+--------------+------------------+
gltest26 1:9bef044f45ad 183 * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR |
gltest26 1:9bef044f45ad 184 * +------+------+--------------+------------------+
gltest26 1:9bef044f45ad 185 *
gltest26 1:9bef044f45ad 186 * 3 2 1 0
gltest26 1:9bef044f45ad 187 * +---+------------+------------+---+
gltest26 1:9bef044f45ad 188 * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 |
gltest26 1:9bef044f45ad 189 * +---+------------+------------+---+
gltest26 1:9bef044f45ad 190 *
gltest26 1:9bef044f45ad 191 * ACTL Logic level for INT output pin; 1 = active low, 0 = active high.
gltest26 1:9bef044f45ad 192 * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull.
gltest26 1:9bef044f45ad 193 * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared,
gltest26 1:9bef044f45ad 194 * 0 = 50us pulse.
gltest26 1:9bef044f45ad 195 * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read,
gltest26 1:9bef044f45ad 196 * 0 = status register read only.
gltest26 1:9bef044f45ad 197 * ITG_RDY_EN Enable interrupt when device is ready,
gltest26 1:9bef044f45ad 198 * (PLL ready after changing clock source).
gltest26 1:9bef044f45ad 199 * RAW_RDY_EN Enable interrupt when data is available.
gltest26 1:9bef044f45ad 200 * 0 Bits 1 and 3 of the INT_CFG register should be zero.
gltest26 1:9bef044f45ad 201 *
gltest26 1:9bef044f45ad 202 * @return the contents of the INT_CFG register.
gltest26 1:9bef044f45ad 203 */
gltest26 1:9bef044f45ad 204 char getInterruptConfiguration(void);
gltest26 1:9bef044f45ad 205
gltest26 1:9bef044f45ad 206 /**
gltest26 1:9bef044f45ad 207 * Set the interrupt configuration.
gltest26 1:9bef044f45ad 208 *
gltest26 1:9bef044f45ad 209 * See datasheet for configuration byte details.
gltest26 1:9bef044f45ad 210 *
gltest26 1:9bef044f45ad 211 * 7 6 5 4
gltest26 1:9bef044f45ad 212 * +------+------+--------------+------------------+
gltest26 1:9bef044f45ad 213 * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR |
gltest26 1:9bef044f45ad 214 * +------+------+--------------+------------------+
gltest26 1:9bef044f45ad 215 *
gltest26 1:9bef044f45ad 216 * 3 2 1 0
gltest26 1:9bef044f45ad 217 * +---+------------+------------+---+
gltest26 1:9bef044f45ad 218 * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 |
gltest26 1:9bef044f45ad 219 * +---+------------+------------+---+
gltest26 1:9bef044f45ad 220 *
gltest26 1:9bef044f45ad 221 * ACTL Logic level for INT output pin; 1 = active low, 0 = active high.
gltest26 1:9bef044f45ad 222 * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull.
gltest26 1:9bef044f45ad 223 * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared,
gltest26 1:9bef044f45ad 224 * 0 = 50us pulse.
gltest26 1:9bef044f45ad 225 * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read,
gltest26 1:9bef044f45ad 226 * 0 = status register read only.
gltest26 1:9bef044f45ad 227 * ITG_RDY_EN Enable interrupt when device is ready,
gltest26 1:9bef044f45ad 228 * (PLL ready after changing clock source).
gltest26 1:9bef044f45ad 229 * RAW_RDY_EN Enable interrupt when data is available.
gltest26 1:9bef044f45ad 230 * 0 Bits 1 and 3 of the INT_CFG register should be zero.
gltest26 1:9bef044f45ad 231 *
gltest26 1:9bef044f45ad 232 * @param config Configuration byte to write to INT_CFG register.
gltest26 1:9bef044f45ad 233 */
gltest26 1:9bef044f45ad 234 void setInterruptConfiguration(char config);
gltest26 1:9bef044f45ad 235
gltest26 1:9bef044f45ad 236 /**
gltest26 1:9bef044f45ad 237 * Check the ITG_RDY bit of the INT_STATUS register.
gltest26 1:9bef044f45ad 238 *
gltest26 1:9bef044f45ad 239 * @return True if the ITG_RDY bit is set, corresponding to PLL ready,
gltest26 1:9bef044f45ad 240 * false if the ITG_RDY bit is not set, corresponding to PLL not
gltest26 1:9bef044f45ad 241 * ready.
gltest26 1:9bef044f45ad 242 */
gltest26 1:9bef044f45ad 243 bool isPllReady(void);
gltest26 1:9bef044f45ad 244
gltest26 1:9bef044f45ad 245 /**
gltest26 1:9bef044f45ad 246 * Check the RAW_DATA_RDY bit of the INT_STATUS register.
gltest26 1:9bef044f45ad 247 *
gltest26 1:9bef044f45ad 248 * @return True if the RAW_DATA_RDY bit is set, corresponding to new data
gltest26 1:9bef044f45ad 249 * in the sensor registers, false if the RAW_DATA_RDY bit is not
gltest26 1:9bef044f45ad 250 * set, corresponding to no new data yet in the sensor registers.
gltest26 1:9bef044f45ad 251 */
gltest26 1:9bef044f45ad 252 bool isRawDataReady(void);
gltest26 1:9bef044f45ad 253
gltest26 1:9bef044f45ad 254 /**
gltest26 1:9bef044f45ad 255 * Get the temperature in raw format.
gltest26 1:9bef044f45ad 256 *
gltest26 1:9bef044f45ad 257 * @return The temperature in raw 16bit integer.
gltest26 1:9bef044f45ad 258 */
gltest26 4:155c44407af5 259 int getRawTemperature(void){ return getWord(TEMP_OUT_H_REG); }
gltest26 1:9bef044f45ad 260
gltest26 1:9bef044f45ad 261 /**
gltest26 1:9bef044f45ad 262 * Get the temperature of the device.
gltest26 1:9bef044f45ad 263 *
gltest26 1:9bef044f45ad 264 * @return The temperature in degrees celsius.
gltest26 1:9bef044f45ad 265 */
gltest26 1:9bef044f45ad 266 float getTemperature(void);
gltest26 1:9bef044f45ad 267
gltest26 1:9bef044f45ad 268 /**
gltest26 1:9bef044f45ad 269 * Get the output for the x-axis gyroscope.
gltest26 1:9bef044f45ad 270 *
gltest26 1:9bef044f45ad 271 * Typical sensitivity is 14.375 LSB/(degrees/sec).
gltest26 1:9bef044f45ad 272 *
gltest26 1:9bef044f45ad 273 * @return The output on the x-axis in raw ADC counts.
gltest26 1:9bef044f45ad 274 */
gltest26 4:155c44407af5 275 int getGyroX(void){ return getWord(GYRO_XOUT_H_REG); }
gltest26 1:9bef044f45ad 276
gltest26 1:9bef044f45ad 277 /**
gltest26 1:9bef044f45ad 278 * Get the output for the y-axis gyroscope.
gltest26 1:9bef044f45ad 279 *
gltest26 1:9bef044f45ad 280 * Typical sensitivity is 14.375 LSB/(degrees/sec).
gltest26 1:9bef044f45ad 281 *
gltest26 1:9bef044f45ad 282 * @return The output on the y-axis in raw ADC counts.
gltest26 1:9bef044f45ad 283 */
gltest26 4:155c44407af5 284 int getGyroY(void){ return getWord(GYRO_YOUT_H_REG); }
gltest26 1:9bef044f45ad 285
gltest26 1:9bef044f45ad 286 /**
gltest26 1:9bef044f45ad 287 * Get the output on the z-axis gyroscope.
gltest26 1:9bef044f45ad 288 *
gltest26 1:9bef044f45ad 289 * Typical sensitivity is 14.375 LSB/(degrees/sec).
gltest26 1:9bef044f45ad 290 *
gltest26 1:9bef044f45ad 291 * @return The output on the z-axis in raw ADC counts.
gltest26 1:9bef044f45ad 292 */
gltest26 4:155c44407af5 293 int getGyroZ(void){ return getWord(GYRO_ZOUT_H_REG); }
gltest26 1:9bef044f45ad 294
gltest26 1:9bef044f45ad 295 /**
gltest26 1:9bef044f45ad 296 * Burst read the outputs on the x,y,z-axis gyroscope.
gltest26 1:9bef044f45ad 297 *
gltest26 1:9bef044f45ad 298 * Typical sensitivity is 14.375 LSB/(degrees/sec).
gltest26 1:9bef044f45ad 299 *
gltest26 1:9bef044f45ad 300 * @param readings The output buffer array that has at least 3 length.
gltest26 7:43b936a53b64 301 * @param subtractOffset Make the returned values subtracted of zero offset.
gltest26 1:9bef044f45ad 302 */
gltest26 7:43b936a53b64 303 void getGyroXYZ(int readings[3], bool subtractOffset = true);
gltest26 1:9bef044f45ad 304
gltest26 1:9bef044f45ad 305 /**
gltest26 1:9bef044f45ad 306 * Burst read the outputs on the x,y,z-axis gyroscope and convert them into degrees per second.
gltest26 1:9bef044f45ad 307 *
gltest26 1:9bef044f45ad 308 * @param readings The output buffer array that has at least 3 length.
gltest26 1:9bef044f45ad 309 */
gltest26 1:9bef044f45ad 310 void getGyroXYZDegrees(double readings[3]);
gltest26 1:9bef044f45ad 311
gltest26 1:9bef044f45ad 312 /**
gltest26 1:9bef044f45ad 313 * Burst read the outputs on the x,y,z-axis gyroscope and convert them into degrees per second.
gltest26 1:9bef044f45ad 314 *
gltest26 1:9bef044f45ad 315 * @param readings The output buffer array that has at least 3 length.
gltest26 7:43b936a53b64 316 * @param subtractOffset Make the returned values subtracted of zero offset.
gltest26 1:9bef044f45ad 317 */
gltest26 7:43b936a53b64 318 void getGyroXYZRadians(double readings[3], bool subtractOffset = true);
gltest26 1:9bef044f45ad 319
gltest26 1:9bef044f45ad 320 /**
gltest26 1:9bef044f45ad 321 * Get the power management configuration.
gltest26 1:9bef044f45ad 322 *
gltest26 1:9bef044f45ad 323 * See the datasheet for register contents details.
gltest26 1:9bef044f45ad 324 *
gltest26 1:9bef044f45ad 325 * 7 6 5 4
gltest26 1:9bef044f45ad 326 * +---------+-------+---------+---------+
gltest26 1:9bef044f45ad 327 * | H_RESET | SLEEP | STBY_XG | STBY_YG |
gltest26 1:9bef044f45ad 328 * +---------+-------+---------+---------+
gltest26 1:9bef044f45ad 329 *
gltest26 1:9bef044f45ad 330 * 3 2 1 0
gltest26 1:9bef044f45ad 331 * +---------+----------+----------+----------+
gltest26 1:9bef044f45ad 332 * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
gltest26 1:9bef044f45ad 333 * +---------+----------+----------+----------+
gltest26 1:9bef044f45ad 334 *
gltest26 1:9bef044f45ad 335 * H_RESET Reset device and internal registers to the power-up-default settings.
gltest26 1:9bef044f45ad 336 * SLEEP Enable low power sleep mode.
gltest26 1:9bef044f45ad 337 * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
gltest26 1:9bef044f45ad 338 * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
gltest26 1:9bef044f45ad 339 * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
gltest26 1:9bef044f45ad 340 * CLK_SEL Select device clock source:
gltest26 1:9bef044f45ad 341 *
gltest26 1:9bef044f45ad 342 * CLK_SEL | Clock Source
gltest26 1:9bef044f45ad 343 * --------+--------------
gltest26 1:9bef044f45ad 344 * 0 Internal oscillator
gltest26 1:9bef044f45ad 345 * 1 PLL with X Gyro reference
gltest26 1:9bef044f45ad 346 * 2 PLL with Y Gyro reference
gltest26 1:9bef044f45ad 347 * 3 PLL with Z Gyro reference
gltest26 1:9bef044f45ad 348 * 4 PLL with external 32.768kHz reference
gltest26 1:9bef044f45ad 349 * 5 PLL with external 19.2MHz reference
gltest26 1:9bef044f45ad 350 * 6 Reserved
gltest26 1:9bef044f45ad 351 * 7 Reserved
gltest26 1:9bef044f45ad 352 *
gltest26 1:9bef044f45ad 353 * @return The contents of the PWR_MGM register.
gltest26 1:9bef044f45ad 354 */
gltest26 1:9bef044f45ad 355 char getPowerManagement(void);
gltest26 1:9bef044f45ad 356
gltest26 1:9bef044f45ad 357 /**
gltest26 1:9bef044f45ad 358 * Set power management configuration.
gltest26 1:9bef044f45ad 359 *
gltest26 1:9bef044f45ad 360 * See the datasheet for configuration byte details
gltest26 1:9bef044f45ad 361 *
gltest26 1:9bef044f45ad 362 * 7 6 5 4
gltest26 1:9bef044f45ad 363 * +---------+-------+---------+---------+
gltest26 1:9bef044f45ad 364 * | H_RESET | SLEEP | STBY_XG | STBY_YG |
gltest26 1:9bef044f45ad 365 * +---------+-------+---------+---------+
gltest26 1:9bef044f45ad 366 *
gltest26 1:9bef044f45ad 367 * 3 2 1 0
gltest26 1:9bef044f45ad 368 * +---------+----------+----------+----------+
gltest26 1:9bef044f45ad 369 * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
gltest26 1:9bef044f45ad 370 * +---------+----------+----------+----------+
gltest26 1:9bef044f45ad 371 *
gltest26 1:9bef044f45ad 372 * H_RESET Reset device and internal registers to the power-up-default settings.
gltest26 1:9bef044f45ad 373 * SLEEP Enable low power sleep mode.
gltest26 1:9bef044f45ad 374 * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
gltest26 1:9bef044f45ad 375 * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
gltest26 1:9bef044f45ad 376 * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
gltest26 1:9bef044f45ad 377 * CLK_SEL Select device clock source:
gltest26 1:9bef044f45ad 378 *
gltest26 1:9bef044f45ad 379 * CLK_SEL | Clock Source
gltest26 1:9bef044f45ad 380 * --------+--------------
gltest26 1:9bef044f45ad 381 * 0 Internal oscillator
gltest26 1:9bef044f45ad 382 * 1 PLL with X Gyro reference
gltest26 1:9bef044f45ad 383 * 2 PLL with Y Gyro reference
gltest26 1:9bef044f45ad 384 * 3 PLL with Z Gyro reference
gltest26 1:9bef044f45ad 385 * 4 PLL with external 32.768kHz reference
gltest26 1:9bef044f45ad 386 * 5 PLL with external 19.2MHz reference
gltest26 1:9bef044f45ad 387 * 6 Reserved
gltest26 1:9bef044f45ad 388 * 7 Reserved
gltest26 1:9bef044f45ad 389 *
gltest26 1:9bef044f45ad 390 * @param config The configuration byte to write to the PWR_MGM register.
gltest26 1:9bef044f45ad 391 */
gltest26 1:9bef044f45ad 392 void setPowerManagement(char config);
gltest26 3:eea9733ca427 393
gltest26 3:eea9733ca427 394 /**
gltest26 3:eea9733ca427 395 * Calibrate the sensor drift by sampling zero offset.
gltest26 3:eea9733ca427 396 *
gltest26 3:eea9733ca427 397 * Be sure to keep the sensor stationary while sampling offset.
gltest26 3:eea9733ca427 398 *
gltest26 3:eea9733ca427 399 * Once this function is invoked, following getGyroXYZ*() functions return
gltest26 3:eea9733ca427 400 * corrected values.
gltest26 3:eea9733ca427 401 *
gltest26 3:eea9733ca427 402 * If the drift value changes over time, you can call this function once in a while
gltest26 3:eea9733ca427 403 * to follow it. But don't forget to fix the sensor while calibrating!
gltest26 3:eea9733ca427 404 *
gltest26 3:eea9733ca427 405 * @param time The time span to sample and average offset.
gltest26 3:eea9733ca427 406 * Sampling rate is limited, so giving long time to calibrate will improve
gltest26 3:eea9733ca427 407 * correction quality.
gltest26 3:eea9733ca427 408 */
gltest26 3:eea9733ca427 409 void calibrate(double time);
gltest26 6:a7ad6046824c 410
gltest26 6:a7ad6046824c 411 long getCalibrationSamples()const{
gltest26 6:a7ad6046824c 412 return calibSamples;
gltest26 6:a7ad6046824c 413 }
gltest26 3:eea9733ca427 414
gltest26 7:43b936a53b64 415 /**
gltest26 7:43b936a53b64 416 * Returns the I2C object that this object is using for communication.
gltest26 7:43b936a53b64 417 */
gltest26 7:43b936a53b64 418 I2C &getI2C(){
gltest26 7:43b936a53b64 419 return i2c_;
gltest26 7:43b936a53b64 420 }
gltest26 7:43b936a53b64 421
gltest26 7:43b936a53b64 422 /**
gltest26 7:43b936a53b64 423 * Returns internal offset values for zero adjusting. Returned pointer is pointing an array of 3 elements.
gltest26 7:43b936a53b64 424 */
gltest26 7:43b936a53b64 425 const int *getOffset()const{
gltest26 7:43b936a53b64 426 return offset;
gltest26 7:43b936a53b64 427 }
gltest26 7:43b936a53b64 428
gltest26 3:eea9733ca427 429 protected:
gltest26 4:155c44407af5 430
gltest26 4:155c44407af5 431 /**
gltest26 4:155c44407af5 432 * Reads a word (2 bytes) from the sensor via I2C bus.
gltest26 4:155c44407af5 433 *
gltest26 4:155c44407af5 434 * The queried value is assumed big-endian, 2's complement value.
gltest26 4:155c44407af5 435 *
gltest26 4:155c44407af5 436 * This protected function is added because we shouldn't write getGyroX(), getGyroY() and getGyroZ()
gltest26 4:155c44407af5 437 * independently, but collect common codes.
gltest26 4:155c44407af5 438 *
gltest26 4:155c44407af5 439 * @param regi Register address to be read.
gltest26 4:155c44407af5 440 */
gltest26 4:155c44407af5 441 int getWord(int regi);
gltest26 4:155c44407af5 442
gltest26 3:eea9733ca427 443 /**
gltest26 3:eea9733ca427 444 * An internal method to acquire gyro sensor readings before calibration correction.
gltest26 3:eea9733ca427 445 *
gltest26 3:eea9733ca427 446 * Protected for the time being, although there could be cases that raw values are
gltest26 3:eea9733ca427 447 * appreciated by the user.
gltest26 3:eea9733ca427 448 */
gltest26 3:eea9733ca427 449 void getRawGyroXYZ(int readings[3]);
gltest26 3:eea9733ca427 450
gltest26 3:eea9733ca427 451 /**
gltest26 3:eea9733ca427 452 * Offset values that will be subtracted from output.
gltest26 3:eea9733ca427 453 *
gltest26 3:eea9733ca427 454 * TODO: temperature drift calibration
gltest26 3:eea9733ca427 455 */
gltest26 3:eea9733ca427 456 int offset[3];
gltest26 6:a7ad6046824c 457
gltest26 6:a7ad6046824c 458 long calibSamples;
gltest26 7:43b936a53b64 459
gltest26 1:9bef044f45ad 460 private:
gltest26 1:9bef044f45ad 461
gltest26 8:ac0365ab3cef 462 I2C &i2c_;
gltest26 8:ac0365ab3cef 463
gltest26 8:ac0365ab3cef 464 /**
gltest26 8:ac0365ab3cef 465 * The raw buffer for allocating I2C object in its own without heap memory.
gltest26 8:ac0365ab3cef 466 */
gltest26 8:ac0365ab3cef 467 char i2cRaw[sizeof(I2C)];
gltest26 1:9bef044f45ad 468
gltest26 5:0a0315f0f34e 469 /**
gltest26 5:0a0315f0f34e 470 * Converts big-endian 2's complement byte pair to native byte order of
gltest26 5:0a0315f0f34e 471 * the CPU and then sign extend it to the CPU's register size.
gltest26 5:0a0315f0f34e 472 *
gltest26 5:0a0315f0f34e 473 * Implemented here to make the compiler inline expand it.
gltest26 5:0a0315f0f34e 474 */
gltest26 5:0a0315f0f34e 475 int swapExtend(const char rx[2]){
gltest26 5:0a0315f0f34e 476 // Readings are expressed in 16bit 2's complement, so we must first
gltest26 5:0a0315f0f34e 477 // concatenate two bytes to make a word and sign extend it to obtain
gltest26 5:0a0315f0f34e 478 // correct negative values.
gltest26 5:0a0315f0f34e 479 // ARMCC compiles char as unsigned, which means no sign extension is
gltest26 5:0a0315f0f34e 480 // performed during bitwise operations to chars. But we should make sure
gltest26 5:0a0315f0f34e 481 // that lower byte won't extend its sign past upper byte for other
gltest26 5:0a0315f0f34e 482 // compilers if we want to keep it portable.
gltest26 5:0a0315f0f34e 483 return int16_t(((unsigned char)rx[0] << 8) | (unsigned char)rx[1]);
gltest26 5:0a0315f0f34e 484 }
gltest26 1:9bef044f45ad 485 };
gltest26 1:9bef044f45ad 486
gltest26 1:9bef044f45ad 487
gltest26 7:43b936a53b64 488 inline void ITG3200::getGyroXYZ(int readings[3], bool subtractOffset){
gltest26 3:eea9733ca427 489 getRawGyroXYZ(readings);
gltest26 7:43b936a53b64 490 if(subtractOffset){
gltest26 7:43b936a53b64 491 for(int i = 0; i < 3; i++)
gltest26 7:43b936a53b64 492 readings[i] -= offset[i];
gltest26 7:43b936a53b64 493 }
gltest26 3:eea9733ca427 494 }
gltest26 3:eea9733ca427 495
gltest26 1:9bef044f45ad 496 inline void ITG3200::getGyroXYZDegrees(double readings[3]){
gltest26 1:9bef044f45ad 497 int intData[3];
gltest26 1:9bef044f45ad 498 getGyroXYZ(intData);
gltest26 1:9bef044f45ad 499 for(int i = 0; i < 3; i++)
gltest26 1:9bef044f45ad 500 readings[i] = intData[i] * 2000. / 32767.;
gltest26 1:9bef044f45ad 501 }
gltest26 1:9bef044f45ad 502
gltest26 7:43b936a53b64 503 inline void ITG3200::getGyroXYZRadians(double readings[3], bool subtractOffset){
gltest26 1:9bef044f45ad 504 int intData[3];
gltest26 7:43b936a53b64 505 getGyroXYZ(intData, subtractOffset);
gltest26 1:9bef044f45ad 506 for(int i = 0; i < 3; i++)
gltest26 1:9bef044f45ad 507 readings[i] = intData[i] * 2000. / 32767. * 2. * M_PI / 360.;
gltest26 1:9bef044f45ad 508 }
gltest26 1:9bef044f45ad 509
gltest26 1:9bef044f45ad 510 #endif /* ITG3200_H */