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.cpp
- Committer:
- Ductapemaster
- Date:
- 2012-02-02
- Revision:
- 12:cab3f7305522
- Parent:
- 11:a70c76711630
- Child:
- 13:eca05448904d
File content as of revision 12:cab3f7305522:
/* 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 IMU::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); int16_t result = ( peek[0] << 8 | peek[1] ); return result; } int IMU::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); int16_t result = ( peek[0] << 8 | peek[1] ); return result; } int IMU::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); int16_t result = ( peek[0] << 8 | peek[1] ); return result; } int* IMU::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[] = { ( peek[0] << 8 | peek[1] ), // X ( peek[2] << 8 | peek[3] ), // Y ( peek[4] << 8 | peek[5] ) // Z }; return result; } void IMU::gyroSetLPF(char _BW) { char poke[2] = { GYRO_DLPF_REG, _BW }; _i2c.write(GYRO_ADR, poke, 2, false); }