通通

[Training Camp Official Case] ​​Real-time attitude display based on N32G430

 
Overview

1. Plan introduction

1.1 System solution structure
The architecture of this solution is shown in Figure 1 below. It is mainly divided into the following parts: host computer, main controller N32G430C8L7, MPU6050 sensor, and OLED display module. It aims to use the MPU6050 sensor to collect original acceleration and gyroscope acceleration. , N32G430 converts the data into the attitude of the module after receiving the data, and displays it using OLED and the host computer.

1.2 Hardware circuit connection design
For details, please refer to the schematic design.

2. Introduction to the main controller N32G430
The N32G430C8L7 microcontroller product uses a high-performance 32-bit ARM Cortex™-M4F core, integrates a floating-point unit (FPU) and digital signal processing (DSP), and supports parallel computing instructions. The maximum operating frequency is 128MHz, it integrates up to 64KB of on-chip encrypted storage Flash, supports multi-user partition permission management, and supports 16KB of embedded SRAM. Built-in an internal high-speed AHB bus, two low-speed peripheral clock buses APB and bus matrix, supports 40 reusable I/Os, provides rich high-performance analog interfaces, including a 12-bit 4.7Msps ADC, supports 16 external Input channels and 3 internal channels also provide a variety of digital communication interfaces, including 4 U(S)ART, 2 I2C, 2 SPI/I2S, and 1 CAN 2.0B communication interface. The N32G430C8L7 microcontroller product can operate stably in the temperature range of -40°C to +105°C, with a power supply voltage of 2.4V to 3.6V, and provides a variety of power consumption modes. The picture below is the pin distribution diagram of the N32G430C8L7 microcontroller;
picture.png

3. Introduction to sensors

3.1 MPU6050 module
MPU6050 is the world's first integrated 6-axis motion processing component launched by InvenSense. Compared with multi-component solutions, it eliminates the problem of inter-axis differences when combining gyroscopes and accelerators, and reduces installation space. The MPU6050 integrates a 3-axis gyroscope and a 3-axis acceleration sensor internally, and contains a second IIC interface that can be used to connect an external magnetic sensor, and utilizes its own digital motion processor (DMP: Digital Motion Processor) hardware acceleration engine, through The main IIC interface outputs complete 9-axis fusion calculation data to the application end. With DMP, we can use the motion processing database provided by InvenSense to realize attitude calculation very conveniently, reduce the load of processing operations on the operating system, and greatly reduce the difficulty of development.

MPU6050 features include
  • Output 6-axis or 9-axis (requires external magnetic sensor) rotation matrix, quaternion, and Euler Angle format fusion calculation data in digital form (requires DMP support)
  • 3-axis angular velocity sensor (gyro) with 131 LSBs/°/sec sensitivity and full-scale sensing ranges of ±250, ±500, ±1000 and ±2000°/sec,
  • Integrated programmable 3-axis acceleration sensor with ranges of ±2g, ±4g, ±8g and ±16g,
  • Remove the sensitivity between the accelerometer and gyroscope axes to reduce the impact of settings and sensor drift;
  • The built-in Digital Motion Processing (DMP) engine can reduce the load on the MCU from complex fusion calculation data, sensor synchronization, gesture sensing, etc.;
  • Built-in operation time deviation and magnetic sensor calibration calculation technology eliminate the need for customers to perform additional calibration;
  • Comes with a digital temperature sensor;
  • With digital input synchronization pin (Sync pin) supports video electronic image stabilization technology and GPS;
  • Programmable interrupt, supporting gesture recognition, panning, screen zoom, scrolling, rapid descent interrupt, high-G interrupt, zero motion sensing, touch sensing, and shake sensing functions;
  • VDD supply voltage is 2.5V± 5%, 3.0V± 5%, 3.3V± 5%; VLOGIC can be as low as 1.8V± 5%;
  • Gyroscope operating current: 5mA, gyroscope standby current: 5uA; accelerator operating current: 500uA, accelerator power saving mode current: 40uA@10Hz;
  • Comes with 1024-byte FIFO, which helps reduce system power consumption;
  • IIC communication interface up to 400Khz;
  • Ultra-small package size: 4x4x0.9mm (QFN)

The detection axis and its direction of the MPU6050 sensor are shown in Figure 1 below:
picture.png

1.MPU6050 hardware connection
picture.png
As shown in the figure above, here our MCU controls the MPU6050 through this I2C interface (IIC_SCL/IIC_SDA is connected to PB13/PB14 of the MCU). AD0 is the address control pin from the I2C interface (connected to the MCU). This pin controls the IIC address. lowest position. If connected to GND, the IIC address of MPU6050 is: 0x68. If connected to VDD, it is 0x69. In this project, this pin is directly connected to PB2 of the MCU, and the software directly pulls it low, so the I2C address of MPU6050 is 0x68.
<b>Note:</b> In order to facilitate learning of the I2C protocol, the driver code of the MPU6050 in this example uses the GPIO simulated I2C interface.
<b>Note:</b> The address here does not include the lowest bit of data transmission (the lowest bit is used to indicate reading and writing)!

