mbed implementation of the FreeIMU IMU for HobbyKing's 10DOF board

Dependents:   testHK10DOF

Committer:
pommzorz
Date:
Wed Jul 17 18:53:37 2013 +0000
Revision:
1:85fcfcb7b137
Parent:
0:9a1682a09c50
oops forgot one file...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pommzorz 0:9a1682a09c50 1 /**
pommzorz 0:9a1682a09c50 2 * Copyright (c) 2011 Pololu Corporation. For more information, see
pommzorz 0:9a1682a09c50 3 *
pommzorz 0:9a1682a09c50 4 * http://www.pololu.com/
pommzorz 0:9a1682a09c50 5 * http://forum.pololu.com/
pommzorz 0:9a1682a09c50 6 *
pommzorz 0:9a1682a09c50 7 * Permission is hereby granted, free of charge, to any person
pommzorz 0:9a1682a09c50 8 * obtaining a copy of this software and associated documentation
pommzorz 0:9a1682a09c50 9 * files (the "Software"), to deal in the Software without
pommzorz 0:9a1682a09c50 10 * restriction, including without limitation the rights to use,
pommzorz 0:9a1682a09c50 11 * copy, modify, merge, publish, distribute, sublicense, and/or sell
pommzorz 0:9a1682a09c50 12 * copies of the Software, and to permit persons to whom the
pommzorz 0:9a1682a09c50 13 * Software is furnished to do so, subject to the following
pommzorz 0:9a1682a09c50 14 * conditions:
pommzorz 0:9a1682a09c50 15 *
pommzorz 0:9a1682a09c50 16 * The above copyright notice and this permission notice shall be
pommzorz 0:9a1682a09c50 17 * included in all copies or substantial portions of the Software.
pommzorz 0:9a1682a09c50 18 *
pommzorz 0:9a1682a09c50 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
pommzorz 0:9a1682a09c50 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
pommzorz 0:9a1682a09c50 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
pommzorz 0:9a1682a09c50 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
pommzorz 0:9a1682a09c50 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
pommzorz 0:9a1682a09c50 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
pommzorz 0:9a1682a09c50 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
pommzorz 0:9a1682a09c50 26 * OTHER DEALINGS IN THE SOFTWARE.
pommzorz 0:9a1682a09c50 27 */
pommzorz 0:9a1682a09c50 28
pommzorz 0:9a1682a09c50 29 #include "mbed.h"
pommzorz 0:9a1682a09c50 30 #include "L3G4200D.h"
pommzorz 0:9a1682a09c50 31 #include <math.h>
pommzorz 0:9a1682a09c50 32
pommzorz 0:9a1682a09c50 33 // Defines ////////////////////////////////////////////////////////////////
pommzorz 0:9a1682a09c50 34
pommzorz 0:9a1682a09c50 35 // The Arduino two-wire interface uses a 7-bit number for the address,
pommzorz 0:9a1682a09c50 36 // and sets the last bit correctly based on reads and writes
pommzorz 0:9a1682a09c50 37 // mbed I2C libraries take the 7-bit address shifted left 1 bit
pommzorz 0:9a1682a09c50 38 // #define GYR_ADDRESS (0xD2 >> 1)
pommzorz 0:9a1682a09c50 39 #define GYR_ADDRESS 0xD2
pommzorz 0:9a1682a09c50 40
pommzorz 0:9a1682a09c50 41 // Public Methods //////////////////////////////////////////////////////////////
pommzorz 0:9a1682a09c50 42
pommzorz 0:9a1682a09c50 43 // Constructor
pommzorz 0:9a1682a09c50 44 L3G4200D::L3G4200D(PinName sda, PinName scl):
pommzorz 0:9a1682a09c50 45 _device(sda, scl)
pommzorz 0:9a1682a09c50 46 {
pommzorz 0:9a1682a09c50 47 _device.frequency(400000);
pommzorz 0:9a1682a09c50 48 // Turns on the L3G4200D's gyro and places it in normal mode.
pommzorz 0:9a1682a09c50 49 // 0x0F = 0b00001111
pommzorz 0:9a1682a09c50 50 // Normal power mode, all axes enabled
pommzorz 0:9a1682a09c50 51 writeReg(L3G4200D_CTRL_REG1, 0x0F);
pommzorz 0:9a1682a09c50 52 writeReg(L3G4200D_CTRL_REG4, 0x20); // 2000 dps full scale
pommzorz 0:9a1682a09c50 53
pommzorz 0:9a1682a09c50 54 setGains(1.0,1.0,1.0);
pommzorz 0:9a1682a09c50 55 setOffsets(0.0,0.0,0.0);
pommzorz 0:9a1682a09c50 56
pommzorz 0:9a1682a09c50 57 }
pommzorz 0:9a1682a09c50 58
pommzorz 0:9a1682a09c50 59 // Writes a gyro register
pommzorz 0:9a1682a09c50 60 void L3G4200D::writeReg(byte reg, byte value)
pommzorz 0:9a1682a09c50 61 {
pommzorz 0:9a1682a09c50 62 data[0] = reg;
pommzorz 0:9a1682a09c50 63 data[1] = value;
pommzorz 0:9a1682a09c50 64
pommzorz 0:9a1682a09c50 65 _device.write(GYR_ADDRESS, data, 2);
pommzorz 0:9a1682a09c50 66 }
pommzorz 0:9a1682a09c50 67
pommzorz 0:9a1682a09c50 68 // Reads a gyro register
pommzorz 0:9a1682a09c50 69 byte L3G4200D::readReg(byte reg)
pommzorz 0:9a1682a09c50 70 {
pommzorz 0:9a1682a09c50 71 byte value = 0;
pommzorz 0:9a1682a09c50 72
pommzorz 0:9a1682a09c50 73 _device.write(GYR_ADDRESS, &reg, 1);
pommzorz 0:9a1682a09c50 74 _device.read(GYR_ADDRESS, &value, 1);
pommzorz 0:9a1682a09c50 75
pommzorz 0:9a1682a09c50 76 return value;
pommzorz 0:9a1682a09c50 77 }
pommzorz 0:9a1682a09c50 78
pommzorz 0:9a1682a09c50 79 void L3G4200D::setGains(float _Xgain, float _Ygain, float _Zgain) {
pommzorz 0:9a1682a09c50 80 gains[0] = _Xgain;
pommzorz 0:9a1682a09c50 81 gains[1] = _Ygain;
pommzorz 0:9a1682a09c50 82 gains[2] = _Zgain;
pommzorz 0:9a1682a09c50 83 }
pommzorz 0:9a1682a09c50 84
pommzorz 0:9a1682a09c50 85 void L3G4200D::setOffsets(int _Xoffset, int _Yoffset, int _Zoffset) {
pommzorz 0:9a1682a09c50 86 offsets[0] = _Xoffset;
pommzorz 0:9a1682a09c50 87 offsets[1] = _Yoffset;
pommzorz 0:9a1682a09c50 88 offsets[2] = _Zoffset;
pommzorz 0:9a1682a09c50 89 }
pommzorz 0:9a1682a09c50 90
pommzorz 0:9a1682a09c50 91 void L3G4200D::setRevPolarity(bool _Xpol, bool _Ypol, bool _Zpol) {
pommzorz 0:9a1682a09c50 92 polarities[0] = _Xpol ? -1 : 1;
pommzorz 0:9a1682a09c50 93 polarities[1] = _Ypol ? -1 : 1;
pommzorz 0:9a1682a09c50 94 polarities[2] = _Zpol ? -1 : 1;
pommzorz 0:9a1682a09c50 95 }
pommzorz 0:9a1682a09c50 96
pommzorz 0:9a1682a09c50 97
pommzorz 0:9a1682a09c50 98 void L3G4200D::zeroCalibrate(unsigned int totSamples, unsigned int sampleDelayMS) {
pommzorz 0:9a1682a09c50 99 int xyz[3];
pommzorz 0:9a1682a09c50 100 float tmpOffsets[] = {0,0,0};
pommzorz 0:9a1682a09c50 101
pommzorz 0:9a1682a09c50 102 for (int i = 0;i < totSamples;i++){
pommzorz 0:9a1682a09c50 103 wait_ms(sampleDelayMS);
pommzorz 0:9a1682a09c50 104 read(xyz);
pommzorz 0:9a1682a09c50 105 tmpOffsets[0] += xyz[0];
pommzorz 0:9a1682a09c50 106 tmpOffsets[1] += xyz[1];
pommzorz 0:9a1682a09c50 107 tmpOffsets[2] += xyz[2];
pommzorz 0:9a1682a09c50 108 }
pommzorz 0:9a1682a09c50 109 setOffsets(-tmpOffsets[0] / totSamples, -tmpOffsets[1] / totSamples, -tmpOffsets[2] / totSamples);
pommzorz 0:9a1682a09c50 110 }
pommzorz 0:9a1682a09c50 111
pommzorz 0:9a1682a09c50 112
pommzorz 0:9a1682a09c50 113 // Reads the 3 gyro channels and stores them in vector g
pommzorz 0:9a1682a09c50 114 void L3G4200D::read(int *g)
pommzorz 0:9a1682a09c50 115 {
pommzorz 0:9a1682a09c50 116 // assert the MSB of the address to get the gyro
pommzorz 0:9a1682a09c50 117 // to do slave-transmit subaddress updating.
pommzorz 0:9a1682a09c50 118 data[0] = L3G4200D_OUT_X_L | (1 << 7);
pommzorz 0:9a1682a09c50 119 _device.write(GYR_ADDRESS, data, 1);
pommzorz 0:9a1682a09c50 120
pommzorz 0:9a1682a09c50 121 // Wire.requestFrom(GYR_ADDRESS, 6);
pommzorz 0:9a1682a09c50 122 // while (Wire.available() < 6);
pommzorz 0:9a1682a09c50 123
pommzorz 0:9a1682a09c50 124 _device.read(GYR_ADDRESS, data, 6);
pommzorz 0:9a1682a09c50 125
pommzorz 0:9a1682a09c50 126 uint8_t xla = data[0];
pommzorz 0:9a1682a09c50 127 uint8_t xha = data[1];
pommzorz 0:9a1682a09c50 128 uint8_t yla = data[2];
pommzorz 0:9a1682a09c50 129 uint8_t yha = data[3];
pommzorz 0:9a1682a09c50 130 uint8_t zla = data[4];
pommzorz 0:9a1682a09c50 131 uint8_t zha = data[5];
pommzorz 0:9a1682a09c50 132
pommzorz 0:9a1682a09c50 133 g[0] = (short) (yha << 8 | yla);
pommzorz 0:9a1682a09c50 134 g[1] = (short) (xha << 8 | xla);
pommzorz 0:9a1682a09c50 135 g[2] = (short) (zha << 8 | zla);
pommzorz 0:9a1682a09c50 136 }
pommzorz 0:9a1682a09c50 137
pommzorz 0:9a1682a09c50 138 void L3G4200D::readRawCal(int *_GyroXYZ) {
pommzorz 0:9a1682a09c50 139 read(_GyroXYZ);
pommzorz 0:9a1682a09c50 140 _GyroXYZ[0] += offsets[0];
pommzorz 0:9a1682a09c50 141 _GyroXYZ[1] += offsets[1];
pommzorz 0:9a1682a09c50 142 _GyroXYZ[2] += offsets[2];
pommzorz 0:9a1682a09c50 143 }
pommzorz 0:9a1682a09c50 144
pommzorz 0:9a1682a09c50 145
pommzorz 0:9a1682a09c50 146
pommzorz 0:9a1682a09c50 147 void L3G4200D::read3(int x, int y, int z) {
pommzorz 0:9a1682a09c50 148 int* readings;
pommzorz 0:9a1682a09c50 149 read(readings);
pommzorz 0:9a1682a09c50 150
pommzorz 0:9a1682a09c50 151 // each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
pommzorz 0:9a1682a09c50 152 // thus we are converting both bytes in to one int
pommzorz 0:9a1682a09c50 153 x = readings[0];
pommzorz 0:9a1682a09c50 154 y = readings[1];
pommzorz 0:9a1682a09c50 155 z = readings[2];
pommzorz 0:9a1682a09c50 156 }
pommzorz 0:9a1682a09c50 157
pommzorz 0:9a1682a09c50 158 void L3G4200D::readFin(float *_GyroXYZ){
pommzorz 0:9a1682a09c50 159 int xyz[3];
pommzorz 0:9a1682a09c50 160
pommzorz 0:9a1682a09c50 161 readRawCal(xyz); // x,y,z will contain calibrated integer values from the sensor
pommzorz 0:9a1682a09c50 162 _GyroXYZ[0] = xyz[0] / 14.375 * polarities[0] * gains[0];
pommzorz 0:9a1682a09c50 163 _GyroXYZ[1] = xyz[1]/ 14.375 * polarities[1] * gains[1];
pommzorz 0:9a1682a09c50 164 _GyroXYZ[2] = xyz[2] / 14.375 * polarities[2] * gains[2];
pommzorz 0:9a1682a09c50 165 }