Code for autonomous ground vehicle, Data Bus, 3rd place winner in 2012 Sparkfun AVC.

Dependencies:   Watchdog mbed Schedule SimpleFilter LSM303DLM PinDetect DebounceIn Servo

Committer:
shimniok
Date:
Wed Jun 20 14:57:48 2012 +0000
Revision:
0:826c6171fc1b
Updated documentation

Who changed what in which revision?

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