STM32CubeMX(05) transplants DMP Library of gyroscope MPU6050 to read triaxial angle and acceleration

preface

Today, let's learn the gyroscope mpu6050, and use the DMP Library of mpu6050 to read the three-axis angle and acceleration. What is the DMP library, that is, the built-in digital motion processing (DMP) engine can reduce the load of MCU complex fusion calculus data, sensor synchronization, attitude and potential sensing, etc. In other words, the timing and functions are encapsulated, and you just need to call them. The gospel of the lazy. As long as we initialize the IIC pin in cubeMX.

Hardware conditions: STM32F407ZGT6, USB to TTL, mpu6050 module
Software conditions: STM32CubeMX, keil5

1, What is MPU6050?

MPU6050 is a gyroscope with high cost performance. It can read x, y, Z three-axis angle, x, y, Z three-axis acceleration and built-in temperature sensor. It is widely used in attitude analysis.
A treasure also sells a lot. The introduction is also very comprehensive. I won't introduce it here.

2, STM32CubeMX configuration

Since we use serial port to print debugging information, we continue with an article Serial port interrupt experiment
Continue configuration based on. We open our cubeMX project

2.1.IIC configuration

2.2. Open interrupt

At the same time, check whether the pin configuration on the cubeMX is consistent with the actual schematic diagram. Interrupt of IIC is enabled at the same time

The serial port has been repeated in the previous section. Nothing here remains the same.

2.3 hardware connection

Due to the use of IIC communication, the most basic needs only four wires. VCC,GND,SCL,SDA
Connect to MCU
SCL-----PB6
SDA-----PB7
VCC can be connected to 3V or 5V.

2.4 software preparation

Reference blog: jackxu
After the above configuration, generate the project.
Because the printf function is required, it needs to be in usatr Add redirection function to c function.

//printf redirects the code and modifies its underlying fputc
#if 1
#include <stdio.h>

/* Tell the connector not to link semi host functions from the C library */
#pragma import(__use_no_semihosting)

/* Definition_ sys_exit() to avoid using half host mode */
void _sys_exit(int x)
{
    x = x;
}

/* Types of support required by the standard library */
struct __FILE
{
    int handle;
};

FILE __stdout;

/*  */
int fputc(int ch, FILE *stream)
{
    /* Block to judge whether the serial port is sent */
	/* The serial port flag bits of different chips are not necessarily the same! */
    while((USART1->SR & 0X40) == 0);

    /* After the serial port is sent, send the character */
    USART1->DR = (uint8_t) ch;

    return ch;
}

#endif

Add location

3, Import DMP Library

Enter the code project generated by CubeMX and create a new Hardware folder under the project folder
It's an old classic. When learning the standard library

Download the jackxu blogger's driver code, 0 points
Download the driver code of DMP Library
After downloading, unzip it and put it in the folder we just downloaded

3.1 keil configuration

Click our character, this box, and put mpu6050 in the folder c file added.

3.2 add header file path

Click on our magic wand, C + +, and select the path.

3.3 add header file

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "../../../Hardware/MPU6050/mpu6050.h"
#include "../../../Hardware/MPU6050/delay.h"
#include "../../../Hardware/MPU6050/eMPL/inv_mpu.h"
#include "../../../Hardware/MPU6050/eMPL/inv_mpu_dmp_motion_driver.h" 

/* USER CODE END Includes */

Insertion position

Add user variable

/* USER CODE BEGIN PV */
float pitch,roll,yaw; 		//Euler angle
short aacx,aacy,aacz;		//Acceleration sensor raw data
short gyrox,gyroy,gyroz;	//Gyroscope raw data
short temp;					//temperature


/* USER CODE END PV */

Insertion position

3.4 add initialization code

    
	while(MPU_Init());					//Initialize MPU6050
	while(mpu_dmp_init())
	{
		delay_ms(200);
		printf("%s\r\n","Mpu6050 Init Wrong!");
	}
	  printf("%s\r\n","Mpu6050 Init OK!");

Add location

3.5 adding code to main cycle

Call our function

/* USER CODE BEGIN 3 */

    
    if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
		{
			temp=MPU_Get_Temperature();								//Get the temperature value
			MPU_Get_Accelerometer(&aacx,&aacy,&aacz);	//Get acceleration sensor data
			MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz);	//Get gyroscope data
			printf("Triaxial angle:%f-%f-%f\r\n",pitch,roll,yaw);
			printf("Triaxial acceleration:%d-%d-%d\r\n",aacx,aacy,aacz);
			printf("Triaxial angle:%d-%d-%d\r\n",gyrox,gyroy,gyroz);
		}
		delay_ms(100);

    
  }
  /* USER CODE END 3 */

Insertion position

This is the end. We can burn the program to the single chip microcomputer to observe the phenomenon.

4, Test results

Good hardware connection
Then open the serial port debugging assistant, you can see the data sent back by the sensor, and then shake, shake our sensor, you can find that our sensor has changed.

summary

Refer to blog post 1: STM32CubeMX transplants DMP Library of MPU6050 to read angle information
Refer to blog post 2: MPU6050 learning
The possible problem is that U8 and U16 may not be defined
Let's define ourselves as a header file, a common h

#ifndef __COMMON_H
#define __COMMON_H	


typedef   signed          char int8_t;
typedef   signed short     int int16_t;
typedef   signed           int int32_t;
typedef   signed       __int64 int64_t;
typedef unsigned          char uint8_t;
typedef unsigned short     int uint16_t;
typedef unsigned           int uint32_t;
typedef unsigned       __int64 uint64_t;


typedef uint32_t  u32;
typedef uint16_t u16;
typedef uint8_t  u8;


#endif

Keywords: Embedded system Single-Chip Microcomputer stm32 STM32CUBEMX

Added by besbajah on Sun, 26 Dec 2021 05:44:56 +0200