|
|
不知道各位大神有沒有做個這個問題
小的主要是要用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
小弟先在這,感謝大家了!!
|
|