极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10782|回复: 5

Arduino+MPU 6050 学习记录

[复制链接]
发表于 2014-12-6 19:07:17 | 显示全部楼层 |阅读模式
本帖最后由 pijunkuan 于 2014-12-7 00:39 编辑

研究了mpu 6050 一个多月了,做了一个简单的展示上位机,对mpu传回来的数据做了一点小小的处理,废话不多说了,这里直接上程序,希望各位高手们多多指点一下,个人感觉程序里面有不少的bug,但是自己是学机械的,对电子和软件一窍不通,一直处于自学阶段,望得到高手们的帮助。
坐等各位高手们的回复。



Arduino程序:
  1. #include "Wire.h"
  2. #include "I2Cdev.h"
  3. #include "MPU6050.h"
  4. MPU6050 mpu;
  5. #define LED_PIN 13

  6. unsigned long last_read_time;
  7. float         last_x_angle;  // These are the filtered angles
  8. float         last_y_angle;
  9. float         last_z_angle;  
  10. float         last_gyro_x_angle;  // Store the gyro angles to compare drift
  11. float         last_gyro_y_angle;
  12. float         last_gyro_z_angle;

  13. void set_last_read_angle_data(unsigned long time, float x, float y, float z, float x_gyro, float y_gyro, float z_gyro)
  14. {
  15.   last_read_time = time;
  16.   last_x_angle = x;
  17.   last_y_angle = y;
  18.   last_z_angle = z;
  19.   last_gyro_x_angle = x_gyro;
  20.   last_gyro_y_angle = y_gyro;
  21.   last_gyro_z_angle = z_gyro;
  22. }

  23. inline unsigned long get_last_time() {return last_read_time;}
  24. inline float get_last_x_angle() {return last_x_angle;}
  25. inline float get_last_y_angle() {return last_y_angle;}
  26. inline float get_last_z_angle() {return last_z_angle;}
  27. inline float get_last_gyro_x_angle() {return last_gyro_x_angle;}
  28. inline float get_last_gyro_y_angle() {return last_gyro_y_angle;}
  29. inline float get_last_gyro_z_angle() {return last_gyro_z_angle;}

  30. float    base_x_gyro = 0;
  31. float    base_y_gyro = 0;
  32. float    base_z_gyro = 0;
  33. float    base_x_accel = 0;
  34. float    base_y_accel = 0;
  35. float    base_z_accel = 0;

  36. float    GYRO_FACTOR;
  37. float    ACCEL_FACTOR;

  38. int16_t ax, ay, az;
  39. int16_t gx, gy, gz;

  40. char dataOut[256];

  41. void calibrate_sensors() {
  42.   int       num_readings = 10;

  43.   // Discard the first reading (don't know if this is needed or
  44.   // not, however, it won't hurt.)
  45.   mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  46.   
  47.   // Read and average the raw values
  48.   for (int i = 0; i < num_readings; i++) {
  49.     mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  50.     base_x_gyro += gx;
  51.     base_y_gyro += gy;
  52.     base_z_gyro += gz;
  53.     base_x_accel += ax;
  54.     base_y_accel += ay;
  55.     base_y_accel += az;
  56.   }
  57.   
  58.   base_x_gyro /= num_readings;
  59.   base_y_gyro /= num_readings;
  60.   base_z_gyro /= num_readings;
  61.   base_x_accel /= num_readings;
  62.   base_y_accel /= num_readings;
  63.   base_z_accel /= num_readings;
  64. }

  65. void setup() {
  66.     Wire.begin();
  67.     Serial.begin(57600);
  68.     while (!Serial);
  69.     mpu.initialize();
  70.     Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  71.   
  72.     pinMode(LED_PIN, OUTPUT);
  73.    
  74.     calibrate_sensors();
  75.     set_last_read_angle_data(millis(), 0, 0, 0, 0, 0, 0);
  76. }

  77. void loop()
  78. {
  79.     const float RADIANS_TO_DEGREES = 57.2958; //180/3.14159
  80.     unsigned long t_now = millis();
  81.     mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  82.     float gyro_x = (gx - base_x_gyro)/131.0;
  83.     float gyro_y = (gy - base_y_gyro)/131.0;
  84.     float gyro_z = (gz - base_z_gyro)/131.0;
  85.     float accel_x = ax; // - base_x_accel;
  86.     float accel_y = ay; // - base_y_accel;
  87.     float accel_z = az; // - base_z_accel;
  88.         
  89.               
  90.         float accel_angle_y = atan(-1*accel_x/sqrt(pow(accel_y,2) + pow(accel_z,2)))*RADIANS_TO_DEGREES;
  91.         float accel_angle_x = atan(accel_y/sqrt(pow(accel_x,2) + pow(accel_z,2)))*RADIANS_TO_DEGREES;
  92.         float accel_angle_z = 0;

  93.         // Compute the (filtered) gyro angles
  94.         float dt =(t_now - get_last_time())/1000.0;
  95.         float gyro_angle_x = gyro_x*dt + get_last_x_angle();
  96.         float gyro_angle_y = gyro_y*dt + get_last_y_angle();
  97.         float gyro_angle_z = gyro_z*dt + get_last_z_angle();
  98.         
  99.         // Compute the drifting gyro angles
  100.         float unfiltered_gyro_angle_x = gyro_x*dt + get_last_gyro_x_angle();
  101.         float unfiltered_gyro_angle_y = gyro_y*dt + get_last_gyro_y_angle();
  102.         float unfiltered_gyro_angle_z = gyro_z*dt + get_last_gyro_z_angle();     
  103.         
  104.         // Apply the complementary filter to figure out the change in angle - choice of alpha is
  105.         // estimated now.  Alpha depends on the sampling rate...
  106.         const float alpha = 0.96;
  107.         float angle_x = alpha*gyro_angle_x + (1.0 - alpha)*accel_angle_x;
  108.         float angle_y = alpha*gyro_angle_y + (1.0 - alpha)*accel_angle_y;
  109.         float angle_z = gyro_angle_z;  //Accelerometer doesn't give z-angle
  110.         
  111.         // Update the saved data with the latest values
  112.         set_last_read_angle_data(t_now, angle_x, angle_y, angle_z, unfiltered_gyro_angle_x, unfiltered_gyro_angle_y, unfiltered_gyro_angle_z);      

  113.   
  114.        // Output complementary data and DMP data to the serial port.  The signs on the data needed to be
  115.        // fudged to get the angle direction correct.
  116.        Serial.print(get_last_x_angle(), 2);
  117.        Serial.print(",");
  118.        Serial.print(get_last_y_angle(), 2);
  119.        Serial.print(",");
  120.        Serial.print(-get_last_z_angle(), 2);
  121.        Serial.print(",");
  122.        Serial.print(ax/16384.0, 2);
  123.        Serial.print(",");
  124.        Serial.print(ay/16384.0, 2);
  125.        Serial.print(",");
  126.        Serial.println(az/16384.0, 2);
  127.       

  128.         // blink LED to indicate activity


  129.         delay(110);
  130.    
  131. }
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-12-6 21:15:21 | 显示全部楼层
为什么没有人回复呢?
回复 支持 反对

使用道具 举报

发表于 2014-12-6 22:10:51 | 显示全部楼层
pijunkuan 发表于 2014-12-6 21:15
为什么没有人回复呢?

多特蒙德
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-7 00:25:44 | 显示全部楼层
Tottiii 发表于 2014-12-6 22:10
多特蒙德

恩啊,你也是吗
回复 支持 反对

使用道具 举报

发表于 2014-12-7 00:28:14 | 显示全部楼层
是,可惜最近排名太差。。。惨不忍睹
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-7 00:39:09 | 显示全部楼层
Tottiii 发表于 2014-12-7 00:28
是,可惜最近排名太差。。。惨不忍睹

你在多特蒙德哪啊?什么排名太差了啊
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 15:32 , Processed in 0.033312 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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