Graduation project - Title: design and implementation of intelligent sweeping robot based on stm32

0 Introduction

Hi, everyone, this is senior Dan Cheng. Today I'd like to introduce a single chip microcomputer project to you

Design and implementation of intelligent sweeping robot based on stm32

It can be used in curriculum design or graduation design

Technical solutions, design help and problem opening guidance
print("Q 746876041") 

1 project background

With the advent of population aging and people's need to improve the quality of life, people have an urgent need to replace human service robots in real life scenes. At the same time, the development of Electromechanical, automatic control, computer, sensor and other technologies also provides technical support for manufacturing service robots. Sweeping robot is the most mature and widely used robot in service robot. It can automatically walk indoors, absorb the debris on the ground into the garbage collection device through brushing and dust suction, complete the task of cleaning the ground, effectively reduce people's simple and repeated housework of cleaning the ground, save labor and improve the quality of life. For many people who are busy with work and life, sweeping robots have become a necessary product for families.

2 overall framework of hardware system

The senior designed a traditional sweeping robot, which uses infrared sensor and ultrasonic sensor to detect obstacles, and an MPU6050 motion sensor to calculate the rotation angle. Two DC reduction motors are used to drive the wheels to realize walking. The encoders on both sides can calculate the speed and distance of the wheels by calculating the number of pulses generated by the encoder. In order to facilitate the experiment process, timely obtain sensor data and send startup and shutdown commands to the sweeping robot, an atk-esp8266 WIFI module is connected, and the network debugging assistant software is installed on the laptop to realize the communication with the WIFI module. The power unit of the platform is a battery pack with a voltage of 12V and a capacity of 9000mAh, which provides power for motor drive and provides power for the control unit after voltage stabilization. The main control unit is used to control the whole robot. STM32F103ZET6 is used as the control chip. The main control unit performs tasks such as mechanical drive, motion control and sensor data acquisition.

2.1 motor drive

Because the current output by the single chip microcomputer is not enough to drive the motor, the motor must be driven through the motor drive module. The DC motor is usually driven by H-bridge drive chip, and two L298N drive modules are used in this subject. L298N is a special drive integrated circuit and belongs to H-bridge integrated circuit. The original brand-new chip L298N of ST company is used, the SMT process with high stability is adopted, and the high-quality aluminum electrolytic capacitor is used to make the circuit work stably. As a DC motor drive, it can drive two 3-16V DC motors at the same time. At the same time, the driver board also provides a 5V power output interface, which can be used to supply power to the circuit system of 5V single chip microcomputer. It supports 3.3VMCUARM control. It can easily control the power and rotation direction of DC motor through single chip microcomputer. It can also be used to control 2-phase stepping motor and 5-wire 4-phase stepping motor.

Although the L298N drive board can drive two DC motors at the same time, the drive board may overheat and burn under high power conditions for a long time. In order to improve the stability of the hardware system, two L298N drive boards are used in this subject. One drive board controls a motor. IN3IN4EN2OUT3OUT4 of L298N drive board floats directly. VCC inputs 12V motor drive power supply positive pole, negative pole is connected to GND, VCC5V is connected to 5V power supply positive pole, negative pole is grounded, EN1IN1 is connected to STM32 to control motor forward and reverse rotation and start and stop, and IN2 inputs PWM wave. OUT1OUT2 connect the motor.

2.2 infrared sensor

In this project, the senior designed to detect whether there are obstacles in the moving direction of the sweeping robot through infrared sensors, so as to realize the function of automatic obstacle avoidance of the sweeping robot. The circuit of E18-D80NK infrared sensor is shown in the figure.

The infrared sensor uses 5V power supply. STM32 determines whether there are obstacles by reading the high and low levels of the yellow signal line. The sensing distance can be adjusted within the range of 3-77cm through the knob on the infrared sensor. In the circuit design, it will be more stable to add the pull resistance 10K to 5V power supply to the yellow line at the output end, and then connect it to the single chip microcomputer for detection.

