查看: 8741|回复: 1

Arduino/Genuino 101 入门教程四:processing玩转六轴数据

[复制链接]
主要就是用processing对获取的六轴数据进行可视化处理。
先看下效果:软件在电脑桌面虚拟化一个Arduino 101模型,当你转动手中的实体Arduino 101开发板的时候,桌面的模型也会跟着转动。(因为Yaw的数据没有电子罗盘的修正,模型会飘,这里注释掉对Yaw数据的读取会得到一个较好的体验,期待拿到DFRobot集成了电子罗盘的Inter Curie开发板)

1.png


【产品链接】:  Arduino 101

操作步骤:
1.下载必要的软件,Processing (我下载的是Windows64位版本的)
   下载地址:http://pan.baidu.com/s/1o6Taziy
   官网下载地址(需翻墙):https://processing.org/


2.下载Arduino IDE 的代码到开发板,这里注释了对Yaw数据的获取:
  1. /*
  2.   ===============================================
  3.   Example sketch for CurieIMU library for Intel(R) Curie(TM) devices.
  4.   Copyright (c) 2015 Intel Corporation.  All rights reserved.

  5.   Based on I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050
  6.   class by Jeff Rowberg: https://github.com/jrowberg/i2cdevlib

  7.   ===============================================
  8.   I2Cdev device library code is placed under the MIT license
  9.   Copyright (c) 2011 Jeff Rowberg

  10.   Permission is hereby granted, free of charge, to any person obtaining a copy
  11.   of this software and associated documentation files (the "Software"), to deal
  12.   in the Software without restriction, including without limitation the rights
  13.   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14.   copies of the Software, and to permit persons to whom the Software is
  15.   furnished to do so, subject to the following conditions:

  16.   The above copyright notice and this permission notice shall be included in
  17.   all copies or substantial portions of the Software.

  18.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19.   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20.   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21.   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22.   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23.   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24.   THE SOFTWARE.
  25.   ===============================================

  26.   Genuino 101 CurieIMU Orientation Visualiser
  27.   Hardware Required:
  28.   * Arduino/Genuino 101

  29.   Modified Nov 2015
  30.   by Helena Bisby <support@arduino.cc>
  31.   This example code is in the public domain
  32.   http://arduino.cc/en/Tutorial/Genuino101CurieIMUOrientationVisualiser
  33. */

  34. #include <CurieIMU.h>
  35. #include <MadgwickAHRS.h>

  36. Madgwick filter; // initialise Madgwick object
  37. int ax, ay, az;
  38. int gx, gy, gz;
  39. float yaw = 0.0;
  40. float pitch;
  41. float roll;
  42. int factor = 800; // variable by which to divide gyroscope values, used to control sensitivity
  43. // note that an increased baud rate requires an increase in value of factor

  44. int calibrateOffsets = 1; // int to determine whether calibration takes place or not


  45. void setup() {
  46.   // initialize Serial communication
  47.   Serial.begin(9600);

  48.   // initialize device
  49.   CurieIMU.begin();

  50.   if (calibrateOffsets == 1) {
  51.     // use the code below to calibrate accel/gyro offset values
  52.     Serial.println("Internal sensor offsets BEFORE calibration...");
  53.     Serial.print(CurieIMU.getAccelerometerOffset(X_AXIS)); Serial.print("\t");
  54.     Serial.print(CurieIMU.getAccelerometerOffset(Y_AXIS)); Serial.print("\t");
  55.     Serial.print(CurieIMU.getAccelerometerOffset(Z_AXIS)); Serial.print("\t");
  56.     Serial.print(CurieIMU.getGyroOffset(X_AXIS)); Serial.print("\t");
  57.     Serial.print(CurieIMU.getGyroOffset(Y_AXIS)); Serial.print("\t");
  58.     Serial.print(CurieIMU.getGyroOffset(Z_AXIS)); Serial.print("\t");
  59.     Serial.println("");

  60.     // To manually configure offset compensation values, use the following methods instead of the autoCalibrate...() methods below
  61.     //    CurieIMU.setGyroOffset(X_AXIS, 220);
  62.     //    CurieIMU.setGyroOffset(Y_AXIS, 76);
  63.     //    CurieIMU.setGyroOffset(Z_AXIS, -85);
  64.     //    CurieIMU.setAccelerometerOffset(X_AXIS, -76);
  65.     //    CurieIMU.setAccelerometerOffset(Y_AXIS, -235);
  66.     //    CurieIMU.setAccelerometerOffset(Z_AXIS, 168);

  67.     //IMU device must be resting in a horizontal position for the following calibration procedure to work correctly!

  68.     Serial.print("Starting Gyroscope calibration...");
  69.     CurieIMU.autoCalibrateGyroOffset();
  70.     Serial.println(" Done");
  71.     Serial.print("Starting Acceleration calibration...");
  72.     CurieIMU.autoCalibrateAccelerometerOffset(X_AXIS, 0);
  73.     CurieIMU.autoCalibrateAccelerometerOffset(Y_AXIS, 0);
  74.     CurieIMU.autoCalibrateAccelerometerOffset(Z_AXIS, 1);
  75.     Serial.println(" Done");

  76.     Serial.println("Internal sensor offsets AFTER calibration...");
  77.     Serial.print(CurieIMU.getAccelerometerOffset(X_AXIS)); Serial.print("\t");
  78.     Serial.print(CurieIMU.getAccelerometerOffset(Y_AXIS)); Serial.print("\t");
  79.     Serial.print(CurieIMU.getAccelerometerOffset(Z_AXIS)); Serial.print("\t");
  80.     Serial.print(CurieIMU.getAccelerometerOffset(X_AXIS)); Serial.print("\t");
  81.     Serial.print(CurieIMU.getAccelerometerOffset(Y_AXIS)); Serial.print("\t");
  82.     Serial.print(CurieIMU.getAccelerometerOffset(Z_AXIS)); Serial.print("\t");
  83.     Serial.println("");
  84.   }
  85. }

  86. void loop() {
  87.   // read raw accel/gyro measurements from device
  88.   CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz);

  89.   // use function from MagdwickAHRS.h to return quaternions
  90.   filter.updateIMU(gx / factor, gy / factor, gz / factor, ax, ay, az);

  91.   // functions to find yaw roll and pitch from quaternions
  92.   //yaw = filter.getYaw();
  93.   roll = filter.getRoll();
  94.   pitch = filter.getPitch();

  95.   // print gyro and accel values for debugging only, comment out when running Processing
  96.   /*
  97.   Serial.print(ax); Serial.print("\t");
  98.   Serial.print(ay); Serial.print("\t");
  99.   Serial.print(az); Serial.print("\t");
  100.   Serial.print(gx); Serial.print("\t");
  101.   Serial.print(gy); Serial.print("\t");
  102.   Serial.print(gz); Serial.print("\t");
  103.   Serial.println("");
  104.   */

  105.   if (Serial.available() > 0) {
  106.     int val = Serial.read();
  107.     if (val == 's') { // if incoming serial is "s"
  108.       Serial.print(yaw);
  109.       Serial.print(","); // print comma so values can be parsed
  110.       Serial.print(pitch);
  111.       Serial.print(","); // print comma so values can be parsed
  112.       Serial.println(roll);
  113.     }
  114.   }
  115. }
