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

Committer:
Ductapemaster
Date:
Thu Feb 02 07:34:36 2012 +0000
Revision:
0:5a636973285e
Child:
10:85636c7eb8aa
Basic library test #1: Gyro functions for reading axis data and setting low pass filter are implemented.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ductapemaster 0:5a636973285e 1 /* Atmel Inertial One IMU Library
Ductapemaster 0:5a636973285e 2 * File: IMU.H
Ductapemaster 0:5a636973285e 3 *
Ductapemaster 0:5a636973285e 4 * Copyright (c) 2012 Dan Kouba
Ductapemaster 0:5a636973285e 5 *
Ductapemaster 0:5a636973285e 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
Ductapemaster 0:5a636973285e 7 * of this software and associated documentation files (the "Software"), to deal
Ductapemaster 0:5a636973285e 8 * in the Software without restriction, including without limitation the rights
Ductapemaster 0:5a636973285e 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Ductapemaster 0:5a636973285e 10 * copies of the Software, and to permit persons to whom the Software is
Ductapemaster 0:5a636973285e 11 * furnished to do so, subject to the following conditions:
Ductapemaster 0:5a636973285e 12 *
Ductapemaster 0:5a636973285e 13 * The above copyright notice and this permission notice shall be included in
Ductapemaster 0:5a636973285e 14 * all copies or substantial portions of the Software.
Ductapemaster 0:5a636973285e 15 *
Ductapemaster 0:5a636973285e 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Ductapemaster 0:5a636973285e 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Ductapemaster 0:5a636973285e 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Ductapemaster 0:5a636973285e 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Ductapemaster 0:5a636973285e 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Ductapemaster 0:5a636973285e 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Ductapemaster 0:5a636973285e 22 * THE SOFTWARE.
Ductapemaster 0:5a636973285e 23 *
Ductapemaster 0:5a636973285e 24 */
Ductapemaster 0:5a636973285e 25
Ductapemaster 0:5a636973285e 26 #include "IMU.h"
Ductapemaster 0:5a636973285e 27
Ductapemaster 0:5a636973285e 28 IMU::IMU(PinName sda, PinName scl) : _i2c(sda, scl) {
Ductapemaster 0:5a636973285e 29
Ductapemaster 0:5a636973285e 30 _i2c.frequency(400000); //400kHz bus speed
Ductapemaster 0:5a636973285e 31
Ductapemaster 0:5a636973285e 32 //Initial gyro config - must set DLPF[4:3] to 0x03 for proper operation
Ductapemaster 0:5a636973285e 33 char poke[2];
Ductapemaster 0:5a636973285e 34
Ductapemaster 0:5a636973285e 35 poke[0] = GYRO_DLPF_REG;
Ductapemaster 0:5a636973285e 36 poke[1] = FS_SEL_INIT;
Ductapemaster 0:5a636973285e 37
Ductapemaster 0:5a636973285e 38 _i2c.write(GYRO_ADR, poke, 2, false);
Ductapemaster 0:5a636973285e 39
Ductapemaster 0:5a636973285e 40 }
Ductapemaster 0:5a636973285e 41
Ductapemaster 0:5a636973285e 42 int gyroX(void) {
Ductapemaster 0:5a636973285e 43
Ductapemaster 0:5a636973285e 44 char poke = GYRO_XOUT_H_REG;
Ductapemaster 0:5a636973285e 45 char peek[2];
Ductapemaster 0:5a636973285e 46
Ductapemaster 0:5a636973285e 47 _i2c.write(GYRO_ADR, &poke, 1, false);
Ductapemaster 0:5a636973285e 48 _i2c.read(GYRO_ADR, peek, 2, false);
Ductapemaster 0:5a636973285e 49
Ductapemaster 0:5a636973285e 50 int result = ( peek[0] << 8 | peek[1] );
Ductapemaster 0:5a636973285e 51 return result;
Ductapemaster 0:5a636973285e 52
Ductapemaster 0:5a636973285e 53 }
Ductapemaster 0:5a636973285e 54
Ductapemaster 0:5a636973285e 55 int gyroY(void) {
Ductapemaster 0:5a636973285e 56
Ductapemaster 0:5a636973285e 57 char poke = GYRO_YOUT_H_REG;
Ductapemaster 0:5a636973285e 58 char peek[2];
Ductapemaster 0:5a636973285e 59
Ductapemaster 0:5a636973285e 60 _i2c.write(GYRO_ADR, &poke, 1, false);
Ductapemaster 0:5a636973285e 61 _i2c.read(GYRO_ADR, peek, 2, false);
Ductapemaster 0:5a636973285e 62
Ductapemaster 0:5a636973285e 63 int result = ( peek[0] << 8 | peek[1] );
Ductapemaster 0:5a636973285e 64 return result;
Ductapemaster 0:5a636973285e 65
Ductapemaster 0:5a636973285e 66 }
Ductapemaster 0:5a636973285e 67
Ductapemaster 0:5a636973285e 68 int gyroZ(void){
Ductapemaster 0:5a636973285e 69
Ductapemaster 0:5a636973285e 70 char poke = GYRO_ZOUT_H_REG;
Ductapemaster 0:5a636973285e 71 char peek[2];
Ductapemaster 0:5a636973285e 72
Ductapemaster 0:5a636973285e 73 _i2c.write(GYRO_ADR, &poke, 1, false);
Ductapemaster 0:5a636973285e 74 _i2c.read(GYRO_ADR, peek, 2, false);
Ductapemaster 0:5a636973285e 75
Ductapemaster 0:5a636973285e 76 int result = ( peek[0] << 8 | peek[1] );
Ductapemaster 0:5a636973285e 77 return result;
Ductapemaster 0:5a636973285e 78
Ductapemaster 0:5a636973285e 79 }
Ductapemaster 0:5a636973285e 80
Ductapemaster 0:5a636973285e 81 int[3] gyroXYZ(void) {
Ductapemaster 0:5a636973285e 82
Ductapemaster 0:5a636973285e 83 char poke = GYRO_XOUT_H_REG;
Ductapemaster 0:5a636973285e 84 char peek[6];
Ductapemaster 0:5a636973285e 85
Ductapemaster 0:5a636973285e 86 _i2c.write(GYRO_ADR, &poke, 1, false);
Ductapemaster 0:5a636973285e 87 _i2c.read(GYRO_ADR, peek, 6, false);
Ductapemaster 0:5a636973285e 88
Ductapemaster 0:5a636973285e 89 int result[3] = [
Ductapemaster 0:5a636973285e 90 ( peek[0] << 8 | peek[1] ), // X
Ductapemaster 0:5a636973285e 91 ( peek[2] << 8 | peek[3] ), // Y
Ductapemaster 0:5a636973285e 92 ( peek[4] << 8 | peek[5] ) // Z
Ductapemaster 0:5a636973285e 93 };
Ductapemaster 0:5a636973285e 94 return result;
Ductapemaster 0:5a636973285e 95
Ductapemaster 0:5a636973285e 96 }
Ductapemaster 0:5a636973285e 97
Ductapemaster 0:5a636973285e 98 void gyroSetLPF(char _BW) {
Ductapemaster 0:5a636973285e 99
Ductapemaster 0:5a636973285e 100 char poke[2] = [ GYRO_DLPF_REG, _BW };
Ductapemaster 0:5a636973285e 101
Ductapemaster 0:5a636973285e 102 _i2c.write(GYRO_ADR, poke, 2, false);
Ductapemaster 0:5a636973285e 103
Ductapemaster 0:5a636973285e 104 }
Ductapemaster 0:5a636973285e 105