智能小车stm32(寻迹测速)

1,小车寻迹

智能小车stm32(寻迹测速)_第1张图片



原理:循迹模块我用的是红外传感器。黑线的检测原理是红外发射管发射光线到路面,红外光遇到白底则被反射,接收管接收到反射光,经施密特触发器整形后输出低电平;当红外光遇到黑线时则被吸收,接收管没有接收到反射光,经施密特触发器整形后输出高电平。

程序

#include "stm32f10x.h"
#include "interface.h"
#include "LCD1602.h"
#include "IRCtrol.h"
#include "motor.h"
#include "uart.h"

//È«¾Ö±äÁ¿¶¨Òå
unsigned int speed_count=0;//Õ¼¿Õ±È¼ÆÊýÆ÷ 50´ÎÒ»ÖÜÆÚ
char front_left_speed_duty=SPEED_DUTY;
char front_right_speed_duty=SPEED_DUTY;
char behind_left_speed_duty=SPEED_DUTY;
char behind_right_speed_duty=SPEED_DUTY;

unsigned char tick_5ms = 0;//5ms¼ÆÊýÆ÷£¬×÷ΪÖ÷º¯ÊýµÄ»ù±¾ÖÜÆÚ
unsigned char tick_1ms = 0;//1ms¼ÆÊýÆ÷£¬×÷Ϊµç»úµÄ»ù±¾¼ÆÊýÆ÷
unsigned char tick_200ms = 0;//Ë¢ÐÂÏÔʾ

char ctrl_comm = COMM_STOP;//¿ØÖÆÖ¸Áî
char ctrl_comm_last = COMM_STOP;//ÉÏÒ»´ÎµÄÖ¸Áî
unsigned char continue_time=0;

//Ñ­¼££¬Í¨¹ýÅжÏÈý¸ö¹âµç¶Ô¹ÜµÄ״̬À´¿ØÖÆС³µÔ˶¯
void SearchRun(void)
{
	//Èý·¶¼¼ì²âµ½
	if(SEARCH_M_IO == BLACK_AREA && SEARCH_L_IO == BLACK_AREA && SEARCH_R_IO == BLACK_AREA)
	{
		ctrl_comm = COMM_UP;
		return;
	}
	
	if(SEARCH_R_IO == BLACK_AREA)//ÓÒ
	{
		ctrl_comm = COMM_RIGHT;
	}
	else if(SEARCH_L_IO == BLACK_AREA)//×ó
	{
		ctrl_comm = COMM_LEFT;
	}
	else if(SEARCH_M_IO == BLACK_AREA)//ÖÐ
	{
		ctrl_comm = COMM_UP;
	}
}


int main(void)
{
	delay_init();
	GPIOCLKInit();
	UserLEDInit();
	LCD1602Init();
	//IRCtrolInit();
	TIM2_Init();
	MotorInit();
	ServoInit();
	
	RedRayInit();
	//USART3Conf(9600);

 while(1)
 {	 
	 		if(tick_5ms >= 5)
		{
			tick_5ms = 0;
			tick_200ms++;
			if(tick_200ms >= 40)
			{
				tick_200ms = 0;
				LEDToggle(LED_PIN);
			}
//			continue_time--;//200ms ÎÞ½ÓÊÕÖ¸Áî¾ÍÍ£³µ
//			if(continue_time == 0)
//			{
//				continue_time = 1;
//				CarStop();
//			}
			//do something
			SearchRun();
			if(ctrl_comm_last != ctrl_comm)//Ö¸Áî·¢Éú±ä»¯
			{
				ctrl_comm_last = ctrl_comm;
				switch(ctrl_comm)
				{
					case COMM_UP:    CarGo();break;
					case COMM_DOWN:  CarBack();break;
					case COMM_LEFT:  CarLeft();break;
					case COMM_RIGHT: CarRight();break;
					case COMM_STOP:  CarStop();break;
					default : break;
				}
				Delayms(10);//·À¶¶
				LCD1602WriteCommand(ctrl_comm);
			}
		}
		
 }
}



2,小车测速

光电码盘

智能小车stm32(寻迹测速)_第2张图片


程序

SpeedCtrol.h

#ifndef __SPEEDCTROL_H_
#define __SPEEDCTROL_H_

#define FOSC 48000000L //¾§ÕñÉèÖã¬Ê¹ÓÃ24M Hz 6TË«ËÙ±¶Ä£Ê½

//Ëٶȷ´À¡IO¿Ú¶¨Òå
//sbit FRONT_LEFT_S_IO =  P1^3; //×óÇ°Ç°½øIO
//sbit FRONT_RIGHT_S_IO =  P1^2; //ÓÒÇ°Ç°½øIO

