STM32 HAL库PID控制电机
第二章 TB6612FNG芯片驱动GB37-520电机(HAL库)
1 电路图
2 TB6612简介
TB6612是双驱动,可同时驱动两个电机
STBY:接单片机的IO口清零电机全部停止,置1通过AIN1 AIN2,BIN1,BIN2 来控制正反转
VM:建议接10V以内电源( 瞬间上电12V可能会有尖峰电压击穿器件 )
VCC:接5V电源
GND:接电源负极
PWMA:接单片机的PWM口 ,控制转速
PWMB:接单片机的PWM口 ,控制转速
AO1、AO2:接电机1的两个脚
BO1、BO2:接电机2的两个脚
赋值 | 停止 | 正转 | 反转 |
---|---|---|---|
AIN1 | 0 | 0 | 1 |
AIN2 | 0 | 1 | 0 |
BIN1 | 0 | 0 | 1 |
BIN2 | 0 | 1 | 0 |
所连接引脚如下表:
引脚号 | 定时器通道 | 备注 |
---|---|---|
PA11 | TIM1_CH4 | PWMA |
PB12 | AIN2 | |
PB13 | AIN1 | |
PB14 | BIN1 | |
PB15 | BIN2 | |
PA8 | TIM1_CH1 | PWMB |
3 STM32CubeIDE配置
3.1 配置PWM输出引脚
有电路图知PWM产生于PA11、PA8引脚,对应分别为TIM1_CH4、TIM1_CH1通道,所以在cubeIDE在左栏的Timers中选择TIM1,Clock Source选择内部时钟Internal Clock,然后选择打开通道1与通道4的PWM输出模式,在下方的参数设置中填入预分频数(psc)和自动重装载值(arr),输出的PWM频率:
P
W
M
输出频率(计数器溢出频率)
=
计数器的时钟输入(预分频器的时钟输出)
A
R
R
+
1
=
预分频器的输入时钟(内部时钟,一般为
72
M
H
z
)
(
P
S
C
+
1
)
(
A
R
R
+
1
)
PWM输出频率(计数器溢出频率)=\frac{计数器的时钟输入(预分频器的时钟输出)}{ARR+1} \\=\frac{预分频器的输入时钟(内部时钟,一般为72MHz)}{(PSC+1)(ARR+1)}
PWM输出频率(计数器溢出频率)=ARR+1计数器的时钟输入(预分频器的时钟输出)=(PSC+1)(ARR+1)预分频器的输入时钟(内部时钟,一般为72MHz)
在本次测试中,根据使用的减速电机,设置PWM的输出频率为18KHz,根据上述公式可以计算得到,并将两个通道都设置为PWM模式:
18,000=72,000,000/(3+1)*(999+1)
解释:
ARR:自动重装载值,arr是计数,从0到设定值,然后返回至0重新开始计数(也可以看成pwm的机械周期:将一个pwm周期分为多少份)
PSC:预分频器的值,psc是预分频系数,决定一次计数的时间。公式:一次计数时间(也是一个脉冲输出时间)=(psc+1)/主频。计数周期(也是一个pwm周期)=(psc+1)*(arr+1)/主频。
PWM的频率是指每秒钟信号从高电平到低电平再回到高电平的次数。
3.2 配置正反转控制引脚
配置用于控制电机正反转的两组引脚,分别为AIN1和AIN2、BIN1和BIN2,对应单片机引脚为PB13、PB12、PB14、PB15配置为GPIO_Output模式。
3.3 修改标签
为了方便管理和移植,我们对应TB6612芯片修改相应的标签。
(1)修改GPIO
(2)修改TIM
3.4 生成代码
4 编写电机驱动程序
4.1 新建motor文件夹
(1)右击新建source folder,文件名icode,用于存放自写库函数。
(2)在icode文件夹下新建motor文件,再motor文件下新建header file:motor.h和source file:motor.c文件。
4.2 编写驱动程序
motor.h
/*
* motor.h
*
* Created on: Apr 4, 2023
* Author: 77454
*/
#ifndef MOTOR_MOTOR_H_
#define MOTOR_MOTOR_H_
#include "main.h"
#include "tim.h"
void MotorControl(char motorDirection,int leftMotorPWM, int rightMotorPWM);
#endif /* MOTOR_MOTOR_H_ */
motor.c文章来源:https://www.toymoban.com/news/detail-420991.html
/*
* motor.c
*
* Created on: Apr 4, 2023
* Author: 77454
*/
#include "motor.h"
/**
* @brief 控制电机进行正转、反转、停止
* @param None
* @retval None
*/
void LeftMotor_Go() //左电机正转 AIN输出相反电平 BIN也输出相反电平
{
HAL_GPIO_WritePin(AIN1_GPIO_Port, AIN1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(AIN2_GPIO_Port, AIN2_Pin, GPIO_PIN_RESET);
}
void LeftMotor_Back() //左电机反转
{
HAL_GPIO_WritePin(AIN1_GPIO_Port, AIN1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(AIN2_GPIO_Port, AIN2_Pin, GPIO_PIN_SET);
}
void LeftMotor_Stop() //左电机停止 AIN和BIN输出相同电平
{
HAL_GPIO_WritePin(AIN1_GPIO_Port, AIN1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(AIN2_GPIO_Port, AIN2_Pin, GPIO_PIN_RESET);
}
void RightMotor_Go() //右电机正转 AIN输出相反电平 BIN也输出相反电平
{
HAL_GPIO_WritePin(BIN1_GPIO_Port, BIN1_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(BIN2_GPIO_Port, BIN2_Pin, GPIO_PIN_RESET);
}
void RightMotor_Back() //右电机反转
{
HAL_GPIO_WritePin(BIN1_GPIO_Port, BIN1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(BIN2_GPIO_Port, BIN2_Pin, GPIO_PIN_SET);
}
void RightMotor_Stop() //右电机停止 AIN和BIN输出相同电平
{
HAL_GPIO_WritePin(BIN1_GPIO_Port, BIN1_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(BIN2_GPIO_Port, BIN2_Pin, 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(&htim1, TIM_CHANNEL_4, leftMotorPWM);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, rightMotorPWM);
break;
case 1:
LeftMotor_Back();
RightMotor_Back();
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_4, leftMotorPWM);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, rightMotorPWM);
break;
case 2:
LeftMotor_Stop();
RightMotor_Stop();
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_4, 0);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
break;
default:
break;
}
}
main.c文章来源地址https://www.toymoban.com/news/detail-420991.html
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "rtc.h"
#include "tim.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "../../icode/motor/motor.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void) {
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_RTC_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
MotorControl(0, 5000, 5000); //直行
HAL_Delay(2000);
MotorControl(2, 0, 0); //停止
HAL_Delay(2000);
MotorControl(1, 500, 500); //后退
HAL_Delay(2000);
MotorControl(0, 0, 200); //前进左转
HAL_Delay(2000);
MotorControl(0, 200, 0); //前进右转
HAL_Delay(2000);
MotorControl(1, 0, 200); //左转退回
HAL_Delay(2000);
MotorControl(1, 200, 0); //右转退回
HAL_Delay(2000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
RCC_PeriphCLKInitTypeDef PeriphClkInit = { 0 };
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI
| RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void) {
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1) {
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
到了这里,关于STM32 HAL库PID控制电机 第二章 TB6612FNG芯片驱动GB37-520电机的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!