当前位置:   article > 正文

Arduino 系列 - MPU6050 六轴姿态传感器模块实验-加速度测量_ardiino的六轴传感器

ardiino的六轴传感器

内容简介

MPU6050六轴姿态传感器,通常指的是三轴加速度计+三轴陀螺仪。加速度计的本次实验将测量一下加速度。

把每次实验的过程记录下来是一种好的习惯。更是为了方便后续查阅。

开发环境

Arduino UNO+MPU6050模块
模块采用IIC接口,接线如下,INT不用接也行。
在这里插入图片描述

Arduino范例代码

模块初始化

#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); 
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

主程序


 
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);*/ 
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

随便晃动几下,用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}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/911479
推荐阅读
相关标签
  

闽ICP备14008679号