我正在测试MPU6050,我注意到,在我开始移动设备之后,这些值一直在变化;请看一下代码。
启动串行监视器后,一切都将为零;如果我持有该设备并开始使用它,则可以看到该值略有移动,即使我将它以平坦的位置返回到表中,该值也会增加,直到设备呈现非平坦值为止。
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
// ============== LEDs Setup (Imad) ================
int roll_Left = 9;
int pitch_Up = 10;
int center = 11;
int pitch_Down = 12;
int roll_Right = 13;
// =================================================
// Timers
unsigned long timer = 0;
float timeStep = 0.01;
// Pitch, Roll and Yaw values
float pitch = 0;
float roll = 0;
float yaw = 0;
void setup()
{
Serial.begin(115200);
//============= Light On Imad ===========
pinMode(roll_Left, OUTPUT);
pinMode(pitch_Up, OUTPUT);
pinMode(center, OUTPUT);
pinMode(pitch_Down, OUTPUT);
pinMode(roll_Right, OUTPUT);
//======================================
// Initialize MPU6050
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
// Calibrate gyroscope. The calibration must be at rest.
// If you don't want calibrate, comment this line.
mpu.calibrateGyro();
// Set threshold sensivty. Default 3.
// If you don't want use threshold, comment this line or set 0.
mpu.setThreshold(3);
}
void loop()
{
timer = millis();
/*
//================ Imad Lights ==========
digitalWrite(roll_Left, HIGH);
digitalWrite(pitch_Up, HIGH);
digitalWrite(center, HIGH);
digitalWrite(pitch_Down, HIGH);
digitalWrite(roll_Right, HIGH);
//=======================================
*/
// Read normalized values
Vector norm = mpu.readNormalizeGyro();
// Calculate Pitch, Roll and Yaw
pitch = pitch + norm.YAxis * timeStep;
roll = roll + norm.XAxis * timeStep;
yaw = yaw + norm.ZAxis * timeStep;
// Output raw
if ((pitch <= 2) && (pitch >= -2) && (roll <= 2 )&& (roll >= -2)) {
digitalWrite(center, HIGH);
}else {
digitalWrite(center, LOW);
}
Serial.print(" Pitch = ");
Serial.print(pitch);
if (pitch > 3) {
digitalWrite(pitch_Up, HIGH);
}else if (pitch < -3) {
digitalWrite(pitch_Down, HIGH);
}else {
digitalWrite(pitch_Up, LOW);
digitalWrite(pitch_Down, LOW);
}
Serial.print(" Roll = ");
Serial.print(roll);
if (roll > 3) {
digitalWrite(roll_Right, HIGH);
}else if (roll < -3) {
digitalWrite(roll_Left, HIGH);
}else {
digitalWrite(roll_Left, LOW);
digitalWrite(roll_Right, LOW);
}
Serial.print(" Yaw = ");
Serial.println(yaw);
// Wait to full timeStep period
delay((timeStep*1000) - (millis() - timer));
}如果我们把设备放回相同的位置,我们需要添加什么来保持值不变吗?
发布于 2022-07-11 05:40:08
Serial.print在代码中造成了巨大的滞后,使得滚动、俯仰和偏航的计算不准确。本文的第4步讨论了这种滞后:https://www.instructables.com/How-to-Make-a-Robot-Car-Drive-Straight-and-Turn-Ex/
本文还包括MPU6050的Arduino代码。
https://stackoverflow.com/questions/72568708
复制相似问题