STM32F103RCT6 – MPU6050 use

Directory

1. Introduction to MPU6050

1. Pin introduction

2. Find SCL, SDA interface

3. Working principle

?edit

2. Implementation of MPU6050

1. STM32 development board wiring

2. DMP transplantation

A. Create a new STM32CubeMX project

B. Transplanting DMPs

C. Call the API

D. Modify the error report

3. Achieve results


1. Introduction to MPU6050

1. Pin Introduction

Only need to connect 4 pins, namely: VCC, GND, SCL, SDA
The rest of the pins are used to connect the electromagnetic sensor to form a nine-axis sensor

SCL and SDA are the IIC interfaces connected to the MCU. The MCU controls the MPU6050 through this IIC interface. There is also an IIC interface: AXCL and XDA. This interface can be used to connect external slave devices, such as magnetic sensors, so that it can form a nine-axis sensor. VLOGIC is the voltage of the IO port, the pin can be as low as 1.8V, we usually connect it directly to VDD. AD0 is the address control pin from IIC interface (connected to MCU), this pin controls the lowest bit of IIC address. If connected to GND, the IIC address of MPU6050 is: 0X68, if connected to VDD, it is 0X69, note: the address here does not include the lowest bit of data transmission (the lowest bit is used to indicate read and write)

It can be seen that SCL and SDA must be connected to pins with IIC functions

2. Find SCL, SDA interface

1. The quickest way is to use your board model, go to STM32CubeMX to set the I2C1 / I2C2 of Connectivity, and see the green pin display that it lights up.

2. Look at the schematic diagram of the development board.

3. How it works

(1) MPU6050 is the world’s first integrated 6-axis motion processing component launched by InvenSense, with a 3-axis gyroscope and a 3-axis acceleration sensor inside, and a second IIC interface that can be used to connect to an external magnetic sensor. Digital Motion Processor (DMP: Digital Motion Processor) hardware acceleration engine can output complete 9-axis attitude fusion calculation data to the application side through the main IIC interface. With DMP, we can use the motion processing database provided by InvenSense to realize attitude calculation very conveniently, reduce the load of motion processing operations on the operating system, and greatly reduce the difficulty of development.

The three-axis accelerometer is used to detect lateral acceleration, and the three-axis gyroscope is used to detect angular rotation and balance

The x-axis is forward, the y-axis is on the left side of the robot, and the z-axis is up

(2) Obtain data (I only need acceleration, so only acceleration processing is provided)

G=aacx/16384 The unit is m/s^2

2. MPU6050 implementation

1. STM32 development board wiring

(1) All STM32 development boards have dedicated SCL and SDA interfaces, and the corresponding interfaces can be found and connected according to the schematic diagram. Here is an example of the elite STM32F103ZET6 of the punctual atom:

(2) When it comes to other applications, such as the STM32 car, some of its connections may have been preset, and you need to choose if it is inconvenient to change. The example is as follows (the development board model is F103RCT6):

There are some situations when I use MPU6050 for this car:

(a) According to the schematic diagram, it is directly known that PB14 and PB15 are used as SCL and SDA interfaces respectively

But when I enter the motor PB14 pin during STM32Cube initialization, it does not have the corresponding IIC interface function (left picture), and the normal IIC interface function should be as shown in the right picture:

In this case, I choose to connect the pins with IIC function according to the schematic diagram of the development board. Finally, I choose to use I2C2 (PB10, PB11), because PB6 and PB7 corresponding to I2C1 are encoder interfaces:

(b) When the MPU6050 is directly inserted into the interface, it is found that the order of the pins is incorrect, so the control is directly led out with a DuPont line.

Here the order of SCL and SDA cannot be changed, which comes from the working principle of MPU6050:

Start signal:

When SCL is high level, SDA transitions from high level to low level and starts to transmit data.

End signal:

When SCL is high level, SDA jumps from low level to high level, and the data transmission ends.

2. DMP transplant

DMP: InvenSense provides an embedded motion driver library for MPU6050. Combined with the DMP of MPU6050, we can directly convert our original data into quaternion output, and after getting the quaternion, we can easily calculate the Euler angle to get yaw, roll and pitch.

A. Create a new STM32CubeMX project

(1) Select the chip model

(2) SYS

(3) RCC

(4) I2C2

(5) Set the serial port

(6) Clock tree

(7) Generate code

B. Transplant DMP

(1) Create a new folder in the generated project: Hardware

(2) Download the MPU6050 configuration file and copy it in Hardware

MPU6050-DMP porting https://download.csdn.net/download/northern_light_/87813248

(3)

(4)

(5) Add redirection code in the user code 0 section of the usart.c file to facilitate debugging with printf. The code is as follows:

/* USER CODE BEGIN 0 */
#define USE_PRINT
#ifdef USE_PRINT
//The compiler does not use the MicroLib library
#pragma import(__use_no_semihosting)
//Define _sys_exit() to avoid using semihosting
void _sys_exit(int x)
{
    x = x;
}
struct __FILE
{
    int handle;
};
FILE__stdout;
//Remap fputc
int fputc(int ch, FILE *stream)
{
    / / Determine whether the serial port is sent
//The serial port flags of different chips are not necessarily the same, check the manual for details
    while((USART1->SR & 0X40) == 0);
    //If the serial port has been sent, send the next character
    USART1->DR = (uint8_t) ch;
    return ch;
}
#endif
/* USER CODE END 0 */

20210606164526

(6) Add printf library in usart.h

#include <stdio.h>

20210811222834

C. Call API

(1) Add the header file used in the user environment of main.c

#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" 

20210606165027

(2) Add the following variables in the user variable code section to store various data of the MPU6050;

float pitch,roll,yaw; //Euler angle
short aacx,aacy,aacz; // raw data of acceleration sensor
short gyrox, gyroy, gyroz; // gyroscope raw data
short temp; //temperature

20210606165159

(3) User initial code segment 2 inserts 6050 and dmp initialization code

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

20210606165343

(4) Insert the following code into the main loop of the code, which is used to upload the obtained data to the host computer through the serial port

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

20210606170314

D. Modify the error report

We set up I2C2 but the configuration file is I2C1, so an error is reported, replace all hi2c1 with hi2c2

3. Realize the result

Because we set up serial port 1, use the serial port debugging assistant to observe the results

Serial debugging assistant installation package download: https://download.csdn.net/download/northern_light_/87813330https://download.csdn.net/download/northern_light_/87813330

Reference link:

MPU6050_mpu6050 initialization_K11mvp’s blog-CSDN blog STM32 MPU6050 lighting experiment through data feedback https://blog.csdn.net/K11mvp/article/details/126658550 MPU6050 acceleration, angular velocity calculation and complementary filter use_mpu6050 angular velocity_Huang Baige’s Blog- The CSDN blog MPU6050 acceleration calculation first establishes the relationship between each angle according to the two pictures RollRollRoll (roll angle) the rotation angle with the xxx axis as the rotation axis YawYawYaw (yaw angle) the rotation angle with the zzz axis as the rotation axis PitchPitchPitch (pitch Angle) The rotation angle with the yyy axis as the rotation axis. Let us first understand the unit and range of the acceleration for easy calculation. It can be found in the MPU initialization function, the function of initializing the acceleration sensor //Set the MPU6050 acceleration sensor full-scale range//fsr:0, ±2g;1,±4g;2,±8g;3,±16g//return value: 0, set successfully https://blog.csdn.net/qq_49979053/article/details/117395838

Calculation of MPU6050 acceleration and angular velocity and use of complementary filtering Rotation angle YawYawYaw (yaw angle) Rotation angle with the zzz axis as the rotation axis PitchPitchPitch (pitch angle) Rotation angle with the yyy axis as the rotation axis We first understand the unit and range of acceleration for easy calculation. Find it in the MPU initialization function. The function of initializing the acceleration sensor//Set the full-scale range of the MPU6050 acceleration sensor//fsr:0,±2g;1,±4g;2,±8g;3,±16g//Return value: 0, set successfully https://blog.csdn .net/qq_49979053/article/details/117395838