2.MPU6050 initialization configuration process
<b>1. Initialize IIC interface:</b>
Initialize the SDA and SCL data lines connected to the MPU6050, set AD0 to low level, and then select the I2C address as 0x68.
<b>2. Reset MPU6050:</b>
This step restores all registers inside the MPU6050 to their default values, which is achieved by writing 1 to bit7 of the power management register 1 (0X6B). After reset, the power management register 1 returns to the default value (0X40), and then this register must be set to 0X00 to wake up the MPU6050 and enter the normal working state.
<b>3. Set the full-scale range of the angular velocity sensor (gyro) and acceleration sensor:</b>
In this step, we set the full-scale range (FSR) of the two sensors through the gyroscope configuration register (0X1B) and the acceleration sensor configuration register (0X1C). We generally set the full-scale range of the gyroscope to ±2000dps and the full-scale range of the acceleration sensor to ±2g.
<b>4. Set other parameters:</b>
Here, we also need to configure the parameters: turn off interrupts, turn off the AUX IIC interface, disable FIFO, set the gyroscope sampling rate and set the digital low-pass filter (DLPF), etc. In this chapter, we do not use interrupts to read data, so we turn off interrupts. We do not use the AUXIIC interface to connect other external sensors, so we also turn off this interface. Controlled by interrupt enable register (0X38) and user control register (0X6A) respectively. MPU6050 can use FIFO to store sensor data, but we have not used it in this chapter, so all FIFO channels are closed. This is controlled by the FIFO enable register (0X23). The default value is 0 (that is, FIFO is disabled), so the default value can be used. The gyroscope sampling rate is controlled by the sampling rate division register (0X19). We generally set this sampling rate to 50. The digital low-pass filter (DLPF) is set through the configuration register (0X1A). Generally, the DLPF is set to 1/2 of the bandwidth;
<b>5. Configure the system clock source and enable the angular velocity sensor and acceleration sensor:</b>
The system clock source is also set through power management register 1 (0X1B). The lowest three bits of this register are used to set the system clock source selection. The default value is 0 (internal 8MRC oscillation), but we generally set it to 1 and select the x-axis. Gyro PLL is used as the clock source to obtain a higher precision clock. At the same time, the angular velocity sensor and acceleration sensor are enabled. These two operations are set through the power management register 2 (0X6C). Set the corresponding bit to 0 to enable it;

3.MPU6050 important register description
For this part, please refer to the document "MPU-6000 & MPU-6050 Register Table and Description (Chinese Version)"

4.DMP use
From the MPU6050 related registers, we can read the raw data of the acceleration sensor and angular velocity sensor. However, these raw data are not very useful for beginners who want to do four-axis. What we expect to get is attitude data, that is, Euler angles: heading angle (yaw), roll angle (roll) and pitch pitch. With these three angles, we can get the current four-axis attitude, which is the result we want. To obtain the Euler angle data, we have to use our original data to perform attitude fusion calculation. This is more complicated, has many knowledge points, and is not easy for beginners to master. The MPU6050 comes with a digital motion processor, or DMP, and InvenSense provides an embedded motion driver library for the MPU6050. Combined with the DMP of the MPU6050, our original data can be directly converted into quaternion output, and four After the element number, Euler angles can be easily calculated to obtain yaw, roll and pitch. Using the built-in DMP greatly simplifies the four-axis code design, and the MCU does not need to perform the attitude calculation process, which greatly reduces the burden on the MCU, leaving more time to process other events and improving the real-time performance of the system.
It is relatively simple to transplant the official DMP driver library. First, add the two files inv_mpu.c and inv_mpu_dmp_motion_driver.c to the project, and then implement these four functions: i2c_write, i2c_read, delay_ms and get_ms. We will not discuss the specific details. Introduced in detail, the transplanted driver code is placed in the folder of this routine→src→eMPL, with a total of 6 files, as shown in the figure below:
picture.png
Among them, we have added several functions to inv_mpu.c to facilitate our use. The focus is on two functions: mpu_dmp_init and mpu_dmp_get_data. Here we briefly introduce these two functions. u8 mpu_dmp_init(void); //mpu initialization, note: this function is time-consuming, about 3-4s, no actual measurement. u8 mpu_dmp_get_data(float *pitch, float *roll, float *yaw); Get attitude data pitch, roll, yaw. For the specific implementation of this function, please refer to the source code comments.

3.2 OLED module

