我想和我的MPU6050一起使用我的ESP32。(与特定ESP董事会的链接)
这不是我第一次使用MPU6050。我以前在一个Arduino Uno和MPU6050_light.h库的项目中使用过它。效果很好。现在我正在用我的新ESP32试一试。
我尝试了MPU6050_light和Adafruit MPU6050库,但是第一个返回
MPU6050状况:2
最后一个抛出错误:
找不到MPU6050芯片
这些电线绝对是正确的:
我使用I2C扫描仪来检查是否找到了MPU,以及它有什么附加功能。这也起作用了。这是标准入口0x68。我观察到,当我拔出一根线,而不是再次连接它,发现的入口变化到0x69。
所以MPU被发现了。有谁知道怎样才能解决我的问题吗?
使用MPU6050_light.h的代码
#include <Arduino.h>
#include <MPU6050_light.h>
#include "Wire.h"
MPU6050 mpu(Wire);
long angle = 0;
unsigned long timer = 0;
void gyroSetUp()
{
byte status = mpu.begin();
Serial.print("MPU6050 status: ");
Serial.println(status);
while (status != 0)
{
} // stop everything if could not connect to MPU6050
Serial.println("Calculating offsets, do not move MPU6050");
delay(1000);
mpu.calcOffsets(true, true); // gyro and accelero
Serial.println("Done!\n");
}
void PrintAngles()
{
Serial.print("X: ");
Serial.print(mpu.getAngleX());
Serial.print("\tY: ");
Serial.print(mpu.getAngleY());
Serial.print("\tZ: ");
Serial.print(mpu.getAngleZ());
Serial.print("\n");
}
void setup()
{
Serial.begin(115200);
Serial.println("Starting");
Wire.begin();
gyroSetUp();
}
void loop()
{
mpu.update();
if ((millis() - timer) > 1000)
{
PrintAngles();
timer = millis();
}
}带有Adafruit MPU6050的代码
/* Get tilt angles on X and Y, and rotation angle on Z
* Angles are given in degrees
*
* License: MIT
*/
#include "Wire.h"
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
sensors_event_t a, g, temp;
float gyroX, gyroY, gyroZ;
float gyroXerror = 0.07;
float gyroYerror = 0.03;
float gyroZerror = 0.01;
unsigned long lastTime = 0;
unsigned long gyroDelay = 10;
void initMPU()
{
if (!mpu.begin())
{
Serial.println("Failed to find MPU6050 chip");
while (1)
{
delay(10);
}
}
Serial.println("MPU6050 Found!");
}
void getGyroReadings()
{
mpu.getEvent(&a, &g, &temp);
float gyroX_temp = g.gyro.x;
if (abs(gyroX_temp) > gyroXerror)
{
gyroX += gyroX_temp / 50.00;
}
float gyroY_temp = g.gyro.y;
if (abs(gyroY_temp) > gyroYerror)
{
gyroY += gyroY_temp / 70.00;
}
float gyroZ_temp = g.gyro.z;
if (abs(gyroZ_temp) > gyroZerror)
{
gyroZ += gyroZ_temp / 90.00;
}
Serial.print("X: ");
Serial.print(String(gyroX));
Serial.print("\tY: ");
Serial.print(String(gyroY));
Serial.print("\tZ: ");
Serial.print(String(gyroZ));
Serial.print("\n");
}
void setup()
{
Serial.begin(115200);
initMPU();
}
void loop()
{
if ((millis() - lastTime) > gyroDelay)
{
getGyroReadings();
lastTime = millis();
}
}发布于 2022-07-06 16:29:40
我找到了解决我的具体问题的方法。花了我一生中的4小时。如果使用MPU6050_light.h,则示例草图中有一个错误的函数:
mpu.calcOffsets();
你必须把它改为:
mpu.calcOffsets(true, true);
我不明白为什么这个函数是这样设计的,但是没有参数就可以使用它。
我仍然不知道为什么其他图书馆不能工作,但我希望我已经节省了别人的时间在未来。
https://stackoverflow.com/questions/72886269
复制相似问题