Skip to content

Commit e8a13f7

Browse files
committed
Add accelerometer only example
1 parent c45ece5 commit e8a13f7

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
===============================================
3+
Example sketch for CurieIMU library for Intel(R) Curie(TM) devices.
4+
Copyright (c) 2015 Intel Corporation. All rights reserved.
5+
6+
Based on I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050
7+
class by Jeff Rowberg: https://github.com/jrowberg/i2cdevlib
8+
9+
===============================================
10+
I2Cdev device library code is placed under the MIT license
11+
Copyright (c) 2011 Jeff Rowberg
12+
13+
Permission is hereby granted, free of charge, to any person obtaining a copy
14+
of this software and associated documentation files (the "Software"), to deal
15+
in the Software without restriction, including without limitation the rights
16+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
copies of the Software, and to permit persons to whom the Software is
18+
furnished to do so, subject to the following conditions:
19+
20+
The above copyright notice and this permission notice shall be included in
21+
all copies or substantial portions of the Software.
22+
23+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29+
THE SOFTWARE.
30+
===============================================
31+
*/
32+
33+
#include "CurieImu.h"
34+
35+
void setup() {
36+
Serial.begin(9600); // initialize Serial communication
37+
while (!Serial); // wait for the serial port to open
38+
39+
// initialize device
40+
Serial.println("Initializing IMU device...");
41+
CurieIMU.begin();
42+
43+
// Set the accelerometer range to 2G
44+
CurieIMU.setAccelerometerRange(CURIE_IMU_ACCELEROMETER_RANGE_2G);
45+
46+
Serial.println("About to calibrate accelerometer. Make sure your board is stable and upright");
47+
delay(5000);
48+
49+
// start accelerometer
50+
Serial.print("Starting accelerometer calibration...");
51+
CurieIMU.autoCalibrateAccelerometerOffset(X_AXIS, 0);
52+
CurieIMU.autoCalibrateAccelerometerOffset(Y_AXIS, 0);
53+
CurieIMU.autoCalibrateAccelerometerOffset(Z_AXIS, 1);
54+
Serial.println(" Done");
55+
56+
Serial.println("Enabling accelerometer offset compensation");
57+
CurieIMU.enableAccelerometerOffset(true);
58+
}
59+
60+
void loop() {
61+
short int axRaw, ayRaw, azRaw; // raw accelerometer values
62+
float ax, ay, az;
63+
64+
// read raw accelerometer measurements from device
65+
CurieIMU.readAccelerometer(axRaw, ayRaw, azRaw);
66+
67+
// convert the raw accelerometer data to G's
68+
ax = convertRawAcceleration(axRaw);
69+
ay = convertRawAcceleration(ayRaw);
70+
az = convertRawAcceleration(azRaw);
71+
72+
// display tab-separated accelerometer x/y/z values
73+
Serial.print("a:\t");
74+
Serial.print(ax);
75+
Serial.print("\t");
76+
Serial.print(ay);
77+
Serial.print("\t");
78+
Serial.print(az);
79+
Serial.println();
80+
81+
// wait 5 seconds
82+
delay(5000);
83+
}
84+
85+
float convertRawAcceleration(short aRaw) {
86+
// since we are using 2G range
87+
// -2g maps to a raw value of -32768
88+
// +2g maps to a raw value of 32767
89+
90+
float a = (aRaw * 2.0) / 32768.0;
91+
92+
return a;
93+
}
94+

0 commit comments

Comments
 (0)