路遥~倒转回忆 发表于 2013-4-13 23:03:36

Mpu6050与HMC5883一起用?

把MPU6050和HMC5883的代码复制到了一起,为什么显示的就只剩下MPU的数据,而HMC的xyz全是0!!
新人求指教?


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

#define HMC5883_WriteAddress 0x1E //i.e 0x3C >> 1
#define HMC5883_ModeRegisterAddress 0x02
#define HMC5883_ContinuousModeCommand 0x00
#define HMC5883_DataOutputXMSBAddress0x03
int regb=0x01;
int regbdata=0x40;

int outputData;

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

    Wire.beginTransmission(HMC5883_WriteAddress);
    Wire.write(regb);
    Wire.write(regbdata);
    Wire.endTransmission();

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


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

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

    x=outputData << 8 | outputData; //Combine MSB and LSB of X Data output register
    z=outputData << 8 | outputData; //Combine MSB and LSB of Z Data output register
    y=outputData << 8 | outputData; //Combine MSB and LSB of Y Data output register
Serial.print("x");
Serial.print(x);
Serial.print("   y ");
Serial.print(y);
Serial.print("   z ");
Serial.print(z);
    angle= atan2((double)y,(double)x) * (180 / 3.14159265) + 180; // angle in degrees

    /*

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

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

----------------->y
|
|
|
|
|
|
\/
x



   N
NW|NE
   |
W----------E
   |
SW|SE
   S

*/


    //Print the approximate direction

/*Serial.print("heading ");
    if((angle < 22.5) || (angle > 337.5 ))
      Serial.print("South");
    if((angle > 22.5) && (angle < 67.5 ))
      Serial.print("South-West");
    if((angle > 67.5) && (angle < 112.5 ))
      Serial.print("West");
    if((angle > 112.5) && (angle < 157.5 ))
      Serial.print("North-West");
    if((angle > 157.5) && (angle < 202.5 ))
      Serial.print("North");
    if((angle > 202.5) && (angle < 247.5 ))
      Serial.print("NorthEast");
    if((angle > 247.5) && (angle < 292.5 ))
      Serial.print("East");
    if((angle > 292.5) && (angle < 337.5 ))
      Serial.print("SouthEast");*/

   // Serial.print(": Angle between X-axis and the South direction ");
    if((0 < angle) && (angle < 180) )
    {
      angle=angle;
    }
    else
    {
      angle=360-angle;
    }
    Serial.print("   angle");
    Serial.println(angle);
    //Serial.println(" Deg");
    delay(100);
//Serial.print(ax/16384.00);
//Serial.print(",");
//Serial.print(ay/16384.00);
//Serial.print(",");
//Serial.print(az/16384.00);
//Serial.print(",");
//Serial.print(gx/131.00);
//Serial.print(",");
//Serial.print(gy/131.00);
//Serial.print(",");
//Serial.println(gz/131.00);
//======一下三行是对加速度进行量化,得出单位为g的加速度值
   Ax=ax/16384.00;
   Ay=ay/16384.00;
   Az=az/16384.00;
   //==========以下三行是用加速度计算三个轴和水平面坐标系之间的夹角
//请参考:http://www.geek-workshop.com/forum.php?mod=viewthread&tid=2328&page=1#pid27876
//个人觉得原帖中case0算的不对,应该是z/sqrt(x*x+y*y),估计是江南楼主写错了
   Angel_accX=atan(Ax/sqrt(Az*Az+Ay*Ay))*180/3.14;
   Angel_accY=atan(Ay/sqrt(Ax*Ax+Az*Az))*180/3.14;
   Angel_accZ=atan(Az/sqrt(Ax*Ax+Ay*Ay))*180/3.14;
   //==========以下三行是对角速度做量化==========
   ggx=gx/131.00;
   ggy=gy/131.00;
   ggz=gz/131.00;
