
作者:Jason_G
加速度计和陀螺仪是101一大特色,当然得玩玩啦,就是冲这个来的嘛。本来我还在纠结怎么获取传感器的数据?怎么对数据进行滤波?怎么对数据进行融合?但是,这些现在都变的简单了,有现成的库文件CurieIMU和MadgwickAHRS可以用来获取和处理传感器得到的数据。
咳咳,玩起来:
1、下载程序:
在库文件中能找到六轴姿态传感器驱动库libraries\CurieIMU,里面有测试程序Accelerometer,这个程序一开始没有对数据进行校正,可以手动天加一下校正代码就好了,下面是添加好校正代码的程序:
01 /*
02 Copyright (c) 2015 Intel Corporation. All rights reserved.
03
04 This library is free software; you can redistribute it and/or
05 modify it under the terms of the GNU Lesser General Public
06 License as published by the Free Software Foundation; either
07 version 2.1 of the License, or (at your option) any later version.
08
09 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 #include
25 int calibrateOffsets = 1; // int to determine whether calibration takes place or not
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 if (calibrateOffsets == 1) {
36 // use the code below to calibrate accel/gyro offset values
37 Serial.println("Internal sensor offsets BEFORE calibration...");
38 Serial.print(CurieIMU.getAccelerometerOffset(X_AXIS)); Serial.print("\t");
39 Serial.print(CurieIMU.getAccelerometerOffset(Y_AXIS)); Serial.print("\t");
40 Serial.print(CurieIMU.getAccelerometerOffset(Z_AXIS)); Serial.print("\t");
41 Serial.println("");
42
43 Serial.print("Starting Acceleration calibration...");
44 CurieIMU.autoCalibrateAccelerometerOffset(X_AXIS, 0);
45 CurieIMU.autoCalibrateAccelerometerOffset(Y_AXIS, 0);
46 CurieIMU.autoCalibrateAccelerometerOffset(Z_AXIS, 1);
47 Serial.println(" Done");
48
49 Serial.println("Internal sensor offsets AFTER calibration...");
50 Serial.print(CurieIMU.getAccelerometerOffset(X_AXIS)); Serial.print("\t");
51 Serial.print(CurieIMU.getAccelerometerOffset(Y_AXIS)); Serial.print("\t");
52 Serial.print(CurieIMU.getAccelerometerOffset(Z_AXIS)); Serial.print("\t");
53 Serial.println("");
54 }
55
56 // Set the accelerometer range to 2G
57 CurieIMU.setAccelerometerRange(2);
58 }
59
60 void loop() {
61 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(500);
83 }
84
85 float convertRawAcceleration(int 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 }
串口读出来的数据,很准确,可以转动板子看看每个轴的数据变化:

2、读取陀螺仪的数据:下载程序,在库文件中能找到六轴姿态传感器驱动库libraries\CurieIMU,里面有测试程序Gyro,同样加几句代码事先校正一下数据,下面是添加好校正代码的程序:
01 /*
02 Copyright (c) 2015 Intel Corporation. All rights reserved.
03
04 This library is free software; you can redistribute it and/or
05 modify it under the terms of the GNU Lesser General Public
06 License as published by the Free Software Foundation; either
07 version 2.1 of the License, or (at your option) any later version.
08
09 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 Serial.print("Starting Gyroscope calibration...");
36 CurieIMU.autoCalibrateGyroOffset();
37 Serial.println(" Done");
38
39 // Set the accelerometer range to 250 degrees/second
40 CurieIMU.setGyroRange(250);
41 }
42
43 void loop() {
44 int gxRaw, gyRaw, gzRaw; // raw gyro values
45 float gx, gy, gz;
46
47 // read raw gyro measurements from device
48 CurieIMU.readGyro(gxRaw, gyRaw, gzRaw);
49
50 // convert the raw gyro data to degrees/second
51 gx = convertRawGyro(gxRaw);
52 gy = convertRawGyro(gyRaw);
53 gz = convertRawGyro(gzRaw);
54
55 // display tab-separated gyro x/y/z values
56 Serial.print("g:\t");
57 Serial.print(gx);
58 Serial.print("\t");
59 Serial.print(gy);
60 Serial.print("\t");
61 Serial.print(gz);
62 Serial.println();
63
64 // wait 5 seconds
65 delay(1000);
66 }
67
68 float convertRawGyro(int gRaw) {
69 // since we are using 250 degrees/seconds range
70 // -250 maps to a raw value of -32768
71 // +250 maps to a raw value of 32767
72
73 float g = (gRaw * 250.0) / 32768.0;
74
75 return g;
76 }
串口数据输出,这里我让开发板绕X轴旋转得到的数据,考虑手动操作的误差,y轴和z轴基本保持不变,数据看起来不错哦:

拓展:
既然加速度计和陀螺仪的数据读出来了,那么是不是该用这个数据来干点什么,嘿嘿嘿!好吧,我们下个教程再见!
文章来源:DF创客社区