Don't lie to you, a robot motor drive tutorial that you can see at a glance!

01 Preface

Before, we published a series of STM32 robot control development tutorials, and received feedback from many small partners, so we optimized the tutorials and added new content and tools.

The robot control board used in this tutorial has 4 motor interfaces with encoders, 4 steering gear interfaces, serial communication interface, SWD download debugging interface, aircraft model remote control interface, USB5V output interface and 40PIN interface for direct connection with Raspberry Pi etc., the onboard resources are abundant, convenient for debugging! It can control two-wheel, four-wheel, Ackerman and Mecanum wheel steering robot/car.

Robot driver board:

The effect of connecting with the Raspberry Pi saves additional serial communication cables and power cables, simplifying the complexity:

02 Development of robot car motor drive - let the car run!

Use STM32CubeIDE to build a development environment.

first step:

  • STM32CubeIDE integrates STM32CubeMX, you can directly open STM32CubeIDE, choose to create a new STM32 Project, customize the project name, select STM32Cube for the project file type, and then enter the STM32CubeMX interface, select the chip model used, the chip used by the robot control board is STM32F103RCT6.

Step two:

  • Carry out the initial configuration of the chip.

Select the RCC clock setting in the System Core on the left column, select the high-speed clock and use the external crystal oscillator

For debugging configuration in SYS, select Serial Wire in the Debug drop-down list, and select SysTick as the clock source here.

Set the clock tree in Clock Configuration, set the frequency of HCLK to 72MHz, enter 72 and press Enter to confirm, the software will automatically complete the setting of the internal frequency multiplier.

The first two steps are applicable to most STM32 bare-metal development processes. When adding RTOS, you need to pay attention to the selection of the system clock source.

third step:

  • After completing the above two steps, you can find the corresponding interface to configure according to the schematic diagram of the NANO driver board, and start driving the motor!

The robot control board designed this time comes with a TB6612FNG motor driver chip, through which the TB6612FNG chip can drive the motor to complete the functions of forward and reverse rotation and PWM speed regulation.

As shown in the figure, the green box on the left is the output pin connected to the motor, and the red box on the right is the drive pin connected to the STM32. One TB6612FNG chip can realize the control of two motors. Find the corresponding pins of STM32 in the schematic diagram and perform initialization configuration.

Find the PC6 and PC7 pins in STM32CubeMX, and select them as channel 1 and channel 2 of timer 8. When the initialization configuration is not completed, the color of the pin is yellow. After the relevant initialization configuration is completed, the color of the pin turns green. .

Select TIM8 in Timers on the left column, select internal clock Internal Clock as Clock Source, then select to enable the PWM output mode of channel 1 and channel 2, and fill in the prescaler frequency (psc) and auto-reload value in the parameter settings below (arr), output PWM frequency

frequency=sysclk/(psc+1)*(arr+1)

In this test, according to the geared motor used, set the PWM output frequency to 18KHz, which can be calculated according to the above formula, and set both channels to PWM1 mode

18,000=72,000,000/(3+1)*(999+1)

After completing the corresponding initialization configuration, you can see that the PA6 and PA7 pins on the right have turned green.

After configuring the timer, you also need to configure two sets of pins for controlling the forward and reverse rotation of the motor, which are AIN1 and AIN2, BIN1 and BIN2, corresponding to the PC0~PC3 pins of STM32, and configure them as ordinary output tubes. Feet will do.

the fourth step:

  • Generate project files

Switch to the Project Manager interface, the content of the Project is set as follows, choose to use ST's official STM32CubeIDE as the program development.

Check in Code Generator to generate a single pair of .c/.h files.

After completing the above steps, press and hold the ctrl+s shortcut key to generate the program.

Compile the generated program and continue to the next step if no errors are displayed.

the fifth step:

  • Write a motor driver

Write the generated project file in STM32CubeIDE , and write the related functions of motor control under USER CODE BEGIN 0

