Arduino/Genuino 101 入门教程二:获取加速度计和陀螺仪数据

2016-03-203.5万
AI 快速预览详细 收起
本文介绍了如何使用CurieIMU库在Arduino 101上获取加速度计和陀螺仪数据。首先下载程序并添加校正代码以确保数据准确性。通过串口读取数据,并展示了如何读取和处理加速度计和陀螺仪的数据。适用于初学者和项目开发,帮助用户轻松获取和处理传感器数据。

加速度计和陀螺仪是101一大特色,当然得玩玩啦,就是冲这个来的嘛。本来我还在纠结怎么获取传感器的数据?怎么对数据进行滤波?怎么对数据进行融合?但是,这些现在都变的简单了,有现成的库文件CurieIMU和MadgwickAHRS可以用来获取和处理传感器得到的数据。

【产品链接】: Arduino 101

咳咳,玩起来:

1.下载程序:

在库文件中能找到六轴姿态传感器驱动库libraries\CurieIMU,里面有测试程序Accelerometer,这个程序一开始没有对数据进行校正,可以手动天加一下校正代码就好了,下面是添加好校正代码的程序:

/*
 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>
int calibrateOffsets = 1; // int to determine whether calibration takes place or not

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();

 if (calibrateOffsets == 1) {
  // use the code below to calibrate accel/gyro offset values
  Serial.println("Internal sensor offsets BEFORE calibration...");
  Serial.print(CurieIMU.getAccelerometerOffset(X_AXIS)); Serial.print("\t");
  Serial.print(CurieIMU.getAccelerometerOffset(Y_AXIS)); Serial.print("\t");
  Serial.print(CurieIMU.getAccelerometerOffset(Z_AXIS)); Serial.print("\t");
  Serial.println("");

  Serial.print("Starting Acceleration calibration...");
  CurieIMU.autoCalibrateAccelerometerOffset(X_AXIS, 0);
  CurieIMU.autoCalibrateAccelerometerOffset(Y_AXIS, 0);
  CurieIMU.autoCalibrateAccelerometerOffset(Z_AXIS, 1);
  Serial.println(" Done");

  Serial.println("Internal sensor offsets AFTER calibration...");
  Serial.print(CurieIMU.getAccelerometerOffset(X_AXIS)); Serial.print("\t");
  Serial.print(CurieIMU.getAccelerometerOffset(Y_AXIS)); Serial.print("\t");
  Serial.print(CurieIMU.getAccelerometerOffset(Z_AXIS)); Serial.print("\t");
  Serial.println("");
 }

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

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(500);
}

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;
}

串口读出来的数据,很准确,可以转动板子看看每个轴的数据变化:
Arduino/Genuino 101 入门教程二:获取加速度计和陀螺仪数据图2

2.读取陀螺仪的数据:

下载程序,在库文件中能找到六轴姿态传感器驱动库libraries\CurieIMU,里面有测试程序Gyro,同样加几句代码事先校正一下数据,下面是添加好校正代码的程序:

/*
 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();

 Serial.print("Starting Gyroscope calibration...");
 CurieIMU.autoCalibrateGyroOffset();
 Serial.println(" Done");

 // Set the accelerometer range to 250 degrees/second
 CurieIMU.setGyroRange(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(1000);
}

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;
}

串口数据输出,这里我让开发板绕X轴旋转得到的数据,考虑手动操作的误差,y轴和z轴基本保持不变,数据看起来不错哦:
Arduino/Genuino 101 入门教程二:获取加速度计和陀螺仪数据图1

拓展:

既然加速度计和陀螺仪的数据读出来了,那么是不是该用这个数据来干点什么,嘿嘿嘿,是不是可以干坏事了啊:lol!下个教程教你怎么干坏事,不要太激动哦{:5_161:}。
看累了没,开心一刻:
刚才和朋友聊天,其中有谈到你,知道吗?我和他们吵了起来,还差点动手打起来,因为他们有的说你像猴子,有的说你像猩猩,实在太过分了!根本没有把你当猪看!{:5_199:}

Arduino/Genuino 101 入门教程】

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(6)
1025285443
1025285443
小白请教,校正具体是指什么地方呢?为什么要进行校正?不进行校正会如何呢?望不吝赐教!
sxl001
sxl001
good,good
gyy
gyy
陀螺仪的z轴是哪个方向啊?
iooops
iooops
根本就没有把你当猪看233333
qq815076543322
qq815076543322
这么速度吗
qq865650214158
qq865650214158
作者
回复qq815076543322
昨晚把笔记整理了一下!
苦海
苦海
INTEL的专用库是下完之后可以在example看到例程么,专用库是不是装了居里后自动就装了 ??
qq865650214158
qq865650214158
作者
回复苦海
当在开发板管理器中安装Arduino 101后,Intel的专用库就会自动装好。库文件参考路径:C:\Users\(用户名)\AppData\Local\Arduino15\packages\Intel\hardware\arc32\1.0.4\libraries
qq865650214158
qq865650214158
作者
回复苦海
现在的Arduino IDE安装位置里面的libraries是没有Curie专用库的,即使装了居里也是没有的,需要下载后手动添加到里面,库文件的example里面有例程代码,直接用就能看到效果,偶尔需要稍微修改一下库文件。
- 没有更多了 -