赞
踩
MPU6050六轴姿态传感器,通常指的是三轴加速度计+三轴陀螺仪。加速度计的本次实验将测量一下加速度。
把每次实验的过程记录下来是一种好的习惯。更是为了方便后续查阅。
Arduino UNO+MPU6050模块
模块采用IIC接口,接线如下,INT不用接也行。
#include "Wire.h" #include "I2Cdev.h" #include "MPU6050.h" #define LED_PIN 13 MPU6050 accelgyro; struct RAW_type { uint8_t x; uint8_t y; uint8_t z; }; int16_t ax, ay, az; int16_t gx, gy, gz; struct RAW_type accel_zero_offsent; char str[512]; bool blinkState = false ; float AcceRatio = 16384.0; float accx,accy,accz; void setup() { int i ; int32_t ax_zero = 0,ay_zero = 0,az_zero = 0 ; Wire.begin(); Serial.begin(9600); Serial.println("Initializing I2C devices..."); accelgyro.initialize(); delay(500) ; accelgyro.setFullScaleAccelRange(MPU6050_ACCEL_FS_2); Serial.println("Testing device connections..."); Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); for( i = 0 ; i < 200 ; i++) { accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); ax_zero += ax ; ay_zero += ay ; az_zero += az ; } accel_zero_offsent.x = ax_zero/200 ; accel_zero_offsent.y = ay_zero/200 ; accel_zero_offsent.z = az_zero/200 ; Serial.print(accel_zero_offsent.x); Serial.print("\t"); Serial.print(accel_zero_offsent.y); Serial.print("\t"); Serial.print(accel_zero_offsent.z); Serial.print("\n"); pinMode(LED_PIN, OUTPUT); }
void loop() { delay(200); accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); //sprintf(str,"%d,%d,%d\n",ax-accel_zero_offsent.x, ay-accel_zero_offsent.y ,az-accel_zero_offsent.z); //Serial.print(str); //delay(50); accx = (float)( ax-accel_zero_offsent.x ); accy = (float)( ay-accel_zero_offsent.y ); accz = (float)( az-accel_zero_offsent.z ); Serial.print("{accx,");Serial.print(accx);Serial.print("}"); Serial.print("{accy,");Serial.print(accy);Serial.print("}"); Serial.print("{accz,");Serial.print(accz);Serial.print("}"); /*delay(50); accx = (float)( ax-accel_zero_offsent.x )/AcceRatio; accy = (float)( ay-accel_zero_offsent.y )/AcceRatio ; accz = (float)( az-accel_zero_offsent.z )/AcceRatio ; Serial.print("{x,");Serial.print(accx);Serial.print("}"); Serial.print("{y,");Serial.print(accy);Serial.print("}"); Serial.print("{z,");Serial.print(accz);Serial.print("}"); blinkState = !blinkState; digitalWrite(LED_PIN, blinkState);*/ }
随便晃动几下,用JXChart(查看使用方法) 采集如下图:
实时接收数据如下:
2024/06/07 20:04:15.336(接收)ascii:{accx,-144.00}{accy,-2405.00}{accz,15747.00}
2024/06/07 20:04:15.541(接收)ascii:{accx,-1032.00}{accy,-2449.00}{accz,16167.00}
2024/06/07 20:04:15.744(接收)ascii:{accx,-1232.00}{accy,-2617.00}{accz,15907.00}
2024/06/07 20:04:15.948(接收)ascii:{accx,-1140.00}{accy,-2309.00}{accz,16147.00}
2024/06/07 20:04:16.150(接收)ascii:{accx,-324.00}{accy,-2881.00}{accz,15567.00}
2024/06/07 20:04:16.354(接收)ascii:{accx,-784.00}{accy,-2457.00}{accz,15895.00}
2024/06/07 20:04:16.557(接收)ascii:{accx,-724.00}{accy,-2705.00}{accz,15855.00}
2024/06/07 20:04:16.762(接收)ascii:{accx,-1152.00}{accy,-2217.00}{accz,16423.00}
2024/06/07 20:04:16.966(接收)ascii:{accx,-1132.00}{accy,-3105.00}{accz,16043.00}
2024/06/07 20:04:17.169(接收)ascii:{accx,-1480.00}{accy,-3193.00}{accz,16467.00}
2024/06/07 20:04:17.372(接收)ascii:{accx,-520.00}{accy,-2665.00}{accz,15747.00}
2024/06/07 20:04:17.576(接收)ascii:{accx,-1088.00}{accy,-2585.00}{accz,15687.00}
2024/06/07 20:04:17.779(接收)ascii:{accx,-160.00}{accy,-3121.00}{accz,15795.00}
2024/06/07 20:04:17.982(接收)ascii:{accx,-804.00}{accy,-3109.00}{accz,15915.00}
2024/06/07 20:04:18.186(接收)ascii:{accx,-236.00}{accy,-3305.00}{accz,15727.00}
2024/06/07 20:04:18.389(接收)ascii:{accx,-660.00}{accy,-2853.00}{accz,15647.00}
2024/06/07 20:04:18.593(接收)ascii:{accx,-780.00}{accy,-2721.00}{accz,15899.00}
2024/06/07 20:04:18.797(接收)ascii:{accx,-1284.00}{accy,-2353.00}{accz,15887.00}
2024/06/07 20:04:19.001(接收)ascii:{accx,-1000.00}{accy,-3085.00}{accz,16043.00}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。