2.3 ultrasonic sensor

The seniors select US-100 ultrasonic ranging module, which can measure the temperature through its own temperature sensor and correct the measurement results. It can communicate with MCU through GPIO or serial port, with stable performance.

The US-100 ultrasonic ranging module has a mode selection jumper and a 5Pin interface. This topic uses the serial port mode, and the jumper cap must be plugged in. There are five interfaces. No. 1 pin is connected to the VCC 5V power supply, No. 2 pin is connected to the TX end of the external circuit UART, No. 3 pin is connected to the RX end of the external circuit UART, and No. 4 pin is connected to the ground of the external circuit. Pin 5 is connected to the ground of the external circuit.

2.4 MPU6050

MPU6050 is the world's first 9-axis motion processing sensor. It integrates 3-axis MEMS gyroscope, 3-axis MEMS accelerometer and a scalable digital motion processor DMP.

MPU6050 circuit connection diagram

2.5 ATK-ESP8266 WI FI module

ATK-ESP8266 is a high-performance UART WiFi (serial port wireless) module launched by ALIENTEK. ATK-ESP8266 onboard AI thinker's ESP8266 module.

ATK_ Circuit connection diagram of esp8266 module

2.6 power management module

The power management module is used to supply power to circuit modules of the control system. Since the battery voltage is 12V and the power supply voltage of the main control chip is 3.3V, it can not directly supply power to the main control chip. At the same time, some external function modules need 5V power supply. Therefore, this subject designs a step-down circuit, a voltage selection circuit and a 3.3v/5v output circuit, It can meet the power supply needs of the control system. DC_IN is used for external DC power input, with the range of DC6~24V. The input voltage is converted into 5V power output through MP2359 chip. D4 is an anti reverse diode to avoid burning the control system circuit when the polarity of external DC power supply is wrong.

3 software system design

The overall software architecture designed by the senior is as follows:

3.1 MPU6050 data reading

MPU6050 sensor contains gyroscopes and accelerometers, and has its own digital motion processor, that is, DMP can directly convert the original data of gyroscopes and accelerometers into quaternion output through MPU6050 embedded motion drive library provided by InvenSense, and directly calculate Euler angle through quaternion conversion to obtain heading angle (yaw) and roll angle (roll) and pitch angle (pitch).

The senior student here selected I2C communication to read the quaternion output by DMP of MPU6050.

Initialize the I2C interface under the programming code

1 void HmcIICInit(void)
3. /*GPIO initialization*/
4. GPIO_InitTypeDef GPIO_InitStructure;
5. /* Variables required to configure hardware IIC */
6. I2C_InitTypeDef I2C_InitStructure;
8. /* Enable clock associated with I2C1 */
9. RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB,ENABLE );
10. RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
12. /* PB6-I2C1_SCL, PB7-I2C1_SDA*/
13. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
14. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
15. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
16. GPIO_Init(GPIOB, &GPIO_InitStructure);
18. /*IIC Peripheral initialization*/
20. /* I2C to configure */
21. I2C_InitStructure.I2C_Mode = I2C_Mode_I2C ; //Configure to normal IIC mode
22. //I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
23.//I2C_InitStructure.I2C_OwnAddress1 = SlaveAddress;
24. I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; //Enable auto answer
25. I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
26. I2C_InitStructure.I2C_ClockSpeed = 50000; //5K speed
29. /* I2C1 initialization */
30. I2C_Init(I2C1, &I2C_InitStructure);
32. /* Enable I2C1 */
33. I2C_Cmd (I2C1,ENABLE);
34. /*Allow reply mode*/
35. I2C_AcknowledgeConfig(I2C1, ENABLE);

Configure the system clock source and enable the angular velocity sensor and acceleration sensor. The code is as follows:

