|
|
把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_DataOutputXMSBAddress 0x03
- int regb=0x01;
- int regbdata=0x40;
-
- int outputData[6];
-
- //====一下三个定义了陀螺仪的偏差===========
- //#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[i]=Wire.read(); //Store the data in outputData buffer
- }
- }
-
- x=outputData[0] << 8 | outputData[1]; //Combine MSB and LSB of X Data output register
- z=outputData[2] << 8 | outputData[3]; //Combine MSB and LSB of Z Data output register
- y=outputData[4] << 8 | outputData[5]; //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.
- [url]http://www.ssec.honeywell.com/magnetic/datasheets/lowcost.pdf[/url]
- ----------------------------------------------------------------------------------------
- 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;
- //==========以下三行是用加速度计算三个轴和水平面坐标系之间的夹角
- //请参考:[url]http://www.geek-workshop.com/forum.php?mod=viewthread&tid=2328&page=1#pid27876[/url]
- //个人觉得原帖中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);//这个用来控制采样速度
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|