使用i2c设备控制6轴姿态传感器
时间:10-02
整理:3721RD
点击:
换装了Armbian系统镜像之后就可以使用系统自带的i2c设备访问i2c外设,我的试用项目上有用到6轴姿态传感器,所以这次把我的四轴上使用的MPU6050模块拆下来了,接到了orangepi zero的i2c0上。再进行程序编写之前可以使用i2ctools进行检测,用于调试很方便。安装i2ctools步骤如下:
查看系统的i2c设备。使用命令
主函数如下所示:
- apt-get install i2c-tools
- i2cdetect -l
查看系统的i2c设备。使用命令
- i2cdetect -y 0
主函数如下所示:
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <linux/i2c-dev.h>
- #include <errno.h>
- #include "gpio.h"
- #include "i2c.h"
- #include "math.h"
- #include "./DMP/inv_mpu.h"
- #include "./DMP/inv_mpu_dmp_motion_driver.h"
- static signed char gyro_orientation[9] = {-1, 0, 0,
- 0,-1, 0,
- 0, 0, 1};
- #define q30 1073741824.0f
- float q0=1.0f,q1=0.0f,q2=0.0f,q3=0.0f;
- char num[50];
- float Pitch,Roll,Yaw;
- unsigned long sensor_timestamp;
- short gyro[3], accel[3], sensors;
- unsigned char more;
- long quat[4];
- char buf[256];
- void DataProcess(void)
- {
- if(sensors & INV_WXYZ_QUAT)
- {
- q0 = quat[0] / q30;
- q1 = quat[1] / q30;
- q2 = quat[2] / q30;
- q3 = quat[3] / q30;
- Pitch = asin(2 * q1 * q3 - 2 * q0* q2)* 57.3; // pitch
- Roll = atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2* q2 + 1)* 57.3; // roll
- Yaw = atan2(2*(q1*q2 + q0*q3),q0*q0+q1*q1-q2*q2-q3*q3) * 57.3; // yaw
- }
- }
- int main(void)
- {
- GPIO_Init();
- GPIO_ConfigPin(PA,15,OUT);
- int result;
- result = IIC_Open();
- if(!result)
- {
- int i;
- if(IIC_ReadBuff(0x00,buf,256) == 0)
- {
- for(i=0;i<256;i++)
- {
- if(i % 16 == 0)
- printf("\r\n");
- printf("%.2X ",buf[i]);
- }
- }
- else
- {
- printf("read error\r\n");
- exit(0);
- }
-
- result = mpu_init();
- if(!result)
- {
- printf("mpu initialization complete......\n\r\n");
- //mpu_set_sensor 设置传感器
- if(!mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL))
- printf("mpu_set_sensor complete ......\n\r\n");
- else
- printf("mpu_set_sensor come across error ......\n\r\n");
- //mpu_configure_fifo 配置FIFO
- if(!mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL))
- printf("mpu_configure_fifo complete ......\n\r\n");
- else
- printf("mpu_configure_fifo come across error ......\n\r\n");
- //mpu_set_sample_rate-----------------设置数据采样速率,默认是DEFAULT_MPU_HZ频率为100hz,在4hz到1000hz之间
- if(!mpu_set_sample_rate(iMPU_HZ))
- printf("mpu_set_sample_rate complete ......\n\r\n");
- else
- printf("mpu_set_sample_rate error ......\n\r\n");
- //dmp_load_motion_driver_firmvare-----加载运动引擎固件
- if(!dmp_load_motion_driver_firmware())
- printf("dmp_load_motion_driver_firmware complete ......\n\r\n");
- else
- printf("dmp_load_motion_driver_firmware come across error ......\n\r\n");
- //dmp_set_orientation-----------------设定方向
- if(!dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation)))
- printf("dmp_set_orientation complete ......\n\r\n");
- else
- printf("dmp_set_orientation come across error ......\n\r\n");
- //dmp_enable_feature------------------使能功能特性
- if(!dmp_enable_feature( DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_TAP | DMP_FEATURE_ANDROID_ORIENT |
- DMP_FEATURE_SEND_RAW_ACCEL | DMP_FEATURE_SEND_CAL_GYRO | DMP_FEATURE_GYRO_CAL))
- printf("dmp_enable_feature complete ......\n\r\n");
- else
- printf("dmp_enable_feature come across error ......\n\r\n");
- //dmp_set_fifo_rate-------------------设置FIFO速率,默认是DEFAULT_MPU_HZ频率为100hz,采样速率不得大于 DMP_SAMPLE_RATE(200hz)
- if(!dmp_set_fifo_rate(iFIFO_HZ))
- printf("dmp_set_fifo_rate complete ......\n\r\n");
- else
- printf("dmp_set_fifo_rate come across error ......\n\r\n");
- run_self_test();
- if(!mpu_set_dmp_state(1))
- printf("mpu_set_dmp_state complete ......\n\r\n");
- else
- printf("mpu_set_dmp_state come across error ......\n\r\n");
- }
- else
- {
- printf("mpu initialization error......\n\r\n");
- exit(0);
- }
- }
- else
- {
- exit(0);
- }
-
- int a = 0;
- while(1)
- {
- // printf(".");
- // fflush(stdout);
-
- DataProcess();
- printf("%.4f,%.4f,%.4f\r\n",Pitch,Roll,Yaw);
- usleep(50000);
-
- GPIO_SetPin(PA,15,a=~a);
- }
- }
非常棒!
O(∩_∩)O谢谢,我继续加油
好棒的资料 跟着小编学习一下