OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 8975|回复: 8

使用mpu6050和hmc5883l集成的9轴芯片测量数据,如果开启了DMP驱动,就没法获得hmc的数据了,只能获得mpu6050相关数据?

[复制链接]

1

主题

1

帖子

0

精华

新手上路

积分
28
金钱
28
注册时间
2015-9-6
在线时间
1 小时
发表于 2015-9-6 21:43:31 | 显示全部楼层 |阅读模式
5金钱
前提:
       我使用的是mpu6050和hmc5883l集成的9轴芯片LEADIY D3B芯片。
       芯片内部的hmc5883l的SCL和SDA线是连接在mpu6050的AUX_CL和AUX_DA上的,所以我在初始化mpu6050的时候关闭了I2C主模式,并且设置中断/旁路设置寄存器使他开启Bypass功能,使mpu6050可以通过I2C辅助总线直接访问HMC5883l。

问题:
       当我不开启DMP驱动(即没有进行mpu_dmp_init()),直接只是测量mpu6050的三轴加速度和角速度,以及hmc5883l的三轴磁分量。测量数据输出正常。
     但是当我卡其DMP驱动,则hmc5883l就无法输出了,三轴磁分量都是为一个很小的定值不变,而且每次reset这三个定值也不变。

     以上配置mpu6050和开启DMP驱动的方法,都是移植原子哥的代码,自己只不过加了开启hmc5883l的代码。在不开启DMP的时候hmc5883l输出正常,但开启DMP,就不正常了,所以问题应噶是出在DMP这一块,是不是在驱动DMP的时候关闭了Bypass功能,使mpu6050不能通过I2C辅助总线直接访问HMC5883l还是其他原因?应该怎么改呢?
     求助大神应该怎么在开启DMP的功能的基础上,让hmc的功能也实现。


不开启DMP时,主程序代码:
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
uart_init(115200); 
delay_init(); 
usmart_dev.init(72); 
KEY_Init();
LED_Init();     
MPU_Init(); 
 init_hmc5883l();
while(1)
{
if((t%1000)==0)

temp=MPU_Get_Temperature(); 
 MPU_Get_Accelerometer(&aacx,&aacy,&aacz);
 MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz);
 get_hmc5883l(&bbx,&bby,&bbz,&angle);           //以上四句为获得数据

print_HMC5883L(&bbx,&bby,&bbz,&angle);
 MPU_show_Gyroscope(&gyrox,&gyroy,&gyroz);
MPU_show_Accelerometer(&aacx,&aacy,&aacz);    //以上三句为打印数据
delay_ms(500);
 t=0;
 LED0=!LED0;
}
t++:
}

开启DMP时代码:
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
uart_init(115200); 
delay_init(); 
usmart_dev.init(72); 
KEY_Init();
LED_Init();     
MPU_Init(); 
 init_hmc5883l();
while(mpu_dmp_init())                        
 {
   printf("MPUdmp6050 Error\r");
   delay_ms(200);
 }  

while(1)
{
if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
{

if((t%1000)==0)

temp=MPU_Get_Temperature(); 
 MPU_Get_Accelerometer(&aacx,&aacy,&aacz);
 MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz);
 get_hmc5883l(&bbx,&bby,&bbz,&angle);           //以上四句为获得数据

print_HMC5883L(&bbx,&bby,&bbz,&angle);
 MPU_show_Gyroscope(&gyrox,&gyroy,&gyroz);
MPU_show_Accelerometer(&aacx,&aacy,&aacz);    //以上三句为打印数据
delay_ms(500);
 t=0; 
 LED0=!LED0; 
}
}

t++:
}



正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165524
金钱
165524
注册时间
2010-12-1
在线时间
2116 小时
发表于 2015-9-7 21:26:37 | 显示全部楼层
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

2

主题

33

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
377
金钱
377
注册时间
2015-10-19
在线时间
173 小时
发表于 2015-10-27 08:40:14 | 显示全部楼层
請參考國外這一帖有完美的解決方案

Attached File  MPU6050_DMP_mag.zip   5.4KB   2884 downloads 
 
http://www.i2cdevlib.com/forums/topic/111-arduino-example-sketch-to-read-magnetometer-while-dmp-is-on/
回复

使用道具 举报

2

主题

33

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
377
金钱
377
注册时间
2015-10-19
在线时间
173 小时
发表于 2015-10-27 08:50:18 | 显示全部楼层

貼上 Arduino 代碼及附件


