Read motor encoder data

Read motor encoder data1. Experimental Purpose2. Hardware Connection3. Core code analysis4. Compile, download and burn firmware5. Experimental Results

 

1. Experimental Purpose

Use the encoder motor interface of the STM32 control board and learn to use the STM32 timer to capture the number of motor encoder pulses.

2. Hardware Connection

As shown in the figure below, the STM32 control board integrates four encoder motor control interfaces. This requires additional connection to an encoder motor. The motor control interface supports 520 encoder motors. Because encoder motors require high voltage and current, they must be powered by a battery.

Use a type-C data cable to connect the computer USB and the USB Connect port of the STM32 control board.

The corresponding names of the four motor interfaces are: left front wheel -> M1, left rear wheel -> M2, right front wheel -> M3, right rear wheel -> M4.

image-20250618162614850

image-20250612100250376

image-20250612151045745

image-20250613101257354image-20250613101049814image-20250613100936706

The corresponding relationship of motor encoder GPIO is shown in the following table:

Motor interface encoder signalSTM32 GPIO numberingSTM32 timer channels
H1APB4TIM3_CH1
H1BPB5TIM3_CH2
H2APA15TIM2_CH1
H2BPB3TIM2_CH2
H3APA0TIM5_CH1
H3BPA1TIM5_CH2
H4APD12TIM4_CH1
H4BPD13TIM4_CH2

3. Core code analysis

The path corresponding to the program source code is:

Since the initialization process for the four motor encoders is similar, set timer channels 1 and 2 to encoder mode and configure the rising and falling edge trigger signals. Since timers TIM2 and TIM5 are 32-bit timers, and TIM3 and TIM4 are 16-bit timers, for ease of calculation, we uniformly set the maximum count value to 65535. This example uses the encoder initialization for timers TIM2 and TIM3.

image-20250716110630048

image-20250716110825856

During initialization, start channels 1 and 2 of timers TIM2, TIM3, TIM4, and TIM5.

Get the encoder data cache value.

In order to update the total count value of the encoder in time, this function needs to be called every 10 milliseconds.

Based on Encoder_id, read the total encoder count from the time a certain channel is powered on to the present.

It is also possible to obtain the encoder values of four motors at one time.

Define the value of the encoder for a full rotation of the wheel as: reduction ratio * number of encoder lines * number of channels * signal trigger source

Here we take the M3 car motor as an example. The parameters are reduction ratio: 56, number of encoder lines: 11, number of channels (two Hall sensors): 2, signal trigger source (including rising and falling edges): 2. The calculated encoder value for one wheel rotation is approximately 2464.

Call the Encoder_Init function in App_Handle to initialize the motor encoders. In the loop, print the accumulated pulse counts of the four motor encoders every 300 milliseconds.

4. Compile, download and burn firmware

Select the project to be compiled in the file management interface of STM32CUBEIDE and click the compile button on the toolbar to start compiling.

image-20250609174806368

If there are no errors or warnings, the compilation is complete.

image-20250609175148035

Press and hold the BOOT0 button, then press the RESET button to reset, release the BOOT0 button to enter the serial port burning mode. Then use the serial port burning tool to burn the firmware to the board.

If you have STlink or JLink, you can also use STM32CUBEIDE to burn the firmware with one click, which is more convenient and quick.

5. Experimental Results

The MCU_LED light flashes every 200 milliseconds.

Taking motor 3 as an example, when the wheel rotates forward, the encoder data accumulates. When the wheel rotates forward one circle, the encoder data increases by approximately 2464. Due to a certain error in manual rotation, there may be some difference in the values, as long as the difference is not too large.

image-20250618183319355

Press the reset button on the STM32 control board to reset the value to 0.

When the wheel rotates backward, the encoder data decreases. If the wheel rotates backward one circle, it decreases by about 2464.