写一个stm32智能循迹避障小车代码
时间: 2023-09-21 11:10:54 浏览: 163
基于stm32的循迹避障小车
5星 · 资源好评率100%
循迹避障小车是一个很有趣的项目,下面是一个基于STM32的循迹避障小车的代码示例:
```c
#include "stm32f10x.h"
#define LeftMotorEN_RCC RCC_APB2Periph_GPIOC
#define LeftMotorEN_GPIO GPIOC
#define LeftMotorEN_Pin GPIO_Pin_13
#define LeftMotorIN1_RCC RCC_APB2Periph_GPIOB
#define LeftMotorIN1_GPIO GPIOB
#define LeftMotorIN1_Pin GPIO_Pin_14
#define LeftMotorIN2_RCC RCC_APB2Periph_GPIOB
#define LeftMotorIN2_GPIO GPIOB
#define LeftMotorIN2_Pin GPIO_Pin_15
#define RightMotorEN_RCC RCC_APB2Periph_GPIOC
#define RightMotorEN_GPIO GPIOC
#define RightMotorEN_Pin GPIO_Pin_14
#define RightMotorIN1_RCC RCC_APB2Periph_GPIOA
#define RightMotorIN1_GPIO GPIOA
#define RightMotorIN1_Pin GPIO_Pin_11
#define RightMotorIN2_RCC RCC_APB2Periph_GPIOA
#define RightMotorIN2_GPIO GPIOA
#define RightMotorIN2_Pin GPIO_Pin_12
#define LeftSensor_RCC RCC_APB2Periph_GPIOB
#define LeftSensor_GPIO GPIOB
#define LeftSensor_Pin GPIO_Pin_0
#define MiddleSensor_RCC RCC_APB2Periph_GPIOB
#define MiddleSensor_GPIO GPIOB
#define MiddleSensor_Pin GPIO_Pin_1
#define RightSensor_RCC RCC_APB2Periph_GPIOB
#define RightSensor_GPIO GPIOB
#define RightSensor_Pin GPIO_Pin_2
#define LeftThreshold 2000
#define MiddleThreshold 2000
#define RightThreshold 2000
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(LeftMotorEN_RCC | LeftMotorIN1_RCC | LeftMotorIN2_RCC | RightMotorEN_RCC | RightMotorIN1_RCC | RightMotorIN2_RCC, ENABLE);
RCC_APB2PeriphClockCmd(LeftSensor_RCC | MiddleSensor_RCC | RightSensor_RCC, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = LeftMotorEN_Pin;
GPIO_Init(LeftMotorEN_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = LeftMotorIN1_Pin;
GPIO_Init(LeftMotorIN1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = LeftMotorIN2_Pin;
GPIO_Init(LeftMotorIN2_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = RightMotorEN_Pin;
GPIO_Init(RightMotorEN_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = RightMotorIN1_Pin;
GPIO_Init(RightMotorIN1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = RightMotorIN2_Pin;
GPIO_Init(RightMotorIN2_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = LeftSensor_Pin;
GPIO_Init(LeftSensor_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = MiddleSensor_Pin;
GPIO_Init(MiddleSensor_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = RightSensor_Pin;
GPIO_Init(RightSensor_GPIO, &GPIO_InitStructure);
}
void Motor_Control(int LeftMotorDirection, int RightMotorDirection, int LeftMotorSpeed, int RightMotorSpeed)
{
if (LeftMotorDirection == 1) {
GPIO_SetBits(LeftMotorIN1_GPIO, LeftMotorIN1_Pin);
GPIO_ResetBits(LeftMotorIN2_GPIO, LeftMotorIN2_Pin);
} else if (LeftMotorDirection == -1) {
GPIO_ResetBits(LeftMotorIN1_GPIO, LeftMotorIN1_Pin);
GPIO_SetBits(LeftMotorIN2_GPIO, LeftMotorIN2_Pin);
} else {
GPIO_ResetBits(LeftMotorIN1_GPIO, LeftMotorIN1_Pin);
GPIO_ResetBits(LeftMotorIN2_GPIO, LeftMotorIN2_Pin);
}
if (RightMotorDirection == 1) {
GPIO_SetBits(RightMotorIN1_GPIO, RightMotorIN1_Pin);
GPIO_ResetBits(RightMotorIN2_GPIO, RightMotorIN2_Pin);
} else if (RightMotorDirection == -1) {
GPIO_ResetBits(RightMotorIN1_GPIO, RightMotorIN1_Pin);
GPIO_SetBits(RightMotorIN2_GPIO, RightMotorIN2_Pin);
} else {
GPIO_ResetBits(RightMotorIN1_GPIO, RightMotorIN1_Pin);
GPIO_ResetBits(RightMotorIN2_GPIO, RightMotorIN2_Pin);
}
TIM_SetCompare1(TIM3, LeftMotorSpeed);
TIM_SetCompare2(TIM3, RightMotorSpeed);
GPIO_SetBits(LeftMotorEN_GPIO, LeftMotorEN_Pin);
GPIO_SetBits(RightMotorEN_GPIO, RightMotorEN_Pin);
}
void Delay(unsigned int nCount)
{
volatile unsigned int i;
for(i=0; i<nCount; i++);
}
int main()
{
GPIO_Configuration();
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 20000 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = (72 - 1);
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
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_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
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_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_Cmd(TIM3, ENABLE);
while (1) {
if (GPIO_ReadInputDataBit(LeftSensor_GPIO, LeftSensor_Pin) > LeftThreshold) {
Motor_Control(-1, 1, 1000, 1000);
} else if (GPIO_ReadInputDataBit(MiddleSensor_GPIO, MiddleSensor_Pin) > MiddleThreshold) {
Motor_Control(1, 1, 1000, 1000);
} else if (GPIO_ReadInputDataBit(RightSensor_GPIO, RightSensor_Pin) > RightThreshold) {
Motor_Control(1, -1, 1000, 1000);
} else {
Motor_Control(0, 0, 0, 0);
Delay(10000);
}
}
}
```
这个代码是很基础的,需要根据你的具体需求进行修改。这个代码的主要思路是使用三个红外线传感器来检测小车是否偏离轨道,并根据检测结果来控制小车的运动方向和速度。同时,当小车遇到障碍物时,它会停下来。
阅读全文