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