NA

Fork of L3G4200D by Michael Shimniok

Committer:
tedparrott6
Date:
Thu Nov 16 14:59:30 2017 +0000
Revision:
4:44176c2a6121
Parent:
3:14914cd8fdf3
NA

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 3:14914cd8fdf3 1 /**
shimniok 3:14914cd8fdf3 2 * Copyright (c) 2011 Pololu Corporation. For more information, see
shimniok 3:14914cd8fdf3 3 *
shimniok 3:14914cd8fdf3 4 * http://www.pololu.com/
shimniok 3:14914cd8fdf3 5 * http://forum.pololu.com/
shimniok 3:14914cd8fdf3 6 *
shimniok 3:14914cd8fdf3 7 * Permission is hereby granted, free of charge, to any person
shimniok 3:14914cd8fdf3 8 * obtaining a copy of this software and associated documentation
shimniok 3:14914cd8fdf3 9 * files (the "Software"), to deal in the Software without
shimniok 3:14914cd8fdf3 10 * restriction, including without limitation the rights to use,
shimniok 3:14914cd8fdf3 11 * copy, modify, merge, publish, distribute, sublicense, and/or sell
shimniok 3:14914cd8fdf3 12 * copies of the Software, and to permit persons to whom the
shimniok 3:14914cd8fdf3 13 * Software is furnished to do so, subject to the following
shimniok 3:14914cd8fdf3 14 * conditions:
shimniok 3:14914cd8fdf3 15 *
shimniok 3:14914cd8fdf3 16 * The above copyright notice and this permission notice shall be
shimniok 3:14914cd8fdf3 17 * included in all copies or substantial portions of the Software.
shimniok 3:14914cd8fdf3 18 *
shimniok 3:14914cd8fdf3 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
shimniok 3:14914cd8fdf3 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
shimniok 3:14914cd8fdf3 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
shimniok 3:14914cd8fdf3 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
shimniok 3:14914cd8fdf3 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
shimniok 3:14914cd8fdf3 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
shimniok 3:14914cd8fdf3 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
shimniok 3:14914cd8fdf3 26 * OTHER DEALINGS IN THE SOFTWARE.
shimniok 3:14914cd8fdf3 27 */
shimniok 3:14914cd8fdf3 28
shimniok 3:14914cd8fdf3 29 #include "mbed.h"
shimniok 3:14914cd8fdf3 30 #include <L3G4200D.h>
shimniok 3:14914cd8fdf3 31 #include <math.h>
shimniok 3:14914cd8fdf3 32
shimniok 3:14914cd8fdf3 33 // Defines ////////////////////////////////////////////////////////////////
shimniok 3:14914cd8fdf3 34
shimniok 3:14914cd8fdf3 35 // The Arduino two-wire interface uses a 7-bit number for the address,
shimniok 3:14914cd8fdf3 36 // and sets the last bit correctly based on reads and writes
shimniok 3:14914cd8fdf3 37 // mbed I2C libraries take the 7-bit address shifted left 1 bit
shimniok 3:14914cd8fdf3 38 // #define GYR_ADDRESS (0xD2 >> 1)
shimniok 3:14914cd8fdf3 39 #define GYR_ADDRESS 0xD2
shimniok 3:14914cd8fdf3 40
shimniok 3:14914cd8fdf3 41 // Public Methods //////////////////////////////////////////////////////////////
shimniok 3:14914cd8fdf3 42
shimniok 3:14914cd8fdf3 43 // Constructor
shimniok 3:14914cd8fdf3 44 L3G4200D::L3G4200D(PinName sda, PinName scl):
shimniok 3:14914cd8fdf3 45 _device(sda, scl)
shimniok 3:14914cd8fdf3 46 {
shimniok 3:14914cd8fdf3 47 _device.frequency(400000);
shimniok 3:14914cd8fdf3 48 // Turns on the L3G4200D's gyro and places it in normal mode.
shimniok 3:14914cd8fdf3 49 // 0x0F = 0b00001111
shimniok 3:14914cd8fdf3 50 // Normal power mode, all axes enabled
shimniok 3:14914cd8fdf3 51 writeReg(L3G4200D_CTRL_REG1, 0x0F);
shimniok 3:14914cd8fdf3 52 writeReg(L3G4200D_CTRL_REG4, 0x20); // 2000 dps full scale
shimniok 3:14914cd8fdf3 53
shimniok 3:14914cd8fdf3 54 }
shimniok 3:14914cd8fdf3 55
shimniok 3:14914cd8fdf3 56 // Writes a gyro register
shimniok 3:14914cd8fdf3 57 void L3G4200D::writeReg(byte reg, byte value)
shimniok 3:14914cd8fdf3 58 {
shimniok 3:14914cd8fdf3 59 data[0] = reg;
shimniok 3:14914cd8fdf3 60 data[1] = value;
shimniok 3:14914cd8fdf3 61
shimniok 3:14914cd8fdf3 62 _device.write(GYR_ADDRESS, data, 2);
shimniok 3:14914cd8fdf3 63 }
shimniok 3:14914cd8fdf3 64
shimniok 3:14914cd8fdf3 65 // Reads a gyro register
shimniok 3:14914cd8fdf3 66 byte L3G4200D::readReg(byte reg)
shimniok 3:14914cd8fdf3 67 {
shimniok 3:14914cd8fdf3 68 byte value = 0;
shimniok 3:14914cd8fdf3 69
shimniok 3:14914cd8fdf3 70 _device.write(GYR_ADDRESS, &reg, 1);
shimniok 3:14914cd8fdf3 71 _device.read(GYR_ADDRESS, &value, 1);
shimniok 3:14914cd8fdf3 72
shimniok 3:14914cd8fdf3 73 return value;
shimniok 3:14914cd8fdf3 74 }
shimniok 3:14914cd8fdf3 75
shimniok 3:14914cd8fdf3 76 // Reads the 3 gyro channels and stores them in vector g
tedparrott6 4:44176c2a6121 77 void L3G4200D::read(float g[3])
shimniok 3:14914cd8fdf3 78 {
shimniok 3:14914cd8fdf3 79 // assert the MSB of the address to get the gyro
shimniok 3:14914cd8fdf3 80 // to do slave-transmit subaddress updating.
shimniok 3:14914cd8fdf3 81 data[0] = L3G4200D_OUT_X_L | (1 << 7);
shimniok 3:14914cd8fdf3 82 _device.write(GYR_ADDRESS, data, 1);
shimniok 3:14914cd8fdf3 83
shimniok 3:14914cd8fdf3 84 // Wire.requestFrom(GYR_ADDRESS, 6);
shimniok 3:14914cd8fdf3 85 // while (Wire.available() < 6);
shimniok 3:14914cd8fdf3 86
shimniok 3:14914cd8fdf3 87 _device.read(GYR_ADDRESS, data, 6);
shimniok 3:14914cd8fdf3 88
shimniok 3:14914cd8fdf3 89 uint8_t xla = data[0];
shimniok 3:14914cd8fdf3 90 uint8_t xha = data[1];
shimniok 3:14914cd8fdf3 91 uint8_t yla = data[2];
shimniok 3:14914cd8fdf3 92 uint8_t yha = data[3];
shimniok 3:14914cd8fdf3 93 uint8_t zla = data[4];
shimniok 3:14914cd8fdf3 94 uint8_t zha = data[5];
shimniok 3:14914cd8fdf3 95
shimniok 3:14914cd8fdf3 96 g[0] = (short) (xha << 8 | xla);
shimniok 3:14914cd8fdf3 97 g[1] = (short) (yha << 8 | yla);
shimniok 3:14914cd8fdf3 98 g[2] = (short) (zha << 8 | zla);
shimniok 0:6d43e8289cc5 99 }