复制代码

3.打开Processing软件:解压下载的Processing软件包,里面有个exe文件,双击运行,添加Processing程序:
  1. import processing.serial.*;
  2. Serial myPort;

  3. int newLine = 13; // new line character in ASCII
  4. float yaw;
  5. float pitch;
  6. float roll;
  7. String message;
  8. String [] ypr = new String [3];

  9. void setup()
  10. {
  11.   size(600, 500, P3D);

  12.   /*Set my serial port to same as Arduino, baud rate 9600*/
  13.   myPort = new Serial(this, Serial.list()[0], 9600); // if you have only ONE COM port active
  14.   //myPort = new Serial(this, "COM5", 9600);  // if you know the 101 COM port

  15.   textSize(16); // set text size
  16.   textMode(SHAPE); // set text mode to shape
  17. }

  18. void draw()
  19. {
  20.   serialEvent();  // read and parse incoming serial message
  21.   background(255); // set background to white

  22.   translate(width/2, height/2); // set position to centre

  23.   pushMatrix(); // begin object

  24.   rotateX(pitch); // RotateX pitch value
  25.   rotateY(-yaw); // yaw
  26.   rotateZ(-roll); // roll

  27.   drawArduino(); // function to draw rough Arduino shape

  28.   popMatrix(); // end of object

  29.   // Print values to console
  30.   print(pitch);
  31.   print("\t");
  32.   print(roll);
  33.   print("\t");
  34.   print(-yaw);  
  35.   println("\t");

  36.   myPort.write("s"); // write an "s" to receive more data from Arduino
  37. }

  38. void serialEvent()
  39. {
  40.   message = myPort.readStringUntil(newLine); // read from port until new line (ASCII code 13)
  41.   if (message != null) {
  42.     ypr = split(message, ","); // split message by commas and store in String array
  43.     yaw = float(ypr[0]); // convert to float yaw
  44.     pitch = float(ypr[1]); // convert to float pitch
  45.     roll = float(ypr[2]); // convert to float roll
  46.   }
  47. }
  48. void drawArduino() {
  49.   /* function contains shape(s) that are rotated with the IMU */
  50.   stroke(0, 90, 90); // set outline colour to darker teal
  51.   fill(0, 130, 130); // set fill colour to lighter teal
  52.   box(300, 10, 200); // draw Arduino board base shape

  53.   stroke(0); // set outline colour to black
  54.   fill(80); // set fill colour to dark grey

  55.   translate(60, -10, 90); // set position to edge of Arduino box
  56.   box(170, 20, 10); // draw pin header as box

  57.   translate(-20, 0, -180); // set position to other edge of Arduino box
  58.   box(210, 20, 10); // draw other pin header as box
  59. }
复制代码

4.保持数据线与101开发板的连接,打开软件左上角的三角形按钮,稍等片刻,模型就出来了,后面就可以自己转着玩了。
2.jpg

OK,操作完毕,很简单吧,试试看!



我再来转一会玩玩
3.jpg


看累了没,开心一刻:
一大学生被敌人抓了,敌人把他绑在了电线杆上,然后问他:说,你是哪里的?不说就电死你!大学生回了敌人一句话,结果被电死了,他说:我是电大的!

Arduino/Genuino 101 入门教程】

  * DF创客社区版权所有,
欢迎转载
  转载请务必标注来源: DF创客社区+作者姓名+原文网址。





iooops  版主

发表于 2016-3-20 21:37:10

电大的 - -
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
wifi气象站

硬件清单

btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2019 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail