微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > 嵌入式系统设计讨论 > 使用i2c设备控制6轴姿态传感器

使用i2c设备控制6轴姿态传感器

时间:10-02 整理:3721RD 点击:
换装了Armbian系统镜像之后就可以使用系统自带的i2c设备访问i2c外设,我的试用项目上有用到6轴姿态传感器,所以这次把我的四轴上使用的MPU6050模块拆下来了,接到了orangepi zero的i2c0上。再进行程序编写之前可以使用i2ctools进行检测,用于调试很方便。安装i2ctools步骤如下:

  1. apt-get install i2c-tools

复制代码

安装完之后可以使用命令

  1. i2cdetect -l

复制代码


查看系统的i2c设备。使用命令

  1. i2cdetect -y 0

复制代码

可以查看i2c-0上挂载的外设,例如我的MPU6050的地址为68,所以就会检测到0x68地址处有设备存在,接下来就是进行MPU6050的程序移植工作了,我使用的MPU6050官方提供的DMP库进行姿态获取,免去了自己进行姿态解算步骤,之前四轴上使用STM32加上互补滤波求姿态的。移植程序很简单,使用官方的源文件即可,然后进行DMP功能的初始化操作,运行结果截图如下:



主函数如下所示:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <linux/i2c-dev.h>
  5. #include <errno.h>
  6. #include "gpio.h"  
  7. #include "i2c.h"
  8. #include "math.h"
  9. #include "./DMP/inv_mpu.h"
  10. #include "./DMP/inv_mpu_dmp_motion_driver.h"

  11. static signed char gyro_orientation[9] = {-1, 0, 0,
  12.                                            0,-1, 0,
  13.                                            0, 0, 1};
  14. #define                                 q30  1073741824.0f
  15. float                                         q0=1.0f,q1=0.0f,q2=0.0f,q3=0.0f;
  16. char                                        num[50];
  17. float                                         Pitch,Roll,Yaw;
  18. unsigned long                         sensor_timestamp;
  19. short                                         gyro[3], accel[3], sensors;
  20. unsigned char                         more;
  21. long                                         quat[4];

  22. char                                        buf[256];

  23. void DataProcess(void)
  24. {       
  25.         if(sensors & INV_WXYZ_QUAT)
  26.         {
  27.                 q0 = quat[0] / q30;
  28.                 q1 = quat[1] / q30;
  29.                 q2 = quat[2] / q30;
  30.                 q3 = quat[3] / q30;
  31.                 Pitch          = asin(2 * q1 * q3 - 2 * q0* q2)* 57.3;        // pitch
  32.                 Roll        = atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2* q2 + 1)* 57.3;        // roll
  33.                 Yaw                = atan2(2*(q1*q2 + q0*q3),q0*q0+q1*q1-q2*q2-q3*q3) * 57.3;        // yaw
  34.         }
  35. }

  36. int main(void)
  37. {
  38.         GPIO_Init();
  39.         GPIO_ConfigPin(PA,15,OUT);

  40.         int result;
  41.         result = IIC_Open();
  42.         if(!result)
  43.         {
  44.                 int i;
  45.                 if(IIC_ReadBuff(0x00,buf,256) == 0)
  46.                 {
  47.                         for(i=0;i<256;i++)
  48.                         {
  49.                                 if(i % 16 == 0)
  50.                                         printf("\r\n");
  51.                                 printf("%.2X ",buf[i]);
  52.                         }
  53.                 }
  54.                 else
  55.                 {
  56.                         printf("read error\r\n");
  57.                         exit(0);
  58.                 }
  59.                
  60.                 result = mpu_init();
  61.                 if(!result)
  62.                 {
  63.                         printf("mpu initialization complete......\n\r\n");
  64.                         //mpu_set_sensor                        设置传感器
  65.                         if(!mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL))
  66.                                 printf("mpu_set_sensor complete ......\n\r\n");
  67.                         else
  68.                                 printf("mpu_set_sensor come across error ......\n\r\n");
  69.                         //mpu_configure_fifo                                                                        配置FIFO
  70.                         if(!mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL))
  71.                                 printf("mpu_configure_fifo complete ......\n\r\n");
  72.                         else
  73.                                 printf("mpu_configure_fifo come across error ......\n\r\n");
  74.                         //mpu_set_sample_rate-----------------设置数据采样速率,默认是DEFAULT_MPU_HZ频率为100hz,在4hz到1000hz之间
  75.                         if(!mpu_set_sample_rate(iMPU_HZ))
  76.                                 printf("mpu_set_sample_rate complete ......\n\r\n");
  77.                         else
  78.                                 printf("mpu_set_sample_rate error ......\n\r\n");
  79.                         //dmp_load_motion_driver_firmvare-----加载运动引擎固件
  80.                         if(!dmp_load_motion_driver_firmware())
  81.                                 printf("dmp_load_motion_driver_firmware complete ......\n\r\n");
  82.                         else
  83.                                 printf("dmp_load_motion_driver_firmware come across error ......\n\r\n");
  84.                         //dmp_set_orientation-----------------设定方向
  85.                         if(!dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation)))
  86.                                 printf("dmp_set_orientation complete ......\n\r\n");
  87.                         else
  88.                                 printf("dmp_set_orientation come across error ......\n\r\n");
  89.                         //dmp_enable_feature------------------使能功能特性
  90.                         if(!dmp_enable_feature(        DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_TAP | DMP_FEATURE_ANDROID_ORIENT |
  91.                                                                         DMP_FEATURE_SEND_RAW_ACCEL | DMP_FEATURE_SEND_CAL_GYRO | DMP_FEATURE_GYRO_CAL))
  92.                                 printf("dmp_enable_feature complete ......\n\r\n");
  93.                         else
  94.                                 printf("dmp_enable_feature come across error ......\n\r\n");
  95.                         //dmp_set_fifo_rate-------------------设置FIFO速率,默认是DEFAULT_MPU_HZ频率为100hz,采样速率不得大于 DMP_SAMPLE_RATE(200hz)
  96.                         if(!dmp_set_fifo_rate(iFIFO_HZ))
  97.                                 printf("dmp_set_fifo_rate complete ......\n\r\n");
  98.                         else
  99.                                 printf("dmp_set_fifo_rate come across error ......\n\r\n");
  100.                         run_self_test();
  101.                         if(!mpu_set_dmp_state(1))
  102.                                 printf("mpu_set_dmp_state complete ......\n\r\n");
  103.                         else
  104.                                 printf("mpu_set_dmp_state come across error ......\n\r\n");
  105.                 }
  106.                 else
  107.                 {
  108.                         printf("mpu initialization error......\n\r\n");
  109.                         exit(0);
  110.                 }
  111.         }
  112.         else
  113.         {
  114.                 exit(0);
  115.         }
  116.        
  117.         int a = 0;
  118.         while(1)
  119.         {
  120.                 // printf(".");
  121.                 // fflush(stdout);
  122.                
  123.                 DataProcess();
  124.                 printf("%.4f,%.4f,%.4f\r\n",Pitch,Roll,Yaw);
  125.                 usleep(50000);
  126.                
  127.                 GPIO_SetPin(PA,15,a=~a);
  128.         }
  129. }


复制代码

官方的库文件网上很多的,这里就不贴出来了,太占篇幅,使用到的GPIO操作在我之前的评测中有详细说明。


非常棒!

O(∩_∩)O谢谢,我继续加油

好棒的资料  跟着小编学习一下     

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top