极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14037|回复: 2

用Arduino nuo+MPU6050做头追

[复制链接]
发表于 2015-8-19 21:51:06 | 显示全部楼层 |阅读模式
最近和朋友在玩航模,想玩FPV。就想着搞一个“头追”。上X宝一搜,一般的都要一百多。再上上各种diy论坛发现有各种大神都已经diy出来了。本着自己动手丰衣足食的精神。我也买来板子准备自己做一个。首先说明,如果你不想深入地了解整个原理,可以看其他帖子,按其他帖子来是可以做出来的。这个帖子我希望有更多的大神来给我指导一下,因为我想借做头追,了解一下mpu6050以及卡尔曼滤波。后面呢我还想自己做一个简单的飞控。废话少说。先说一下材料:

1:Arduino Uno R3板一块;

2:MPU6050一块

硬件准备好,接下来就要软件了。使用arduino,当然用arduinoIDE了,找度娘很简单就找到了。还有一个看串口波形的软件,我用的Serialchart 挺简单的(我是新手,也只用过那个。。。)
接下来说程序
  1. /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.

  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").

  8. Contact information
  9. -------------------

  10. Kristian Lauszus, TKJ Electronics
  11. Web : http://www.tkjelectronics.com
  12. e-mail : [email][email protected][/email]
  13. */

  14. #include <Wire.h> //mpu6050和arduino通过I2C通信,所以要包含这个库
  15. #include <Kalman.h> //卡尔曼滤波函数
  16. Kalman kalmanX; // 对X,Y轴创建卡尔曼实例
  17. Kalman kalmanY;
  18. /* IMU Data */
  19. double accX, accY, accZ;
  20. double gyroX, gyroY, gyroZ;//从mpu6050读出的六个数据

  21. double gyroXangle, gyroYangle; // Angle calculate using the gyro only
  22. double compAngleX, compAngleY; // Calculated angle using a complementary filter
  23. double kalAngleX, kalAngleY; // Calculated angle using a Kalman filter
  24. double angleX, angleY, angle, angleZ; //finall angle
  25. uint32_t timer;
  26. uint8_t i2cData[14]; // Buffer for I2C data

  27. // TODO: Make calibration routine

  28. void setup() {
  29. Serial.begin(115200);
  30. Wire.begin();
  31. #if ARDUINO >= 157
  32. Wire.setClock(400000UL); // Set I2C frequency to 400kHz
  33. #else
  34. TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
  35. #endif

  36. i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
  37. i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
  38. i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
  39. i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
  40. while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once
  41. while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode

  42. while (i2cRead(0x75, i2cData, 1));
  43. if (i2cData[0] != 0x68) { // Read "WHO_AM_I" register
  44. Serial.print(F("Error reading sensor"));
  45. while (1);
  46. }

  47. delay(100); // Wait for sensor to stabilize

  48. /* Set kalman and gyro starting angle */
  49. while (i2cRead(0x3B, i2cData, 6));
  50. accX = (i2cData[0] << 8) | i2cData[1];
  51. accY = (i2cData[2] << 8) | i2cData[3];
  52. accZ = (i2cData[4] << 8) | i2cData[5];

  53. // Source: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf eq. 25 and eq. 26
  54. // atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
  55. // It is then converted from radians to degrees
  56. #ifdef RESTRICT_PITCH // Eq. 25 and 26
  57. double roll = atan2(accY, accZ) * RAD_TO_DEG;
  58. double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
  59. #else // Eq. 28 and 29
  60. double roll = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
  61. double pitch = atan2(-accX, accZ) * RAD_TO_DEG;
  62. #endif

  63. kalmanX.setAngle(roll); // Set starting angle
  64. kalmanY.setAngle(pitch);
  65. gyroXangle = roll;
  66. gyroYangle = pitch;
  67. compAngleX = roll;
  68. compAngleY = pitch;

  69. //Calibration();
  70. timer = micros();
  71. }

  72. void loop() {
  73. /* Update all the values */
  74. while (i2cRead(0x3B, i2cData, 14));
  75. accX = ((i2cData[0] << 8) | i2cData[1]);
  76. accY = ((i2cData[2] << 8) | i2cData[3]);
  77. accZ = ((i2cData[4] << 8) | i2cData[5]);
  78. //tempRaw = (i2cData[6] << 8) | i2cData[7];
  79. gyroX = (i2cData[8] << 8) | i2cData[9];
  80. gyroY = (i2cData[10] << 8) | i2cData[11];
  81. gyroZ = (i2cData[12] << 8) | i2cData[13];

  82. double dt = (double)(micros() - timer) / 1000000; // Calculate delta time
  83. timer = micros();

  84. // Source: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf eq. 25 and eq. 26
  85. // atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
  86. // It is then converted from radians to degrees
  87. #ifdef RESTRICT_PITCH // Eq. 25 and 26
  88. double roll = atan2(accY, accZ) * RAD_TO_DEG;
  89. double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
  90. #else // Eq. 28 and 29
  91. double roll = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
  92. double pitch = atan2(-accX, accZ) * RAD_TO_DEG;
  93. #endif

  94. double gyroXrate = gyroX / 131.0; // Convert to deg/s
  95. double gyroYrate = gyroY / 131.0; // Convert to deg/s

  96. #ifdef RESTRICT_PITCH
  97. // This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
  98. if ((roll < -90 && kalAngleX > 90) || (roll > 90 && kalAngleX < -90)) {
  99. kalmanX.setAngle(roll);
  100. compAngleX = roll;
  101. kalAngleX = roll;
  102. gyroXangle = roll;
  103. } else
  104. kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter

  105. if (abs(kalAngleX) > 90)
  106. gyroYrate = -gyroYrate; // Invert rate, so it fits the restriced accelerometer reading
  107. kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt);
  108. #else
  109. // This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
  110. if ((pitch < -90 && kalAngleY > 90) || (pitch > 90 && kalAngleY < -90)) {
  111. kalmanY.setAngle(pitch);
  112. compAngleY = pitch;
  113. kalAngleY = pitch;
  114. gyroYangle = pitch;
  115. } else
  116. kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt); // Calculate the angle using a Kalman filter

  117. if (abs(kalAngleY) > 90)
  118. gyroXrate = -gyroXrate; // Invert rate, so it fits the restriced accelerometer reading
  119. kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter
  120. #endif

  121. gyroXangle += gyroXrate * dt; // Calculate gyro angle without any filter
  122. gyroYangle += gyroYrate * dt;
  123. angle += gyroZ * dt / 131.0;

  124. //gyroXangle += kalmanX.getRate() * dt; // Calculate gyro angle using the unbiased rate
  125. //gyroYangle += kalmanY.getRate() * dt;

  126. compAngleX = 0.93 * (compAngleX + gyroXrate * dt) + 0.07 * roll; // Calculate the angle using a Complimentary filter
  127. compAngleY = 0.93 * (compAngleY + gyroYrate * dt) + 0.07 * pitch;

  128. // Reset the gyro angle when it has drifted too much
  129. if (gyroXangle < -180 || gyroXangle > 180)
  130. gyroXangle = kalAngleX;
  131. if (gyroYangle < -180 || gyroYangle > 180)
  132. gyroYangle = kalAngleY;

  133. angleX = kalAngleX - 2.32;
  134. angleY = kalAngleY + 0.38;
  135. angleZ = angle + 5;

  136. /* Print Data */
  137. #if 0 // Set to 1 to activate
  138. Serial.print(accX); Serial.print(",");
  139. Serial.print(accY); Serial.print(",");
  140. Serial.print(accZ); Serial.print(",");
  141. Serial.print(tempRaw); Serial.print(",");
  142. Serial.print(gyroX / 131.0); Serial.print(",");
  143. Serial.print(gyroY / 131.0); Serial.print(",");
  144. Serial.print(gyroZ / 131.0); Serial.print(",");

  145. Serial.print("\t");
  146. #endif
  147. //Serial.print(roll); Serial.print("\t");
  148. //Serial.print(gyroXangle); Serial.print("\t");
  149. //Serial.print(compAngleX); Serial.print("\t");
  150. Serial.print(angleX); Serial.print(",");

  151. //Serial.print("\t");

  152. //Serial.print(pitch); Serial.print("\t");
  153. //Serial.print(gyroYangle); Serial.print("\t");
  154. //Serial.print(compAngleY); Serial.print("\t");
  155. Serial.print(angleY); Serial.print(",");
  156. Serial.print(angleZ); Serial.print(",");

  157. }
复制代码

以上代码主要部分来自github里kalman函数带的一个例子mpu6050,做了一些细微的改变,毕竟新手嘛。现在呢是可以读出三轴的角度,接下来就是通过遥控器教练通道将数据通过遥控器发给接受器了。这里我还没有弄懂怎么将俩路pwm信号通过教练通道发给遥控器。等了解了再写。第一次接触mpu6050,第一次接触卡尔曼,第一次写帖子。希望大家轻拍。如果有问题,我尽我所知道的给予解答。也希望大神能够指点一二。谢谢。所需的库文件我都放在附件里。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2015-8-20 10:43:40 | 显示全部楼层
先收藏着,以后试试!
回复 支持 反对

使用道具 举报

发表于 2015-8-20 13:37:14 | 显示全部楼层
先收藏,以后试试!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 15:05 , Processed in 0.039668 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表