1. u8 MPU_Init(void)
2. {
3. u8 res;
4. IIC_Init();//Initialize IIC bus
5. MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X80); //Reset MPU6050
6. delay_ms(100);
7. MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00); //Wake up MPU6050
8. MPU_Set_Gyro_Fsr(3); //Gyro sensor, ± 2000dps
9. MPU_Set_Accel_Fsr(0); //Acceleration sensor, ± 2g
10. MPU_Set_Rate(50); //Set sampling rate 50Hz
11. MPU_Write_Byte(MPU_INT_EN_REG, 0X00); //Turn off all interrupts
12. MPU_Write_Byte(MPU_USER_CTRL_REG,0X00); //I2C main mode off
13. MPU_Write_Byte(MPU_FIFO_EN_REG,0X00); //Close FIFO
14. MPU_Write_Byte(MPU_INTBP_CFG_REG,0X80); //INT pin low level active
15. res=MPU_Read_Byte(MPU_DEVICE_ID_REG);
16. if(res==MPU_ADDR)//The device ID is correct
17. {
18. MPU_Write_Byte(MPU_PWR_MGMT1_REG, 0X01); //Set clksel and PLL X axis as reference
19. MPU_Write_Byte(MPU_PWR_MGMT2_REG, 0X00); //Both acceleration and gyroscope work
20. MPU_Set_Rate(50); //Set the sampling rate to 50Hz
21. }else return 1;
22. return 0;
23. }
24. //Set MPU6050 gyro sensor full scale range
25. //fsr: 0,±250dps;1,±500dps;2, ±1000dps;3,±2000dps
26.//Return value: 0, setting succeeded

3.2 ultrasonic sensor data reading

Write ultrasonic transmission function, and send 0X55 to ultrasonic module through serial port. Relevant codes are as follows;

1. void uart_init(u32 bound) {
2. //GPIO port settings
3. GPIO_InitTypeDef GPIO_InitStructure;
4. USART_InitTypeDef USART_InitStructure;
5. NVIC_InitTypeDef NVIC_InitStructure;
7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1| RCC_APB2Periph_GPIOA,  ENABLE) ;
//Enable USART1, GPIOA clock
8. //USART1_TX PA. 9
9. GPIO_InitStructure. GPIO_Pin = GPIO_Pin_9; //PA. 9
10. GPIO_InitStructure. GPIO_Speed = GPIO_Speed_50MHz;
11. GPIO_InitStructure. GPIO_Mode = GPIO_Mode_AF_PP; //Multiplexed push-pull output
12. GPIO_Init(GPIOA, &GPIO_InitStructure) ;
14.//USART1_RX PA. 10
15. GPIO_InitStructure. GPIO_Pin = GPIO_Pin_10;
16. GPIO_InitStructure. GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input
17. GPIO_Init(GPIOA, &GPIO_InitStructure) ;
19. //Usart1 NVIC configuration
21. NVIC_InitStructure. NVIC_IRQChannel = USART1_IRQn;
22. NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=3 ; //Preemption priority 3
23. NVIC_InitStructure. NVIC_IRQChannelSubPriority = 3; //Sub priority 3
24. NVIC_InitStructure. NVIC_IRQChannelCmd = ENABLE; //IRQ pass
 Tao enabling
25. NVIC_Init(&NVIC_InitStructure) ; //Initializes the VIC register according to the specified parameters
27. //USART initialization settings
29. USART_InitStructure. USART_BaudRate = bound; //Generally set to 9600;
30. USART_InitStructure. USART_WordLength = USART_WordLength_8b; //The word length is in 8-bit data format
31. USART_InitStructure. USART_StopBits = USART_StopBits_1; //A stop bit
32. USART_InitStructure. USART_Parity = USART_Parity_No; //No parity bit
33. USART_InitStructure. USART_HardwareFlowControl  =
USART_HardwareFlowControl_None; //No hardware data flow control
34. USART_InitStructure. USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Transceiver module
36.USART_Init(USART1, &USART_InitStructure) ; //Initialize serial port
37. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE) ; //Open interrupt
38. USART_Cmd(USART1, ENABLE) ; //Enable serial port
40. }

