stm32f103c8t6用pid算法写一个五路的循迹小车(四个直流减速电机)
时间: 2023-09-27 17:12:34 浏览: 600
循迹小车是一种常见的机器人,它通过感知地面上的黑色线条,实现自动寻路的功能。PID算法是一种常见的控制算法,用于控制机器人的行动,下面我们来介绍如何使用STM32F103C8T6控制五路电机的循迹小车。
首先,需要准备以下硬件材料:
- STM32F103C8T6开发板
- L298N电机驱动模块
- TCRT5000红外线传感器5个
- 直流减速电机4个
- 小车底盘
接下来,我们需要进行以下步骤:
1. 连接硬件
将L298N电机驱动模块与STM32F103C8T6开发板连接,连接方式如下:
| L298N引脚 | STM32F103C8T6引脚 |
|---------|------------------|
| ENA | PB0 |
| IN1 | PB1 |
| IN2 | PB2 |
| IN3 | PB10 |
| IN4 | PB11 |
| ENB | PB12 |
将TCRT5000红外线传感器连接到STM32F103C8T6开发板的引脚上,连接方式如下:
| TCRT5000引脚 | STM32F103C8T6引脚 |
|-------------|------------------|
| VCC | 5V |
| GND | GND |
| DO | PA0~PA4 |
将直流减速电机连接到L298N电机驱动模块上,连接方式如下:
| 直流减速电机引脚 | L298N引脚 |
|----------------|----------|
| 正极 | OUT1 |
| 负极 | OUT2 |
| 正极 | OUT3 |
| 负极 | OUT4 |
2. 编写代码
接下来,我们需要编写代码来实现循迹小车的功能。首先,我们需要对红外线传感器进行初始化,然后读取传感器的数据,并根据数据来控制小车的移动方向。具体代码如下:
```c
#include "stm32f10x.h"
void delay_us(u32 nus)
{
u32 i;
for(i=0;i<nus*8;i++);
}
void delay_ms(u16 nms)
{
u16 i;
for(i=0;i<nms;i++)
delay_us(1000);
}
void init_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void motor_forward(u8 speed)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET);
GPIO_WriteBit(GPIOB, GPIO_Pin_10, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_11, Bit_SET);
TIM_SetCompare3(TIM2, speed);
TIM_SetCompare4(TIM2, speed);
TIM_SetCompare1(TIM3, speed);
TIM_SetCompare2(TIM3, speed);
}
void motor_backward(u8 speed)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET);
GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_10, Bit_SET);
GPIO_WriteBit(GPIOB, GPIO_Pin_11, Bit_RESET);
TIM_SetCompare3(TIM2, speed);
TIM_SetCompare4(TIM2, speed);
TIM_SetCompare1(TIM3, speed);
TIM_SetCompare2(TIM3, speed);
}
void motor_left(u8 speed)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET);
GPIO_WriteBit(GPIOB, GPIO_Pin_10, Bit_SET);
GPIO_WriteBit(GPIOB, GPIO_Pin_11, Bit_RESET);
TIM_SetCompare3(TIM2, speed);
TIM_SetCompare4(TIM2, speed);
TIM_SetCompare1(TIM3, speed);
TIM_SetCompare2(TIM3, speed);
}
void motor_right(u8 speed)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET);
GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_10, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_11, Bit_SET);
TIM_SetCompare3(TIM2, speed);
TIM_SetCompare4(TIM2, speed);
TIM_SetCompare1(TIM3, speed);
TIM_SetCompare2(TIM3, speed);
}
void motor_stop(void)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_10, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_11, Bit_RESET);
TIM_SetCompare3(TIM2, 0);
TIM_SetCompare4(TIM2, 0);
TIM_SetCompare1(TIM3, 0);
TIM_SetCompare2(TIM3, 0);
}
u8 read_sensor(void)
{
u8 i,sensor_data=0;
for(i=0;i<5;i++)
{
if(GPIO_ReadInputDataBit(GPIOA,1<<i)==0)
sensor_data|=1<<i;
}
return sensor_data;
}
void pid_control(u8 sensor_data)
{
s16 error;
s16 p_term;
s16 i_term;
s16 d_term;
static s16 last_error=0;
static s16 integral=0;
error=sensor_data-0x0F;
p_term=error*2;
integral+=error;
i_term=integral*0.001;
d_term=(error-last_error)*40;
last_error=error;
s16 speed=p_term+i_term+d_term;
if(speed>255)
speed=255;
if(speed<-255)
speed=-255;
if(speed>0)
motor_forward(speed);
else if(speed<0)
motor_backward(-speed);
else
motor_stop();
}
int main(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_TIM8, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM_OC4Init(TIM8, &TIM_OCInitStructure);
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM1, ENABLE);
TIM_Cmd(TIM8, ENABLE);
TIM_Cmd(TIM2, ENABLE);
TIM_Cmd(TIM3, ENABLE);
init_GPIO();
while(1)
{
u8 sensor_data=read_sensor();
pid_control(sensor_data);
delay_ms(10);
}
}
```
3. 调试测试
将代码烧录到STM32F103C8T6开发板上,然后将小车放在地面上,让它自行行驶,观察小车的行动是否符合预期。如果出现异常情况,可以通过调试代码来解决问题。
这样,我们就成功地使用STM32F103C8T6控制五路电机的循迹小车了。
阅读全文