我爱胆机妙音 发表于 2012-6-20 14:02:08

请教:MPU-6050数据读取

本帖最后由 我爱胆机妙音 于 2012-6-21 21:15 编辑

先上代码:#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;

float final_ax;
float final_ay;
float final_az;
float final_gx;
float final_gy;
float final_gz;

#define LED_PIN 13
bool blinkState = false;

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(38400);

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

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

    // configure Arduino LED for
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
   final_ax=ax/16384;
   final_ay=ay/16384;
   final_az=az/16384;
   final_gx=gx/131;
   final_gy=gy/131;
   final_gz=gz/131;

    // 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(final_ax); Serial.print("\t");
    Serial.print(final_ay); Serial.print("\t");
    Serial.print(final_az); Serial.print("\t");
    Serial.print(final_gx); Serial.print("\t");
    Serial.print(final_gy); Serial.print("\t");
    Serial.println(final_gz);

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
}已经添加过简单的算法,但为什么读出的数据全是整数?用力摇晃传感器时加速度显示为1g或-1g
怎样显示小数?谢谢各位

Randy 发表于 2012-6-20 14:34:34

请看i一下下面两个帖子,看是否对你有帮助!
http://www.geek-workshop.com/forum.php?mod=viewthread&tid=1017&extra=page%3D1
http://www.geek-workshop.com/forum.php?mod=viewthread&tid=894&extra=page%3D4

yimenwang 发表于 2012-9-26 14:51:24

final_ax=ax/16384.0;
都这样修改一下吧!后面加.0
:lol

玄冰之神 发表于 2013-5-22 17:49:39

。。。。真郁闷。一天数据还是没调取出来:Q:Q:Q

潇湘子轩 发表于 2013-5-27 20:14:49

改成:final_ax=ax/16384.00;
   final_ay=ay/16384.00;
   final_az=az/16384.00;
   final_gx=gx/131.00;
   final_gy=gy/131.00;
   final_gz=gz/131.00;
试一试
页: [1]
查看完整版本: 请教:MPU-6050数据读取