Skip to content

Add new API's and examples to CurieIMU #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jan 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions libraries/CurieIMU/examples/Accelerometer/Accelerometer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright (c) 2015 Intel Corporation. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

*/

/*
This sketch example demonstrates how the BMI160 on the
Intel(R) Curie(TM) module can be used to read accelerometer data
*/

#include "CurieIMU.h"

void setup() {
Serial.begin(9600); // initialize Serial communication
while (!Serial); // wait for the serial port to open

// initialize device
Serial.println("Initializing IMU device...");
CurieIMU.begin();

// Set the accelerometer range to 2G
CurieIMU.setAccelerometerRange(CURIE_IMU_ACCELEROMETER_RANGE_2G);
}

void loop() {
int axRaw, ayRaw, azRaw; // raw accelerometer values
float ax, ay, az;

// read raw accelerometer measurements from device
CurieIMU.readAccelerometer(axRaw, ayRaw, azRaw);

// convert the raw accelerometer data to G's
ax = convertRawAcceleration(axRaw);
ay = convertRawAcceleration(ayRaw);
az = convertRawAcceleration(azRaw);

// display tab-separated accelerometer x/y/z values
Serial.print("a:\t");
Serial.print(ax);
Serial.print("\t");
Serial.print(ay);
Serial.print("\t");
Serial.print(az);
Serial.println();

// wait 5 seconds
delay(5000);
}

float convertRawAcceleration(int aRaw) {
// since we are using 2G range
// -2g maps to a raw value of -32768
// +2g maps to a raw value of 32767

float a = (aRaw * 2.0) / 32768.0;

return a;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
Copyright (c) 2015 Intel Corporation. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

*/

/*
This sketch example demonstrates how the BMI160 on the
Intel(R) Curie(TM) module can be used to read accelerometer data
and translate it to an orientation
*/

#include "CurieIMU.h"

int lastOrientation = - 1; // previous orientation (for comparison)

void setup() {
Serial.begin(9600); // initialize Serial communication
while (!Serial); // wait for the serial port to open

// initialize device
Serial.println("Initializing IMU device...");
CurieIMU.begin();

// Set the accelerometer range to 2G
CurieIMU.setAccelerometerRange(CURIE_IMU_ACCELEROMETER_RANGE_2G);
}

void loop() {
int orientation = - 1; // the board's orientation
String orientationString; // string for printing description of orientation
/*
The orientations of the board:
0: flat, processor facing up
1: flat, processor facing down
2: landscape, analog pins down
3: landscape, analog pins up
4: portrait, USB connector up
5: portrait, USB connector down
*/
// read accelerometer:
int x = CurieIMU.readAccelerometer(X_AXIS);
int y = CurieIMU.readAccelerometer(Y_AXIS);
int z = CurieIMU.readAccelerometer(Z_AXIS);

// calculate the absolute values, to determine the largest
int absX = abs(x);
int absY = abs(y);
int absZ = abs(z);

if ( (absZ > absX) && (absZ > absY)) {
// base orientation on Z
if (z > 0) {
orientationString = "up";
orientation = 0;
} else {
orientationString = "down";
orientation = 1;
}
} else if ( (absY > absX) && (absY > absZ)) {
// base orientation on Y
if (y > 0) {
orientationString = "digital pins up";
orientation = 2;
} else {
orientationString = "analog pins up";
orientation = 3;
}
} else {
// base orientation on X
if (x < 0) {
orientationString = "connector up";
orientation = 4;
} else {
orientationString = "connector down";
orientation = 5;
}
}

// if the orientation has changed, print out a description:
if (orientation != lastOrientation) {
Serial.println(orientationString);
lastOrientation = orientation;
}
}


73 changes: 73 additions & 0 deletions libraries/CurieIMU/examples/Gyro/Gyro.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright (c) 2015 Intel Corporation. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

*/

/*
This sketch example demonstrates how the BMI160 on the
Intel(R) Curie(TM) module can be used to read gyroscope data
*/

#include "CurieIMU.h"

void setup() {
Serial.begin(9600); // initialize Serial communication
while (!Serial); // wait for the serial port to open

// initialize device
Serial.println("Initializing IMU device...");
CurieIMU.begin();

// Set the accelerometer range to 250 degrees/second
CurieIMU.setGyroRange(CURIE_IMU_GYRO_RANGE_250);
}

void loop() {
int gxRaw, gyRaw, gzRaw; // raw gyro values
float gx, gy, gz;

// read raw gyro measurements from device
CurieIMU.readGyro(gxRaw, gyRaw, gzRaw);

// convert the raw gyro data to degrees/second
gx = convertRawGyro(gxRaw);
gy = convertRawGyro(gyRaw);
gz = convertRawGyro(gzRaw);

// display tab-separated gyro x/y/z values
Serial.print("g:\t");
Serial.print(gx);
Serial.print("\t");
Serial.print(gy);
Serial.print("\t");
Serial.print(gz);
Serial.println();

// wait 5 seconds
delay(5000);
}

float convertRawGyro(int gRaw) {
// since we are using 250 degrees/seconds range
// -250 maps to a raw value of -32768
// +250 maps to a raw value of 32767

float g = (gRaw * 250.0) / 32768.0;

return g;
}

Loading