找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3087|回复: 0
收起左侧

基于nrf51822单片机的工程:MPU6050输出姿态数据

[复制链接]
ID:326995 发表于 2018-5-10 17:23 | 显示全部楼层 |阅读模式
给大家分享一个nrf51822+MPU6050输出姿态数据的单片机源程序如下:
  1. /****************************************Copyright (c)****************************************************
  2. **                                       
  3. **                                 
  4. **
  5. **--------------File Info---------------------------------------------------------------------------------
  6. ** File name:                             main.c
  7. ** Last modified Date:      
  8. ** Last Version:                  
  9. ** Descriptions:                   使用的SDK版本-SDK_11.0.0
  10. **                               
  11. **--------------------------------------------------------------------------------------------------------
  12. ** Created by:                        FiYu
  13. ** Created date:                2016-7-1
  14. ** Version:                            1.0
  15. ** Descriptions:                MPU6050输出姿态数据实验
  16. **--------------------------------------------------------------------------------------------------------*/
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include "app_uart.h"
  21. #include "app_error.h"
  22. #include "nrf_delay.h"
  23. #include "nrf_gpio.h"
  24. #include "boards.h"
  25. #include "mpu6050.h"
  26. #include "twi_master.h"
  27. #include "inv_mpu.h"
  28. #include "inv_mpu_dmp_motion_driver.h"

  29. /* 开发板中MPU6050模块和串口串口占用的nRF51822管脚资源
  30. P0.09:UART_TXD   :串口发送
  31. P0.11:UART_RXD   :串口接收
  32. P0.08:UART_CTS
  33. P0.10:UART_RTS

  34. 串口需要短接对应的跳线帽

  35. P0.01:IIC时钟
  36. P0.04:IIC数据
  37. */

  38. #define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
  39. #define UART_RX_BUF_SIZE 1                           /**< UART RX buffer size. */


  40. void uart_error_handle(app_uart_evt_t * p_event)
  41. {
  42.     if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
  43.     {
  44.         APP_ERROR_HANDLER(p_event->data.error_communication);
  45.     }
  46.     else if (p_event->evt_type == APP_UART_FIFO_ERROR)
  47.     {
  48.         APP_ERROR_HANDLER(p_event->data.error_code);
  49.     }
  50. }

  51. //串口初始化。禁止流控,波特率:115200
  52. void uart_init(void)
  53. {
  54.           uint32_t err_code;
  55.     const app_uart_comm_params_t comm_params =
  56.     {
  57.           RX_PIN_NUMBER,
  58.           TX_PIN_NUMBER,
  59.           RTS_PIN_NUMBER,
  60.           CTS_PIN_NUMBER,
  61.           APP_UART_FLOW_CONTROL_DISABLED, //禁止流控
  62.           false,
  63.           UART_BAUDRATE_BAUDRATE_Baud115200//波特率115200
  64.     };

  65.     APP_UART_FIFO_INIT(&comm_params,
  66.                          UART_RX_BUF_SIZE,
  67.                          UART_TX_BUF_SIZE,
  68.                          uart_error_handle,
  69.                          APP_IRQ_PRIORITY_LOW,
  70.                          err_code);

  71.     APP_ERROR_CHECK(err_code);
  72. }
  73. /***********************************************************************************
  74. * 描  述 : 串口发送数据,数据格式为匿名四轴上位机软件(V2.6版本)数据格式
  75. * 入  参 : fun:功能码
  76. *        : dat:数据缓存区地址,最多28字节
  77. *        : len:数据长度,最大28字节
  78. * 返回值 : 无
  79. **********************************************************************************/
  80. void Uart_SendDat_ToPC(uint8_t fun,uint8_t *dat,uint8_t len)
  81. {
  82.         uint8_t send_buf[32];
  83.         uint8_t i;
  84.         if(len>28)return;          //最多28字节数据
  85.         send_buf[len+3]=0;        //校验数置零
  86.         send_buf[0]=0x88;          //帧头
  87.         send_buf[1]=fun;          //功能码
  88.         send_buf[2]=len;          //数据长度
  89.         for(i=0;i<len;i++)send_buf[3+i]=dat[i];                              //复制数据
  90.         for(i=0;i<len+3;i++)send_buf[len+3]+=send_buf[i];        //计算校验和       
  91.         for(i=0;i<len+4;i++)app_uart_put(send_buf[i]);          //串口输出数据
  92. }
  93. /***********************************************************************************
  94. * 描  述 : 发送加速度传感器数据和陀螺仪数据
  95. * 入  参 : aacx,aacy,aacz:x,y,z三个方向上面的加速度值
  96. *          gyrox,gyroy,gyroz:x,y,z三个方向上面的陀螺仪值
  97. * 返回值 : 无
  98. **********************************************************************************/
  99. void mpu6050_send_dat(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz)
  100. {
  101.         uint8_t tx_buf[12];
  102.         tx_buf[0]=(aacx>>8)&0xFF;
  103.         tx_buf[1]=aacx&0xFF;
  104.         tx_buf[2]=(aacy>>8)&0xFF;
  105.         tx_buf[3]=aacy&0xFF;
  106.         tx_buf[4]=(aacz>>8)&0xFF;
  107.         tx_buf[5]=aacz&0xFF;
  108.         tx_buf[6]=(gyrox>>8)&0xFF;
  109.         tx_buf[7]=gyrox&0xFF;
  110.         tx_buf[8]=(gyroy>>8)&0xFF;
  111.         tx_buf[9]=gyroy&0xFF;
  112.         tx_buf[10]=(gyroz>>8)&0xFF;
  113.         tx_buf[11]=gyroz&0xFF;
  114.         Uart_SendDat_ToPC(0xA1,tx_buf,12);//自定义帧,0XA1
  115. }       

  116. /***********************************************************************************
  117. * 描  述 : 串口上传MPU6050姿态数据
  118. * 入  参 : aacx,aacy,aacz:x,y,z三个方向上面的加速度值
  119. *        : gyrox,gyroy,gyroz:x,y,z三个方向上面的陀螺仪值
  120. *        : roll:横滚角.单位0.01度。 -18000 -> 18000 对应 -180.00  ->  180.00度
  121. *        : pitch:俯仰角.单位 0.01度。-9000 - 9000 对应 -90.00 -> 90.00 度
  122. *        : yaw:航向角.单位为0.1度 0 -> 3600  对应 0 -> 360.0度
  123. * 返回值 : 无
  124. **********************************************************************************/
  125. void Uart_ReportIMU(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,short roll,short pitch,short yaw)
  126. {
  127.         uint8_t i,tx_buf[28];

  128.         for(i=0;i<28;i++)tx_buf[i]=0;//清0
  129.         tx_buf[0]=(aacx>>8)&0xFF;
  130.         tx_buf[1]=aacx&0xFF;
  131.         tx_buf[2]=(aacy>>8)&0xFF;
  132.         tx_buf[3]=aacy&0xFF;
  133.         tx_buf[4]=(aacz>>8)&0xFF;
  134.         tx_buf[5]=aacz&0xFF;
  135.         tx_buf[6]=(gyrox>>8)&0xFF;
  136.         tx_buf[7]=gyrox&0xFF;
  137.         tx_buf[8]=(gyroy>>8)&0xFF;
  138.         tx_buf[9]=gyroy&0xFF;
  139.         tx_buf[10]=(gyroz>>8)&0xFF;
  140.         tx_buf[11]=gyroz&0xFF;       
  141.         tx_buf[18]=(roll>>8)&0xFF;
  142.         tx_buf[19]=roll&0xFF;
  143.         tx_buf[20]=(pitch>>8)&0xFF;
  144.         tx_buf[21]=pitch&0xFF;
  145.         tx_buf[22]=(yaw>>8)&0xFF;
  146.         tx_buf[23]=yaw&0xFF;
  147.         Uart_SendDat_ToPC(0xAF,tx_buf,28);//匿名四轴飞控显示帧,0xAF
  148. }
  149. /**********************************************************************************************
  150. * 描  述 : main函数
  151. * 入  参 : 无
  152. * 返回值 : 无
  153. ***********************************************************************************************/
  154. int main(void)
  155. {
  156.     int16_t AccValue[3],GyroValue[3];
  157.           uint8_t id;
  158.           float pitch,roll,yaw;                 //欧拉角
  159.           short aacx,aacy,aacz;                  //加速度传感器原始数据
  160.           short gyrox,gyroy,gyroz;        //陀螺仪原始数据
  161.        
  162.           nrf_gpio_cfg_output(LED_1);//配置管脚P0.21为输出,驱动指示灯D1
  163.     nrf_gpio_pin_set(LED_1);   //设置指示灯D1初始状态为熄灭
  164.        
  165.     uart_init();  //配置串口,禁止流控,波特率:115200       
  166.                
  167.                 twi_master_init();
  168.        
  169.           if(mpu6050_init(0x68) == false)
  170.                 {
  171.                     while (true)
  172.                           {
  173.                                     printf("mpu6050 init fail\r\n");
  174.                                           nrf_delay_ms(100);
  175.                                 }       
  176.                 }
  177.                 else
  178.                 {nrf_delay_ms(1000);printf("mpu6050 init ok\r\n");}
  179.                
  180.     mpu6050_register_read(0x75U, &id, 1);
  181.                 printf("mpu6050 id is %d \r\n",id);
  182.                
  183.                 while(mpu_dmp_init())
  184.                 {
  185.                     nrf_delay_ms(1000);
  186.                           printf("mpu6050 init Error\r\n");
  187.                 }

  188.     while (true)
  189.     {
  190.                           if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
  191.                     {
  192.                               MPU6050_ReadAcc(&aacx,&aacy,&aacz);            //读取加速度传感器数据
  193.                               MPU6050_ReadGyro(&gyrox,&gyroy,&gyroz);        //读取陀螺仪数据
  194.                               mpu6050_send_dat(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定义帧发送加速度和陀螺仪原始数据
  195.                               Uart_ReportIMU(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)(pitch*100),(int)(yaw*10));
  196.             nrf_delay_ms(50);
  197.                     }
  198.     }
  199. }
  200. /********************************************END FILE*******************************************/
复制代码

所有资料51hei提供下载:
实验1 - MPU6050输出姿态数据.rar (2.59 MB, 下载次数: 54)
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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