我正在尝试计算我的Quadcopter项目的偏航俯仰和滚转。我从Here获取了代码。 我成功地计算了滚动和俯仰角度,并对这些值应用了补充过滤器,这些值非常接近Jeff Rowberg的代码计算出的值。 我想从这个RAW数据计算偏航值,而不是使用Jeff的代码。我知道由RAW数据计算出的偏航不会像杰夫那样准确,但有人可以告诉我计算加速度计和陀螺仪RAW数据的偏航值背后的公式或数学吗? 这是我正在使用的代码
#include<Wire.h>
const int MPU=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
double compAngleX,compAngleY,/*gzangle,*/compAnglez,timer;
double accXangle ,accYangle,acczangle ,gyroXrate ,gyroYrate,gyroZrate;
double gyroXAngle, gyroYAngle, gyroZAngle;
// float rgyro,w;
int ap=0.955;
void setup()
{
Serial.begin(115200);
/////////////////////// SENSOR READING//////////
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
///////////////////////////////////////
}
void loop()
{
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
accXangle = (atan2(AcY, AcZ) * RAD_TO_DEG);
accYangle = (atan2(AcX, AcZ) * RAD_TO_DEG);
acczangle = (atan2(AcX,AcY) * RAD_TO_DEG);/* my attempt to calculate yaw but not correct*/
gyroXrate = GyX / 16.5;
gyroYrate = GyY / 16.5;
gyroZrate = GyZ/ 16.5;
timer = millis();
//angular position
gyroXAngle += gyroXrate * (millis()-timer)/1000;
gyroYAngle += gyroYrate * (millis()-timer)/1000;
gyroZAngle += gyroZrate * (millis()-timer)/1000;/* my attempt to calculate yaw but not correct*/
//---------------------------\\
//COMPLIMENTRY FILTER STARTED\\
//---------------------------\\
compAngleX = ap * (compAngleX + gyroXAngle) + (1-ap) * accXangle;
compAngleY = ap * (compAngleY + gyroYAngle) + (1-ap) * accYangle;
compAnglez = ap * (compAnglez + gyroZAngle) + (1-ap) * acczangle; /*yaw but not correct*/
//---------------------------\\
//COMPLIMENTRY FILTER ENDED \\
//---------------------------\\
Serial.print("ypr\t");
Serial.print(compAnglez);
Serial.print("\t");
Serial.print(compAngleY);
Serial.print("\t");
Serial.println(compAngleX);
}