This is my quadcopter prototype software, still in development!

Committer:
Anaesthetix
Date:
Tue Jul 23 14:01:42 2013 +0000
Revision:
1:ac68f0368a77
Parent:
0:978110f7f027
Other accelerometer added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anaesthetix 0:978110f7f027 1 /**
Anaesthetix 0:978110f7f027 2 * @author Aaron Berk
Anaesthetix 0:978110f7f027 3 *
Anaesthetix 0:978110f7f027 4 * @section LICENSE
Anaesthetix 0:978110f7f027 5 *
Anaesthetix 0:978110f7f027 6 * Copyright (c) 2010 ARM Limited
Anaesthetix 0:978110f7f027 7 *
Anaesthetix 0:978110f7f027 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
Anaesthetix 0:978110f7f027 9 * of this software and associated documentation files (the "Software"), to deal
Anaesthetix 0:978110f7f027 10 * in the Software without restriction, including without limitation the rights
Anaesthetix 0:978110f7f027 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Anaesthetix 0:978110f7f027 12 * copies of the Software, and to permit persons to whom the Software is
Anaesthetix 0:978110f7f027 13 * furnished to do so, subject to the following conditions:
Anaesthetix 0:978110f7f027 14 *
Anaesthetix 0:978110f7f027 15 * The above copyright notice and this permission notice shall be included in
Anaesthetix 0:978110f7f027 16 * all copies or substantial portions of the Software.
Anaesthetix 0:978110f7f027 17 *
Anaesthetix 0:978110f7f027 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Anaesthetix 0:978110f7f027 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Anaesthetix 0:978110f7f027 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Anaesthetix 0:978110f7f027 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Anaesthetix 0:978110f7f027 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Anaesthetix 0:978110f7f027 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Anaesthetix 0:978110f7f027 24 * THE SOFTWARE.
Anaesthetix 0:978110f7f027 25 *
Anaesthetix 0:978110f7f027 26 * @section DESCRIPTION
Anaesthetix 0:978110f7f027 27 *
Anaesthetix 0:978110f7f027 28 * ITG-3200 triple axis, digital interface, gyroscope.
Anaesthetix 0:978110f7f027 29 *
Anaesthetix 0:978110f7f027 30 * Datasheet:
Anaesthetix 0:978110f7f027 31 *
Anaesthetix 0:978110f7f027 32 * http://invensense.com/mems/gyro/documents/PS-ITG-3200-00-01.4.pdf
Anaesthetix 0:978110f7f027 33 */
Anaesthetix 0:978110f7f027 34
Anaesthetix 0:978110f7f027 35 /**
Anaesthetix 0:978110f7f027 36 * Includes
Anaesthetix 0:978110f7f027 37 */
Anaesthetix 0:978110f7f027 38
Anaesthetix 0:978110f7f027 39
Anaesthetix 0:978110f7f027 40 #ifndef ITG3200_H
Anaesthetix 0:978110f7f027 41 #define ITG3200_H
Anaesthetix 0:978110f7f027 42 /**
Anaesthetix 0:978110f7f027 43 * Includes
Anaesthetix 0:978110f7f027 44 */
Anaesthetix 0:978110f7f027 45 #include "mbed.h"
Anaesthetix 0:978110f7f027 46
Anaesthetix 0:978110f7f027 47 /**
Anaesthetix 0:978110f7f027 48 * Defines
Anaesthetix 0:978110f7f027 49 */
Anaesthetix 0:978110f7f027 50 #define ITG3200_I2C_ADDRESS 0x68 //7-bit address.
Anaesthetix 0:978110f7f027 51
Anaesthetix 0:978110f7f027 52 //-----------
Anaesthetix 0:978110f7f027 53 // Registers
Anaesthetix 0:978110f7f027 54 //-----------
Anaesthetix 0:978110f7f027 55 #define WHO_AM_I_REG 0x00
Anaesthetix 0:978110f7f027 56 #define SMPLRT_DIV_REG 0x15
Anaesthetix 0:978110f7f027 57 #define DLPF_FS_REG 0x16
Anaesthetix 0:978110f7f027 58 #define INT_CFG_REG 0x17
Anaesthetix 0:978110f7f027 59 #define INT_STATUS 0x1A
Anaesthetix 0:978110f7f027 60 #define TEMP_OUT_H_REG 0x1B
Anaesthetix 0:978110f7f027 61 #define TEMP_OUT_L_REG 0x1C
Anaesthetix 0:978110f7f027 62 #define GYRO_XOUT_H_REG 0x1D
Anaesthetix 0:978110f7f027 63 #define GYRO_XOUT_L_REG 0x1E
Anaesthetix 0:978110f7f027 64 #define GYRO_YOUT_H_REG 0x1F
Anaesthetix 0:978110f7f027 65 #define GYRO_YOUT_L_REG 0x20
Anaesthetix 0:978110f7f027 66 #define GYRO_ZOUT_H_REG 0x21
Anaesthetix 0:978110f7f027 67 #define GYRO_ZOUT_L_REG 0x22
Anaesthetix 0:978110f7f027 68 #define PWR_MGM_REG 0x3E
Anaesthetix 0:978110f7f027 69
Anaesthetix 0:978110f7f027 70 //----------------------------
Anaesthetix 0:978110f7f027 71 // Low Pass Filter Bandwidths
Anaesthetix 0:978110f7f027 72 //----------------------------
Anaesthetix 0:978110f7f027 73 #define LPFBW_256HZ 0x00
Anaesthetix 0:978110f7f027 74 #define LPFBW_188HZ 0x01
Anaesthetix 0:978110f7f027 75 #define LPFBW_98HZ 0x02
Anaesthetix 0:978110f7f027 76 #define LPFBW_42HZ 0x03
Anaesthetix 0:978110f7f027 77 #define LPFBW_20HZ 0x04
Anaesthetix 0:978110f7f027 78 #define LPFBW_10HZ 0x05
Anaesthetix 0:978110f7f027 79 #define LPFBW_5HZ 0x06
Anaesthetix 0:978110f7f027 80 /**
Anaesthetix 0:978110f7f027 81 * ITG-3200 triple axis digital gyroscope.
Anaesthetix 0:978110f7f027 82 */
Anaesthetix 0:978110f7f027 83 class ITG3200 {
Anaesthetix 0:978110f7f027 84 public:
Anaesthetix 0:978110f7f027 85 /**
Anaesthetix 0:978110f7f027 86 * Constructor.
Anaesthetix 0:978110f7f027 87 *
Anaesthetix 0:978110f7f027 88 * Sets FS_SEL to 0x03 for proper opertaion.
Anaesthetix 0:978110f7f027 89 *
Anaesthetix 0:978110f7f027 90 * @param sda - mbed pin to use for the SDA I2C line.
Anaesthetix 0:978110f7f027 91 * @param scl - mbed pin to use for the SCL I2C line.
Anaesthetix 0:978110f7f027 92 */
Anaesthetix 0:978110f7f027 93 ITG3200(PinName sda, PinName scl);
Anaesthetix 0:978110f7f027 94 /**
Anaesthetix 0:978110f7f027 95 * Get the identity of the device.
Anaesthetix 0:978110f7f027 96 *
Anaesthetix 0:978110f7f027 97 * @return The contents of the Who Am I register which contains the I2C
Anaesthetix 0:978110f7f027 98 * address of the device.
Anaesthetix 0:978110f7f027 99 */
Anaesthetix 0:978110f7f027 100 char getWhoAmI(void);
Anaesthetix 0:978110f7f027 101 /**
Anaesthetix 0:978110f7f027 102 * Set the address of the device.
Anaesthetix 0:978110f7f027 103 *
Anaesthetix 0:978110f7f027 104 * @param address The I2C slave address to write to the Who Am I register
Anaesthetix 0:978110f7f027 105 * on the device.
Anaesthetix 0:978110f7f027 106 */
Anaesthetix 0:978110f7f027 107 void setWhoAmI(char address);
Anaesthetix 0:978110f7f027 108 /**
Anaesthetix 0:978110f7f027 109 * Get the sample rate divider.
Anaesthetix 0:978110f7f027 110 *
Anaesthetix 0:978110f7f027 111 * @return The sample rate divider as a number from 0-255.
Anaesthetix 0:978110f7f027 112 */
Anaesthetix 0:978110f7f027 113 char getSampleRateDivider(void);
Anaesthetix 0:978110f7f027 114 /**
Anaesthetix 0:978110f7f027 115 * Set the sample rate divider.
Anaesthetix 0:978110f7f027 116 *
Anaesthetix 0:978110f7f027 117 * Fsample = Finternal / (divider + 1), where Finternal = 1kHz or 8kHz,
Anaesthetix 0:978110f7f027 118 * as decidied by the DLPF_FS register.
Anaesthetix 0:978110f7f027 119 *
Anaesthetix 0:978110f7f027 120 * @param The sample rate divider as a number from 0-255.
Anaesthetix 0:978110f7f027 121 */
Anaesthetix 0:978110f7f027 122 void setSampleRateDivider(char divider);
Anaesthetix 0:978110f7f027 123 /**
Anaesthetix 0:978110f7f027 124 * Get the internal sample rate.
Anaesthetix 0:978110f7f027 125 *
Anaesthetix 0:978110f7f027 126 * @return The internal sample rate in kHz - either 1 or 8.
Anaesthetix 0:978110f7f027 127 */
Anaesthetix 0:978110f7f027 128 int getInternalSampleRate(void);
Anaesthetix 0:978110f7f027 129
Anaesthetix 0:978110f7f027 130 void setLpBandwidth(char bandwidth);
Anaesthetix 0:978110f7f027 131 /**
Anaesthetix 0:978110f7f027 132 * Get the interrupt configuration.
Anaesthetix 0:978110f7f027 133 *
Anaesthetix 0:978110f7f027 134 * See datasheet for register contents details.
Anaesthetix 0:978110f7f027 135 *
Anaesthetix 0:978110f7f027 136 * 7 6 5 4
Anaesthetix 0:978110f7f027 137 * +------+------+--------------+------------------+
Anaesthetix 0:978110f7f027 138 * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR |
Anaesthetix 0:978110f7f027 139 * +------+------+--------------+------------------+
Anaesthetix 0:978110f7f027 140 *
Anaesthetix 0:978110f7f027 141 * 3 2 1 0
Anaesthetix 0:978110f7f027 142 * +---+------------+------------+---+
Anaesthetix 0:978110f7f027 143 * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 |
Anaesthetix 0:978110f7f027 144 * +---+------------+------------+---+
Anaesthetix 0:978110f7f027 145 *
Anaesthetix 0:978110f7f027 146 * ACTL Logic level for INT output pin; 1 = active low, 0 = active high.
Anaesthetix 0:978110f7f027 147 * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull.
Anaesthetix 0:978110f7f027 148 * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared,
Anaesthetix 0:978110f7f027 149 * 0 = 50us pulse.
Anaesthetix 0:978110f7f027 150 * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read,
Anaesthetix 0:978110f7f027 151 * 0 = status register read only.
Anaesthetix 0:978110f7f027 152 * ITG_RDY_EN Enable interrupt when device is ready,
Anaesthetix 0:978110f7f027 153 * (PLL ready after changing clock source).
Anaesthetix 0:978110f7f027 154 * RAW_RDY_EN Enable interrupt when data is available.
Anaesthetix 0:978110f7f027 155 * 0 Bits 1 and 3 of the INT_CFG register should be zero.
Anaesthetix 0:978110f7f027 156 *
Anaesthetix 0:978110f7f027 157 * @return the contents of the INT_CFG register.
Anaesthetix 0:978110f7f027 158 */
Anaesthetix 0:978110f7f027 159 char getInterruptConfiguration(void);
Anaesthetix 0:978110f7f027 160 /**
Anaesthetix 0:978110f7f027 161 * Set the interrupt configuration.
Anaesthetix 0:978110f7f027 162 *
Anaesthetix 0:978110f7f027 163 * See datasheet for configuration byte details.
Anaesthetix 0:978110f7f027 164 *
Anaesthetix 0:978110f7f027 165 * 7 6 5 4
Anaesthetix 0:978110f7f027 166 * +------+------+--------------+------------------+
Anaesthetix 0:978110f7f027 167 * | ACTL | OPEN | LATCH_INT_EN | INT_ANYRD_2CLEAR |
Anaesthetix 0:978110f7f027 168 * +------+------+--------------+------------------+
Anaesthetix 0:978110f7f027 169 *
Anaesthetix 0:978110f7f027 170 * 3 2 1 0
Anaesthetix 0:978110f7f027 171 * +---+------------+------------+---+
Anaesthetix 0:978110f7f027 172 * | 0 | ITG_RDY_EN | RAW_RDY_EN | 0 |
Anaesthetix 0:978110f7f027 173 * +---+------------+------------+---+
Anaesthetix 0:978110f7f027 174 *
Anaesthetix 0:978110f7f027 175 * ACTL Logic level for INT output pin; 1 = active low, 0 = active high.
Anaesthetix 0:978110f7f027 176 * OPEN Drive type for INT output pin; 1 = open drain, 0 = push-pull.
Anaesthetix 0:978110f7f027 177 * LATCH_INT_EN Latch mode; 1 = latch until interrupt is cleared,
Anaesthetix 0:978110f7f027 178 * 0 = 50us pulse.
Anaesthetix 0:978110f7f027 179 * INT_ANYRD_2CLEAR Latch clear method; 1 = any register read,
Anaesthetix 0:978110f7f027 180 * 0 = status register read only.
Anaesthetix 0:978110f7f027 181 * ITG_RDY_EN Enable interrupt when device is ready,
Anaesthetix 0:978110f7f027 182 * (PLL ready after changing clock source).
Anaesthetix 0:978110f7f027 183 * RAW_RDY_EN Enable interrupt when data is available.
Anaesthetix 0:978110f7f027 184 * 0 Bits 1 and 3 of the INT_CFG register should be zero.
Anaesthetix 0:978110f7f027 185 *
Anaesthetix 0:978110f7f027 186 * @param config Configuration byte to write to INT_CFG register.
Anaesthetix 0:978110f7f027 187 */
Anaesthetix 0:978110f7f027 188 void setInterruptConfiguration(char config);
Anaesthetix 0:978110f7f027 189 /**
Anaesthetix 0:978110f7f027 190 * Check the ITG_RDY bit of the INT_STATUS register.
Anaesthetix 0:978110f7f027 191 *
Anaesthetix 0:978110f7f027 192 * @return True if the ITG_RDY bit is set, corresponding to PLL ready,
Anaesthetix 0:978110f7f027 193 * false if the ITG_RDY bit is not set, corresponding to PLL not
Anaesthetix 0:978110f7f027 194 * ready.
Anaesthetix 0:978110f7f027 195 */
Anaesthetix 0:978110f7f027 196 bool isPllReady(void);
Anaesthetix 0:978110f7f027 197
Anaesthetix 0:978110f7f027 198 bool isRawDataReady(void);
Anaesthetix 0:978110f7f027 199 /**
Anaesthetix 0:978110f7f027 200 * Get the temperature of the device.
Anaesthetix 0:978110f7f027 201 *
Anaesthetix 0:978110f7f027 202 * @return The temperature in degrees celsius.
Anaesthetix 0:978110f7f027 203 */
Anaesthetix 0:978110f7f027 204 float getTemperature(void);
Anaesthetix 0:978110f7f027 205 /**
Anaesthetix 0:978110f7f027 206 * Get the output for the x-axis gyroscope.
Anaesthetix 0:978110f7f027 207 *
Anaesthetix 0:978110f7f027 208 * Typical sensitivity is 14.375 LSB/(degrees/sec).
Anaesthetix 0:978110f7f027 209 *
Anaesthetix 0:978110f7f027 210 * @return The output on the x-axis in raw ADC counts.
Anaesthetix 0:978110f7f027 211 */
Anaesthetix 0:978110f7f027 212 int getGyroX(void);
Anaesthetix 0:978110f7f027 213 /**
Anaesthetix 0:978110f7f027 214 * Get the output for the y-axis gyroscope.
Anaesthetix 0:978110f7f027 215 *
Anaesthetix 0:978110f7f027 216 * Typical sensitivity is 14.375 LSB/(degrees/sec).
Anaesthetix 0:978110f7f027 217 *
Anaesthetix 0:978110f7f027 218 * @return The output on the y-axis in raw ADC counts.
Anaesthetix 0:978110f7f027 219 */
Anaesthetix 0:978110f7f027 220 int getGyroY(void);
Anaesthetix 0:978110f7f027 221
Anaesthetix 0:978110f7f027 222 int getGyroZ(void);
Anaesthetix 0:978110f7f027 223 /**
Anaesthetix 0:978110f7f027 224 * Get the power management configuration.
Anaesthetix 0:978110f7f027 225 *
Anaesthetix 0:978110f7f027 226 * See the datasheet for register contents details.
Anaesthetix 0:978110f7f027 227 *
Anaesthetix 0:978110f7f027 228 * 7 6 5 4
Anaesthetix 0:978110f7f027 229 * +---------+-------+---------+---------+
Anaesthetix 0:978110f7f027 230 * | H_RESET | SLEEP | STBY_XG | STBY_YG |
Anaesthetix 0:978110f7f027 231 * +---------+-------+---------+---------+
Anaesthetix 0:978110f7f027 232 *
Anaesthetix 0:978110f7f027 233 * 3 2 1 0
Anaesthetix 0:978110f7f027 234 * +---------+----------+----------+----------+
Anaesthetix 0:978110f7f027 235 * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
Anaesthetix 0:978110f7f027 236 * +---------+----------+----------+----------+
Anaesthetix 0:978110f7f027 237 *
Anaesthetix 0:978110f7f027 238 * H_RESET Reset device and internal registers to the power-up-default settings.
Anaesthetix 0:978110f7f027 239 * SLEEP Enable low power sleep mode.
Anaesthetix 0:978110f7f027 240 * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
Anaesthetix 0:978110f7f027 241 * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
Anaesthetix 0:978110f7f027 242 * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
Anaesthetix 0:978110f7f027 243 * CLK_SEL Select device clock source:
Anaesthetix 0:978110f7f027 244 *
Anaesthetix 0:978110f7f027 245 * CLK_SEL | Clock Source
Anaesthetix 0:978110f7f027 246 * --------+--------------
Anaesthetix 0:978110f7f027 247 * 0 Internal oscillator
Anaesthetix 0:978110f7f027 248 * 1 PLL with X Gyro reference
Anaesthetix 0:978110f7f027 249 * 2 PLL with Y Gyro reference
Anaesthetix 0:978110f7f027 250 * 3 PLL with Z Gyro reference
Anaesthetix 0:978110f7f027 251 * 4 PLL with external 32.768kHz reference
Anaesthetix 0:978110f7f027 252 * 5 PLL with external 19.2MHz reference
Anaesthetix 0:978110f7f027 253 * 6 Reserved
Anaesthetix 0:978110f7f027 254 * 7 Reserved
Anaesthetix 0:978110f7f027 255 *
Anaesthetix 0:978110f7f027 256 * @return The contents of the PWR_MGM register.
Anaesthetix 0:978110f7f027 257 */
Anaesthetix 0:978110f7f027 258 char getPowerManagement(void);
Anaesthetix 0:978110f7f027 259 /**
Anaesthetix 0:978110f7f027 260 * Set power management configuration.
Anaesthetix 0:978110f7f027 261 *
Anaesthetix 0:978110f7f027 262 * See the datasheet for configuration byte details
Anaesthetix 0:978110f7f027 263 *
Anaesthetix 0:978110f7f027 264 * 7 6 5 4
Anaesthetix 0:978110f7f027 265 * +---------+-------+---------+---------+
Anaesthetix 0:978110f7f027 266 * | H_RESET | SLEEP | STBY_XG | STBY_YG |
Anaesthetix 0:978110f7f027 267 * +---------+-------+---------+---------+
Anaesthetix 0:978110f7f027 268 *
Anaesthetix 0:978110f7f027 269 * 3 2 1 0
Anaesthetix 0:978110f7f027 270 * +---------+----------+----------+----------+
Anaesthetix 0:978110f7f027 271 * | STBY_ZG | CLK_SEL2 | CLK_SEL1 | CLK_SEL0 |
Anaesthetix 0:978110f7f027 272 * +---------+----------+----------+----------+
Anaesthetix 0:978110f7f027 273 *
Anaesthetix 0:978110f7f027 274 * H_RESET Reset device and internal registers to the power-up-default settings.
Anaesthetix 0:978110f7f027 275 * SLEEP Enable low power sleep mode.
Anaesthetix 0:978110f7f027 276 * STBY_XG Put gyro X in standby mode (1=standby, 0=normal).
Anaesthetix 0:978110f7f027 277 * STBY_YG Put gyro Y in standby mode (1=standby, 0=normal).
Anaesthetix 0:978110f7f027 278 * STBY_ZG Put gyro Z in standby mode (1=standby, 0=normal).
Anaesthetix 0:978110f7f027 279 * CLK_SEL Select device clock source:
Anaesthetix 0:978110f7f027 280 *
Anaesthetix 0:978110f7f027 281 * CLK_SEL | Clock Source
Anaesthetix 0:978110f7f027 282 * --------+--------------
Anaesthetix 0:978110f7f027 283 * 0 Internal oscillator
Anaesthetix 0:978110f7f027 284 * 1 PLL with X Gyro reference
Anaesthetix 0:978110f7f027 285 * 2 PLL with Y Gyro reference
Anaesthetix 0:978110f7f027 286 * 3 PLL with Z Gyro reference
Anaesthetix 0:978110f7f027 287 * 4 PLL with external 32.768kHz reference
Anaesthetix 0:978110f7f027 288 * 5 PLL with external 19.2MHz reference
Anaesthetix 0:978110f7f027 289 * 6 Reserved
Anaesthetix 0:978110f7f027 290 * 7 Reserved
Anaesthetix 0:978110f7f027 291 *
Anaesthetix 0:978110f7f027 292 * @param config The configuration byte to write to the PWR_MGM register.
Anaesthetix 0:978110f7f027 293 */
Anaesthetix 0:978110f7f027 294 void setPowerManagement(char config);
Anaesthetix 0:978110f7f027 295 private:
Anaesthetix 0:978110f7f027 296 I2C i2c_;
Anaesthetix 0:978110f7f027 297 };
Anaesthetix 0:978110f7f027 298 #endif /* ITG3200_H */