東北大学学友会準加盟団体 From The Earth の高高度ロケットFTE-06(通称:海豚)にて使用したソフトウェアです.ご自由にお使いください.このプログラムによって生じた損害について当団体は一切責任を負いません.また,各モジュールのライブラリは当団体が作成したものではないので再配布は禁止します.
Dependencies: mbed FATFileSystem
Fork of FTE-06 by
L3GD20/L3GD20.cpp@57:d78497a079cf, 2017-08-25 (annotated)
- Committer:
- mizuki_akaike
- Date:
- Fri Aug 25 03:42:30 2017 +0000
- Revision:
- 57:d78497a079cf
- Parent:
- 23:9be034083282
??????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mizuki_akaike | 23:9be034083282 | 1 | /** |
mizuki_akaike | 23:9be034083282 | 2 | * Copyright (c) 2011 Pololu Corporation. For more information, see |
mizuki_akaike | 23:9be034083282 | 3 | * |
mizuki_akaike | 23:9be034083282 | 4 | * http://www.pololu.com/ |
mizuki_akaike | 23:9be034083282 | 5 | * http://forum.pololu.com/ |
mizuki_akaike | 23:9be034083282 | 6 | * |
mizuki_akaike | 23:9be034083282 | 7 | * Permission is hereby granted, free of charge, to any person |
mizuki_akaike | 23:9be034083282 | 8 | * obtaining a copy of this software and associated documentation |
mizuki_akaike | 23:9be034083282 | 9 | * files (the "Software"), to deal in the Software without |
mizuki_akaike | 23:9be034083282 | 10 | * restriction, including without limitation the rights to use, |
mizuki_akaike | 23:9be034083282 | 11 | * copy, modify, merge, publish, distribute, sublicense, and/or sell |
mizuki_akaike | 23:9be034083282 | 12 | * copies of the Software, and to permit persons to whom the |
mizuki_akaike | 23:9be034083282 | 13 | * Software is furnished to do so, subject to the following |
mizuki_akaike | 23:9be034083282 | 14 | * conditions: |
mizuki_akaike | 23:9be034083282 | 15 | * |
mizuki_akaike | 23:9be034083282 | 16 | * The above copyright notice and this permission notice shall be |
mizuki_akaike | 23:9be034083282 | 17 | * included in all copies or substantial portions of the Software. |
mizuki_akaike | 23:9be034083282 | 18 | * |
mizuki_akaike | 23:9be034083282 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
mizuki_akaike | 23:9be034083282 | 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
mizuki_akaike | 23:9be034083282 | 21 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mizuki_akaike | 23:9be034083282 | 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
mizuki_akaike | 23:9be034083282 | 23 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
mizuki_akaike | 23:9be034083282 | 24 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
mizuki_akaike | 23:9be034083282 | 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
mizuki_akaike | 23:9be034083282 | 26 | * OTHER DEALINGS IN THE SOFTWARE. |
mizuki_akaike | 23:9be034083282 | 27 | */ |
mizuki_akaike | 23:9be034083282 | 28 | |
mizuki_akaike | 23:9be034083282 | 29 | #include "mbed.h" |
mizuki_akaike | 23:9be034083282 | 30 | #include "L3GD20.h" |
mizuki_akaike | 23:9be034083282 | 31 | |
mizuki_akaike | 23:9be034083282 | 32 | |
mizuki_akaike | 23:9be034083282 | 33 | // Defines //////////////////////////////////////////////////////////////// |
mizuki_akaike | 23:9be034083282 | 34 | |
mizuki_akaike | 23:9be034083282 | 35 | // The Arduino two-wire interface uses a 7-bit number for the address, |
mizuki_akaike | 23:9be034083282 | 36 | // and sets the last bit correctly based on reads and writes |
mizuki_akaike | 23:9be034083282 | 37 | // mbed I2C libraries take the 7-bit address shifted left 1 bit |
mizuki_akaike | 23:9be034083282 | 38 | // #define GYR_ADDRESS (0xD2 >> 1) |
mizuki_akaike | 23:9be034083282 | 39 | #define GYR_ADDRESS 0xD6 |
mizuki_akaike | 23:9be034083282 | 40 | |
mizuki_akaike | 23:9be034083282 | 41 | |
mizuki_akaike | 23:9be034083282 | 42 | // Public Methods ////////////////////////////////////////////////////////////// |
mizuki_akaike | 23:9be034083282 | 43 | |
mizuki_akaike | 23:9be034083282 | 44 | // Constructor |
mizuki_akaike | 23:9be034083282 | 45 | L3GD20::L3GD20(PinName sda, PinName scl): |
mizuki_akaike | 23:9be034083282 | 46 | _L3GD20(sda, scl) |
mizuki_akaike | 23:9be034083282 | 47 | { |
mizuki_akaike | 23:9be034083282 | 48 | char reg_v; |
mizuki_akaike | 23:9be034083282 | 49 | _L3GD20.frequency(200000); |
mizuki_akaike | 23:9be034083282 | 50 | |
mizuki_akaike | 23:9be034083282 | 51 | reg_v = 0; |
mizuki_akaike | 23:9be034083282 | 52 | write_reg(GYR_ADDRESS,L3GD20_LOW_ODR,reg_v); |
mizuki_akaike | 23:9be034083282 | 53 | |
mizuki_akaike | 23:9be034083282 | 54 | // 0x6F |
mizuki_akaike | 23:9be034083282 | 55 | // DR = 01 (200 Hz ODR); BW = 10 (50 Hz bandwidth); PD = 1 (normal mode); Zen = Yen = Xen = 1 (all axes enabled) |
mizuki_akaike | 23:9be034083282 | 56 | reg_v = 0; |
mizuki_akaike | 23:9be034083282 | 57 | reg_v |= 0x6F; |
mizuki_akaike | 23:9be034083282 | 58 | write_reg(GYR_ADDRESS,L3GD20_CTRL_REG1,reg_v); |
mizuki_akaike | 23:9be034083282 | 59 | |
mizuki_akaike | 23:9be034083282 | 60 | |
mizuki_akaike | 23:9be034083282 | 61 | |
mizuki_akaike | 23:9be034083282 | 62 | } |
mizuki_akaike | 23:9be034083282 | 63 | |
mizuki_akaike | 23:9be034083282 | 64 | |
mizuki_akaike | 23:9be034083282 | 65 | |
mizuki_akaike | 23:9be034083282 | 66 | bool L3GD20::read(float *gx, float *gy, float *gz) { |
mizuki_akaike | 23:9be034083282 | 67 | char gyr[6]; |
mizuki_akaike | 23:9be034083282 | 68 | |
mizuki_akaike | 23:9be034083282 | 69 | if (recv(GYR_ADDRESS, L3GD20_OUT_X_L, gyr, 6)) { |
mizuki_akaike | 23:9be034083282 | 70 | //scale is 8.75 mdps/digit |
mizuki_akaike | 23:9be034083282 | 71 | *gx = float(short(gyr[1] << 8 | gyr[0]))*0.00875; |
mizuki_akaike | 23:9be034083282 | 72 | *gy = float(short(gyr[3] << 8 | gyr[2]))*0.00875; |
mizuki_akaike | 23:9be034083282 | 73 | *gz = float(short(gyr[5] << 8 | gyr[4]))*0.00875; |
mizuki_akaike | 23:9be034083282 | 74 | |
mizuki_akaike | 23:9be034083282 | 75 | |
mizuki_akaike | 23:9be034083282 | 76 | return true; |
mizuki_akaike | 23:9be034083282 | 77 | } |
mizuki_akaike | 23:9be034083282 | 78 | |
mizuki_akaike | 23:9be034083282 | 79 | return false; |
mizuki_akaike | 23:9be034083282 | 80 | } |
mizuki_akaike | 23:9be034083282 | 81 | |
mizuki_akaike | 23:9be034083282 | 82 | |
mizuki_akaike | 23:9be034083282 | 83 | |
mizuki_akaike | 23:9be034083282 | 84 | |
mizuki_akaike | 23:9be034083282 | 85 | bool L3GD20::write_reg(int addr_i2c,int addr_reg, char v) |
mizuki_akaike | 23:9be034083282 | 86 | { |
mizuki_akaike | 23:9be034083282 | 87 | char data[2] = {addr_reg, v}; |
mizuki_akaike | 23:9be034083282 | 88 | return L3GD20::_L3GD20.write(addr_i2c, data, 2) == 0; |
mizuki_akaike | 23:9be034083282 | 89 | } |
mizuki_akaike | 23:9be034083282 | 90 | |
mizuki_akaike | 23:9be034083282 | 91 | bool L3GD20::read_reg(int addr_i2c,int addr_reg, char *v) |
mizuki_akaike | 23:9be034083282 | 92 | { |
mizuki_akaike | 23:9be034083282 | 93 | char data = addr_reg; |
mizuki_akaike | 23:9be034083282 | 94 | bool result = false; |
mizuki_akaike | 23:9be034083282 | 95 | |
mizuki_akaike | 23:9be034083282 | 96 | __disable_irq(); |
mizuki_akaike | 23:9be034083282 | 97 | if ((_L3GD20.write(addr_i2c, &data, 1) == 0) && (_L3GD20.read(addr_i2c, &data, 1) == 0)){ |
mizuki_akaike | 23:9be034083282 | 98 | *v = data; |
mizuki_akaike | 23:9be034083282 | 99 | result = true; |
mizuki_akaike | 23:9be034083282 | 100 | } |
mizuki_akaike | 23:9be034083282 | 101 | __enable_irq(); |
mizuki_akaike | 23:9be034083282 | 102 | return result; |
mizuki_akaike | 23:9be034083282 | 103 | } |
mizuki_akaike | 23:9be034083282 | 104 | |
mizuki_akaike | 23:9be034083282 | 105 | |
mizuki_akaike | 23:9be034083282 | 106 | bool L3GD20::recv(char sad, char sub, char *buf, int length) { |
mizuki_akaike | 23:9be034083282 | 107 | if (length > 1) sub |= 0x80; |
mizuki_akaike | 23:9be034083282 | 108 | |
mizuki_akaike | 23:9be034083282 | 109 | return _L3GD20.write(sad, &sub, 1, true) == 0 && _L3GD20.read(sad, buf, length) == 0; |
mizuki_akaike | 23:9be034083282 | 110 | } |