Welcome to RDK X5 Robot repository
10. Control serial steering gear
10. Control serial steering gear
10. Control serial steering gear10.1. Experimental purpose10.2. Configure pin information10.3. Analysis of experimental flow chart10.4. Core code interpretation10.5. Hardware connection10.6. Experimental effect
10.1. Experimental purpose
STM32 serial port function is used to control serial actuator and read the position of serial actuator.
10.2. Configure pin information
- Import the ioc file from the Serial project and name it Serial_Servo.
According to the schematic diagram, the serial actuator is connected to the pins of serial ports 3, PC10 and PC11.


- Set serial port 3 to Asynchronous mode, other parameters as shown below.

- Because the default pins of serial port 3 are PB10 and PB11, and serial port 3 in the expansion module schematic is connected to PC10 and PC11, the serial port remapping function is required.
First click on pin PC11 and select USART3_RX. After this operation, the pins of serial port 3 will be remapped to PC10 and PC11.


10.3. Analysis of experimental flow chart

10.4. Core code interpretation
- Create bsp_uart_servo.h and bsp_uart_servo.c, and add the following information to bsp_uart_servo.

- Create the following content in the bsp_uart_servo.c file:
According to the communication protocol of serial actuator, create UartServo_Ctrl(id, value, time) to control the actuator. id corresponds to the ID of the steering gear to be controlled. If id=0xFE(254), all steering gear is controlled. value indicates the position to which the steering gear is controlled, and time indicates the running time. Before reaching the maximum speed, the shorter the time, the faster the running.

- Create the following content in the bsp_uart_servo.c file:
- The UartServo_Get_Angle() function requests the current position of the steering gear.

- The UartServo_Revice(Rx_Temp) function receives data from serial port 3, determines whether it conforms to the serial port steering gear communication protocol, and updates the Rx_Data array and sets New_Frame to 1 if it conforms to a frame of data.

- Analyze the data returned by the serial actuator, return 1 after reading successfully, and print the data, otherwise return 0.

- Add the following write and read functions for serial port 3 in bsp_uart.c.


- Add the content initialized by serial port 3 to the Bsp_Init() function.

- Add the function of key reading and control serial actuator in Bsp_Loop() function.

10.5. Hardware connection
The serial serial steering gear must be connected to the serial serial steering gear port on the expansion board. The serial serial steering gear port has the anti-reverse connection function. Serial actuator can be cascaded multiple, because the expansion board power supply current is limited, so do not connect too many steering gear, the current test six steering gear can be used normally.

Because the power of the serial port actuator is relatively large, do not directly use the USB 5V power supply for the expansion board. Instead, use the battery power supply.
10.6. Experimental effect
After burning the program, the LED light flashes every 200 milliseconds. By pressing the button several times, the serial actuator will return between 1000 position and 3000 position, and return the position data before the movement.