极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 29564|回复: 13

Mpu6050与HMC5883一起用?

[复制链接]
发表于 2013-4-13 23:03:36 | 显示全部楼层 |阅读模式
把MPU6050和HMC5883的代码复制到了一起,为什么显示的就只剩下MPU的数据,而HMC的xyz全是0!!
新人求指教?


  1. #include <I2Cdev.h>
  2. #include <MPU6050.h>
  3. #include <Wire.h>//添加必须的库文件

  4. #define HMC5883_WriteAddress 0x1E //  i.e 0x3C >> 1
  5. #define HMC5883_ModeRegisterAddress 0x02
  6. #define HMC5883_ContinuousModeCommand 0x00
  7. #define HMC5883_DataOutputXMSBAddress  0x03
  8. int regb=0x01;
  9. int regbdata=0x40;

  10. int outputData[6];

  11. //====一下三个定义了陀螺仪的偏差===========
  12. //#define Gx_offset -3.06
  13. //#define Gy_offset 1.01
  14. //#define Gz_offset -0.88
  15. //====================
  16. MPU6050 accelgyro;
  17. int16_t ax,ay,az;
  18. int16_t gx,gy,gz;//存储原始数据
  19. float aax,aay,aaz,ggx,ggy,ggz;//存储量化后的数据
  20. float Ax,Ay,Az;//单位 g(9.8m/s^2)
  21. float Gx,Gy,Gz;//单位 °/s
  22. float Angel_accX,Angel_accY,Angel_accZ;//存储加速度计算出的角度
  23. long LastTime,NowTime,TimeSpan;//用来对角速度积分的
  24. #define LED_PIN 13
  25. bool blinkState=false;
  26. void setup()//MPU6050的设置都采用了默认值,请参看库文件
  27. {
  28. Wire.begin();
  29. Serial.begin(9600);
  30. Serial.println("Initializing I2C device.....");
  31. accelgyro.initialize();
  32. Serial.println("Testing device connections...");
  33. Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful":"MPU6050 connection failure");
  34. pinMode(LED_PIN,OUTPUT);
  35. }
  36. void loop()
  37. {
  38.   int i,x,y,z;
  39.     double angle;

  40.     Wire.beginTransmission(HMC5883_WriteAddress);
  41.     Wire.write(regb);
  42.     Wire.write(regbdata);
  43.     Wire.endTransmission();

  44.     delay(100);
  45.     Wire.beginTransmission(HMC5883_WriteAddress); //Initiate a transmission with HMC5883 (Write address).
  46.     Wire.write(HMC5883_ModeRegisterAddress);       //Place the Mode Register Address in send-buffer.
  47.     Wire.write(HMC5883_ContinuousModeCommand);     //Place the command for Continuous operation Mode in send-buffer.
  48.     Wire.endTransmission();                       //Send the send-buffer to HMC5883 and end the I2C transmission.
  49.     delay(100);


  50.     Wire.beginTransmission(HMC5883_WriteAddress);  //Initiate a transmission with HMC5883 (Write address).
  51.     Wire.requestFrom(HMC5883_WriteAddress,6);      //Request 6 bytes of data from the address specified.

  52.   accelgyro.getMotion6(&ax,&ay,&az,&gx,&gy,&gz);//获取三个轴的加速度和角速度
  53.     if(6 <= Wire.available()) // If the number of bytes available for reading be <=6.
  54.     {
  55.         for(i=0;i<6;i++)
  56.         {
  57.             outputData[i]=Wire.read();  //Store the data in outputData buffer
  58.         }
  59.     }

  60.     x=outputData[0] << 8 | outputData[1]; //Combine MSB and LSB of X Data output register
  61.     z=outputData[2] << 8 | outputData[3]; //Combine MSB and LSB of Z Data output register
  62.     y=outputData[4] << 8 | outputData[5]; //Combine MSB and LSB of Y Data output register
  63.   Serial.print("x");
  64.   Serial.print(x);
  65.   Serial.print("     y ");
  66.   Serial.print(y);
  67.   Serial.print("     z ");
  68.   Serial.print(z);
  69.     angle= atan2((double)y,(double)x) * (180 / 3.14159265) + 180; // angle in degrees

  70.     /*

  71.   Refer the following application note for heading calculation.
  72.   [url]http://www.ssec.honeywell.com/magnetic/datasheets/lowcost.pdf[/url]  
  73.   ----------------------------------------------------------------------------------------
  74.   atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point
  75.   given by the coordinates (x, y) on it.
  76.   ----------------------------------------------------------------------------------------

  77.   This sketch does not utilize the magnetic component Z as tilt compensation can not be done without an Accelerometer

  78.   ----------------->y
  79.   |
  80.   |
  81.   |
  82.   |
  83.   |
  84.   |
  85. \/
  86.   x



  87.      N
  88. NW  |  NE
  89.      |  
  90. W----------E
  91.      |
  92. SW  |  SE
  93.      S

  94. */


  95.     //Print the approximate direction

  96.   /*  Serial.print("heading ");
  97.     if((angle < 22.5) || (angle > 337.5 ))
  98.         Serial.print("South");
  99.     if((angle > 22.5) && (angle < 67.5 ))
  100.         Serial.print("South-West");
  101.     if((angle > 67.5) && (angle < 112.5 ))
  102.         Serial.print("West");
  103.     if((angle > 112.5) && (angle < 157.5 ))
  104.         Serial.print("North-West");
  105.     if((angle > 157.5) && (angle < 202.5 ))
  106.         Serial.print("North");
  107.     if((angle > 202.5) && (angle < 247.5 ))
  108.         Serial.print("NorthEast");
  109.     if((angle > 247.5) && (angle < 292.5 ))
  110.         Serial.print("East");
  111.     if((angle > 292.5) && (angle < 337.5 ))
  112.         Serial.print("SouthEast");*/

  113.    // Serial.print(": Angle between X-axis and the South direction ");
  114.     if((0 < angle) && (angle < 180) )
  115.     {
  116.         angle=angle;
  117.     }
  118.     else
  119.     {
  120.         angle=360-angle;
  121.     }
  122.     Serial.print("     angle");
  123.     Serial.println(angle);
  124.     //Serial.println(" Deg");
  125.     delay(100);
  126. //  Serial.print(ax/16384.00);
  127. //  Serial.print(",");
  128. //  Serial.print(ay/16384.00);
  129. //  Serial.print(",");
  130. //  Serial.print(az/16384.00);
  131. //  Serial.print(",");
  132. //  Serial.print(gx/131.00);
  133. //  Serial.print(",");
  134. //  Serial.print(gy/131.00);
  135. //  Serial.print(",");
  136. //  Serial.println(gz/131.00);
  137. //======一下三行是对加速度进行量化,得出单位为g的加速度值
  138.    Ax=ax/16384.00;
  139.    Ay=ay/16384.00;
  140.    Az=az/16384.00;
  141.    //==========以下三行是用加速度计算三个轴和水平面坐标系之间的夹角
  142. //请参考:[url]http://www.geek-workshop.com/forum.php?mod=viewthread&tid=2328&page=1#pid27876[/url]
  143. //个人觉得原帖中case0算的不对,应该是z/sqrt(x*x+y*y),估计是江南楼主写错了
  144.    Angel_accX=atan(Ax/sqrt(Az*Az+Ay*Ay))*180/3.14;
  145.    Angel_accY=atan(Ay/sqrt(Ax*Ax+Az*Az))*180/3.14;
  146.    Angel_accZ=atan(Az/sqrt(Ax*Ax+Ay*Ay))*180/3.14;
  147.    //==========以下三行是对角速度做量化==========
  148.    ggx=gx/131.00;
  149.    ggy=gy/131.00;
  150.    ggz=gz/131.00;
  151. //===============以下是对角度进行积分处理================
  152.   NowTime=millis();//获取当前程序运行的毫秒数
  153.   TimeSpan=NowTime-LastTime;//积分时间这样算不是很严谨
  154. //下面三行就是通过对角速度积分实现各个轴的角度测量,当然假设各轴的起始角度都是0
  155. // Gx=Gx+(ggx-Gx_offset)*TimeSpan/1000;
  156.   //Gy=Gy+(ggy-Gy_offset)*TimeSpan/1000;
  157.   //Gz=Gz+(ggz-Gz_offset)*TimeSpan/1000;
  158.   Gx=Gx+(ggx)*TimeSpan/1000;
  159.   Gy=Gy+(ggy)*TimeSpan/1000;
  160.   Gz=Gz+(ggz)*TimeSpan/1000;
  161.   LastTime=NowTime;
  162.   //==============================
  163.   Serial.print(Angel_accX);
  164.   Serial.print(",");
  165.   Serial.print(Angel_accY);
  166.   Serial.print(",");
  167.   Serial.print(Angel_accZ);
  168.   Serial.print(",");
  169.   Serial.print(Gx);
  170.   Serial.print(",");
  171.   Serial.print(Gy);
  172.   Serial.print(",");
  173.   Serial.println(Gz);
  174. //  Serial.print(ggx-Gx_offset);
  175. //  Serial.print(",");
  176. //  Serial.print(ggy-Gy_offset);
  177. //  Serial.print(",");
  178. //  Serial.println(ggz-Gz_offset);
  179. blinkState=!blinkState;
  180.   digitalWrite(LED_PIN,blinkState);
  181.   delay(1000);//这个用来控制采样速度
  182. }
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

 楼主| 发表于 2013-4-13 23:04:08 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-13 23:39:14 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-14 10:24:10 | 显示全部楼层
