本帖最后由 我爱胆机妙音 于 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
怎样显示小数?谢谢各位 |