1. OLED module hardware connection
As shown in Figure 7 above, here our MCU controls the OLED display through the IIC interface (OLED_SCL/OLED_SDA is connected to the MCU's PA4/PA5). In order to become familiar with the N32G430 chip and OLED I2C driver as quickly as possible, this case uses hardware I2C1. As shown in Figure 6 above, the address selection resistor on the back of the OLED module jumps to the left, then its slave address is 0x78. Note: The address here does not include the lowest bit of data transmission (the lowest bit is used to indicate reading and writing)!
picture.png
picture.png

2.OLED initialization configuration process
<b>1. Initialize IIC interface</b>
Initialize the SDA and SCL data lines connected to the OLED.
<b>2. Turn off the OLED display and set the OLED register parameters</b>
After turning off the OLED display, configure the parameters of the register, such as setting the clock division factor, oscillation frequency, setting display contrast, scanning direction, precharge cycle, brightness and other parameters. For specific meanings, please refer to the OLED register description document or other information on the Internet. There will be no detailed explanation here.
<b>3. Turn on OLED display</b>
4.3 OLED important register description

4. Spatial coordinate conversion algorithm

4.1 Three-dimensional coordinate system conversion
Three-dimensional coordinate transformation in space is generally implemented in the following three ways:
  • Rotation matrix and rotation vector;
  • Euler angles;
  • Quaternion.
    Here we introduce the method of realizing three-dimensional space coordinate transformation by rotation matrix (rotation vector) and Euler angle and the relationship between the two. Here we take the common conversion from camera coordinate system to world coordinate system, that is, the more general conversion from body to world coordinate system, as an example. Then the rotated Euler angle follows the process of converting from the world coordinate system to the camera coordinate system. First, rotate on the z-axis, then rotate on the y-axis, and then rotate on the x-axis. Finally, the camera coordinate system is obtained. The obtained angles are yaw, pitch, and roll, then the rotation matrix from the camera coordinate system to the world coordinate system is defined as follows.
picture.png
picture.png
For the above matrix algorithm, you can refer to Baidu related information. Since the x and y axes of the cube are inconsistent with the x and y axes of the sensor in the code, the code exchanges Rx and Ry to ensure that the direction of the OLED cube is consistent with the sensor. The direction is consistent, thereby adjusting the posture to be consistent. Since the three-dimensional coordinate point is to be displayed on a two-dimensional plane, if the point P (x0, y0, z0), the P point needs to be projected on the XOY plane, that is, z0 = 0, that is, the coordinate of P1 on the plane is (x0 , y0). Based on the above analysis, if you want to display a cube on a plane, you only need to project the eight coordinate points of the converted cube onto the XOY plane, and then connect the above points in sequence to obtain a cube. In order to highlight the changes in the attitude of the cube, this routine connects an extra x-shaped surface.

5. Software design process

5.1 Software flow chart
The software initialization process is divided into the following steps:
  • initialization:
    Part includes serial port, hardware IIC, GPIO simulation I2C, OLED, MPU6050 and DMP initialization; -log_init(): 500000Bd, no parity, 8-bit data, 1 stop bit. This interface can send and receive data and print logs; -i2c_master_init (): In order to improve the refresh speed of OLED, the IIC speed is set to 400k, without ACK response; -oled_init(): It is mainly the register parameter configuration of OLED, for details, please refer to SSD1306 Controller; -MPU_Init(): Configure the register parameter configuration of the MPU6050, including the configuration of parameters such as address, sampling rate, acceleration/gyro full-scale range, etc. -mpu_dmp_init(): DMP initialization, here wait for DMP initialization to be completed.
    picture.png

6. Physical demonstration

6.1 Power on
After power-on reset, the module remains horizontally.
picture.png
The upper computer displays the screen when at rest.
picture.png

6.2 Module attitude tilted 45 degrees to the right
OLED display tilted 45 degrees to the right.
picture.png
The host computer display screen is tilted 45 degrees to the right.
picture.png

6.3 Module posture tilted 35 degrees to the left and 21 degrees downward
The OLED display tilts 35 degrees to the left and 21 degrees downward.
picture.png
The display screen of the host computer tilted 35 degrees to the left and 21 degrees downward
picture.png

Precautions
  • After resetting, it usually takes 3-4 seconds for the OLED to display normally. This is mainly because during the DMP initialization process, there is a self-calibration process of the MPU6050. Therefore, for the sake of user experience, you can consider displaying "Calibrating" on the OLED as a prompt;
  • After resetting, the module must be placed still and flat, and the module cannot be moved until the sensor calibration is completed;
  • Users can modify the Set_Slaver_Address() function to configure PB2 to be pulled high and low to modify the MPU6050 address. In the code, PB2 is pulled low, so the address of MPU6050 is 0x68.
  • The Euler angle Yaw drifts significantly after being left for a period of time. This is caused by not adding a magnetic sensor for algorithm fusion, and is a normal phenomenon. Those who are interested can study how to solve the drift problem.

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-06-21 15:32:20

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号