extern unsigned char front_left_speed;
extern unsigned char front_right_speed;

unsigned char MeasureSpeed(void);
void MeasureInit(void);
#endif


SpeedCtrol.c

#include "SpeedCtrol.h"
#include "interface.h"

//ÂÖ×ÓÖ±¾¶66mm£¬¹âµçÂëÅ̳ÝÊýΪ20£¬ÂÖ×ÓÖܳ¤ 207mm = 20.7cm 
//³ÌÐò²ÉÓÃÅжϸߵ͵çƽ±ä»¯´ÎÊý¼ÆÊý£¬Ò²¾ÍÊÇ˵ÂÖ×ÓתһÖܼÆÊý´ÎÊýΪ40
//Ò»¸ö¼ÆÊý±ä»¯±íʾÂÖ×ÓÅܹýµÄ¾àÀëΪ 20.7/40 = 0.5175cm

unsigned char front_left_speed=0;
unsigned char front_right_speed=0;
unsigned char sum_speed=0;


unsigned char front_left_speed_temp=0;
unsigned char front_right_speed_temp=0;

static unsigned char front_left_io=0;
static unsigned char front_right_io=0;
static unsigned char count_5ms=0;

void MeasureInit(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = FRONT_RIGHT_S_PIN;//ÅäÖÃʹÄÜGPIO¹Ü½Å
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//ÅäÖÃGPIOģʽ,ÊäÈëÉÏÀ­
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//ÅäÖÃGPIO¶Ë¿ÚËÙ¶È
	GPIO_Init(FRONT_RIGHT_S_GPIO , &GPIO_InitStructure); 
	
	GPIO_InitStructure.GPIO_Pin = FRONT_LEFT_S_PIN;//ÅäÖÃʹÄÜGPIO¹Ü½Å
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//ÅäÖÃGPIOģʽ,ÊäÈëÉÏÀ­
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//ÅäÖÃGPIO¶Ë¿ÚËÙ¶È
	GPIO_Init(FRONT_LEFT_S_GPIO , &GPIO_InitStructure); 
}

/*******************************************************************************
* º¯ Êý Ãû £ºMeasureSpeed
* º¯Êý¹¦ÄÜ £ºËٶȲâÁ¿£¬¼ÆËãIO±ä»¯´ÎÊý£¬¸Ãº¯Êý±ØÐëÿ5msµ÷ÓÃÒ»´Î
* Êä    Èë £ºÎÞ
* Êä    ³ö £ºÎÞ
*******************************************************************************/

unsigned char MeasureSpeed(void)
{
	count_5ms++;
	if(FRONT_LEFT_S_IO != front_left_io)//·¢Éúµçƽ±ä»¯
	{
		front_left_speed_temp++;
		front_left_io = FRONT_LEFT_S_IO;
	}
	
	if(FRONT_RIGHT_S_IO != front_right_io)//·¢Éúµçƽ±ä»¯
	{
		front_right_speed_temp++;
		front_right_io = FRONT_RIGHT_S_IO;
	}
	
	if(count_5ms == 100)//ÿ500ms»ñÈ¡Ò»´ÎËÙ¶È
	{
		count_5ms = 0;
		front_left_speed = front_left_speed_temp *2;//»ñÈ¡1sµÄ¸ßµÍµçƽ±ä»¯´ÎÊý
		front_right_speed = front_right_speed_temp*2;
		front_left_speed_temp = 0;
		front_right_speed_temp = 0;
		
		front_left_speed = (unsigned char)(0.5175 * (double)front_left_speed + 0.5);//¼ÆËãËÙ¶È cm/s ËÄÉáÎåÈë
		front_right_speed = (unsigned char)(0.5175 * (double)front_right_speed + 0.5);//¼ÆËãËÙ¶È cm/s ËÄÉáÎåÈë
		sum_speed=(front_left_speed+front_right_speed)/2;
	}
	return sum_speed;
}

/*******************************************************************************
* º¯ Êý Ãû £ºClearMeasure
* º¯Êý¹¦ÄÜ £ºÇå³ý²âÁ¿£¬ÖØпªÊ¼²âÁ¿
* Êä    Èë £ºÎÞ
* Êä    ³ö £ºÎÞ
*******************************************************************************/
void ClearMeasure(void)
{
	count_5ms = 0;
	front_left_speed_temp = 0;
	front_right_speed_temp = 0;
}




智能小车stm32(寻迹测速)_第3张图片智能小车stm32(寻迹测速)_第4张图片

你可能感兴趣的:(单片机项目,STM32)