3.3 motor control

3.3.1 PID control

When the sweeping robot moves in the two-dimensional plane, it mainly controls the heading angle. Affected by various factors such as ground resistance and collision, it is impossible to establish an accurate mathematical model to write an algorithm to accurately control the sweeping robot. Therefore, PID control algorithm is used to control the sweeping robot in this subject.

As the earliest practical controller, PID (proportion al, integral and derivative) controller has a history of nearly 100 years, and it is still the most widely used industrial controller. PID controller is easy to understand and does not need accurate system model, so it has become the most widely used controller.

PID process

PID implementation

struct _pid{
    float SetSpeed;            //Define setpoint
    float ActualSpeed;        //Define actual value
    float err;                //Define deviation value
    float err_last;            //Define previous deviation value
    float Kp,Ki,Kd;            //Define proportional, integral and differential coefficients
    float voltage;            //Define the voltage value (variable that controls the actuator)
    float integral;            //Define integral value
    float umax;
    float umin;

void PID_init(){
    printf("PID_init begin \n");
   pid.Ki=0.1;       //Note that compared with the previous times, the value of the integral link is increased here
    printf("PID_init end \n");
float PID_realize(float speed){
    int index;

   if(pid.ActualSpeed>pid.umax)  //Gray background color represents the realization of anti integral saturation

       if(abs(pid.err)>200)      //The integral separation process is marked in blue
    }else if(pid.ActualSpeed<pid.umin){
        if(abs(pid.err)>200)      //Integral separation process
        if(abs(pid.err)>200)                    //Integral separation process


    return pid.ActualSpeed;

3.4 path planning of sweeping robot

3.4.1 bow walking

When there are no obstacles or the room structure changes little, the sweeping robot can apply the cruise path planning algorithm.

Step 1

The sweeping robot obtains the yaw angle from the MPU6050 module and sets the yaw angle as the reference angle. Then the sweeping robot starts to move forward while adjusting the yaw angle based on the reference angle. The distance D is recorded by the encoder, and the value of D will be assigned to a named pre_Walkingcount variable. When the ultrasonic sensor detects the presence of obstacles, the robot will reduce its speed to avoid collision. If the infrared sensor returns low, the algorithm switches to step 2.

Step 2

The robot rotates 90 degrees to the left. Then, the robot records the forward moving distance d with an encoder. If d = Ic, it represents the width of the cleaning port of the sweeping robot, and the robot rotates another 90 degrees to the left. After that, the algorithm switches to step 3. In the programming implementation of step 2, the function left to realize the left turn 90 ° of the sweeping robot is written_ Turn() and straight line function Straight_go().

Step 3

Step 3 is similar to step 1, but in the opposite direction. When the robot encounters an obstacle, the distance D is compared with the variable pre_Walkingcount. The results of the comparison will determine whether the re coverage algorithm works. If d = pre_Walkingcount, the algorithm switches to step 4, and the D value will be assigned to the variable
pre_Walkingcount. By comparing with the previous distance, the robot can decide which algorithm to run.

Step 4

The robot turns 90 degrees to the right. Then, the robot records the forward moving distance d with an encoder. If d = Ic, the robot rotates 90 degrees to the right. After that, the algorithm switches to step 1 and starts another new cycle

4 test

The robot path is mainly based on the bow path, and the repeated coverage algorithm is used to clean the area behind the obstacle. Compared with the sweeping robot with random obstacle avoidance algorithm, the robot with the new algorithm can cover 94.61% of the ground and improve the efficiency of the sweeping robot.

Show video

Graduation project: sweeping robot based on stm32

5 Finally

Technical solutions, design help and problem opening guidance
print("Q 746876041") 

Tags: Single-Chip Microcomputer stm32

Posted on Mon, 29 Nov 2021 23:39:12 -0500 by rhosk