Skip to content

Commit b195624

Browse files
committed
Merge pull request #90 from sandeepmistry/imu-api
Add new API's and examples to CurieIMU
2 parents 23e6f16 + 6fc749d commit b195624

File tree

20 files changed

+1672
-707
lines changed

20 files changed

+1672
-707
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 accelerometer 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 2G
36+
CurieIMU.setAccelerometerRange(CURIE_IMU_ACCELEROMETER_RANGE_2G);
37+
}
38+
39+
void loop() {
40+
int axRaw, ayRaw, azRaw; // raw accelerometer values
41+
float ax, ay, az;
42+
43+
// read raw accelerometer measurements from device
44+
CurieIMU.readAccelerometer(axRaw, ayRaw, azRaw);
45+
46+
// convert the raw accelerometer data to G's
47+
ax = convertRawAcceleration(axRaw);
48+
ay = convertRawAcceleration(ayRaw);
49+
az = convertRawAcceleration(azRaw);
50+
51+
// display tab-separated accelerometer x/y/z values
52+
Serial.print("a:\t");
53+
Serial.print(ax);
54+
Serial.print("\t");
55+
Serial.print(ay);
56+
Serial.print("\t");
57+
Serial.print(az);
58+
Serial.println();
59+
60+
// wait 5 seconds
61+
delay(5000);
62+
}
63+
64+
float convertRawAcceleration(int aRaw) {
65+
// since we are using 2G range
66+
// -2g maps to a raw value of -32768
67+
// +2g maps to a raw value of 32767
68+
69+
float a = (aRaw * 2.0) / 32768.0;
70+
71+
return a;
72+
}
73+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 accelerometer data
23+
and translate it to an orientation
24+
*/
25+
26+
#include "CurieIMU.h"
27+
28+
int lastOrientation = - 1; // previous orientation (for comparison)
29+
30+
void setup() {
31+
Serial.begin(9600); // initialize Serial communication
32+
while (!Serial); // wait for the serial port to open
33+
34+
// initialize device
35+
Serial.println("Initializing IMU device...");
36+
CurieIMU.begin();
37+
38+
// Set the accelerometer range to 2G
39+
CurieIMU.setAccelerometerRange(CURIE_IMU_ACCELEROMETER_RANGE_2G);
40+
}
41+
42+
void loop() {
43+
int orientation = - 1; // the board's orientation
44+
String orientationString; // string for printing description of orientation
45+
/*
46+
The orientations of the board:
47+
0: flat, processor facing up
48+
1: flat, processor facing down
49+
2: landscape, analog pins down
50+
3: landscape, analog pins up
51+
4: portrait, USB connector up
52+
5: portrait, USB connector down
53+
*/
54+
// read accelerometer:
55+
int x = CurieIMU.readAccelerometer(X_AXIS);
56+
int y = CurieIMU.readAccelerometer(Y_AXIS);
57+
int z = CurieIMU.readAccelerometer(Z_AXIS);
58+
59+
// calculate the absolute values, to determine the largest
60+
int absX = abs(x);
61+
int absY = abs(y);
62+
int absZ = abs(z);
63+
64+
if ( (absZ > absX) && (absZ > absY)) {
65+
// base orientation on Z
66+
if (z > 0) {
67+
orientationString = "up";
68+
orientation = 0;
69+
} else {
70+
orientationString = "down";
71+
orientation = 1;
72+
}
73+
} else if ( (absY > absX) && (absY > absZ)) {
74+
// base orientation on Y
75+
if (y > 0) {
76+
orientationString = "digital pins up";
77+
orientation = 2;
78+
} else {
79+
orientationString = "analog pins up";
80+
orientation = 3;
81+
}
82+
} else {
83+
// base orientation on X
84+
if (x < 0) {
85+
orientationString = "connector up";
86+
orientation = 4;
87+
} else {
88+
orientationString = "connector down";
89+
orientation = 5;
90+
}
91+
}
92+
93+
// if the orientation has changed, print out a description:
94+
if (orientation != lastOrientation) {
95+
Serial.println(orientationString);
96+
lastOrientation = orientation;
97+
}
98+
}
99+
100+
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
39+
void loop() {
40+
int gxRaw, gyRaw, gzRaw; // raw gyro values
41+
float gx, gy, gz;
42+
43+
// read raw gyro measurements from device
44+
CurieIMU.readGyro(gxRaw, gyRaw, gzRaw);
45+
46+
// convert the raw gyro data to degrees/second
47+
gx = convertRawGyro(gxRaw);
48+
gy = convertRawGyro(gyRaw);
49+
gz = convertRawGyro(gzRaw);
50+
51+
// display tab-separated gyro x/y/z values
52+
Serial.print("g:\t");
53+
Serial.print(gx);
54+
Serial.print("\t");
55+
Serial.print(gy);
56+
Serial.print("\t");
57+
Serial.print(gz);
58+
Serial.println();
59+
60+
// wait 5 seconds
61+
delay(5000);
62+
}
63+
64+
float convertRawGyro(int gRaw) {
65+
// since we are using 250 degrees/seconds range
66+
// -250 maps to a raw value of -32768
67+
// +250 maps to a raw value of 32767
68+
69+
float g = (gRaw * 250.0) / 32768.0;
70+
71+
return g;
72+
}
73+

0 commit comments

Comments
 (0)