|
| 1 | +/* |
| 2 | + Copyright (c) 2015 Intel Corporation. All rights reserved. |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +
|
| 18 | +*/ |
| 19 | + |
| 20 | +/* |
| 21 | + This sketch example demonstrates how the BMI160 on the |
| 22 | + Intel(R) Curie(TM) module can be used to read gyroscope data |
| 23 | +*/ |
| 24 | + |
| 25 | +#include "CurieImu.h" |
| 26 | + |
| 27 | +void setup() { |
| 28 | + Serial.begin(9600); // initialize Serial communication |
| 29 | + while (!Serial); // wait for the serial port to open |
| 30 | + |
| 31 | + // initialize device |
| 32 | + Serial.println("Initializing IMU device..."); |
| 33 | + CurieIMU.begin(); |
| 34 | + |
| 35 | + // Set the accelerometer range to 250 degrees/second |
| 36 | + CurieIMU.setGyroRange(CURIE_IMU_GYRO_RANGE_250); |
| 37 | + |
| 38 | + Serial.println("About to calibrate gyro. Make sure your board is stable and upright"); |
| 39 | + delay(5000); |
| 40 | + |
| 41 | + // start gyro |
| 42 | + Serial.print("Starting gyro calibration..."); |
| 43 | + CurieIMU.autoCalibrateGyroOffset(); |
| 44 | + Serial.println(" Done"); |
| 45 | + |
| 46 | + Serial.println("Enabling gyro offset compensation"); |
| 47 | + CurieIMU.enableGyroOffset(true); |
| 48 | +} |
| 49 | + |
| 50 | +void loop() { |
| 51 | + short int gxRaw, gyRaw, gzRaw; // raw gyro values |
| 52 | + float gx, gy, gz; |
| 53 | + |
| 54 | + // read raw gyro measurements from device |
| 55 | + CurieIMU.readGyro(gxRaw, gyRaw, gzRaw); |
| 56 | + |
| 57 | + // convert the raw gyro data to degrees/second |
| 58 | + gx = convertRawGyro(gxRaw); |
| 59 | + gy = convertRawGyro(gyRaw); |
| 60 | + gz = convertRawGyro(gzRaw); |
| 61 | + |
| 62 | + // display tab-separated gyro x/y/z values |
| 63 | + Serial.print("g:\t"); |
| 64 | + Serial.print(gx); |
| 65 | + Serial.print("\t"); |
| 66 | + Serial.print(gy); |
| 67 | + Serial.print("\t"); |
| 68 | + Serial.print(gz); |
| 69 | + Serial.println(); |
| 70 | + |
| 71 | + // wait 5 seconds |
| 72 | + delay(5000); |
| 73 | +} |
| 74 | + |
| 75 | +float convertRawGyro(short gRaw) { |
| 76 | + // since we are using 250 degrees/seconds range |
| 77 | + // -250 maps to a raw value of -32768 |
| 78 | + // +250 maps to a raw value of 32767 |
| 79 | + |
| 80 | + float g = (gRaw * 250.0) / 32768.0; |
| 81 | + |
| 82 | + return g; |
| 83 | +} |
| 84 | + |
0 commit comments