坐等回复。
回复 支持 反对

使用道具 举报

发表于 2013-4-14 13:11:32 | 显示全部楼层
mpu6050可以扩展一个传感器,经过融合后直接输出9轴数据,HMC5883应该接到mpu6050上吧,我也不太清楚
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-14 13:54:28 | 显示全部楼层
qptimus 发表于 2013-4-14 13:11
mpu6050可以扩展一个传感器,经过融合后直接输出9轴数据,HMC5883应该接到mpu6050上吧,我也不太清楚

是可以接到的,但是不知道软件上怎么操作。
回复 支持 反对

使用道具 举报

发表于 2013-4-14 14:12:01 | 显示全部楼层
传一个IMU的库。。。依稀记得,这里面好像是有MPU6050与HMC5883一起使用的代码。。。你可以研究研究。

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-14 14:56:03 | 显示全部楼层
弘毅 发表于 2013-4-14 14:12
传一个IMU的库。。。依稀记得,这里面好像是有MPU6050与HMC5883一起使用的代码。。。你可以研究研究。

多谢 我的去研究下
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-14 15:06:01 | 显示全部楼层
弘毅 发表于 2013-4-14 14:12
传一个IMU的库。。。依稀记得,这里面好像是有MPU6050与HMC5883一起使用的代码。。。你可以研究研究。

