Stm32旧版库函数2——mpu6050 移植成旧版兼容型库函数DMP

news2024/9/25 3:27:26

 

 main.c:

/*******************************************************************************
// 陀螺仪 MPU6050 IIC测试程序
// 使用单片机STM32F103C8T6
// 晶振:8.00M
// 编译环境 Keil uVision4
// 在3.3V的供电环境下,就能运行
// 波特率 9600
// stm32f103c8t6,8Mhz
// 与模块连接 GPIOB13->SCL GPIOB14->SDA    模块的AD0接10k电阻到地  
// 使用:STM32F103C8T6串口1连接电脑
*******************************************************************************/
#include <stm32f10x_lib.h>
#include "sys_config.h"
#include "mpu6050_delay.h"
#include "inv_mpu_dmp_motion_driver.h"
#include "inv_mpu.h"
#include "math.h"
#include "usart.h"
#include "STM32_I2C.h"
/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */
/* Data requested by client. */

/* Starting sampling rate. */
#define DEFAULT_MPU_HZ  (100)

/** @addtogroup GPIO_IOToggle
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/


/* Private function prototypes -----------------------------------------------*/
//void Delay(__IO uint32_t nCount);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */

struct rx_s {
    unsigned char header[3];
    unsigned char cmd;
};
struct hal_s {
    unsigned char sensors;
    unsigned char dmp_on;
    unsigned char wait_for_tap;
    volatile unsigned char new_gyro;
    unsigned short report;
    unsigned short dmp_features;
    unsigned char motion_int_mode;
    struct rx_s rx;
};
static struct hal_s hal = {0};

/* USB RX binary semaphore. Actually, it's just a flag. Not included in struct
 * because it's declared extern elsewhere.
 */
volatile unsigned char rx_new;

float Pitch,Roll,Yaw;

/* The sensors can be mounted onto the board in any orientation. The mounting
 * matrix seen below tells the MPL how to rotate the raw data from thei
 * driver(s).
 * TODO: The following matrices refer to the configuration on an internal test
 * board at Invensense. If needed, please modify the matrices to match the
 * chip-to-body matrix for your particular set up.
 */
static signed char gyro_orientation[9] = {-1, 0, 0,
                                           0,-1, 0,
                                           0, 0, 1};

enum packet_type_e {
    PACKET_TYPE_ACCEL,
    PACKET_TYPE_GYRO,
    PACKET_TYPE_QUAT,
    PACKET_TYPE_TAP,
    PACKET_TYPE_ANDROID_ORIENT,
    PACKET_TYPE_PEDO,
    PACKET_TYPE_MISC
};

/* These next two functions converts the orientation matrix (see
 * gyro_orientation) to a scalar representation for use by the DMP.
 * NOTE: These functions are borrowed from Invensense's MPL.
 */
static  unsigned short inv_row_2_scale(const signed char *row)
{
    unsigned short b;

    if (row[0] > 0)
        b = 0;
    else if (row[0] < 0)
        b = 4;
    else if (row[1] > 0)
        b = 1;
    else if (row[1] < 0)
        b = 5;
    else if (row[2] > 0)
        b = 2;
    else if (row[2] < 0)
        b = 6;
    else
        b = 7;      // error
    return b;
}

static  unsigned short inv_orientation_matrix_to_scalar(
    const signed char *mtx)
{
    unsigned short scalar;

    /*
       XYZ  010_001_000 Identity Matrix
       XZY  001_010_000
       YXZ  010_000_001
       YZX  000_010_001
       ZXY  001_000_010
       ZYX  000_001_010
     */

    scalar = inv_row_2_scale(mtx);
    scalar |= inv_row_2_scale(mtx + 3) << 3;
    scalar |= inv_row_2_scale(mtx + 6) << 6;


    return scalar;
}


static void run_self_test(void)
{
    int result;
//    char test_packet[4] = {0};
    long gyro[3], accel[3];

    result = mpu_run_self_test(gyro, accel);
    if (result == 0x7) {
        /* Test passed. We can trust the gyro data here, so let's push it down
         * to the DMP.
         */
        float sens;
        unsigned short accel_sens;
        mpu_get_gyro_sens(&sens);
        gyro[0] = (long)(gyro[0] * sens);
        gyro[1] = (long)(gyro[1] * sens);
        gyro[2] = (long)(gyro[2] * sens);
        dmp_set_gyro_bias(gyro);
        mpu_get_accel_sens(&accel_sens);
        accel[0] *= accel_sens;
        accel[1] *= accel_sens;
        accel[2] *= accel_sens;
        dmp_set_accel_bias(accel);
        PrintChar("setting bias succesfully ......\n");
    }
    else
    {
        PrintChar("bias has not been modified ......\n");
    }


}

