Interface library for the Atmel Inertial One IMU. Contains drivers for the ITG 3200 3 axis gyro, BMA-150 3 axis accelerometer, and AK8975 3 axis compass
IMU.h@13:eca05448904d, 2012-02-16 (annotated)
- Committer:
- Ductapemaster
- Date:
- Thu Feb 16 08:56:11 2012 +0000
- Revision:
- 13:eca05448904d
- Parent:
- 12:cab3f7305522
Cleaned up and consolidated gyro methods, implemented accelerometer methods, implemented struct for storing 3d data values now getter methods return that struct.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ductapemaster | 12:cab3f7305522 | 1 | /* Atmel Inertial One IMU Library |
Ductapemaster | 12:cab3f7305522 | 2 | * Copyright (c) 2012 Daniel Kouba |
Ductapemaster | 12:cab3f7305522 | 3 | * |
Ductapemaster | 12:cab3f7305522 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Ductapemaster | 12:cab3f7305522 | 5 | * of this software and associated documentation files (the "Software"), to deal |
Ductapemaster | 12:cab3f7305522 | 6 | * in the Software without restriction, including without limitation the rights |
Ductapemaster | 12:cab3f7305522 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Ductapemaster | 12:cab3f7305522 | 8 | * copies of the Software, and to permit persons to whom the Software is |
Ductapemaster | 12:cab3f7305522 | 9 | * furnished to do so, subject to the following conditions: |
Ductapemaster | 12:cab3f7305522 | 10 | * |
Ductapemaster | 12:cab3f7305522 | 11 | * The above copyright notice and this permission notice shall be included in |
Ductapemaster | 12:cab3f7305522 | 12 | * all copies or substantial portions of the Software. |
Ductapemaster | 12:cab3f7305522 | 13 | * |
Ductapemaster | 12:cab3f7305522 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Ductapemaster | 12:cab3f7305522 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Ductapemaster | 12:cab3f7305522 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Ductapemaster | 12:cab3f7305522 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Ductapemaster | 12:cab3f7305522 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Ductapemaster | 12:cab3f7305522 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Ductapemaster | 12:cab3f7305522 | 20 | * THE SOFTWARE. |
Ductapemaster | 12:cab3f7305522 | 21 | */ |
Ductapemaster | 12:cab3f7305522 | 22 | |
Ductapemaster | 12:cab3f7305522 | 23 | #ifndef IMU_AIO_H //IMU Atmel Inertial One |
Ductapemaster | 12:cab3f7305522 | 24 | #define IMU_AIO_H |
Ductapemaster | 12:cab3f7305522 | 25 | |
Ductapemaster | 12:cab3f7305522 | 26 | #include "mbed.h" |
Ductapemaster | 12:cab3f7305522 | 27 | |
Ductapemaster | 12:cab3f7305522 | 28 | /* Defines */ |
Ductapemaster | 12:cab3f7305522 | 29 | |
Ductapemaster | 12:cab3f7305522 | 30 | //7 bit I2C Addresses, shifted left to allow for read/write bit as LSB |
Ductapemaster | 12:cab3f7305522 | 31 | |
Ductapemaster | 12:cab3f7305522 | 32 | #define GYRO_ADR 0x68 << 1 |
Ductapemaster | 12:cab3f7305522 | 33 | #define ACC_ADR 0x38 << 1 |
Ductapemaster | 12:cab3f7305522 | 34 | #define COMP_ADR 0x0C << 1 |
Ductapemaster | 12:cab3f7305522 | 35 | |
Ductapemaster | 12:cab3f7305522 | 36 | // ITG3200 Gyro Registers |
Ductapemaster | 12:cab3f7305522 | 37 | |
Ductapemaster | 12:cab3f7305522 | 38 | #define GYRO_WHO_AM_I_REG 0x00 |
Ductapemaster | 12:cab3f7305522 | 39 | #define GYRO_SAMPLE_DIV_REG 0x15 |
Ductapemaster | 12:cab3f7305522 | 40 | #define GYRO_DLPF_REG 0x16 |
Ductapemaster | 12:cab3f7305522 | 41 | #define GYRO_INT_CFG_REG 0x17 |
Ductapemaster | 12:cab3f7305522 | 42 | #define GYRO_INT_STATUS_REG 0x1A |
Ductapemaster | 12:cab3f7305522 | 43 | #define GYRO_TEMP_H_REG 0x1B |
Ductapemaster | 12:cab3f7305522 | 44 | #define GYRO_TEMP_L_REG 0x1C |
Ductapemaster | 12:cab3f7305522 | 45 | #define GYRO_XOUT_H_REG 0x1D |
Ductapemaster | 12:cab3f7305522 | 46 | #define GYRO_XOUT_L_REG 0x1E |
Ductapemaster | 12:cab3f7305522 | 47 | #define GYRO_YOUT_H_REG 0x1F |
Ductapemaster | 12:cab3f7305522 | 48 | #define GYRO_YOUT_L_REG 0x20 |
Ductapemaster | 12:cab3f7305522 | 49 | #define GYRO_ZOUT_H_REG 0x21 |
Ductapemaster | 12:cab3f7305522 | 50 | #define GYRO_ZOUT_L_REG 0x22 |
Ductapemaster | 12:cab3f7305522 | 51 | |
Ductapemaster | 12:cab3f7305522 | 52 | // Gyro LPF bandwidths |
Ductapemaster | 12:cab3f7305522 | 53 | |
Ductapemaster | 12:cab3f7305522 | 54 | #define BW_256HZ 0x00 //8kHz sample rate |
Ductapemaster | 12:cab3f7305522 | 55 | #define BW_188HZ 0x01 //1kHz sample rate for 188Hz and below |
Ductapemaster | 12:cab3f7305522 | 56 | #define BW_98HZ 0x02 |
Ductapemaster | 12:cab3f7305522 | 57 | #define BW_42HZ 0x03 |
Ductapemaster | 12:cab3f7305522 | 58 | #define BW_20HZ 0x04 |
Ductapemaster | 12:cab3f7305522 | 59 | #define BW_10HZ 0x05 |
Ductapemaster | 12:cab3f7305522 | 60 | #define BW_5HZ 0x06 |
Ductapemaster | 12:cab3f7305522 | 61 | |
Ductapemaster | 12:cab3f7305522 | 62 | // Gyro Initial FS_SEL Register Config |
Ductapemaster | 12:cab3f7305522 | 63 | |
Ductapemaster | 13:eca05448904d | 64 | #define FS_SEL_INIT 0x03 << 3 |
Ductapemaster | 12:cab3f7305522 | 65 | |
Ductapemaster | 12:cab3f7305522 | 66 | // BMA150 Accelerometer Registers |
Ductapemaster | 12:cab3f7305522 | 67 | |
Ductapemaster | 13:eca05448904d | 68 | #define ACC_XOUT_L_REG 0x02 //Acceleration data registers |
Ductapemaster | 13:eca05448904d | 69 | #define ACC_XOUT_H_REG 0x03 |
Ductapemaster | 13:eca05448904d | 70 | #define ACC_YOUT_L_REG 0x04 |
Ductapemaster | 13:eca05448904d | 71 | #define ACC_YOUT_H_REG 0x05 |
Ductapemaster | 13:eca05448904d | 72 | #define ACC_ZOUT_L_REG 0x06 |
Ductapemaster | 13:eca05448904d | 73 | #define ACC_ZOUT_H_REG 0x07 |
Ductapemaster | 13:eca05448904d | 74 | #define ACC_TEMP_REG 0x08 //Temperature register, 0.5 deg. C / LSB |
Ductapemaster | 13:eca05448904d | 75 | #define ACC_CTRL_REG 0x0A //Control registers |
Ductapemaster | 13:eca05448904d | 76 | #define ACC_OPER_REG 0x14 //Operational registers |
Ductapemaster | 13:eca05448904d | 77 | #define ACC_INT_REG 0x15 //Interrupt registers |
Ductapemaster | 12:cab3f7305522 | 78 | |
Ductapemaster | 13:eca05448904d | 79 | // Accelerometer LPF bandwidths |
Ductapemaster | 12:cab3f7305522 | 80 | |
Ductapemaster | 13:eca05448904d | 81 | #define BW_25HZ 0x00 |
Ductapemaster | 13:eca05448904d | 82 | #define BW_50HZ 0x01 |
Ductapemaster | 13:eca05448904d | 83 | #define BW_100HZ 0x02 |
Ductapemaster | 13:eca05448904d | 84 | #define BW_190HZ 0x03 |
Ductapemaster | 13:eca05448904d | 85 | #define BW_375HZ 0x04 |
Ductapemaster | 13:eca05448904d | 86 | #define BW_750HZ 0x05 |
Ductapemaster | 13:eca05448904d | 87 | #define BW_1500HZ 0x06 |
Ductapemaster | 13:eca05448904d | 88 | |
Ductapemaster | 13:eca05448904d | 89 | // Accelerometer Range values |
Ductapemaster | 13:eca05448904d | 90 | |
Ductapemaster | 13:eca05448904d | 91 | #define RANGE_2G 0x00 |
Ductapemaster | 13:eca05448904d | 92 | #define RANGE_4G 0x01 |
Ductapemaster | 13:eca05448904d | 93 | #define RANGE_8G 0x02 |
Ductapemaster | 13:eca05448904d | 94 | |
Ductapemaster | 12:cab3f7305522 | 95 | // AK8973 Compass Registers |
Ductapemaster | 12:cab3f7305522 | 96 | |
Ductapemaster | 12:cab3f7305522 | 97 | |
Ductapemaster | 12:cab3f7305522 | 98 | /** Atmel Inertial One IMU Control Class |
Ductapemaster | 12:cab3f7305522 | 99 | * |
Ductapemaster | 12:cab3f7305522 | 100 | * Includes control routines for: |
Ductapemaster | 12:cab3f7305522 | 101 | * - ITG-3200 3-axis, 16 bit gyroscope |
Ductapemaster | 13:eca05448904d | 102 | * - BMA-150 3-axis, 10 bit accelerometer |
Ductapemaster | 12:cab3f7305522 | 103 | * - AK8975 3-axis, 16 bit magnetometer |
Ductapemaster | 12:cab3f7305522 | 104 | * |
Ductapemaster | 12:cab3f7305522 | 105 | * Datasheets: |
Ductapemaster | 12:cab3f7305522 | 106 | * |
Ductapemaster | 12:cab3f7305522 | 107 | * http://www.atmel.com/dyn/resources/prod_documents/doc8354.pdf |
Ductapemaster | 12:cab3f7305522 | 108 | * |
Ductapemaster | 12:cab3f7305522 | 109 | * http://invensense.com/mems/gyro/documents/PS-ITG-3200A.pdf |
Ductapemaster | 12:cab3f7305522 | 110 | * |
Ductapemaster | 12:cab3f7305522 | 111 | * http://www.bosch-sensortec.com/content/language1/downloads/BMA150_DataSheet_Rev.1.5_30May2008.pdf |
Ductapemaster | 12:cab3f7305522 | 112 | * |
Ductapemaster | 12:cab3f7305522 | 113 | * http://pdf1.alldatasheet.com/datasheet-pdf/view/219477/AKM/AK8973/+Q18W89VYpLawLCDwv+/datasheet.pdf |
Ductapemaster | 12:cab3f7305522 | 114 | * |
Ductapemaster | 12:cab3f7305522 | 115 | */ |
Ductapemaster | 12:cab3f7305522 | 116 | class IMU { |
Ductapemaster | 12:cab3f7305522 | 117 | |
Ductapemaster | 12:cab3f7305522 | 118 | public: |
Ductapemaster | 13:eca05448904d | 119 | struct data3d |
Ductapemaster | 13:eca05448904d | 120 | { |
Ductapemaster | 13:eca05448904d | 121 | int x, y, z; |
Ductapemaster | 13:eca05448904d | 122 | } gyro_data, acc_data, mag_data; |
Ductapemaster | 13:eca05448904d | 123 | |
Ductapemaster | 13:eca05448904d | 124 | public: |
Ductapemaster | 12:cab3f7305522 | 125 | |
Ductapemaster | 12:cab3f7305522 | 126 | /** Creates IMU object and initializes all three chips |
Ductapemaster | 12:cab3f7305522 | 127 | * |
Ductapemaster | 12:cab3f7305522 | 128 | * Gyro: FS_SEL register is set to 0x03, as required by datasheet |
Ductapemaster | 12:cab3f7305522 | 129 | * |
Ductapemaster | 12:cab3f7305522 | 130 | * Accelerometer: |
Ductapemaster | 12:cab3f7305522 | 131 | * |
Ductapemaster | 12:cab3f7305522 | 132 | * Compass: |
Ductapemaster | 12:cab3f7305522 | 133 | * |
Ductapemaster | 12:cab3f7305522 | 134 | * @param sda pin for I2C sda signal |
Ductapemaster | 12:cab3f7305522 | 135 | * @param scl pin for I2C scl signal |
Ductapemaster | 12:cab3f7305522 | 136 | */ |
Ductapemaster | 12:cab3f7305522 | 137 | IMU(PinName sda, PinName scl); |
Ductapemaster | 12:cab3f7305522 | 138 | |
Ductapemaster | 12:cab3f7305522 | 139 | /* Gyro Methods */ |
Ductapemaster | 12:cab3f7305522 | 140 | |
Ductapemaster | 12:cab3f7305522 | 141 | /** Gets current ADC data from all axes of gyro (uses burst read mode) |
Ductapemaster | 12:cab3f7305522 | 142 | * |
Ductapemaster | 13:eca05448904d | 143 | * @return data3d struct of X, Y, and Z axis gyro measurements (signed 16 bits) |
Ductapemaster | 12:cab3f7305522 | 144 | */ |
Ductapemaster | 13:eca05448904d | 145 | data3d getGyroData(void); |
Ductapemaster | 12:cab3f7305522 | 146 | |
Ductapemaster | 13:eca05448904d | 147 | /** Sets digital LPF bandwidth for all gyro channels - Not working currently |
Ductapemaster | 12:cab3f7305522 | 148 | * |
Ductapemaster | 13:eca05448904d | 149 | * @param bw Filter Bandwidth (use defined bandwidths) |
Ductapemaster | 12:cab3f7305522 | 150 | */ |
Ductapemaster | 13:eca05448904d | 151 | void setGyroLPF(char bw); |
Ductapemaster | 12:cab3f7305522 | 152 | |
Ductapemaster | 12:cab3f7305522 | 153 | /* Accelerometer Methods */ |
Ductapemaster | 12:cab3f7305522 | 154 | |
Ductapemaster | 13:eca05448904d | 155 | /** Gets current X acceleration |
Ductapemaster | 13:eca05448904d | 156 | * |
Ductapemaster | 13:eca05448904d | 157 | * @return Raw X acceleration, 10 bits signed |
Ductapemaster | 13:eca05448904d | 158 | */ |
Ductapemaster | 13:eca05448904d | 159 | int accX(void); |
Ductapemaster | 13:eca05448904d | 160 | |
Ductapemaster | 13:eca05448904d | 161 | /** Gets current Y acceleration |
Ductapemaster | 13:eca05448904d | 162 | * |
Ductapemaster | 13:eca05448904d | 163 | * @return Raw Y acceleration, 10 bits signed |
Ductapemaster | 13:eca05448904d | 164 | */ |
Ductapemaster | 13:eca05448904d | 165 | int accY(void); |
Ductapemaster | 13:eca05448904d | 166 | |
Ductapemaster | 13:eca05448904d | 167 | /** Gets current Z acceleration |
Ductapemaster | 13:eca05448904d | 168 | * |
Ductapemaster | 13:eca05448904d | 169 | * @return Raw Z acceleration, 10 bits signed |
Ductapemaster | 13:eca05448904d | 170 | */ |
Ductapemaster | 13:eca05448904d | 171 | int accZ(void); |
Ductapemaster | 12:cab3f7305522 | 172 | |
Ductapemaster | 13:eca05448904d | 173 | /** Gets current acceleration on all axes |
Ductapemaster | 13:eca05448904d | 174 | * |
Ductapemaster | 13:eca05448904d | 175 | * @return data3d struct of acceleration data, signed ints |
Ductapemaster | 13:eca05448904d | 176 | */ |
Ductapemaster | 13:eca05448904d | 177 | data3d getAccData(void); |
Ductapemaster | 13:eca05448904d | 178 | |
Ductapemaster | 13:eca05448904d | 179 | /** Sets digital LPF filter bandwidth |
Ductapemaster | 13:eca05448904d | 180 | * |
Ductapemaster | 13:eca05448904d | 181 | * @param bw Digital LPF filter bandwidth |
Ductapemaster | 13:eca05448904d | 182 | */ |
Ductapemaster | 13:eca05448904d | 183 | void setAccLPF(char bw); |
Ductapemaster | 13:eca05448904d | 184 | |
Ductapemaster | 13:eca05448904d | 185 | /** Sets accelerometer measurement range (+/-2, 4, or 8g's) |
Ductapemaster | 13:eca05448904d | 186 | * |
Ductapemaster | 13:eca05448904d | 187 | * @param range Range of g measurements |
Ductapemaster | 13:eca05448904d | 188 | */ |
Ductapemaster | 13:eca05448904d | 189 | void setAccRange(char range); |
Ductapemaster | 13:eca05448904d | 190 | |
Ductapemaster | 13:eca05448904d | 191 | /** Updates all image registers with data stored in EEPROM |
Ductapemaster | 13:eca05448904d | 192 | * |
Ductapemaster | 13:eca05448904d | 193 | */ |
Ductapemaster | 13:eca05448904d | 194 | void accUpdateImage(void); |
Ductapemaster | 13:eca05448904d | 195 | |
Ductapemaster | 13:eca05448904d | 196 | /** Set EEPROM write enable |
Ductapemaster | 13:eca05448904d | 197 | * |
Ductapemaster | 13:eca05448904d | 198 | * @param we Write enable value |
Ductapemaster | 13:eca05448904d | 199 | */ |
Ductapemaster | 13:eca05448904d | 200 | void accEEWriteEn(bool we); |
Ductapemaster | 12:cab3f7305522 | 201 | |
Ductapemaster | 12:cab3f7305522 | 202 | /* Compass Methods */ |
Ductapemaster | 12:cab3f7305522 | 203 | |
Ductapemaster | 12:cab3f7305522 | 204 | |
Ductapemaster | 12:cab3f7305522 | 205 | |
Ductapemaster | 12:cab3f7305522 | 206 | private: |
Ductapemaster | 12:cab3f7305522 | 207 | |
Ductapemaster | 12:cab3f7305522 | 208 | I2C _i2c; //I2C object constructor |
Ductapemaster | 12:cab3f7305522 | 209 | |
Ductapemaster | 12:cab3f7305522 | 210 | }; |
Ductapemaster | 12:cab3f7305522 | 211 | |
Ductapemaster | 0:5a636973285e | 212 | #endif |