Drive PWM servo

1. Experimental purpose

Use the PWM output of the microROS control board to learn how ESP32 controls the PWM servo.

2. Hardware connection

As shown in the figure below, the microROS control board integrates the PWM servo control interface, but requires an additional connection to the PWM servo. The PWM servo needs to be prepared by yourself. You also need to connect the type-C data cable to the computer and the microROS control board as a firmware flash function.

image-20240111142618969

The bottom of the PWM servo port is S1, and the top is S2. Please connect according to the silk screen. The brown wire is connected to GND, the red wire is connected to 5V, and the yellow wire is connected to S1/S2.

image-20240111143813069

 

3. Core code analysis

The virtual machine path corresponding to the program source code is as follows

Since the initialization methods of servos S1 and S2 are not much different, the only difference is that servo S1 corresponds to hardware GPIO8 and servo S2 corresponds to hardware GPIO21. The following explanation takes servo S1 as an example.

First, initialize the GPIO of servo S1 to PWM output with a frequency of 50HZ, and use timer group 1 as the PWM output clock.

Convert the input angle of the servo S1 into the PWM duty cycle value.

Among them, the maximum and minimum values of the servo pulse, and the maximum and minimum values of the servo control range, according to the PWM servo control characteristics, just keep the following values unchanged.

image-20240119191623110

To control the servo angle, the control can be distinguished according to the input servo ID, and then the angle value is converted into a PWM duty cycle pulse value and transmitted to the mcpwm module of ESP32, thereby outputting square waves with different duty cycles to drive the servo to rotate.

Note: In order to adapt to the servo gimbal, the control angle of the gimbal has been restricted. If you want to cancel the restriction, please modify the content of the servo.h file to configure the servo angle.

Call the Servo_Init function in app_main to initialize the servo, and then change the angle of the servo S1 every 1000 milliseconds in the loop to achieve the back-and-forth swing of the servo.

 

4. Compile, download and flash firmware

Use a Type-C data cable to connect the virtual machine/computer and the microROS control board. If the system pops up, choose to connect to the virtual machine.

Activate the ESP-IDF development environment. Note that every time you open a new terminal, you need to activate the ESP-IDF development environment before compiling the firmware.

Enter the project directory

Compile, flash, and open the serial port simulator

If you need to exit the serial port simulator, press Ctrl+].

 

5. Experimental results

The serial port simulator prints the "hello yahboom" greeting. And the angle of the servo S1 is changed every second, from -90 to 90, and then from 90 to -90, and continues to run, thereby realizing the function of repeated rotation of the servo.

image-20240111150853493

image-20240111151520253

image-20240111151541241

image-20240111151604919