/* Every time new gyro data is available, this function is called in an
 * ISR context. In this example, it sets a flag protecting the FIFO read
 * function.
 */

#define q30  1073741824.0f
float q0=1.0f,q1=0.0f,q2=0.0f,q3=0.0f;
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
  int result;
  unsigned char aa;     
  /* Configure all unused GPIO port pins in Analog Input mode (floating input
     trigger OFF), this will reduce the power consumption and increase the device
     immunity against EMI/EMC *************************************************/
  RCC_Configuration();
  GPIO_Configuration();
  USART1_Configuration();
  i2cInit();//IIC总线的初始化,尼玛纠结了这么长时间
  for(aa=0;aa<50;aa++)       //为了使USB转串口有时间建立通信争取时间,不然就会掉信息
    delay_ms(100);
  result = mpu_init();
  if(!result)
  {
      //mpu_init();
      PrintChar("mpu initialization complete......\n ");
      //mpu_set_sensor
      if(!mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL))
      {
           PrintChar("mpu_set_sensor complete ......\n");
      }
      else
      {
           PrintChar("mpu_set_sensor come across error ......\n");
      }
      //mpu_configure_fifo
      if(!mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL))
      {
           PrintChar("mpu_configure_fifo complete ......\n");
      }
      else
      {
           PrintChar("mpu_configure_fifo come across error ......\n");
      }
      //mpu_set_sample_rate
      if(!mpu_set_sample_rate(DEFAULT_MPU_HZ))
      {
           PrintChar("mpu_set_sample_rate complete ......\n");
      }
      else
      {
           PrintChar("mpu_set_sample_rate error ......\n");
      }
      //dmp_load_motion_driver_firmvare
      if(!dmp_load_motion_driver_firmware())
      {
          PrintChar("dmp_load_motion_driver_firmware complete ......\n");
      }
      else
      {
          PrintChar("dmp_load_motion_driver_firmware come across error ......\n");
      }
      //dmp_set_orientation
      if(!dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation)))
      {
           PrintChar("dmp_set_orientation complete ......\n");
      }
      else
      {
           PrintChar("dmp_set_orientation come across error ......\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))
      {
           PrintChar("dmp_enable_feature complete ......\n");
      }
      else
      {
           PrintChar("dmp_enable_feature come across error ......\n");
      }
      //dmp_set_fifo_rate
      if(!dmp_set_fifo_rate(DEFAULT_MPU_HZ))
      {
           PrintChar("dmp_set_fifo_rate complete ......\n");
      }
      else
      {
           PrintChar("dmp_set_fifo_rate come across error ......\n");
      }
      run_self_test();
      if(!mpu_set_dmp_state(1))
      {
           PrintChar("mpu_set_dmp_state complete ......\n");
      }
      else
      {
           PrintChar("mpu_set_dmp_state come across error ......\n");
      }
  }
  while(1)
  {
     unsigned long sensor_timestamp;
     short gyro[3], accel[3], sensors;
     unsigned char more;
     long quat[4];
     //float Yaw,Roll,Pitch;
     dmp_read_fifo(gyro, accel, quat, &sensor_timestamp, &sensors,
                &more);    
     /* Gyro and accel data are written to the FIFO by the DMP in chip
     * frame and hardware units. This behavior is convenient because it
     * keeps the gyro and accel outputs of dmp_read_fifo and
     * mpu_read_fifo consistent.
     */
/*     if (sensors & INV_XYZ_GYRO )
       send_packet(PACKET_TYPE_GYRO, gyro);
     if (sensors & INV_XYZ_ACCEL)
        send_packet(PACKET_TYPE_ACCEL, accel); */
     /* Unlike gyro and accel, quaternions are written to the FIFO in
     * the body frame, q30. The orientation is set by the scalar passed
     * to dmp_set_orientation during initialization.
     */
     if (sensors & INV_WXYZ_QUAT )
     {
         //PrintChar("in Calculating quaternion steps.....\n");
          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;
         PrintChar("Pitch:");
         PrintFloat(Pitch);
         PrintChar("Roll:");
         PrintFloat(Roll);
         PrintChar("Yaw:");
         PrintFloat(Yaw);
         USART_SendData(USART1, 0X0D);        //换行
         USART_SendData(USART1, 0X0A);        //回车     
         delay_ms(10);         //可以去掉

     }
     if(sensors & INV_XYZ_GYRO)
     {}
     if(sensors & INV_XYZ_ACCEL)
     {}
     //PrintChar("NO! ");
      //send_packet(PACKET_TYPE_QUAT, quat);
  }
}

