Welcome to RDK X5 Robot repository
13. The timer captures encoder data
13. Timer captures encoder data
13. Timer captures encoder data13.1. Experimental purpose13.2. Configure pin information13.3. Analysis of experimental flow chart13.4. Core code interpretation13.5. Hardware connection13.6. Experimental effect
13.1. Experimental purpose
Capture encoder data using STM32's timer encoder mode function.
13.2. Configure pin information
Import ioc file from Motor project, name it Encoder, and then related driver of serial port 1.
According to the schematic diagram, the two encoders of the four motors are respectively connected to channel 1 and channel 2 of the timers 2 3 4 5. The motor M1 corresponds to the timer TIM2, M2 corresponds to TIM4, M3 corresponds to TIM5, and M4 corresponds to TIM3

Take timer TIM2 as an example. The setting mode of timer TIM3, TIM4, and TIM5 is the same. Select Combined Channels Mode as Encoder Mode.

Change Encoder Mode to Encoder Mode TI1 and TI2, and set the encoder mode to quadruple frequency. Other parameters are shown in the following figure.

The final chip configuration pin is shown in the figure below:

13.3. Analysis of experimental flow chart

13.4. Core code interpretation
Create bsp_encoder.h and bsp_encoder.c, and add the following information to bsp_encoder.

Create the following content in the bsp_encoder.c file:
The encoder timer is initialized.

Read the encoder data.

Update the total count value of the encoder. It needs to be called every 10 milliseconds.

Return the total number of encoders counted from the start to the present, Encoder_Get_Count_Now returns one way, Encoder_Get_ALL returns four ways.

Add the encoder initialization to the Bsp_Init() function.

Create encoder array to store encoder data. show_encoder is used to print encoder count.

On the basis of the original control motor, the function of printing encoder data every 100 milliseconds is added.

13.5. Hardware connection
The motor connection line needs to be connected to the corresponding motor as shown in the figure below, otherwise it may cause the problem that the program does not match the phenomenon. Motor 1 corresponds to the Motor in the upper left corner of the body, Motor 2 corresponds to the Motor in the lower left corner, motor 3 corresponds to the motor in the upper right corner, and Motor 4 corresponds to the motor in the lower right corner.

Because the power of the motor is relatively large, do not directly use USB 5V power supply for the expansion board, and must use battery power supply.
Then connect the micro-USB cable to the expansion board and computer.
13.6. Experimental effect
Since the motor will turn when started, please set up the car before the experiment, and the motor wheels are suspended to avoid rampaging.
After burning the program, the LED light flashes every 200 milliseconds. Open the serial assistant, you can see the encoder data. Press the first forward, the second free stop, the third backward, the fourth brake stop.
