Modified getOffset for calibrating Thermal Drift coefficients.

Dependents:   9Dof_unit_testing

Fork of ITG3200 by James Watanabe

Modified to make getOffset() function easier to use.

Committer:
tylerjw
Date:
Thu Nov 08 20:48:30 2012 +0000
Revision:
15:e61fe4b74daa
Parent:
14:e4bf80188ba7
with calibration curve

Who changed what in which revision?

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