/*************结束***************/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/78729.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

STM32通过DAC产生正弦波

前言 这一讲主要来讲解DAC功能 文章目录 前言一、DAC简介二、DAC通道框图三、DAC输出电压四、输出正弦波五、代码一、DAC简介 数字/模拟转换模块(DAC)是12位数字输入,电压输出的数字/模拟转换器。DAC可以配置为8位或12位模式,也可以与DMA控制器配合使用。DAC工作在12位(1…

[机缘参悟-93]:时间、空间、多维度、动态、全局、系统思考模型汇总

目录 前言&#xff1a; 一、空间 - 广度 - 静态 - 多维度模型 1.1 一元太极本源模型 1.2 二元阴阳组合模型 1.3 三元铁三角稳定模型 1.4 四象限优先级模型 1.5 九宫格二维矩阵模型 二、空间 - 高度 - 静态 - 多层次模型 2.1 倒树形层次模型 2.2 金字塔层次结构模型 …

新年新气象:Stimulsoft Designer 2023.1.0 Crack

使用 Stimulsoft Designer&#xff0c;您可以轻松设计从简单列表到多页、具有复杂计算、条件、函数和变量的报告。只需单击几下&#xff0c;您就可以创建易于理解且信息丰富的仪表板、设置过滤器以及对任何元素进行排序。Ω578867473 报表设计器很简单 这是一个不需要编程技能的…

6 转移指令

转移指令 1 数据存储位置的表示 我们定义的描述性符号&#xff1a; reg 和sreg 。使用描述性的符号reg 来表示一个寄存器&#xff0c;用sreg 表示一个段寄存器。 reg 的集合包括&#xff1a; ax 、bx 、ex 、dx 、ah 、al 、bh 、bl 、ch 、cl 、dh 、di 、sp 、bp 、si 、d…

NAT (Network Address Translations) 网络地址转换

数据来源 1、ipv4地址严重不够用了 X.X.X.X X 0-255 A、B、C类可以使用 D组播 E科研 2、IP地址分为公网IP和私网IP 公网IP只能在公网上使用私网IP只能在内网中使用公网上不允许出现私有IP地址私网IP可以重复在内网使用1&#xff09;私有地址范围 10.0.0.0/8&…

Vite+Vue3构建前端框架及模板代码及重要知识点

Vue3Vite构建步骤 用vite初始化vue项目(回车) npm create vitelatest vueVitePro -- --template vue安装配置路由vue-router npm install vue-router4 import router from ./router/index.js createApp(App).use(router).mount(#app) 安装 element-plus 及图标 npm ins…

一个PCA加速技巧

EVD-PCA PCA推导&#xff1a;PCA主成分分析法浅理解 具体数值如1030410304是我机器学习课程实验的数据集参数&#xff0c;这里关注数字量级即可。 code % EVD-PCA数据降维 % input: DN output:KN function [Z, K] EVD_PCA(X, K, weight)fprintf(Running EVD-PCA dimension…

Matplotlib学习笔记(第二章 2.13 Matplotlib中的图形(二))

路径(Paths) 你可以使用matplotlib.path模块在Matplotlib中添加任意路径&#xff1a; Fig. 6: Path Patch 三维绘图(Three-dimensional plotting) mplot3d工具包(参见see Getting started and mplot3d-examples-index))支持简单的3D图形&#xff0c;包括曲面、线框、散点图和…

【华为上机真题 2022】玩牌高手

&#x1f388; 作者&#xff1a;Linux猿 &#x1f388; 简介&#xff1a;CSDN博客专家&#x1f3c6;&#xff0c;华为云享专家&#x1f3c6;&#xff0c;Linux、C/C、云计算、物联网、面试、刷题、算法尽管咨询我&#xff0c;关注我&#xff0c;有问题私聊&#xff01; &…

【OpenCV-Python】教程:4-5 SURF (Speeded-Up Robust Features) 介绍

OpenCV Python SURF &#xff08;Speeded-Up Robust Features&#xff09; 介绍 【目标】 SURF的基础 【理论】 SURF 是 SIFT 的提速版本&#xff1b; 在SIFT中&#xff0c;Lowe用 DoG 近似 LoG&#xff1b;SURF 走的更远一点&#xff0c;用 box filter 近似 LoG 。下图显…

文本生成公开数据集/开源工具/经典论文详细列表分享

这是一份由清华大学自然语言处理小组整理的文本生成相关的公开数据集/开源工具/经典论文列表&#xff0c;并且不断增加论文和持续修改名单&#xff0c;分享给大家。 源链接&#xff1a;https://github.com/THUNLP-MT/TG-Reading-List 目录 数据集 故事生成 文本生成 工具 经典…

开关电源环路稳定性分析(06)-功率级和控制级

大家好&#xff0c;这里是大话硬件。 根据上一篇文章的分析&#xff0c;开关电源系统主要分为3个部分&#xff0c;功率级&#xff0c;控制级&#xff0c;反馈级。今天这篇文章我们分析功率级和控制级的传递函数。 1.功率级传递函数 从功能框图上可以看出来&#xff0c;功率…

教材征订和下发系统

项目描述 临近学期结束&#xff0c;还是毕业设计&#xff0c;你还在做java程序网络编程&#xff0c;期末作业&#xff0c;老师的作业要求觉得大了吗?不知道毕业设计该怎么办?网页功能的数量是否太多?没有合适的类型或系统?等等。这里根据疫情当下&#xff0c;你想解决的问…

Spring Cloud Alibaba Nacos Config - - - >配置中心

官方文档&#xff1a;https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-config 市面上比较有名的配置中心&#xff1a; Spring Cloud ConfigApolloSpring Cloud Alibaba Nacos Config Spring Cloud Config 大部分场景结合 git 使用&#xff0c;动态变更还需要依赖…

Python获取世界杯热搜榜,并制作脚本自动发送信息到邮箱

前言 现在正是卡塔尔世界杯激战正酣的时候&#xff0c;每天都有各种各样的新闻。而且&#xff0c;不同的球队&#xff0c;随着比赛的进程&#xff0c;关注的热度也会发生翻天覆地的变化。 今天我们就来获取卡塔尔世界的球队热搜榜&#xff0c;并制作自动发送邮件脚本&#xff…

深度优先搜索(DFS)剪枝:记忆化搜索(C++)

目录 一、基本思想 二、样例 三、程序 1、普通的深度优先搜索 2、分析 3、记忆化搜索 程序 四、实际速度样例 一、基本思想 今天我们来讲一下深搜的剪枝方法中的一个&#xff1a;记忆化搜索。 顾名思义&#xff0c;记忆化搜索就是让程序记住一些东西&#xff0c;然后可以…

Stimulsoft Dashboards.JS JavaScript 2203.1.0仪表板

Stimulsoft Dashboards.JS--Ω578867473 Dashboards.JS 是一个功能齐全的工具&#xff0c;用于为 JavaScript 平台创建仪表板。 JavaScript 仪表板 Dashboards.JS 是一个功能齐全的工具&#xff0c;用于为 JavaScript 平台创建仪表板。要生成和查看仪表板&#xff0c;您需要任何…

Qt扫盲-QAbstractButton 笔记总结

QAbstractButton使用总结一、概要1.显示内容2. 快捷键3. 对话框默认按钮4. 按钮状态5. 信号说明6. 自定义按钮QAbstractButton 类实现的是一个抽象按钮。主要是Button类具有的共性&#xff0c;但是处理用户的操作响应、并绘制不同按钮的形式是由子类来完成的。一、概要 QAbstr…

图文深度解析Linux内存碎片整理实现机制以及源码

图文深度解析Linux内存碎片整理实现机制以及源码。 物理内存是以页为单位进行管理的,每个内存页大小默认是4K(大页除外)。申请物理内存时,一般都是按顺序分配的,但释放内存的行为是随机的。随着系统运行时间变长后,将会出现以下情况: 在多道程序当中,如果要让我们的程…

深度解析车载域控制器

文章目录域控制器域控制器的组成ADAS域控制器智能座舱域HUD仪表盘IVI域控制器的发展域控制器对传统ECU的挑战域控制器 ​ 随着车辆的信息化程度的发展&#xff0c;车辆的ECU也越来越多&#xff0c;从引擎控制、转向助力、仪表、影音等&#xff0c;传统的汽车电子电气架构是分布…