极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 5730|回复: 0

求助各位大人,有關於MPU6050與matlab存取資料的問題

[复制链接]
发表于 2014-5-16 21:48:25 | 显示全部楼层 |阅读模式
不知道各位大神有沒有做個這個問題
小的主要是要用MPU6050讀出來的値 讓matlab的GUI可以顯示
可是一直用不好QQ
把它拆成8 8 傳 可是一直用不好 不知道是什麼問題.....

這是小弟在Arduino的code:

#include <helper_3dmath.h>
#include <MPU6050.h>
#include <MPU6050_6Axis_MotionApps20.h>
#include <MPU6050_9Axis_MotionApps41.h>

// ---------------------------------------------------------------------------
// Structure:
//                                         
//                                           Sub I2C
//                                        ______^ ______
//                                       |              |
// ----------
//  Arduino  |               ----------------          ------------
//   UNO     |- 5V -> 3.3 V- | MPU 6050     |          |
//           |- GND ------GND| Acceleration,|---SDA ---|  
//           |- A4 -------SDA| Gyro, Temp   |---SCL ---|
//           |- A5 -------SCL|              |          |           |
//           |               ----------------          -------------
//-----------
//                       |___________________ _______________________|
//                                           V
//                                   Integrated IMU sensor
//
// Pull-up resistors are integrated in the sensor board.
// ---------------------------------------------------------------------------

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#include <Wire.h>

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"

// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define LED_PIN 13
bool blinkState = false;
int mode = -1;

void setup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
  Wire.begin();

  // initialize serial communication
  // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
  // it's really up to you depending on your project)
  Serial.begin(9600);

  // initialize device
  Serial.println("Initializing I2C devices...");
  accelgyro.initialize();

  // verify connection
  Serial.println("Testing device connections...");
  if(accelgyro.testConnection())
    {
    Serial.println("MPU6050 connection successful");
    }
  else
    {
    Serial.println("MPU6050 connection failed");
    }

  // configure Arduino LED for output
  pinMode(LED_PIN, OUTPUT);
    //check serial conmunication - acknowledjenent routine
  Serial.println('a');
  char a = 'b';
  while(a!='a')
  {
  //wait for a specifiv charachter from the PC
  a=Serial.read();
  }

}

void loop() {
  // read raw accel/gyro measurements from device
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
   //String dataString = String(ax) + "\t" + String(ay) + "\t" + String(az)   
  // these methods (and a few others) are also available
  //accelgyro.getAcceleration(&ax, &ay, &az);
  //accelgyro.getRotation(&gx, &gy, &gz);

  // display tab-separated accel/gyro x/y/z values
/*  Serial.print("a/g:\t");
  Serial.print(ax); Serial.print("\t");
  Serial.print(ay); Serial.print("\t");
  Serial.print(az); Serial.print("\t");
  Serial.print(gx); Serial.print("\t");
  Serial.print(gy); Serial.print("\t");
  Serial.println(gz);
  */
   if(Serial.available()>0)  //check is any have data has send to PC
    {  
      mode=Serial.read();     //check if there is a request for
      if (mode == 'R')    //used to read accalerometer values
        {
          Serial.println(  ax & 0xff );
          Serial.println( ( ax >> 8 ) & 0xff);
          Serial.println(  ay & 0xff );
          Serial.println( ( ay >> 8 ) & 0xff);
          Serial.println(  az & 0xff );
          Serial.println( ( az >> 8 ) & 0xff);
        }
      //wait 20 milliseconds before the next loop for ADC to settle after the last reading
      delay(20);
    }
  
  // blink LED to indicate activity
  blinkState = !blinkState;
  digitalWrite(LED_PIN, blinkState);
  delay(20);
}
///
/////////
////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////
/////////
///
Matlab串列傳輸端code:

function [ gx gy gz ] = readAcc( out,calCo )

    fprintf(out.s,'R');
   
    %read value from accelerometer
    a(0) = fscanf(out.s,'%u');
    a(1) = fscanf(out.s,'%u');
    readings(1) = (  bitshift( str2int( a(1) ),8 ) & 0 ) + ( str2int( a(0) ) & 255 );
    a(2) = fscanf(out.s,'%s');
    a(3) = fscanf(out.s,'%s');
    readings(2) = (  bitshift( str2int( a(3) ),8 ) & 0 ) + ( str2int( a(2) ) & 255 ) ;
    a(4) = fscanf(out.s,'%s');
    a(5) = fscanf(out.s,'%s');
    readings(3) = (  bitshift( str2int( a(5) ),8 ) & 0 ) + ( str2int( a(4) ) & 255 ) ;

%     readings(1) = fscanf(out.s,'%u');
%     readings(1) = fscanf(out.s,'%u');
%     readings(1) = fscanf(out.s,'%u');

    %determion what offset and gain values to use
    offset = calCo.offset;
    gain = calCo.g;
    accel = (readings -offset) ./ gain;
   
    %map analog inputs to axes
    gx = accel(1);
    gy = accel(2);
    gz = accel(3);
end

小弟先在這,感謝大家了!!
回复

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 21:03 , Processed in 0.035303 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表