Arduino操作MPU6050模块

setFilterBandwidth():设置滤波器带宽,参数有MPU6050_BAND_260_HZ,MPU6050_BAND_184_HZ,MPU6050_BAND_94_HZ,MPU6050_BAND_44_HZ,MPU6050_BAND_21_HZ,MPU6050_BAND_10_HZ,MPU6050_BAND_5_HZ。Adfruit_MPU6050库里还包括设置中断,停止测量某一数值等功能,详细见官方说明文档。调用以上库, 创建Adafruit_MPU6050对象mpu。
摘要由CSDN通过智能技术生成

MPU6050是集成三轴陀螺仪,三轴加速度计,温度传感器于一体的模块。本文档基于Adafruit_MPU6050实现MPU6050模块基本操作。

Adafruit_MPU6050库:

https://github.com/adafruit/Adafruit_MPU6050

Adafruit_MPU6050依赖以下库,需要在Arduino编译器里分别添加:

Adafruit_BusIO: https://github.com/adafruit/Adafruit_BusIO
Adafruit_Sensor: https://github.com/adafruit/Adafruit_Sensor
Adafruit_GFX_Library: https://github.com/adafruit/Adafruit-GFX-Library
Adafruit_SSD1306: https://github.com/adafruit/Adafruit_SSD1306

1 接线:
在这里插入图片描述

如图:
VCC -> 5V
GND -> GND
SCL -> A5
SDA -> A4
INT -> 2

2 编写读取程序:

Adafruit_MPU6050完整说明文档 https://adafruit.github.io/Adafruit_MPU6050/html/class_adafruit___m_p_u6050.html

#include <Adafruit_MPU6050.h>
#include <Adafruit_sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup() {
   
  // put your setup code here, to run once:
  Serial.begin(115200);

  // initialize mpu
  if (!mpu.begin()) {
   
    Serial.println("Failed to start MPU6050");
  } else {
   
    Serial.println("Start MPU6050");
  }

  // display MPU6050 settings
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
   
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  • 14
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是基于ArduinoMPU6050传感器获取角度的代码示例: ``` #include <Wire.h> #include <MPU6050.h> MPU6050 mpu; int16_t accX, accY, accZ; int16_t gyroX, gyroY, gyroZ; float accAngleX, accAngleY; float gyroAngleX, gyroAngleY, gyroAngleZ; float roll, pitch, yaw; void setup() { Serial.begin(9600); Wire.begin(); mpu.initialize(); mpu.dmpInitialize(); mpu.setDMPEnabled(true); // load offsets from calibrationSketch.ino mpu.setXAccelOffset(846); mpu.setYAccelOffset(1061); mpu.setZAccelOffset(1343); mpu.setXGyroOffset(39); mpu.setYGyroOffset(-9); mpu.setZGyroOffset(-68); // calculate offsets from calibrationSketch.ino AccXYZoffset = (0.5 / 16384) * 9.81 * 1000; // 0.5 -> scale factor to get to SI (9.81 m/s^2 -> 9810 mm/s^2) GyroXYZoffset = 0.9863; // scale factor to get to SI (gyroscope output in degrees/s) delay(1000); } void loop() { mpu.getMotion6(&accX, &accY, &accZ, &gyroX, &gyroY, &gyroZ); // get raw data from MPU6050 accAngleX = atan(accY / sqrt(pow(accX, 2) + pow(accZ, 2))) * RAD_TO_DEG; // Calculate pitch angle using accelerometer accAngleY = atan(-1 * accX / sqrt(pow(accY, 2) + pow(accZ, 2))) * RAD_TO_DEG; // Calculate roll angle using accelerometer gyroAngleX += gyroX / 131.0 * GyroXYZoffset * (millis() - prevTime) / 1000.0; // Calculate pitch angle using gyroscope gyroAngleY += gyroY / 131.0 * GyroXYZoffset * (millis() - prevTime) / 1000.0; // Calculate roll angle using gyroscope gyroAngleZ += gyroZ / 131.0 * GyroXYZoffset * (millis() - prevTime) / 1000.0; // Calculate yaw angle using gyroscope pitch = 0.98 * gyroAngleX + 0.02 * accAngleX; // Complementary filter to fuse acceleration and gyroscope data for pitch roll = 0.98 * gyroAngleY + 0.02 * accAngleY; // Complementary filter to fuse acceleration and gyroscope data for roll Serial.print("Pitch: "); Serial.print(pitch); Serial.print(", Roll: "); Serial.println(roll); prevTime = millis(); } ``` 这段代码使用了MPU6050传感器来获取姿态角度。其中,通过加速度计计算出俯仰角(pitch)和横滚角(roll),通过陀螺仪计算出偏航角(yaw),然后使用互补滤波器(complementary filter)将加速度计和陀螺仪的数据进行融合得到最终的姿态角度。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值