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