Welcome to ROSMASTER R2 repository
12. Control motor forward and reverse
12. Control motor forward and reverse
12. Control motor forward and reverse12.1. Purpose of the experiment12.2 Configuring Pin Information12.3. Experimental flowchart analysis12.4. Core code explanation12.5 Hardware Connection12.6. Experimental effect
12.1. Purpose of the experiment
Use the timer function of STM32 to drive the motor driver chip AM2857, so as to control the motor forward, reverse and stop.
12.2 Configuring Pin Information
- Import the ioc file from Beep's project and name it Motor.
According to the schematic diagram, we can see that there are four AM2857 motor driver modules in total, one motor driver module controls one motor, and the pin configuration is shown below.




- First set timer 1, clock source select internal clock, set the four channel output PWM signal CH1 CH2N CH3N CH4 corresponding pin PA8 PB0 PB1 PA11.

Other parameters are shown below:

- Next, set timer 8, clock source selection of internal clock, set the four channel output PWM signal CH1 CH2 CH3 CH4 corresponding to the pin PC6 PC7 PC8 PC9.

Other parameters are the same as for Timer 1.

The final chip configuration pins are shown below:

12.3. Experimental flowchart analysis

12.4. Core code explanation
- Create a new bsp_motor.h and bsp_motor.c, and add the following to bsp_motor.h:

Where M1 corresponds to the motor in the upper left corner of the body, M2 corresponds to the motor in the lower left corner, M3 corresponds to the motor in the upper right corner, and M4 corresponds to the motor in the lower right corner.
- In the bsp_motor.c file, create the following new content:
Motor timer PWM output start initialization.

- Motor stop function, parameter brake=1 for brake stop, brake=0 for free stop.

- Since the motor has a certain control dead zone, the dead zone can be filtered. If you choose not to filter, please define the MOTOR_IGNORE_PULSE parameter as 0.

- The next step is to set the motor speed, where id is the motor ID, speed speed value range: ± (3600-MOTOR_IGNORE_PULSE), 0 for stop.

- Add motor initialization to the Bsp_Init() function.

- In the Bsp_Loop() function add the key to control the motor, press the first forward, the second free stop, the third backward, the fourth brake stop.

12.5 Hardware Connection
The motor connecting wires need to be connected to the corresponding motors as shown in the following figure, 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.

Due to the relatively large power of the motor, the expansion board should not directly use USB 5V power supply, must use DC 12V power supply.
12.6. Experimental effect
Since the motor will spin when it starts, please set up the cart before the experiment, with the motor wheels hanging in the air, to avoid running across the road.
After burning the program, the LED flashes every 200 milliseconds. Press the first forward, the second free stop, the third backward, the fourth brake stop.