一.材料
二.原理
这里以黑胶布为例,循迹模块将对于黑胶布方向调整的信息反馈到核心板中(原理在之前的51循迹小车所讲过,这里便不在提及51循迹小车),核心板处理信息控制电机对小车方向做出调整,使小车可以沿黑线移动。
三.代码思想
当小车偏左时,会导致右侧循迹模块触发并产生一个低电平。此时核心板检测到右侧循迹模块呈现低电平会执行右转函数,使小车及时修改轨迹,偏右时同理。当速度过快时小车来不及反应会冲出黑线,此时我们可以使用pwm将小车速度控制在一个合理范围。
四.代码
电机控制
#ifndef __MOTO_H
#define __MOTO_H
#include "sys.h"
#define IN1 PFout(6)
#define IN2 PFout(7)
#define IN3 PFout(8)
#define IN4 PEout(2)
#define IN5 PGout(6)
#define IN6 PGout(7)
#define IN7 PGout(8)
#define IN8 PEout(3)
#define R1 PEin(6)
#define R2 PEin(7)
#define R3 PEin(8)
void xunji_Init(void);
void MOTO_GPIO_Init(void);
void Car_Go(void);
void Car_Back(void);
void Car_Turn_Right(void);
void Car_Turn_Left(void);
void Car_Stop(void);
void Car_Right_90_Init(void);
void Car_Left_90_Init(void);
#endif
#include "moto.h"
#include "stm32f10x.h"
#include "pwm.h"
void MOTO_GPIO_Init(void)
{
/*¶¨ÒåÒ»¸öGPIO_InitTypeDefÀàÐ͵ĽṹÌå*/
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF|RCC_APB2Periph_GPIOG|RCC_APB2Periph_GPIOE, ENABLE); /*¿ªÆôGPIOµÄÍâÉèʱÖÓ*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7 | GPIO_Pin_8; /*Ñ¡ÔñÒª¿ØÖƵÄGPIOÒý½Å*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*ÉèÖÃÒý½ÅģʽΪͨÓÃÍÆÍìÊä³ö*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*ÉèÖÃÒý½ÅËÙÂÊΪ50MHz */
GPIO_Init(GPIOF, &GPIO_InitStructure); /*µ÷Óÿ⺯Êý£¬³õʼ»¯GPIO*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2| GPIO_Pin_3; /*Ñ¡ÔñÒª¿ØÖƵÄGPIOÒý½Å*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*ÉèÖÃÒý½ÅģʽΪͨÓÃÍÆÍìÊä³ö*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*ÉèÖÃÒý½ÅËÙÂÊΪ50MHz */
GPIO_Init(GPIOE, &GPIO_InitStructure); /*µ÷Óÿ⺯Êý£¬³õʼ»¯GPIO*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7 | GPIO_Pin_8; /*Ñ¡ÔñÒª¿ØÖƵÄGPIOÒý½Å*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*ÉèÖÃÒý½ÅģʽΪͨÓÃÍÆÍìÊä³ö*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*ÉèÖÃÒý½ÅËÙÂÊΪ50MHz */
GPIO_Init(GPIOG, &GPIO_InitStructure); /*µ÷Óÿ⺯Êý£¬³õʼ»¯GPIO*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; /*Ñ¡ÔñÒª¿ØÖƵÄGPIOÒý½Å*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /*ÉèÖÃÒý½ÅģʽΪͨÓÃÍÆÍìÊä³ö*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*ÉèÖÃÒý½ÅËÙÂÊΪ50MHz */
GPIO_Init(GPIOA, &GPIO_InitStructure); /*µ÷Óÿ⺯Êý£¬³õʼ»¯GPIO*/
}
void Car_Go()
{
TIM_SetCompare3(TIM3,370);
TIM_SetCompare2(TIM5,530);
TIM_SetCompare3(TIM4,370);
TIM_SetCompare4(TIM4,400);
IN1 = 1;
IN2 = 0;
IN3 = 1;
IN4 = 0;
IN5 = 1;
IN6 = 0;
IN7 = 1;
IN8 = 0;
}
void Car_Back()
{
TIM_SetCompare3(TIM3,370);
TIM_SetCompare2(TIM5,530);
TIM_SetCompare3(TIM4,370);
TIM_SetCompare4(TIM4,400);
IN1 = 0;
IN2 = 1;
IN3 = 0;
IN4 = 1;
IN5 = 0;
IN6 = 1;
IN7 = 0;
IN8 = 1;
}
void Car_Turn_Right()
{
TIM_SetCompare3(TIM3,370);
TIM_SetCompare2(TIM5,350);
TIM_SetCompare3(TIM4,370);
TIM_SetCompare4(TIM4,400);
IN1 = 1;
IN2 = 0;
IN3 = 0;
IN4 = 1;
IN5 = 0;
IN6 = 1;
IN7 = 1;
IN8 = 0;
}
void Car_Turn_Left()
{
TIM_SetCompare3(TIM3,370);
TIM_SetCompare2(TIM5,350);
TIM_SetCompare3(TIM4,370);
TIM_SetCompare4(TIM4,400);
IN1 = 0;
IN2 = 1;
IN3 = 1;
IN4 = 0;
IN5 = 1;
IN6 = 0;
IN7 = 0;
IN8 = 1;
}
void Car_Stop()
{
IN1 = 0;
IN2 = 0;
IN3 = 0;
IN4 = 0;
IN5 = 0;
IN6 = 0;
IN7 = 0;
IN8 = 0;
}
void Car_Right_90_Init()
{
TIM_SetCompare3(TIM3,340);
TIM_SetCompare2(TIM5,390);
TIM_SetCompare3(TIM4,385);
TIM_SetCompare4(TIM4,385);
IN1 = 1;
IN2 = 0;
IN3 = 0;
IN4 = 1;
IN5 = 0;
IN6 = 1;
IN7 = 1;
IN8 = 0;
}
void Car_Left_90_Init()
{
TIM_SetCompare3(TIM3,340);
TIM_SetCompare2(TIM5,390);
TIM_SetCompare3(TIM4,385);
TIM_SetCompare4(TIM4,385);
IN1 = 0;
IN2 = 1;
IN3 = 1;
IN4 = 0;
IN5 = 1;
IN6 = 0;
IN7 = 0;
IN8 = 1;
}
PWM
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM4_PWM_Init(u16 arr,u16 psc);
#endif
#include "pwm.h"
#include "stm32f10x.h"
void TIM4_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //ʹÄܶ¨Ê±Æ÷3ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
// GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3²¿·ÖÖØÓ³Éä TIM3_CH2->PB5
//ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM3 CH2µÄPWMÂö³å²¨ÐÎ GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
//³õʼ»¯TIM3 TIM4
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM4 OC2
//³õʼ»¯TIM3 Channel2 PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß
TIM_OC2Init(TIM5, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM4 OC2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß
TIM_OC3Init(TIM4, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM4 OC3
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß
TIM_OC4Init(TIM4, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM4 OC4
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE); //ʹÄÜTIM3
TIM_Cmd(TIM5, ENABLE);
}
main
#include "moto.h"
#include "delay.h"
#include "pwm.h"
#include "usart.h"
u8 flag;
int main()
{
MOTO_GPIO_Init();
uart_init(38400);
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
TIM4_PWM_Init(899,0);
while(1)
{
if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_6)==0)
{
Car_Turn_Right();//×óת
}
else if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_7)==8)
{
Car_Go();
}
else if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_8)==8)
{
Car_Turn_Left();
}
else
{
Car_Stop();
}
}
}
根据IO口作用进行连线,PWM因车而异,需要自己去调整。
如何还有其他问题请留言,我看到了会帮你一起解决。