[应用相关] STM32之MPU6050获取欧拉角

[复制链接]
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:43 | 显示全部楼层
修改几个重要函数
mpu_init() 79303658d351a39e40.png
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:43 | 显示全部楼层
atk_ms6050_dmp_init()uint8_t mpu6050_dmp_init(void)
{
    uint8_t ret;
  
                if(mpu6050_init() == 0){
               
                        ret = mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL);       /* 开启指定传感器 */
                        if(ret) return 1;
                       
                        ret = mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL);    /* 设置FIFO */
                        if(ret) return 2;
                       
                        ret = mpu_set_sample_rate(DEFAULT_MPU_HZ);                 /* 设置采样率 */
                        if(ret) return 3;
                       
                        ret = dmp_load_motion_driver_firmware();                   /* 加载DMP固件 */
                        if(ret) return 4;
                       
                        ret = dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation));  /* 设置陀螺仪方向 */
                        if(ret) return 5;
                       
                        ret = dmp_enable_feature(  DMP_FEATURE_6X_LP_QUAT      |   /* 设置DMP功能 */
                                DMP_FEATURE_TAP             |
                                DMP_FEATURE_ANDROID_ORIENT  |
                                DMP_FEATURE_SEND_RAW_ACCEL  |
                                DMP_FEATURE_SEND_CAL_GYRO   |
                                DMP_FEATURE_GYRO_CAL);
                        if(ret) return 6;
               
                        ret = dmp_set_fifo_rate(DEFAULT_MPU_HZ);                   /* 设置DMP输出速率 */
                        if(ret) return 7;
                       
                        ret = mpu_set_dmp_state(1);                                /* 使能DMP */
                        if(ret) return 8;
                       
                        ret = mpu6050_run_self_test();                          /* 传感器自测试 */
                        if(ret) return 9;
                       
                        return 0;
                }
               
                return 10;
}
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:48 | 显示全部楼层
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:48 | 显示全部楼层
atk_ms6050_get_clock_ms()
这个没什么用,删掉就行
.
.
atk_ms6050_run_self_test()
改名成mpu6050_run_self_test()就行
.
.
atk_ms6050_dmp_get_data()
改名成mpu6050_dmp_get_data()就行
.
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:48 | 显示全部楼层
4. 修改inv_mpu.h文件

uint8_t mpu6050_run_self_test(void);
uint8_t mpu6050_dmp_init(void);
uint8_t mpu6050_dmp_get_data(float *pitch, float *roll, float *yaw);
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:48 | 显示全部楼层
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:49 | 显示全部楼层
. 修改inv_mpu_dmp_motion_driver.c文件

修改头文件
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "inv_mpu.h"
#include "inv_mpu_dmp_motion_driver.h"
#include "dmpKey.h"
#include "dmpmap.h"
#include "delay.h"
#include "usart.h"
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:49 | 显示全部楼层
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:49 | 显示全部楼层
修改部分宏定义。 72068658d369008b6a.png 这样我们的DMP库就移植完成了。
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:49 | 显示全部楼层
四元数
mpu6050 的 DMP 输出的四元数是 q30 格式的,也就是将正常浮点格式的四元数放大了 230 倍,因此在换算之前,需要将 DMP 输出的四元数转换为正常的浮点格式,也就是将其除以 230,然后才能将其转换为欧拉角。四元数的介绍可以看这篇文章四元数与欧拉角(Yaw、Pitch、Roll)的转换。
.
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:50 | 显示全部楼层
MPU6050获取欧拉角:俯仰角(Pitch)、横滚角(Roll)、偏航角(Yaw)
前面我们已经使用过mpu6050获取过加速度计和陀螺仪以及温度传感器的原始数据,同时我们也已经成功移植了DMP库,因此只需要修改main.c文件就行。
 楼主| 发顺丰更大nc 发表于 2023-12-28 16:50 | 显示全部楼层