//===============以下是对角度进行积分处理================
NowTime=millis();//获取当前程序运行的毫秒数
TimeSpan=NowTime-LastTime;//积分时间这样算不是很严谨
//下面三行就是通过对角速度积分实现各个轴的角度测量,当然假设各轴的起始角度都是0
// Gx=Gx+(ggx-Gx_offset)*TimeSpan/1000;
//Gy=Gy+(ggy-Gy_offset)*TimeSpan/1000;
//Gz=Gz+(ggz-Gz_offset)*TimeSpan/1000;
Gx=Gx+(ggx)*TimeSpan/1000;
Gy=Gy+(ggy)*TimeSpan/1000;
Gz=Gz+(ggz)*TimeSpan/1000;
LastTime=NowTime;
//==============================
Serial.print(Angel_accX);
Serial.print(",");
Serial.print(Angel_accY);
Serial.print(",");
Serial.print(Angel_accZ);
Serial.print(",");
Serial.print(Gx);
Serial.print(",");
Serial.print(Gy);
Serial.print(",");
Serial.println(Gz);
//Serial.print(ggx-Gx_offset);
//Serial.print(",");
//Serial.print(ggy-Gy_offset);
//Serial.print(",");
//Serial.println(ggz-Gz_offset);
blinkState=!blinkState;
digitalWrite(LED_PIN,blinkState);
delay(1000);//这个用来控制采样速度
}

路遥~倒转回忆 发表于 2013-4-13 23:04:08

:'(:'(:'(:'(

路遥~倒转回忆 发表于 2013-4-13 23:39:14

:dizzy::dizzy::dizzy::dizzy:

路遥~倒转回忆 发表于 2013-4-14 10:24:10

坐等回复。

qptimus 发表于 2013-4-14 13:11:32

mpu6050可以扩展一个传感器,经过融合后直接输出9轴数据,HMC5883应该接到mpu6050上吧,我也不太清楚

路遥~倒转回忆 发表于 2013-4-14 13:54:28

qptimus 发表于 2013-4-14 13:11 static/image/common/back.gif
mpu6050可以扩展一个传感器,经过融合后直接输出9轴数据,HMC5883应该接到mpu6050上吧,我也不太清楚

是可以接到的,但是不知道软件上怎么操作。

弘毅 发表于 2013-4-14 14:12:01

传一个IMU的库。。。依稀记得,这里面好像是有MPU6050与HMC5883一起使用的代码。。。你可以研究研究。

路遥~倒转回忆 发表于 2013-4-14 14:56:03

弘毅 发表于 2013-4-14 14:12 static/image/common/back.gif
传一个IMU的库。。。依稀记得,这里面好像是有MPU6050与HMC5883一起使用的代码。。。你可以研究研究。

多谢 我的去研究下

路遥~倒转回忆 发表于 2013-4-14 15:06:01

弘毅 发表于 2013-4-14 14:12 static/image/common/back.gif
传一个IMU的库。。。依稀记得,这里面好像是有MPU6050与HMC5883一起使用的代码。。。你可以研究研究。

http://www.geek-workshop.com/forum.php?mod=viewthread&tid=1793

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

qczhao_10 发表于 2013-6-13 20:45:53

搞了几天,终于有点眉目了,不能直接复制代码,需要改6050工作模式,最近有考试,过两天上个教程。

韩韩 发表于 2013-7-25 15:27:55

楼主的问题解决了吗 怎么解决啊 我也遇到了相同的问题 一望楼主帮忙解答一下:'(:'(

spring_city 发表于 2013-8-27 23:47:12

楼主解决了么,求解答

huyukuo 发表于 2013-11-1 20:48:28

楼主,能给个HMC5883的测试程序吗?试了很多个例子都不成功。 [email protected]

古丁 发表于 2014-6-12 11:44:44

还是不会啊 啊啊啊啊啊啊
页: [1]
查看完整版本: Mpu6050与HMC5883一起用?