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
Diff: IMU.cpp
- Revision:
- 0:5a636973285e
- Child:
- 10:85636c7eb8aa
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IMU.cpp Thu Feb 02 07:34:36 2012 +0000 @@ -0,0 +1,105 @@ +/* Atmel Inertial One IMU Library + * File: IMU.H + * + * Copyright (c) 2012 Dan Kouba + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + + #include "IMU.h" + + IMU::IMU(PinName sda, PinName scl) : _i2c(sda, scl) { + + _i2c.frequency(400000); //400kHz bus speed + + //Initial gyro config - must set DLPF[4:3] to 0x03 for proper operation + char poke[2]; + + poke[0] = GYRO_DLPF_REG; + poke[1] = FS_SEL_INIT; + + _i2c.write(GYRO_ADR, poke, 2, false); + + } + + int gyroX(void) { + + char poke = GYRO_XOUT_H_REG; + char peek[2]; + + _i2c.write(GYRO_ADR, &poke, 1, false); + _i2c.read(GYRO_ADR, peek, 2, false); + + int result = ( peek[0] << 8 | peek[1] ); + return result; + + } + + int gyroY(void) { + + char poke = GYRO_YOUT_H_REG; + char peek[2]; + + _i2c.write(GYRO_ADR, &poke, 1, false); + _i2c.read(GYRO_ADR, peek, 2, false); + + int result = ( peek[0] << 8 | peek[1] ); + return result; + + } + + int gyroZ(void){ + + char poke = GYRO_ZOUT_H_REG; + char peek[2]; + + _i2c.write(GYRO_ADR, &poke, 1, false); + _i2c.read(GYRO_ADR, peek, 2, false); + + int result = ( peek[0] << 8 | peek[1] ); + return result; + + } + + int[3] gyroXYZ(void) { + + char poke = GYRO_XOUT_H_REG; + char peek[6]; + + _i2c.write(GYRO_ADR, &poke, 1, false); + _i2c.read(GYRO_ADR, peek, 6, false); + + int result[3] = [ + ( peek[0] << 8 | peek[1] ), // X + ( peek[2] << 8 | peek[3] ), // Y + ( peek[4] << 8 | peek[5] ) // Z + }; + return result; + + } + + void gyroSetLPF(char _BW) { + + char poke[2] = [ GYRO_DLPF_REG, _BW }; + + _i2c.write(GYRO_ADR, poke, 2, false); + + } + \ No newline at end of file