Polo Gyro
Fork of L3GD20 by
L3GD20.cpp@1:c7a53a17adef, 2015-04-05 (annotated)
- Committer:
- Spilly
- Date:
- Sun Apr 05 01:41:05 2015 +0000
- Revision:
- 1:c7a53a17adef
- Parent:
- 0:62dfce144cf7
Changes to format
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bclaus | 0:62dfce144cf7 | 1 | /** |
bclaus | 0:62dfce144cf7 | 2 | * Copyright (c) 2011 Pololu Corporation. For more information, see |
bclaus | 0:62dfce144cf7 | 3 | * |
bclaus | 0:62dfce144cf7 | 4 | * http://www.pololu.com/ |
bclaus | 0:62dfce144cf7 | 5 | * http://forum.pololu.com/ |
bclaus | 0:62dfce144cf7 | 6 | * |
bclaus | 0:62dfce144cf7 | 7 | * Permission is hereby granted, free of charge, to any person |
bclaus | 0:62dfce144cf7 | 8 | * obtaining a copy of this software and associated documentation |
bclaus | 0:62dfce144cf7 | 9 | * files (the "Software"), to deal in the Software without |
bclaus | 0:62dfce144cf7 | 10 | * restriction, including without limitation the rights to use, |
bclaus | 0:62dfce144cf7 | 11 | * copy, modify, merge, publish, distribute, sublicense, and/or sell |
bclaus | 0:62dfce144cf7 | 12 | * copies of the Software, and to permit persons to whom the |
bclaus | 0:62dfce144cf7 | 13 | * Software is furnished to do so, subject to the following |
bclaus | 0:62dfce144cf7 | 14 | * conditions: |
bclaus | 0:62dfce144cf7 | 15 | * |
bclaus | 0:62dfce144cf7 | 16 | * The above copyright notice and this permission notice shall be |
bclaus | 0:62dfce144cf7 | 17 | * included in all copies or substantial portions of the Software. |
bclaus | 0:62dfce144cf7 | 18 | * |
bclaus | 0:62dfce144cf7 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
bclaus | 0:62dfce144cf7 | 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
bclaus | 0:62dfce144cf7 | 21 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
bclaus | 0:62dfce144cf7 | 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
bclaus | 0:62dfce144cf7 | 23 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
bclaus | 0:62dfce144cf7 | 24 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
bclaus | 0:62dfce144cf7 | 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
bclaus | 0:62dfce144cf7 | 26 | * OTHER DEALINGS IN THE SOFTWARE. |
bclaus | 0:62dfce144cf7 | 27 | */ |
bclaus | 0:62dfce144cf7 | 28 | |
Spilly | 1:c7a53a17adef | 29 | #include "mbed.h" |
Spilly | 1:c7a53a17adef | 30 | #include "L3GD20.h" |
Spilly | 1:c7a53a17adef | 31 | |
bclaus | 0:62dfce144cf7 | 32 | #define GYR_ADDRESS 0xD6 |
bclaus | 0:62dfce144cf7 | 33 | |
bclaus | 0:62dfce144cf7 | 34 | |
bclaus | 0:62dfce144cf7 | 35 | // Public Methods ////////////////////////////////////////////////////////////// |
bclaus | 0:62dfce144cf7 | 36 | |
bclaus | 0:62dfce144cf7 | 37 | // Constructor |
bclaus | 0:62dfce144cf7 | 38 | L3GD20::L3GD20(PinName sda, PinName scl): |
bclaus | 0:62dfce144cf7 | 39 | _L3GD20(sda, scl) |
bclaus | 0:62dfce144cf7 | 40 | { |
bclaus | 0:62dfce144cf7 | 41 | char reg_v; |
Spilly | 1:c7a53a17adef | 42 | _L3GD20.frequency(400000); |
bclaus | 0:62dfce144cf7 | 43 | |
bclaus | 0:62dfce144cf7 | 44 | // 0x0F = 0b00001111 |
bclaus | 0:62dfce144cf7 | 45 | // Normal power mode, all axes enabled |
bclaus | 0:62dfce144cf7 | 46 | reg_v = 0; |
bclaus | 0:62dfce144cf7 | 47 | reg_v |= 0x0F; |
bclaus | 0:62dfce144cf7 | 48 | write_reg(GYR_ADDRESS,L3GD20_CTRL_REG1,reg_v); |
bclaus | 0:62dfce144cf7 | 49 | } |
bclaus | 0:62dfce144cf7 | 50 | |
bclaus | 0:62dfce144cf7 | 51 | |
bclaus | 0:62dfce144cf7 | 52 | |
bclaus | 0:62dfce144cf7 | 53 | bool L3GD20::read(float *gx, float *gy, float *gz) { |
bclaus | 0:62dfce144cf7 | 54 | char gyr[6]; |
bclaus | 0:62dfce144cf7 | 55 | |
bclaus | 0:62dfce144cf7 | 56 | if (recv(GYR_ADDRESS, L3GD20_OUT_X_L, gyr, 6)) { |
bclaus | 0:62dfce144cf7 | 57 | //scale is 8.75 mdps/digit |
Spilly | 1:c7a53a17adef | 58 | *gx = float(short(gyr[1] << 8 | gyr[0]))*0.00875f; |
Spilly | 1:c7a53a17adef | 59 | *gy = float(short(gyr[3] << 8 | gyr[2]))*0.00875f; |
Spilly | 1:c7a53a17adef | 60 | *gz = float(short(gyr[5] << 8 | gyr[4]))*0.00875f; |
bclaus | 0:62dfce144cf7 | 61 | return true; |
bclaus | 0:62dfce144cf7 | 62 | } |
bclaus | 0:62dfce144cf7 | 63 | return false; |
bclaus | 0:62dfce144cf7 | 64 | } |
bclaus | 0:62dfce144cf7 | 65 | |
bclaus | 0:62dfce144cf7 | 66 | |
bclaus | 0:62dfce144cf7 | 67 | |
bclaus | 0:62dfce144cf7 | 68 | |
bclaus | 0:62dfce144cf7 | 69 | bool L3GD20::write_reg(int addr_i2c,int addr_reg, char v) |
bclaus | 0:62dfce144cf7 | 70 | { |
bclaus | 0:62dfce144cf7 | 71 | char data[2] = {addr_reg, v}; |
bclaus | 0:62dfce144cf7 | 72 | return L3GD20::_L3GD20.write(addr_i2c, data, 2) == 0; |
bclaus | 0:62dfce144cf7 | 73 | } |
bclaus | 0:62dfce144cf7 | 74 | |
bclaus | 0:62dfce144cf7 | 75 | bool L3GD20::read_reg(int addr_i2c,int addr_reg, char *v) |
bclaus | 0:62dfce144cf7 | 76 | { |
bclaus | 0:62dfce144cf7 | 77 | char data = addr_reg; |
bclaus | 0:62dfce144cf7 | 78 | bool result = false; |
bclaus | 0:62dfce144cf7 | 79 | |
bclaus | 0:62dfce144cf7 | 80 | __disable_irq(); |
bclaus | 0:62dfce144cf7 | 81 | if ((_L3GD20.write(addr_i2c, &data, 1) == 0) && (_L3GD20.read(addr_i2c, &data, 1) == 0)){ |
bclaus | 0:62dfce144cf7 | 82 | *v = data; |
bclaus | 0:62dfce144cf7 | 83 | result = true; |
bclaus | 0:62dfce144cf7 | 84 | } |
bclaus | 0:62dfce144cf7 | 85 | __enable_irq(); |
bclaus | 0:62dfce144cf7 | 86 | return result; |
bclaus | 0:62dfce144cf7 | 87 | } |
bclaus | 0:62dfce144cf7 | 88 | |
bclaus | 0:62dfce144cf7 | 89 | |
bclaus | 0:62dfce144cf7 | 90 | bool L3GD20::recv(char sad, char sub, char *buf, int length) { |
bclaus | 0:62dfce144cf7 | 91 | if (length > 1) sub |= 0x80; |
bclaus | 0:62dfce144cf7 | 92 | |
bclaus | 0:62dfce144cf7 | 93 | return _L3GD20.write(sad, &sub, 1, true) == 0 && _L3GD20.read(sad, buf, length) == 0; |
bclaus | 0:62dfce144cf7 | 94 | } |