最近和朋友在玩航模,想玩FPV。就想着搞一个“头追”。上X宝一搜,一般的都要一百多。再上上各种diy论坛发现有各种大神都已经diy出来了。本着自己动手丰衣足食的精神。我也买来板子准备自己做一个。首先说明,如果你不想深入地了解整个原理,可以看其他帖子,按其他帖子来是可以做出来的。这个帖子我希望有更多的大神来给我指导一下,因为我想借做头追,了解一下mpu6050以及卡尔曼滤波。后面呢我还想自己做一个简单的飞控。废话少说。先说一下材料:
1:Arduino Uno R3板一块;
2:MPU6050一块
硬件准备好,接下来就要软件了。使用arduino,当然用arduinoIDE了,找度娘很简单就找到了。还有一个看串口波形的软件,我用的Serialchart 挺简单的(我是新手,也只用过那个。。。)
接下来说程序
- /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
- This software may be distributed and modified under the terms of the GNU
- General Public License version 2 (GPL2) as published by the Free Software
- Foundation and appearing in the file GPL2.TXT included in the packaging of
- this file. Please note that GPL2 Section 2[b] requires that all works based
- on this software must also be made publicly available under the terms of
- the GPL2 ("Copyleft").
- Contact information
- -------------------
- Kristian Lauszus, TKJ Electronics
- Web : http://www.tkjelectronics.com
- e-mail : [email][email protected][/email]
- */
- #include <Wire.h> //mpu6050和arduino通过I2C通信,所以要包含这个库
- #include <Kalman.h> //卡尔曼滤波函数
- Kalman kalmanX; // 对X,Y轴创建卡尔曼实例
- Kalman kalmanY;
- /* IMU Data */
- double accX, accY, accZ;
- double gyroX, gyroY, gyroZ;//从mpu6050读出的六个数据
- double gyroXangle, gyroYangle; // Angle calculate using the gyro only
- double compAngleX, compAngleY; // Calculated angle using a complementary filter
- double kalAngleX, kalAngleY; // Calculated angle using a Kalman filter
- double angleX, angleY, angle, angleZ; //finall angle
- uint32_t timer;
- uint8_t i2cData[14]; // Buffer for I2C data
- // TODO: Make calibration routine
- void setup() {
- Serial.begin(115200);
- Wire.begin();
- #if ARDUINO >= 157
- Wire.setClock(400000UL); // Set I2C frequency to 400kHz
- #else
- TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
- #endif
- i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
- i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
- i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
- i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
- while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once
- while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode
- while (i2cRead(0x75, i2cData, 1));
- if (i2cData[0] != 0x68) { // Read "WHO_AM_I" register
- Serial.print(F("Error reading sensor"));
- while (1);
- }
- delay(100); // Wait for sensor to stabilize
- /* Set kalman and gyro starting angle */
- while (i2cRead(0x3B, i2cData, 6));
- accX = (i2cData[0] << 8) | i2cData[1];
- accY = (i2cData[2] << 8) | i2cData[3];
- accZ = (i2cData[4] << 8) | i2cData[5];
- // Source: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf eq. 25 and eq. 26
- // atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
- // It is then converted from radians to degrees
- #ifdef RESTRICT_PITCH // Eq. 25 and 26
- double roll = atan2(accY, accZ) * RAD_TO_DEG;
- double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
- #else // Eq. 28 and 29
- double roll = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
- double pitch = atan2(-accX, accZ) * RAD_TO_DEG;
- #endif
- kalmanX.setAngle(roll); // Set starting angle
- kalmanY.setAngle(pitch);
- gyroXangle = roll;
- gyroYangle = pitch;
- compAngleX = roll;
- compAngleY = pitch;
- //Calibration();
- timer = micros();
- }
- void loop() {
- /* Update all the values */
- while (i2cRead(0x3B, i2cData, 14));
- accX = ((i2cData[0] << 8) | i2cData[1]);
- accY = ((i2cData[2] << 8) | i2cData[3]);
- accZ = ((i2cData[4] << 8) | i2cData[5]);
- //tempRaw = (i2cData[6] << 8) | i2cData[7];
- gyroX = (i2cData[8] << 8) | i2cData[9];
- gyroY = (i2cData[10] << 8) | i2cData[11];
- gyroZ = (i2cData[12] << 8) | i2cData[13];
- double dt = (double)(micros() - timer) / 1000000; // Calculate delta time
- timer = micros();
- // Source: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf eq. 25 and eq. 26
- // atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
- // It is then converted from radians to degrees
- #ifdef RESTRICT_PITCH // Eq. 25 and 26
- double roll = atan2(accY, accZ) * RAD_TO_DEG;
- double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
- #else // Eq. 28 and 29
- double roll = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
- double pitch = atan2(-accX, accZ) * RAD_TO_DEG;
- #endif
- double gyroXrate = gyroX / 131.0; // Convert to deg/s
- double gyroYrate = gyroY / 131.0; // Convert to deg/s
- #ifdef RESTRICT_PITCH
- // This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
- if ((roll < -90 && kalAngleX > 90) || (roll > 90 && kalAngleX < -90)) {
- kalmanX.setAngle(roll);
- compAngleX = roll;
- kalAngleX = roll;
- gyroXangle = roll;
- } else
- kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter
- if (abs(kalAngleX) > 90)
- gyroYrate = -gyroYrate; // Invert rate, so it fits the restriced accelerometer reading
- kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt);
- #else
- // This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
- if ((pitch < -90 && kalAngleY > 90) || (pitch > 90 && kalAngleY < -90)) {
- kalmanY.setAngle(pitch);
- compAngleY = pitch;
- kalAngleY = pitch;
- gyroYangle = pitch;
- } else
- kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt); // Calculate the angle using a Kalman filter
- if (abs(kalAngleY) > 90)
- gyroXrate = -gyroXrate; // Invert rate, so it fits the restriced accelerometer reading
- kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter
- #endif
- gyroXangle += gyroXrate * dt; // Calculate gyro angle without any filter
- gyroYangle += gyroYrate * dt;
- angle += gyroZ * dt / 131.0;
- //gyroXangle += kalmanX.getRate() * dt; // Calculate gyro angle using the unbiased rate
- //gyroYangle += kalmanY.getRate() * dt;
- compAngleX = 0.93 * (compAngleX + gyroXrate * dt) + 0.07 * roll; // Calculate the angle using a Complimentary filter
- compAngleY = 0.93 * (compAngleY + gyroYrate * dt) + 0.07 * pitch;
- // Reset the gyro angle when it has drifted too much
- if (gyroXangle < -180 || gyroXangle > 180)
- gyroXangle = kalAngleX;
- if (gyroYangle < -180 || gyroYangle > 180)
- gyroYangle = kalAngleY;
- angleX = kalAngleX - 2.32;
- angleY = kalAngleY + 0.38;
- angleZ = angle + 5;
- /* Print Data */
- #if 0 // Set to 1 to activate
- Serial.print(accX); Serial.print(",");
- Serial.print(accY); Serial.print(",");
- Serial.print(accZ); Serial.print(",");
- Serial.print(tempRaw); Serial.print(",");
- Serial.print(gyroX / 131.0); Serial.print(",");
- Serial.print(gyroY / 131.0); Serial.print(",");
- Serial.print(gyroZ / 131.0); Serial.print(",");
- Serial.print("\t");
- #endif
- //Serial.print(roll); Serial.print("\t");
- //Serial.print(gyroXangle); Serial.print("\t");
- //Serial.print(compAngleX); Serial.print("\t");
- Serial.print(angleX); Serial.print(",");
- //Serial.print("\t");
- //Serial.print(pitch); Serial.print("\t");
- //Serial.print(gyroYangle); Serial.print("\t");
- //Serial.print(compAngleY); Serial.print("\t");
- Serial.print(angleY); Serial.print(",");
- Serial.print(angleZ); Serial.print(",");
- }
复制代码
以上代码主要部分来自github里kalman函数带的一个例子mpu6050,做了一些细微的改变,毕竟新手嘛。现在呢是可以读出三轴的角度,接下来就是通过遥控器教练通道将数据通过遥控器发给接受器了。这里我还没有弄懂怎么将俩路pwm信号通过教练通道发给遥控器。等了解了再写。第一次接触mpu6050,第一次接触卡尔曼,第一次写帖子。希望大家轻拍。如果有问题,我尽我所知道的给予解答。也希望大神能够指点一二。谢谢。所需的库文件我都放在附件里。
|