Drive motor

1. Experimental purpose

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

 

2. Hardware connection

As shown in the figure below, the microROS control board integrates four encoder motor control interfaces. An additional encoder motor needs to be connected. The motor control interface supports 310 motors. A type-C data cable also needs to be connected to the computer and the microROS control board to burn firmware. Function.

image-20240111152814048

The corresponding names of the four motor interfaces are: left front wheel->Motor1, left rear wheel->Motor2, right front wheel->Motor3, right rear wheel->Motor4.

image-20240111155522592

Motor interface line sequence, there is a detailed line sequence silk screen on the back of the microROS control board. Here we take Motor1 as an example. M1+ and M1- are the interfaces for controlling the rotation of the motor. GND and VCC are the power supply circuits of the encoder. H1A and H1B are the encoder pulses. Detection pin.

image-20240111160116671

Note: If you are using the 310 motor and motor cable provided by Yabo Intelligence, connect the white wire shell end to the interface on the microROS control board, and the black wire shell end to the 310 motor interface.

 

3. Core code analysis

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

Before starting to write code, you need to import the bdc_motor component from the ESP32 library, create a new idf_component.yml file in the pwm_motor component, and then add the following content to import the bdc_motor component dependency.

Since the initialization process of four motors is similar, it should be noted that the timer group of ESP32S3 can drive up to three motors, so motors Motor1, Motor2, and Motor3 use timer group 0, and motor Motor4 uses timer group 1. Here we take initializing motor Motor1 as an example.

First, initialize the GPIO M1A and M1B of Motor1 to PWM output with a frequency of 25KHz. Select timer group 0 as the PWM output timer group of Motor1.

Control the speed of motor Motor1. If the speed input is greater than 0, it means the wheel turns forward. If the speed is less than 0, it means the wheel turns backward. If the speed is equal to 0, it means the motor stops.

Since the starting voltage of the 310 motor is 3~5V, dead zone filtering needs to be added to control the motor more quickly.

For the convenience of control, the motor speed is controlled based on the motor ID.

Control the speed of four motors at one time.

Control the motor to stop. There are two ways to stop the motor. The first is to brake to stop, and the second is to coast to stop. The difference between the two methods is that when braking to stop, the wheels stop immediately. When coasting to stop, the wheels will slide for a short distance due to inertia. Stop after distance.

Call the PwmMotor_Init function in app_main to initialize the motor, and change the status of the motor every second in the loop. First, the four-way motor rotates forward for one second, then brakes and stops for one second, then rotates backward for one second, and then coasts to stop for 1 second, and the cycle continues. Control the motor and print out the current motor status.

 

4. Compile, download and flash firmware

Note: Since the connected motor will rotate after the firmware is burned, please lift the car into the air first to prevent the car from moving around on the desktop.

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. After the car robot is elevated, observe the four-way motors of the car. First, it rotates forward for 1 second at the same time, then brakes and stops for 1 second, then rotates backward for 1 second, and then slides to stop for 1 second. This goes back and forth. At the same time, in the serial port simulator It also prints the current motor status.

image-20240111171538270