社区导航

 

搜索
查看: 1809|回复: 1

[设计过程分享] BMI160测试

[复制链接]

9818

TA的帖子

14

TA的资源

版主

Rank: 6Rank: 6

测评达人

发表于 2017-8-2 17:24 | 显示全部楼层 |阅读模式
BMI160是博世的6轴陀螺仪传感器,它的体积小,灵敏度高,支持I2C或SPI接口。


在MAX32630FTHR开发板上,使用了BMI160传感器,利用Mbed以及相关的驱动,我们可以直接测试这个传感器,通过串口查看传感器状态。

  1. #include "mbed.h"
  2. #include "max32630fthr.h"
  3. #include "bmi160.h"


  4. void dumpImuRegisters(BMI160 &imu);
  5. void printRegister(BMI160 &imu, BMI160::Registers reg);
  6. void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg);


  7. int main()
  8. {
  9.     MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
  10.    
  11.     DigitalOut rLED(LED1, LED_OFF);
  12.     DigitalOut gLED(LED2, LED_OFF);
  13.     DigitalOut bLED(LED3, LED_OFF);
  14.    
  15.     I2C i2cBus(P5_7, P6_0);
  16.     i2cBus.frequency(400000);
  17.     BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO);
  18.    
  19.     printf("\033[H");  //home
  20.     printf("\033[0J");  //erase from cursor to end of screen
  21.    
  22.     uint32_t failures = 0;
  23.    
  24.     if(imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
  25.     {
  26.         printf("Failed to set gyroscope power mode\n");
  27.         failures++;
  28.     }
  29.     wait_ms(100);
  30.    
  31.     if(imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
  32.     {
  33.         printf("Failed to set accelerometer power mode\n");
  34.         failures++;
  35.     }
  36.     wait_ms(100);
  37.    
  38.    
  39.     BMI160::AccConfig accConfig;
  40.     //example of using getSensorConfig
  41.     if(imu.getSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
  42.     {
  43.         printf("ACC Range = %d\n", accConfig.range);
  44.         printf("ACC UnderSampling = %d\n", accConfig.us);
  45.         printf("ACC BandWidthParam = %d\n", accConfig.bwp);
  46.         printf("ACC OutputDataRate = %d\n\n", accConfig.odr);
  47.     }
  48.     else
  49.     {
  50.         printf("Failed to get accelerometer configuration\n");
  51.         failures++;
  52.     }
  53.    
  54.     //example of setting user defined configuration
  55.     accConfig.range = BMI160::SENS_4G;
  56.     accConfig.us = BMI160::ACC_US_OFF;
  57.     accConfig.bwp = BMI160::ACC_BWP_2;
  58.     accConfig.odr = BMI160::ACC_ODR_8;
  59.     if(imu.setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
  60.     {
  61.         printf("ACC Range = %d\n", accConfig.range);
  62.         printf("ACC UnderSampling = %d\n", accConfig.us);
  63.         printf("ACC BandWidthParam = %d\n", accConfig.bwp);
  64.         printf("ACC OutputDataRate = %d\n\n", accConfig.odr);
  65.     }
  66.     else
  67.     {
  68.         printf("Failed to set accelerometer configuration\n");
  69.         failures++;
  70.     }
  71.    
  72.     BMI160::GyroConfig gyroConfig;
  73.     if(imu.getSensorConfig(gyroConfig) == BMI160::RTN_NO_ERROR)
  74.     {
  75.         printf("GYRO Range = %d\n", gyroConfig.range);
  76.         printf("GYRO BandWidthParam = %d\n", gyroConfig.bwp);
  77.         printf("GYRO OutputDataRate = %d\n\n", gyroConfig.odr);
  78.     }
  79.     else
  80.     {
  81.         printf("Failed to get gyroscope configuration\n");
  82.         failures++;
  83.     }
  84.    
  85.     wait(1.0);
  86.     printf("\033[H");  //home
  87.     printf("\033[0J");  //erase from cursor to end of screen
  88.    
  89.     if(failures == 0)
  90.     {
  91.         float imuTemperature;
  92.         BMI160::SensorData accData;
  93.         BMI160::SensorData gyroData;
  94.         BMI160::SensorTime sensorTime;
  95.         
  96.         while(1)
  97.         {
  98.             imu.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range);
  99.             imu.getTemperature(&imuTemperature);
  100.             
  101.             printf("ACC xAxis = %s%4.3f\r\n", "\033[K", accData.xAxis.scaled);
  102.             printf("ACC yAxis = %s%4.3f\r\n", "\033[K", accData.yAxis.scaled);
  103.             printf("ACC zAxis = %s%4.3f\r\n\n", "\033[K", accData.zAxis.scaled);
  104.             
  105.             printf("GYRO xAxis = %s%5.1f\r\n", "\033[K", gyroData.xAxis.scaled);
  106.             printf("GYRO yAxis = %s%5.1f\r\n", "\033[K", gyroData.yAxis.scaled);
  107.             printf("GYRO zAxis = %s%5.1f\r\n\n", "\033[K", gyroData.zAxis.scaled);
  108.             
  109.             printf("Sensor Time = %s%f\r\n", "\033[K", sensorTime.seconds);
  110.             printf("Sensor Temperature = %s%5.3f\r\n", "\033[K", imuTemperature);
  111.             
  112.             printf("\033[H");  //home
  113.             gLED = !gLED;
  114.             
  115.             Thread::wait(500);
  116.         }
  117.     }
  118.     else
  119.     {
  120.         while(1)
  121.         {
  122.             rLED = !rLED;
  123.             wait(0.25);
  124.         }
  125.     }
  126. }


  127. //*****************************************************************************
  128. void dumpImuRegisters(BMI160 &imu)
  129. {
  130.     printRegister(imu, BMI160::CHIP_ID);
  131.     printBlock(imu, BMI160::ERR_REG,BMI160::FIFO_DATA);
  132.     printBlock(imu, BMI160::ACC_CONF, BMI160::FIFO_CONFIG_1);
  133.     printBlock(imu, BMI160::MAG_IF_0, BMI160::SELF_TEST);
  134.     printBlock(imu, BMI160::NV_CONF, BMI160::STEP_CONF_1);
  135.     printRegister(imu, BMI160::CMD);
  136.     printf("\n");
  137. }


  138. //*****************************************************************************
  139. void printRegister(BMI160 &imu, BMI160::Registers reg)
  140. {
  141.     uint8_t data;
  142.     if(imu.readRegister(reg, &data) == BMI160::RTN_NO_ERROR)
  143.     {
  144.         printf("IMU Register 0x%02x = 0x%02x\n", reg, data);
  145.     }
  146.     else
  147.     {
  148.         printf("Failed to read register\n");
  149.     }
  150. }


  151. //*****************************************************************************
  152. void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg)
  153. {
  154.     uint8_t numBytes = ((stopReg - startReg) + 1);
  155.     uint8_t buff[numBytes];
  156.     uint8_t offset = static_cast<uint8_t>(startReg);
  157.    
  158.     if(imu.readBlock(startReg, stopReg, buff) == BMI160::RTN_NO_ERROR)
  159.     {
  160.         for(uint8_t idx = offset; idx < (numBytes + offset); idx++)
  161.         {
  162.             printf("IMU Register 0x%02x = 0x%02x\n", idx, buff[idx - offset]);
  163.         }
  164.     }
  165.     else
  166.     {
  167.         printf("Failed to read block\n");
  168.     }
  169. }
复制代码


建议使用putty等终端软件,可以方便的查看数据。

剪贴板01.jpg

附:







回复

使用道具 举报

9818

TA的帖子

14

TA的资源

版主

Rank: 6Rank: 6

测评达人

 楼主| 发表于 2017-8-2 17:33 | 显示全部楼层
修改了一下主程序,可以用加速度传感器控制LED了。

  1. #include "mbed.h"
  2. #include "max32630fthr.h"
  3. #include "bmi160.h"


  4. void dumpImuRegisters(BMI160 &imu);
  5. void printRegister(BMI160 &imu, BMI160::Registers reg);
  6. void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg);


  7. int main()
  8. {
  9.     uint8_t cnt;
  10.    
  11.     MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
  12.    
  13.     PwmOut rLED(LED1);
  14.     PwmOut gLED(LED2);
  15.     PwmOut bLED(LED3);
  16.    
  17.     rLED = gLED = bLED = 1;
  18.    
  19.     I2C i2cBus(P5_7, P6_0);
  20.     i2cBus.frequency(400000);
  21.     BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO);
  22.    
  23.     printf("\033[H");  //home
  24.     printf("\033[0J");  //erase from cursor to end of screen
  25.    
  26.     uint32_t failures = 0;
  27.    
  28.     if(imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
  29.     {
  30.         printf("Failed to set gyroscope power mode\n");
  31.         failures++;
  32.     }
  33.     wait_ms(100);
  34.    
  35.     if(imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR)
  36.     {
  37.         printf("Failed to set accelerometer power mode\n");
  38.         failures++;
  39.     }
  40.     wait_ms(100);
  41.    
  42.    
  43.     BMI160::AccConfig accConfig;
  44.     //example of using getSensorConfig
  45.     if(imu.getSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
  46.     {
  47.         printf("ACC Range = %d\n", accConfig.range);
  48.         printf("ACC UnderSampling = %d\n", accConfig.us);
  49.         printf("ACC BandWidthParam = %d\n", accConfig.bwp);
  50.         printf("ACC OutputDataRate = %d\n\n", accConfig.odr);
  51.     }
  52.     else
  53.     {
  54.         printf("Failed to get accelerometer configuration\n");
  55.         failures++;
  56.     }
  57.    
  58.     //example of setting user defined configuration
  59.     accConfig.range = BMI160::SENS_4G;
  60.     accConfig.us = BMI160::ACC_US_OFF;
  61.     accConfig.bwp = BMI160::ACC_BWP_2;
  62.     accConfig.odr = BMI160::ACC_ODR_8;
  63.     if(imu.setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR)
  64.     {
  65.         printf("ACC Range = %d\n", accConfig.range);
  66.         printf("ACC UnderSampling = %d\n", accConfig.us);
  67.         printf("ACC BandWidthParam = %d\n", accConfig.bwp);
  68.         printf("ACC OutputDataRate = %d\n\n", accConfig.odr);
  69.     }
  70.     else
  71.     {
  72.         printf("Failed to set accelerometer configuration\n");
  73.         failures++;
  74.     }
  75.    
  76.     BMI160::GyroConfig gyroConfig;
  77.     if(imu.getSensorConfig(gyroConfig) == BMI160::RTN_NO_ERROR)
  78.     {
  79.         printf("GYRO Range = %d\n", gyroConfig.range);
  80.         printf("GYRO BandWidthParam = %d\n", gyroConfig.bwp);
  81.         printf("GYRO OutputDataRate = %d\n\n", gyroConfig.odr);
  82.     }
  83.     else
  84.     {
  85.         printf("Failed to get gyroscope configuration\n");
  86.         failures++;
  87.     }
  88.    
  89.     wait(1.0);
  90.     printf("\033[H");  //home
  91.     printf("\033[0J");  //erase from cursor to end of screen
  92.    
  93.     if(failures == 0)
  94.     {
  95.         float imuTemperature;
  96.         BMI160::SensorData accData;
  97.         BMI160::SensorData gyroData;
  98.         BMI160::SensorTime sensorTime;
  99.         
  100.         while(1)
  101.         {
  102.             imu.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range);
  103.             imu.getTemperature(&imuTemperature);
  104.             
  105.             rLED = 1-abs(accData.xAxis.scaled);
  106.             gLED = 1-abs(accData.yAxis.scaled);
  107.             bLED = 1-abs(accData.zAxis.scaled);
  108.             
  109.             cnt++;
  110.             if(cnt > 9)
  111.             {
  112.                 cnt = 0;
  113.                 printf("ACC xAxis = %s%4.3f\r\n", "\033[K", accData.xAxis.scaled);
  114.                 printf("ACC yAxis = %s%4.3f\r\n", "\033[K", accData.yAxis.scaled);
  115.                 printf("ACC zAxis = %s%4.3f\r\n\n", "\033[K", accData.zAxis.scaled);
  116.                
  117.                 printf("GYRO xAxis = %s%5.1f\r\n", "\033[K", gyroData.xAxis.scaled);
  118.                 printf("GYRO yAxis = %s%5.1f\r\n", "\033[K", gyroData.yAxis.scaled);
  119.                 printf("GYRO zAxis = %s%5.1f\r\n\n", "\033[K", gyroData.zAxis.scaled);
  120.                
  121.                 printf("Sensor Time = %s%f\r\n", "\033[K", sensorTime.seconds);
  122.                 printf("Sensor Temperature = %s%5.3f\r\n", "\033[K", imuTemperature);
  123.                
  124.                 printf("\033[H");  //home
  125.             }
  126.             
  127.             Thread::wait(50);
  128.         }
  129.     }
  130.     else
  131.     {
  132.         while(1)
  133.         {
  134.             rLED = !rLED;
  135.             wait(0.25);
  136.         }
  137.     }
  138. }


  139. //*****************************************************************************
  140. void dumpImuRegisters(BMI160 &imu)
  141. {
  142.     printRegister(imu, BMI160::CHIP_ID);
  143.     printBlock(imu, BMI160::ERR_REG,BMI160::FIFO_DATA);
  144.     printBlock(imu, BMI160::ACC_CONF, BMI160::FIFO_CONFIG_1);
  145.     printBlock(imu, BMI160::MAG_IF_0, BMI160::SELF_TEST);
  146.     printBlock(imu, BMI160::NV_CONF, BMI160::STEP_CONF_1);
  147.     printRegister(imu, BMI160::CMD);
  148.     printf("\n");
  149. }


  150. //*****************************************************************************
  151. void printRegister(BMI160 &imu, BMI160::Registers reg)
  152. {
  153.     uint8_t data;
  154.     if(imu.readRegister(reg, &data) == BMI160::RTN_NO_ERROR)
  155.     {
  156.         printf("IMU Register 0x%02x = 0x%02x\n", reg, data);
  157.     }
  158.     else
  159.     {
  160.         printf("Failed to read register\n");
  161.     }
  162. }


  163. //*****************************************************************************
  164. void printBlock(BMI160 &imu, BMI160::Registers startReg, BMI160::Registers stopReg)
  165. {
  166.     uint8_t numBytes = ((stopReg - startReg) + 1);
  167.     uint8_t buff[numBytes];
  168.     uint8_t offset = static_cast<uint8_t>(startReg);
  169.    
  170.     if(imu.readBlock(startReg, stopReg, buff) == BMI160::RTN_NO_ERROR)
  171.     {
  172.         for(uint8_t idx = offset; idx < (numBytes + offset); idx++)
  173.         {
  174.             printf("IMU Register 0x%02x = 0x%02x\n", idx, buff[idx - offset]);
  175.         }
  176.     }
  177.     else
  178.     {
  179.         printf("Failed to read block\n");
  180.     }
  181. }
复制代码


编译后的bin文件,可以直接写入DAPlink运行
bmi160test.ZIP (24.56 KB, 下载次数: 5)


回复

使用道具 举报

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

关闭

站长推荐上一条 /5 下一条

  • 论坛活动 E手掌握

    扫码关注
    EEWORLD 官方微信

  • EE福利  唾手可得

    扫码关注
    EE福利 唾手可得

Archiver|手机版|小黑屋|电子工程世界 ( 京ICP证 060456 )

GMT+8, 2020-2-23 02:19 , Processed in 0.104671 second(s), 18 queries , Gzip On, MemCache On.

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