1, the car to find traces
Principle: The Tracking module I used is an infrared sensor. The black line's detection principle is that the infrared emitter tubes emit light to the pavement, the infrared light encounters the white bottom to be reflected, the receiving tube receives the reflected light, the output low level after the Schmitt trigger is shaped, and when the infrared light encounters the black line, the receiving tube does not receive the reflected light, and the output high level after the Schmitt trigger is shaped.
Program
#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¼æêýæ÷£¬x÷îªö÷º¯êýµä»ù±¾öüæúunsigned char tick_1ms = 0;//1ms¼æêýæ÷£¬x÷ç»úµä»ù± ¾¼æêýæ÷unsigned char tick_200ms = 0;//ë¢ðâïôê¾char Ctrl_comm = Comm_stop;//¿øöæö¸áîchar Ctrl_comm_last = COMM_STOP;//É
ïò»´îµäö¸áîunsigned Char continue_time=0; Ѽ££¬í¨¹ýåð¶ïèý¸ö¹âµç¶ô¹üµäx´ì¬à´¿øöæ𡳵ô붯void Searchrun (void) {//èýâ ¶¼¼ì²âµ½if (search_m_io = = BLACK_AREA &
amp;& 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)//xó{Ctrl_comm = Comm_leItt
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 >=) {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 ();
Case Comm_down:carback ();
Case Comm_left:carleft ();
Case Comm_right:carright ();
Case Comm_stop:carstop ();
Default:break; } delayms (10);//
À¶¶lcd1602writecommand (Ctrl_comm);
}
}
}
}
2, car speed
Optical Code Plate
Program
SpeedCtrol.h
#ifndef __speedctrol_h_
#define __SPEEDCTROL_H_
#define FOSC 48000000L//¾§õñéèö㣬ê¹óã24m Hz 6të«ëù±¶ä£ê½
//ëù¶è ´à¡io¿ú¶¨òå
//sbit front_left_s_io = p1^3;//xóç°ç°½øio//sbit
= Front_right_s_io ; Óòç°ç°½ø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"//âöxóö±¾¶66mm£¬¹âµçâëåì³ýêýîª20£¬âöxóöü³¤207mm = 20.7cm//³ìðò²éóãåð¶
ï¸ßµíµç潱仯´îêý¼æêý£¬ò²¾íêçëµâöxóxªò»öü¼æêý´îêýîª40//Ò»¸Ö¼ÆÊݱĻ¯±ÍʾÂÖXÓÅܹÝΜľÀÀËΪ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;
}