http://www.geek-workshop.com/for ... thread&tid=1793

这个帖子里有硬件的连接方式,5楼说的要配置Bypass模式, 这个我已经找到怎么配置了,剩下的问题便是MPU6050.h  里面读取九轴数据的代码需要自己来写,这种操作位的东西对一个新生来说有点难。
能不能帮我找一下附件里的代码在哪里?
回复 支持 反对

使用道具 举报

发表于 2013-6-13 20:45:53 | 显示全部楼层
搞了几天,终于有点眉目了,不能直接复制代码,需要改6050工作模式,最近有考试,过两天上个教程。
回复 支持 反对

使用道具 举报

发表于 2013-7-25 15:27:55 | 显示全部楼层
楼主的问题解决了吗 怎么解决啊 我也遇到了相同的问题 一望楼主帮忙解答一下
回复 支持 反对

使用道具 举报

发表于 2013-8-27 23:47:12 | 显示全部楼层
楼主解决了么,求解答
回复 支持 反对

使用道具 举报

发表于 2013-11-1 20:48:28 | 显示全部楼层
楼主,能给个HMC5883的测试程序吗?试了很多个例子都不成功。 [email protected]
回复 支持 反对

使用道具 举报

发表于 2014-6-12 11:44:44 | 显示全部楼层
还是不会啊 啊啊啊啊啊啊
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-9 07:57 , Processed in 0.045985 second(s), 27 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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