Welcome to ROSMASTER R2 repository
10. Control serial servos
10. Control serial servos
10. Control serial servos10.1. Purpose of the experiment10.2 Configuring Pin Information10.3. Experimental flow chart analysis10.4. Core Code Explanation10.5 Hardware Connection10.6. Experimental Effect
10.1. Purpose of the experiment
Use the serial port function of STM32 to control the serial servo and read the position of the serial servo.
10.2 Configuring Pin Information
- Import the ioc file from Serial's project and name it Serial_Servo.
According to the schematic diagram, we can see that the serial servo is connected to serial port 3, PC10 and PC11 pins.


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

- Since the default serial port 3 pins are PB10 and PB11, and the expansion board schematic serial port 3 is connected to PC10 and PC11, so you need the serial port remapping function.
First click on the PC11 pin, then select USART3_RX, after this operation, the serial port 3 pins will be remapped to PC10 and PC11.


10.3. Experimental flow chart analysis

10.4. Core Code Explanation
- Create a new bsp_uart_servo.h and bsp_uart_servo.c, and add the following to bsp_uart_servo.h:

- Create the following new contents in the bsp_uart_servo.c file:
According to the communication protocol of the serial servo, create a new UartServo_Ctrl(id, value, time) to control the servo. Where id corresponds to the servo ID to be controlled, if id=0xFE(254), then control all servos. value indicates the position to which the control servo moves to, and time indicates the time to run, the shorter the time, the faster it runs before reaching the maximum speed.

- UartServo_Get_Angle() function requests the current position of the servo.

4.UartServo_Revice(Rx_Temp) function receives data from serial port 3, determines whether it conforms to the serial servo communication protocol, and if it conforms to one frame of data, then updates the Rx_Data array and sets New_Frame to 1.

- Parse the data returned by the serial servo, return 1 for successful reading and print the data, otherwise return 0.

- In bsp_uart.c add the following serial port 3 write and read related functions.


- Add serial port 3 initialization in Bsp_Init() function.

- In the Bsp_Loop() function to add the key to read and control the serial servo.

10.5 Hardware Connection
Serial servos need to be connected to the serial servo interface on the expansion board, the serial servo interface has an anti-reverse function, just insert it in the right direction. Serial servos can be cascaded more than one, due to the limited power supply current of the expansion board, so do not connect too many servos, the current test six servos can be used normally.

Due to the relatively large power of the serial servo, the expansion board should not directly use USB 5V power supply, you need to use DC 12V power supply.
10.6. Experimental Effect
After burning the program, the LED flashes every 200 milliseconds. Pressing the key several times, the serial servo will go back and forth between position 1000 and position 3000, and return the position data before the movement.