STM32-Automatic Driving

STM32-Automatic Driving1. Opening instructions2. Experimental preparationThe relationship between the 4 motor interfaces and the car is as follows:Hardware wiringOverall wiringWiring pins3. Key code analysis4. Experimental operation5. Experimental phenomenon

1. Opening instructions

Please read the "Introduction to Motors and Usage" in the four-way motor driver board information first to understand the motor parameters, wiring methods, and power supply voltage you are currently using. To avoid burning the motherboard or motor.

Motor: The case and code take the 310 motor of our store as an example.

2. Experimental preparation

National Race chassis V2 four-wheel drive version, 4*310 motors, 7.4V lithium battery, K210 vision module, STM32F103C8T6 core board.

The relationship between the 4 motor interfaces and the car is as follows:

M1 -> upper left motor (left front wheel of the car)

M2 -> lower left motor (left rear wheel of the car)

M3 -> upper right motor (right front wheel of the car)

M4 -> lower right motor (right rear wheel of the car)

Hardware wiring

Overall wiring

PixPin_2025-03-28_15-25-56

Wiring pins

Four-way motor driver boardSTM32C8T6
5V5V
GNDGND
SCLPB10
SDAPB11

Take M1 motor as an example below, and other motors are similar.

MotorFour-way motor driver board (Motor)
M2M1-
VCC3V3
AH1A
BH1B
GNDGND
M1M1+
K210STM32C8T6
5V5V
GNDGND
TXDPA3
RXDPA2

3. Key code analysis

The BSP_init() function is the key entry point for system initialization. First, the system clock is initialized through SystemInit(), and then the interrupt priority group is configured as group 2. Then the delay function, serial port (USART1 is used for debugging, USART2 is used to receive infrared signals), RGB light module, and IIC communication interface for four-way motor control are initialized in turn. Finally, the multiplexing function is enabled and JTAG is remapped to SWD, and the PA15, PB3, and PB4 pins are released for use as ordinary GPIOs to ensure that all peripherals work normally and are ready to run the main application.

control_speed: Controls the speed of the motor. It splits the incoming four motor speed values ​​into high and low bytes, and then sends them to the motor controller via the I2C protocol to set the motor speed.

control_pwm: Controls the PWM (pulse width modulation) value of the motor. It splits the incoming four motor PWM values ​​into high and low bytes, and sends them to the motor controller via the I2C protocol to control the speed and direction of the motor.

The Motion_Car_Control function is used to calculate the speed values ​​of the four motors based on the forward speed (V_x), lateral speed (V_y) and rotation speed (V_z) passed in, thereby controlling the movement of the car. The function obtains the wheel axle spacing of the car through Motion_Get_APB, and uses this value to calculate the rotation difference (speed_spin) between the left and right motors, thereby adjusting the rotational movement of the car. If the speed is zero, stop the motor directly; otherwise, control the speed of the four motors of the car according to the calculated speed value to ensure that the car moves in the predetermined direction and speed.

The Get_CMD_Flag function returns the current command flag New_CMD_flag, which is used to check whether there is a new command; the Clear_CMD_Flag function clears the command flag and the data in the receiving buffer.

The Upper_Data_Parse function parses the data sent by the upper layer and performs different operations according to different function words (func_id). FUNC_MOTION parses and processes the speed setting command of the car, and calls Motion_Car_Control to control the movement or stop of the car.

MOTOR_TYPE: used to set the type of motor used. Modify the corresponding number according to the comments based on the motor you are currently using.

Call the BSP_init() function to initialize the hardware settings, and use Set_Motor(MOTOR_TYPE) to set the motor type and parameters. In the while(1) loop, Get_CMD_Flag detects whether there is a new command. If so, call Upper_Data_Parse to parse the data and control the car to run. After processing, use Clear_CMD_Flag to clear the command flag to ensure that the system continues to respond to the host computer instructions.

4. Experimental operation

  1. Burn the program to STM32.
  2. Download the car drive library and PID control library in the K210\library directory to the root directory of the memory card in advance.
  3. Open CanMV IDE and download the follow_line.py code into the K210 module.
  4. Connect all the wires of the car.
  5. Put the car into the map of visual patrol, move the K210 module bracket to the appropriate angle, and connect the power supply.
  6. After the system is initialized, the LCD displays the camera image, and there is a white box in the middle of the screen. Please move the car to fill the white box with the color to be identified, and wait for the white box to turn green to start collecting colors. After the collection is completed, the green box disappears and the program starts running.

5. Experimental phenomenon

After connecting the power supply, wait for the system to be initialized, the LCD displays the camera image, and there is a white box in the middle of the screen. Please put the color to be identified in the white box, and wait for the white box to turn green to start collecting colors. After the collection is completed, the green box disappears and the program starts running.

The car will move forward along the color just identified in the green box.

image-20250331164647500

If you find that the car often fails to patrol the line, please check whether the car can recognize the corresponding color at each position. If the car reacts too slowly or too quickly, you can adjust the value of FollowLinePID appropriately.

image-20230318154948577