[mw_shl_code=c,true]// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class using DMP (MotionApps v2.0) // 6/21/2012 by Jeff Rowberg <jeff@rowberg.net> // Updates (of the library) should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib // 1/30/2014 // This sketch was modified by Luis Ródenas <luisrodenaslorda@gmail.com> to include magnetometer and gyro readings // while DMP is working, in case your magnetometer is attached to aux MPU's I2C lines. It should work with // any magnetometer, but it has only been tested with HMC5883L. // Original DMP example sketch has been simplified to my needs, check the original one to see more options. // This sketch does NOT use the HMC5883L library, but it could be used instead of writing to registers directly. // Credits to GitHub user muzhig for sharing the code to read magnetometer. /* ============================================ I2Cdev device library code is placed under the MIT license Copyright (c) 2012 Jeff Rowberg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =============================================== */ // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files // for both classes must be in the include path of your project #include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h" // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation // is used in I2Cdev.h #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE #include "Wire.h" #endif // ********************** CONFIGURATION ********************************** // class default I2C address is 0x68 // specific I2C addresses may be passed as a parameter here // AD0 low = 0x68 (default for SparkFun breakout and InvenSense evaluation board) // AD0 high = 0x69 MPU6050 mpu(0x68); // Most common is 0x68 int interrupt_pin=0; // Check where your INT pin is connected. Check below. /* ========================================================================= NOTE: In addition to connection 3.3v, GND, SDA, and SCL, this sketch depends on the MPU-6050's INT pin being connected to the Arduino's external interrupt #0 pin. On the Arduino Uno and Mega 2560, this is digital I/O pin 2. On DUE you can use ANY pin. Remember to modify this some lines below. * ========================================================================= */ // Variables used #define HMC5883L_DEFAULT_ADDRESS 0x1E #define HMC5883L_RA_DATAX_H 0x03 #define HMC5883L_RA_DATAZ_H 0x05 #define HMC5883L_RA_DATAY_H 0x07 #define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6) bool blinkState = false; // MPU control/status vars bool dmpReady = false; // set true if DMP init was successful uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU uint8_t devStatus; // return status after each device operation (0 = success, !0 = error) uint16_t packetSize; // expected DMP packet size (default is 42 bytes) uint16_t fifoCount; // count of all bytes currently in FIFO uint8_t fifoBuffer[64]; // FIFO storage buffer // orientation/motion vars Quaternion q; // [w, x, y, z] quaternion container VectorInt16 aa; // [x, y, z] accel sensor measurements int16_t gyro[3]; //To store gyro's measures int16_t mx, my, mz; //To store magnetometer readings VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements VectorFloat gravity; // [x, y, z] gravity vector float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector float heading; // Simple magnetic heading. (NOT COMPENSATED FOR PITCH AND ROLL) // To check DMP frecuency (it can be changed it the MotionApps v2 .h file) int time1,time1old; float frec1; // ================================================================ // === INTERRUPT DETECTION ROUTINE === // ================================================================ volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high void dmpDataReady() { mpuInterrupt = true; } // ================================================================ // === INITIAL SETUP === // ================================================================ void setup() { // join I2C bus (I2Cdev library doesn't do this automatically) #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE Wire.begin(); // ************************************************************** // It is best to configure I2C to 400 kHz. // If you are using an Arduino DUE, modify the variable TWI_CLOCK to 400000, defined in the file: // c:/Program Files/Arduino/hardware/arduino/sam/libraries/Wire/Wire.h // If you are using any other Arduino instead of the DUE, uncomment the following line: //TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz) //This line should be commented if you are using Arduino DUE // ************************************************************** #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE Fastwire::setup(400, true); #endif // initialize serial communication Serial.begin(115200); //while (!Serial); // wait for Leonardo enumeration, others continue immediately // NOTE: 8MHz or slower host processors, like the Teensy @ 3.3v or Ardunio // Pro Mini running at 3.3v, cannot handle this baud rate reliably due to // the baud timing being too misaligned with processor ticks. You must use // 38400 or slower in these cases, or use some kind of external separate // crystal solution for the UART timer. // wait for ready while (Serial.available() && Serial.read()); // empty buffer while (!Serial.available()) { Serial.println(F("\nSend any character to begin DMP programming and demo: ")); // wait for data delay(1000); } while (Serial.available() && Serial.read()); // empty buffer again // initialize device Serial.println(F("Initializing I2C devices...")); mpu.initialize(); // verify connection Serial.println(F("Testing device connections...")); Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed")); // load and configure the DMP Serial.println(F("Initializing DMP...")); devStatus = mpu.dmpInitialize(); // supply your own gyro offsets here, scaled for min sensitivity // If you don't know yours, you can find an automated sketch for this task from: http://www.i2cdevlib.com/forums/topic/96-arduino-sketch-to-automatically-calculate-mpu6050-offsets/ mpu.setXAccelOffset(-3591); mpu.setYAccelOffset(-842); mpu.setZAccelOffset(578); mpu.setXGyroOffset(-51); mpu.setYGyroOffset(-57); mpu.setZGyroOffset(0); // In case you want to change MPU sensors' measurements ranges, you should implement it here. This has not been tested with DMP. // Magnetometer configuration mpu.setI2CMasterModeEnabled(0); mpu.setI2CBypassEnabled(1); Wire.beginTransmission(HMC5883L_DEFAULT_ADDRESS); Wire.write(0x02); Wire.write(0x00); // Set continuous mode Wire.endTransmission(); delay(5); Wire.beginTransmission(HMC5883L_DEFAULT_ADDRESS); Wire.write(0x00); Wire.write(B00011000); // 75Hz Wire.endTransmission(); delay(5); mpu.setI2CBypassEnabled(0); // X axis word mpu.setSlaveAddress(0, HMC5883L_DEFAULT_ADDRESS | 0x80); // 0x80 turns 7th bit ON, according to datasheet, 7th bit controls Read/Write direction mpu.setSlaveRegister(0, HMC5883L_RA_DATAX_H); mpu.setSlaveEnabled(0, true); mpu.setSlaveWordByteSwap(0, false); mpu.setSlaveWriteMode(0, false); mpu.setSlaveWordGroupOffset(0, false); mpu.setSlaveDataLength(0, 2); // Y axis word mpu.setSlaveAddress(1, HMC5883L_DEFAULT_ADDRESS | 0x80); mpu.setSlaveRegister(1, HMC5883L_RA_DATAY_H); mpu.setSlaveEnabled(1, true); mpu.setSlaveWordByteSwap(1, false); mpu.setSlaveWriteMode(1, false); mpu.setSlaveWordGroupOffset(1, false); mpu.setSlaveDataLength(1, 2); // Z axis word mpu.setSlaveAddress(2, HMC5883L_DEFAULT_ADDRESS | 0x80); mpu.setSlaveRegister(2, HMC5883L_RA_DATAZ_H); mpu.setSlaveEnabled(2, true); mpu.setSlaveWordByteSwap(2, false); mpu.setSlaveWriteMode(2, false); mpu.setSlaveWordGroupOffset(2, false); mpu.setSlaveDataLength(2, 2); mpu.setI2CMasterModeEnabled(1); // make sure it worked (returns 0 if so) if (devStatus == 0) { // turn on the DMP, now that it's ready Serial.println(F("Enabling DMP...")); mpu.setDMPEnabled(true); // enable Arduino interrupt detection Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)...")); attachInterrupt(interrupt_pin, dmpDataReady, RISING); mpuIntStatus = mpu.getIntStatus(); // set our DMP Ready flag so the main loop() function knows it's okay to use it Serial.println(F("DMP ready! Waiting for first interrupt...")); dmpReady = true; // get expected DMP packet size for later comparison packetSize = mpu.dmpGetFIFOPacketSize(); } else { // ERROR! // 1 = initial memory load failed // 2 = DMP configuration updates failed // (if it's going to break, usually the code will be 1) Serial.print(F("DMP Initialization failed (code ")); Serial.print(devStatus); Serial.println(F(")")); } // configure LED for output pinMode(LED_PIN, OUTPUT); } // ================================================================ // === MAIN PROGRAM LOOP === // ================================================================ void loop() { // if programming failed, don't try to do anything if (!dmpReady) return; // wait for MPU interrupt or extra packet(s) available while (!mpuInterrupt && fifoCount < packetSize) { // other program behavior stuff here // . // . // . // if you are really paranoid you can frequently test in between other // stuff to see if mpuInterrupt is true, and if so, "break;" from the // while() loop to immediately process the MPU data // . // . // . } // reset interrupt flag and get INT_STATUS byte mpuInterrupt = false; mpuIntStatus = mpu.getIntStatus(); // get current FIFO count fifoCount = mpu.getFIFOCount(); // check for overflow (this should never happen unless our code is too inefficient) if ((mpuIntStatus & 0x10) || fifoCount == 1024) { // reset so we can continue cleanly mpu.resetFIFO(); Serial.println(F("FIFO overflow!")); // otherwise, check for DMP data ready interrupt (this should happen frequently) } else if (mpuIntStatus & 0x02) { // wait for correct available data length, should be a VERY short wait while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount(); // read a packet from FIFO mpu.getFIFOBytes(fifoBuffer, packetSize); // track FIFO count here in case there is > 1 packet available // (this lets us immediately read more without waiting for an interrupt) fifoCount -= packetSize; // Check DMP frecuency time1=micros()-time1old; time1old=micros(); frec1=1000000/time1; // Get and process information from DMP mpu.dmpGetQuaternion(&q, fifoBuffer); mpu.dmpGetGyro(gyro, fifoBuffer); // mpu.dmpGetAccel(&aa, fifoBuffer); //Use this if you want accelerometer measures mpu.dmpGetGravity(&gravity, &q); mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); // mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity); //Use this to get linear acceleration apart from gravity. // mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q); //NOT RECOMMENDED. Gives you linear acceleration rotated to initial position. //Read magnetometer measures mx=mpu.getExternalSensorWord(0); my=mpu.getExternalSensorWord(2); mz=mpu.getExternalSensorWord(4); heading = atan2(my, mx); if(heading < 0) heading += 2 * M_PI; // TIME TO PRINT TO CONSOLE // Printing to console takes lots of time, so if you print too many things you could have timing problems, Fifo overflow errors and variable frecuency. Serial.print("ypr\t"); // Prints are commented for the reason above // Serial.print(ypr[0] * 180/M_PI); // Serial.print("\t"); Serial.print(ypr[1] * 180/M_PI); // Serial.print("\t"); // Serial.print(ypr[2] * 180/M_PI); // Serial.print("\t vang\t"); // Serial.print(gyro[0]); // Serial.print("\t"); // Serial.print(gyro[1]); // Serial.print("\t"); // Serial.print(gyro[2]); Serial.print("\t h:\t"); Serial.print(heading * 180/M_PI); Serial.print("\t F:\t"); Serial.println(frec1); // blink LED to indicate activity blinkState = !blinkState; digitalWrite(LED_PIN, blinkState); } } [/mw_shl_code]

