STM32F407 巡线舵机控制转向代码
时间: 2023-11-12 21:45:11 浏览: 116
2021电赛F题,控制部分代码(freeRTOS,巡线,自动返回)keil工程+stm32f411+HAL库+cubemx
5星 · 资源好评率100%
以下是基于STM32F407的巡线舵机控制转向代码的示例:
```c
#include "stm32f4xx_hal.h"
#include "main.h"
/* 定义舵机PWM输出通道 */
#define SERVO_PWM_CHANNEL TIM_CHANNEL_1
/* 定义舵机PWM输出频率 */
#define SERVO_PWM_FREQ 50 // 50 Hz
/* 定义左右转动的角度范围 */
#define SERVO_MAX_ANGLE 180
#define SERVO_MIN_ANGLE 0
/* 定义巡线传感器IO口 */
#define LINE_SENSOR_GPIO_PORT GPIOA
#define LINE_SENSOR_GPIO_PIN GPIO_PIN_0 // PA0
/* 定义舵机PWM输出IO口 */
#define SERVO_PWM_GPIO_PORT GPIOA
#define SERVO_PWM_GPIO_PIN GPIO_PIN_8 // PA8
/* 定义巡线传感器灰度值阈值 */
#define LINE_SENSOR_THRESHOLD 500
/* 定义左右转动的角度 */
uint16_t servoAngle = 90;
/* 定义PWM输出句柄 */
TIM_HandleTypeDef htim1;
/* 函数声明 */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM1_Init(void);
void servoSetAngle(uint16_t angle);
int main(void)
{
/* 初始化HAL库 */
HAL_Init();
/* 初始化系统时钟 */
SystemClock_Config();
/* 初始化GPIO */
MX_GPIO_Init();
/* 初始化PWM */
MX_TIM1_Init();
/* 启动PWM输出 */
HAL_TIM_PWM_Start(&htim1, SERVO_PWM_CHANNEL);
/* 无限循环 */
while (1)
{
/* 读取巡线传感器的灰度值 */
uint16_t lineSensorValue = HAL_GPIO_ReadPin(LINE_SENSOR_GPIO_PORT, LINE_SENSOR_GPIO_PIN);
/* 如果传感器检测到黑线,则向左转动舵机 */
if (lineSensorValue < LINE_SENSOR_THRESHOLD)
{
servoAngle += 10;
if (servoAngle > SERVO_MAX_ANGLE)
{
servoAngle = SERVO_MAX_ANGLE;
}
servoSetAngle(servoAngle);
}
/* 如果传感器没有检测到黑线,则向右转动舵机 */
else
{
servoAngle -= 10;
if (servoAngle < SERVO_MIN_ANGLE)
{
servoAngle = SERVO_MIN_ANGLE;
}
servoSetAngle(servoAngle);
}
/* 延时 */
HAL_Delay(100);
}
}
/* 设置舵机角度 */
void servoSetAngle(uint16_t angle)
{
/* 计算PWM输出的占空比 */
uint16_t dutyCycle = (angle * 11) + 500;
TIM_OC_InitTypeDef sConfigOC = {0};
/* 配置PWM输出通道的占空比 */
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = dutyCycle;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, SERVO_PWM_CHANNEL);
/* 更新PWM输出 */
HAL_TIM_PWM_Start(&htim1, SERVO_PWM_CHANNEL);
}
/* 初始化GPIO */
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* 使能GPIOA时钟 */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* 配置PA0为输入模式 */
GPIO_InitStruct.Pin = LINE_SENSOR_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(LINE_SENSOR_GPIO_PORT, &GPIO_InitStruct);
/* 配置PA8为复用模式 */
GPIO_InitStruct.Pin = SERVO_PWM_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init(SERVO_PWM_GPIO_PORT, &GPIO_InitStruct);
}
/* 初始化PWM */
static void MX_TIM1_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* 使能TIM1时钟 */
__HAL_RCC_TIM1_CLK_ENABLE();
/* 配置TIM1时钟源 */
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
/* 配置TIM1基本参数 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 83; // 84 MHz / (83 + 1) = 1 MHz
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 20000 / SERVO_PWM_FREQ; // 20000 us / 50 Hz = 400
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
HAL_TIM_Base_Init(&htim1);
/* 配置TIM1 PWM模式 */
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 1500; // 1500 us
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, SERVO_PWM_CHANNEL);
/* 配置TIM1主从模式 */
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
}
/* 初始化系统时钟 */
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/* 错误处理函数 */
void Error_Handler(void)
{
/* 用户自定义错误处理代码 */
}
```
在上述代码中,我们使用TIM1作为PWM输出的定时器,通过HAL库的函数来控制PWM输出的占空比,从而控制舵机的转动角度。同时,我们使用GPIO来读取巡线传感器的灰度值,从而控制舵机的转动方向。
阅读全文