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@12:cab3f7305522, 2012-02-02 (annotated)
- Committer:
- Ductapemaster
- Date:
- Thu Feb 02 08:50:58 2012 +0000
- Revision:
- 12:cab3f7305522
- Parent:
- 11:a70c76711630
- Child:
- 13:eca05448904d
Fixed return values for gyroX, gyroY, and gryoZ functions, now return a proper 16 bit signed value. gyroXYZ has not been fixed, and the LPf bandwidth method has not been tested.
Who changed what in which revision?
User | Revision | Line number | New 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 | 11:a70c76711630 | 42 | int IMU::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 | 12:cab3f7305522 | 50 | int16_t 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 | 11:a70c76711630 | 55 | int IMU::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 | 12:cab3f7305522 | 63 | int16_t 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 | 11:a70c76711630 | 68 | int IMU::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 | 12:cab3f7305522 | 76 | int16_t 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 | 11:a70c76711630 | 81 | int* IMU::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 | 12:cab3f7305522 | 89 | int result[] = { |
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 | 11:a70c76711630 | 98 | void IMU::gyroSetLPF(char _BW) { |
Ductapemaster | 0:5a636973285e | 99 | |
Ductapemaster | 12:cab3f7305522 | 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 |