MPU6050_DMP_mag.zip

5.4 KB, 下载次数: 1200

回复

使用道具 举报

2

主题

33

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
377
金钱
377
注册时间
2015-10-19
在线时间
173 小时
发表于 2015-10-27 09:01:03 | 显示全部楼层

編譯代碼前請先詳讀所有的註釋


我使用的是 Arduino nano 所以第 132 行


  //TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)  //This line should be commented if you are using Arduino DUE


應取消註釋改程如下


[mw_shl_code=c,true]void setup() { // join I2C bus (I2Cdev library doesn't do this automatically) #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE Wire.begin(); // ************************************************************** // It is best to configure I2C to 400 kHz. // If you are using an Arduino DUE, modify the variable TWI_CLOCK to 400000, defined in the file: // c:/Program Files/Arduino/hardware/arduino/sam/libraries/Wire/Wire.h // If you are using any other Arduino instead of the DUE, uncomment the following line: TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz) //This line should be commented if you are using Arduino DUE // ************************************************************** #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE Fastwire::setup(400, true); #endif[/mw_shl_code]

回复

使用道具 举报

2

主题

33

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
377
金钱
377
注册时间
2015-10-19
在线时间
173 小时
发表于 2015-10-27 09:06:44 | 显示全部楼层
由於我尚未入門 STM 所以如何修改代碼移植至 STM 就有勞其他大神了 !!
回复

使用道具 举报

0

主题

1

帖子

0

精华

新手入门

积分
10
金钱
10
注册时间
2016-5-7
在线时间
0 小时
发表于 2016-5-7 21:26:05 | 显示全部楼层
        //以下是开完DMP后 ,重新设置xda,xcl
        //读出后再设置
        u8 temp[1]={0};
        I2C_ByteRead(MPU6050_Addr,MPU_USER_CTRL_REG,1,temp);
        I2C_ByteWrite(MPU6050_Addr,MPU_USER_CTRL_REG,temp[0]&0xfd);         //I2C_MST_EN=0
      
        I2C_ByteRead(MPU6050_Addr,MPU_INTBP_CFG_REG,1,temp);
        I2C_ByteWrite(MPU6050_Addr,MPU_INTBP_CFG_REG,temp[0]|0x02);        // I2C_BYPASS_EN=1;
        
本来不用DMP配置的配置是:
        I2C_ByteWrite(MPU6050_Addr,MPU_USER_CTRL_REG,0X00); //I2C_MST_EN=0
        I2C_ByteWrite(MPU6050_Addr,MPU_INTBP_CFG_REG,0X02);//I2C_BYPASS_EN=1;

数据手册为:
   



我已解决!!!
回复

使用道具 举报

20

主题

108

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
375
金钱
375
注册时间
2016-9-26
在线时间
59 小时
发表于 2016-12-1 10:52:03 | 显示全部楼层
顶一个
回复

使用道具 举报

17

主题

80

帖子

0

精华

高级会员

Rank: 4

积分
531
金钱
531
注册时间
2015-12-2
在线时间
164 小时
发表于 2016-12-1 10:57:19 | 显示全部楼层
  据我所知,我的知识面上了解到的,MPU6050的BYPASS功能如果在DMP模式下,是可以支持他们自己家的地磁计的,但是也仅限于某些。
BYPASS是可以读取HMC5883的,但是没法集成到DMP里面,如果是MPU9250那么是可以的。
飞飞飞飞飞
回复

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-25 19:15

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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