Arduino+Processing 互动 MPU6050

发布于: 27 January, 2014
分享:

/* *@Author: TONYLABS *@Date: 2013.04.03 */

import processing.serial.; import processing.opengl.;

Serial myPort;

float[] value = new float[4]; String[] msg = new String[5]; float x, y, z;

void setup() {  size(600, 600, OPENGL);  noStroke();  String portName = Serial.list()[0];  myPort = new Serial(this, portName, 9600); }

void draw() {  serialEvent();  rotateX(value[0]);  rotateY(value[1]);  rotateZ(value[2]);  background(0);  fill(246, 225, 65);  lights();  translate(200, 200);  box(50); }

void serialEvent() {  //@et msg till line break (ASCII > 13)  for (int i=0;i<4;i++)  {    msg[i] = myPort.readStringUntil(13);    if (msg[i] != null)    {      value[i] = float(msg[i]);      println(value);    }  } }

 MPU6050 FIFO 输出模式例程

#include "Wire.h" #include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h"

MPU6050 mpu;

#define OUTPUT_READABLE_EULER

bool dmpReady =false; uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error) uint16_t packetSize;    // expected DMP packet size (default is 42 bytes) uint16_t fifoCount;     // count of all bytes currently in FIFO uint8_t fifoBuffer[64]; // FIFO storage buffer

float euler[3]; Quaternion q;

volatile bool mpuInterrupt = false; void dmpDataReady() {  mpuInterrupt=true; }

void setup() {  Wire.begin();  Serial.begin(9600);  mpu.initialize();  while(Serial.available() && Serial.read());  devStatus= mpu.dmpInitialize();

 if(devStatus == 0)  {    mpu.setDMPEnabled(true);    attachInterrupt(0,dmpDataReady,RISING);    mpuIntStatus= mpu.getIntStatus();    dmpReady=true;    packetSize=mpu.dmpGetFIFOPacketSize();  }  else  {    Serial.print(F("dmp initialization failed"));  } }

void loop() {

 if(!dmpReady)  return;  mpuInterrupt = false;  mpuIntStatus = mpu.getIntStatus();  fifoCount = mpu.getFIFOCount();

 if ((mpuIntStatus & 0x10) || fifoCount == 1024)  {        mpu.resetFIFO();       Serial.println(F("FIFO overflow!"));

 }    else if (mpuIntStatus & 0x02)    {        while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();        mpu.getFIFOBytes(fifoBuffer, packetSize);

       // track FIFO count here in case there is > 1 packet available        // (this lets us immediately read more without waiting for an interrupt)        fifoCount -= packetSize;

         // #ifdef OUTPUT_READABLE_EULER            // display Euler angles in degrees            mpu.dmpGetQuaternion(&q, fifoBuffer);            mpu.dmpGetEuler(euler, &q);           /* Serial.print("euler\t");            Serial.print(euler[0] * 180/M_PI);            Serial.print("\t");            Serial.print(euler[1] * 180/M_PI);            Serial.print("\t");            Serial.println(euler[2] * 180/M_PI);           #endif*/           for(int i=0;i<4;i++)           {           Serial.println(euler[i],4);           delay(10);           }    }

}

 MPU6050 简单数值输出模式:

#include "Wire.h" #include "I2Cdev.h" #include "MPU6050.h"

MPU6050 accelgyro;

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

void setup() {  Wire.begin();  Serial.begin(115200);  accelgyro.initialize();  accelgyro.testConnection(); }

void loop() {  //@Read raw accel/gyro measurements from device  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);  Serial.println(ax/10000);  delay(100); }  

分享:

0 留言

留言

您的留言将被人工审核,请勿发表色情、反动言论。