用stm32F407ZETx开发板和keil5软件实现三轮小车的前驱的左右两个红外进行避障来实现小车的直行左转右转后退的具体代码是什么
时间: 2024-04-23 18:22:03 浏览: 48
实现三轮小车的前驱的左右两个红外进行避障来实现小车的直行左转右转后退,具体代码如下所示:
```
#include "stm32f4xx.h"
#include "delay.h"
#define IN1 PAout(1)
#define IN2 PAout(2)
#define IN3 PAout(3)
#define IN4 PAout(4)
#define ENA PAout(5)
#define ENB PAout(6)
#define IR1 PAin(7)
#define IR2 PAin(8)
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void delay_ms(int n)
{
while (n--)
{
int i = 5000;
while (i--)
;
}
}
void Forward(int speed)
{
IN1 = 1;
IN2 = 0;
IN3 = 1;
IN4 = 0;
ENA = 1;
ENB = 1;
TIM_SetCompare1(TIM3, speed);
TIM_SetCompare2(TIM3, speed);
}
void Backward(int speed)
{
IN1 = 0;
IN2 = 1;
IN3 = 0;
IN4 = 1;
ENA = 1;
ENB = 1;
TIM_SetCompare1(TIM3, speed);
TIM_SetCompare2(TIM3, speed);
}
void Left(int speed)
{
IN1 = 0;
IN2 = 1;
IN3 = 1;
IN4 = 0;
ENA = 1;
ENB = 1;
TIM_SetCompare1(TIM3, speed);
TIM_SetCompare2(TIM3, speed);
}
void Right(int speed)
{
IN1 = 1;
IN2 = 0;
IN3 = 0;
IN4 = 1;
ENA = 1;
ENB = 1;
TIM_SetCompare1(TIM3, speed);
TIM_SetCompare2(TIM3, speed);
}
void Stop(void)
{
IN1 = 0;
IN2 = 0;
IN3 = 0;
IN4 = 0;
ENA = 0;
ENB = 0;
}
int main(void)
{
GPIO_Configuration();
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 8399; //定时器重载值
TIM_TimeBaseStructure.TIM_Prescaler = 99; //定时器分频系数
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
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(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
while (1)
{
if (IR1 == 0 && IR2 == 0) //前方有障碍物
{
Stop();
delay_ms(500);
Backward(300);
delay_ms(1000);
Stop();
delay_ms(500);
Left(300);
delay_ms(1000);
Stop();
delay_ms(500);
}
else if (IR1 == 0) //左侧有障碍物
{
Stop();
delay_ms(500);
Backward(300);
delay_ms(1000);
Stop();
delay_ms(500);
Right(300);
delay_ms(1000);
Stop();
delay_ms(500);
}
else if (IR2 == 0) //右侧有障碍物
{
Stop();
delay_ms(500);
Backward(300);
delay_ms(1000);
Stop();
delay_ms(500);
Left(300);
delay_ms(1000);
Stop();
delay_ms(500);
}
else //前方无障碍物
{
Forward(500);
}
}
}
```
在这个代码中,我们通过IR1和IR2两个红外传感器来检测小车前方和左右两侧是否有障碍物。如果前方有障碍物,则停车后向后退一段时间,然后左转;如果左侧有障碍物,则停车后向后退一段时间,然后右转;如果右侧有障碍物,则停车后向后退一段时间,然后左转;如果前方无障碍物,则直行。同时,我们通过PWM控制小车的速度和方向,实现小车的运动控制。
阅读全文