/***  @brief 控制电机进行正转、反转、停止*  @param None*  @retval None*/void LeftMotor_Go() //左电机正转 AIN输出相反电平  BIN也输出相反电平{
   
       HAL_GPIO_WritePin(Dir_Port,AIN1,GPIO_PIN_SET);    HAL_GPIO_WritePin(Dir_Port,AIN2,GPIO_PIN_RESET);}void LeftMotor_Back()  //左电机反转 {
   
       HAL_GPIO_WritePin(Dir_Port,AIN1,GPIO_PIN_RESET);    HAL_GPIO_WritePin(Dir_Port,AIN2,GPIO_PIN_SET);}void LeftMotor_Stop()  //左电机停止 AIN和BIN输出相同电平{
   
     HAL_GPIO_WritePin(Dir_Port,AIN1,GPIO_PIN_RESET);    HAL_GPIO_WritePin(Dir_Port,AIN2,GPIO_PIN_RESET);}void RightMotor_Go() //右电机正转 AIN输出相反电平  BIN也输出相反电平{
   
       HAL_GPIO_WritePin(Dir_Port,BIN1,GPIO_PIN_SET);    HAL_GPIO_WritePin(Dir_Port,BIN2,GPIO_PIN_RESET);}void RightMotor_Back()  //右电机反转 {
   
       HAL_GPIO_WritePin(Dir_Port,BIN1,GPIO_PIN_RESET);    HAL_GPIO_WritePin(Dir_Port,BIN2,GPIO_PIN_SET);}void RightMotor_Stop()  //右电机停止 AIN和BIN输出相同电平{
   
       HAL_GPIO_WritePin(Dir_Port,BIN1,GPIO_PIN_RESET);    HAL_GPIO_WritePin(Dir_Port,BIN2,GPIO_PIN_RESET);}/***  @brief 控制电机进行速度控制*  @param 运动方向,左右电机的PWM值*  @retval None*/void MotorControl(char motorDirection,int leftMotorPWM, int rightMotorPWM){
   
       switch(motorDirection)    {            case 0:            LeftMotor_Go();        RightMotor_Go();           __HAL_TIM_SET_COMPARE(motor_TIM,leftMotorChannle,leftMotorPWM);           __HAL_TIM_SET_COMPARE(motor_TIM,rightMotorChannle,rightMotorPWM);break;      case 1:           LeftMotor_Back();        RightMotor_Back();          __HAL_TIM_SET_COMPARE(motor_TIM,leftMotorChannle,leftMotorPWM);          __HAL_TIM_SET_COMPARE(motor_TIM,rightMotorChannle,rightMotorPWM);break;      case 2:         LeftMotor_Stop();        RightMotor_Stop();          __HAL_TIM_SET_COMPARE(motor_TIM,leftMotorChannle,0);           __HAL_TIM_SET_COMPARE(motor_TIM,rightMotorChannle,0);break;       default:break;    }}

Some main functions of the driver are as follows: (The following example omits the configuration function automatically generated by STM32CubeMX, and the specific driver refers to the routine file)

int main(){
   
       while(1)    {
   
         MotorControl(0,500,500); //直行       HAL_Delay(2000);      MotorControl(2,0,0); //停止       HAL_Delay(2000);      MotorControl(1,500,500); //后退       HAL_Delay(2000);      MotorControl(0,0,500);  //前进左转        HAL_Delay(2000);      MotorControl(0,500,0);  //前进右转        HAL_Delay(2000);      MotorControl(1,0,500);  //左转退回        HAL_Delay(2000);      MotorControl(1,500,0);  //右转退回        HAL_Delay(2000);    }}

Step six:

  • Download the debugged program to the robot control board and drive the car to move

You can use ST-LINK or J-LINK to connect to the SWD interface on the driver board to burn the program to the board.

Here we introduce another tool to you: STM32CubeProgrammer. The tool provides multiple burning modes and supports multiple generated file formats. Connect the ST-LINK to the SWD interface on the driver board, open the generated .elf file, and write the program to the board. After the programming is successful, press the reset button on the board, place the car on the flat ground, and you can see The car starts running according to the code setting!

The demonstration effect is as follows:

03

Steering with the steering gear

The above driving routine is based on the left and right wheel speed difference (differential speed) to complete the left turn and right turn functions

But don't forget that there is also a steering gear interface on the NANO driver board! Therefore, the turning of the car can also be realized through the steering gear, adding the control of the steering gear only needs to modify the drive slightly! Let me explain how to drive the Ackerman car.

Open the STM32CubeMx file (suffix .ioc) in STM32CubeIDE , find the corresponding interface of the servo, and set it to PWM output mode

Generally, the control frequency of the steering gear is 50Hz~100Hz, that is, 10ms~20ms. We can set the required PSC and ARR values ​​according to the frequency calculation method mentioned above, and set the control frequency to 50Hz, that is, 20ms.

72,000,000/(719+1)*(1999+1)=50Hz

The control angle of the steering gear is 0° ~ 180°, and the pulse width range is 0.5ms ~ 2.5ms. Through calculation, the expected rotation angle value is converted into the PWM duty cycle, and 90° is taken as the median value, that is, turn left The range is 0° ~ 90°, and the right turn range is 90° ~ 180°.

void Set_Servo_angle(TIM_HandleTypeDef * htim,uint32_t Channel,uint8_t angle){
   
     uint16_t compare_value=0;  if(angle <= 180)   //限制角度为180°  {
   
       compare_value=0.5*2000/20+angle*2000/20/90;    //角度转化为数值    __HAL_TIM_SET_COMPARE(htim, Channel, compare_value);  }

Some main functions of the driver are as follows: (The following example omits the configuration function automatically generated by STM32CubeMX, and the specific driver refers to the routine file)

int main(){
   
       MotorControl(0,500,500); //设定电机初始速度    while(1)    {
   
           Set_Servo_angle(&htim4,TIM_CHANNEL_3,60); //左转30°        HAL_Delay(2000);        Set_Servo_angle(&htim4,TIM_CHANNEL_3,90);  //方向回正        HAL_Delay(2000);        Set_Servo_angle(&htim4,TIM_CHANNEL_3,120);  //右转30°        HAL_Delay(2000);    }}

04 Use the steering gear to demonstrate the steering effect

This article mainly demonstrates some basic functions of the robot control board to control motor movement and steering gear steering, but this control board can do more things. In the next article, I will continue to show you how to use this The robot control board realizes the motor encoder speed measurement and the PID control of the motor !

code? Here: GitHub - COONEO/NEOR-nano

Guess you like

Origin blog.csdn.net/COONEO/article/details/131982458