修改main.c文件

  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2023 STMicroelectronics.
  10.   * All rights reserved.
  11.   *
  12.   * This software is licensed under terms that can be found in the LICENSE file
  13.   * in the root directory of this software component.
  14.   * If no LICENSE file comes with this software, it is provided AS-IS.
  15.   *
  16.   ******************************************************************************
  17.   */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "usart.h"
  22. #include "gpio.h"

  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */

  25. #include <stdio.h>
  26. #include "mpu6050.h"
  27. #include "delay.h"
  28. #include "inv_mpu.h"
  29. #include "inv_mpu_dmp_motion_driver.h"

  30. /* USER CODE END Includes */

  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */

  33. /* USER CODE END PTD */

  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. /* USER CODE END PD */

  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */

  39. /* USER CODE END PM */

  40. /* Private variables ---------------------------------------------------------*/

  41. /* USER CODE BEGIN PV */

  42. /* USER CODE END PV */

  43. /* Private function prototypes -----------------------------------------------*/
  44. void SystemClock_Config(void);
  45. /* USER CODE BEGIN PFP */

  46. /* USER CODE END PFP */

  47. /* Private user code ---------------------------------------------------------*/
  48. /* USER CODE BEGIN 0 */

  49. float temperature = 0;

  50. float pitch,roll,yaw;                 //欧拉角




  51. /* USER CODE END 0 */

  52. /**
  53.   * @brief  The application entry point.
  54.   * @retval int
  55.   */
  56. int main(void)
  57. {
  58.   /* USER CODE BEGIN 1 */

  59.   /* USER CODE END 1 */

  60.   /* MCU Configuration--------------------------------------------------------*/

  61.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  62.   HAL_Init();

  63.   /* USER CODE BEGIN Init */

  64.   /* USER CODE END Init */

  65.   /* Configure the system clock */
  66.   SystemClock_Config();

  67.   /* USER CODE BEGIN SysInit */

  68.   /* USER CODE END SysInit */

  69.   /* Initialize all configured peripherals */
  70.   MX_GPIO_Init();
  71.   MX_USART3_UART_Init();
  72.   /* USER CODE BEGIN 2 */

  73.         while(mpu6050_dmp_init())
  74.         {
  75.                 HAL_Delay(200);
  76.                 printf("%s\r\n","Mpu6050_DMP Init Wrong!");
  77.                 printf("   %d",mpu6050_dmp_init());
  78.         }
  79.        
  80.         printf("%s\r\n","DMP_Mpu6050 Init OK!");
  81.        

  82.   /* USER CODE END 2 */

  83.   /* Infinite loop */
  84.   /* USER CODE BEGIN WHILE */
  85.   while (1)
  86.   {
  87.     /* USER CODE END WHILE */

  88.     /* USER CODE BEGIN 3 */
  89.                
  90.                 temperature= mpu6050_read_temperature();
  91.                
  92.                 if(mpu6050_dmp_get_data(&pitch,&roll,&yaw)==0){
  93.                        
  94.                         printf("三轴角度:%f-%f-%f\r\n",pitch,roll,yaw);
  95.                         printf("temp:%f\r\n",temperature);
  96.                         printf("pitch:%f\r\n",pitch);
  97.                         printf("roll: %f\r\n",roll);
  98.                         printf("yaw:  %f\r\n",yaw);
  99.                        
  100.                 }
  101.                
  102.                 delay_ms(100);
  103.                
  104.                 usart1_receive_data_handle();
  105.                
  106.   }
  107.   /* USER CODE END 3 */
  108. }

  109. /**
  110.   * @brief System Clock Configuration
  111.   * @retval None
  112.   */
  113. void SystemClock_Config(void)
  114. {
  115.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  116.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  117.   /** Initializes the RCC Oscillators according to the specified parameters
  118.   * in the RCC_OscInitTypeDef structure.
  119.   */
  120.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  121.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  122.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  123.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  124.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  125.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  126.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  127.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  128.   {
  129.     Error_Handler();
  130.   }

  131.   /** Initializes the CPU, AHB and APB buses clocks
  132.   */
  133.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  134.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  135.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  136.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  137.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  138.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  139.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  140.   {
  141.     Error_Handler();
  142.   }
  143. }

  144. /* USER CODE BEGIN 4 */

  145. /* USER CODE END 4 */

  146. /**
  147.   * @brief  This function is executed in case of error occurrence.
  148.   * @retval None
  149.   */
  150. void Error_Handler(void)
  151. {
  152.   /* USER CODE BEGIN Error_Handler_Debug */
  153.   /* User can add his own implementation to report the HAL error return state */
  154.   __disable_irq();
  155.   while (1)
  156.   {
  157.   }
  158.   /* USER CODE END Error_Handler_Debug */
  159. }

  160. #ifdef  USE_FULL_ASSERT
  161. /**
  162.   * @brief  Reports the name of the source file and the source line number
  163.   *         where the assert_param error has occurred.
  164.   * @param  file: pointer to the source file name
  165.   * @param  line: assert_param error line source number
  166.   * @retval None
  167.   */
  168. void assert_failed(uint8_t *file, uint32_t line)
  169. {
  170.   /* USER CODE BEGIN 6 */
  171.   /* User can add his own implementation to report the file name and line number,
  172.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  173.   /* USER CODE END 6 */
  174. }
  175. #endif /* USE_FULL_ASSERT */


 楼主| 发顺丰更大nc 发表于 2023-12-28 16:51 | 显示全部楼层
实验效果
15766658d36f95f259.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 在线客服 返回列表 返回顶部
快速回复 在线客服 返回列表 返回顶部