📄 pwmcar.h
字号:
#define RUNTIME 5000//停车时间
#define RUN_STR_L_HLP 40//直走 左占空比
#define RUN_STR_R_HLP 15//直走右占空比
#define TURN_L_L_HLP 5 //左转 左占空比
#define TURN_L_R_HLP 70//左转 右占空比
#define TURN_R_L_HLP 80//右转 左占空比
#define TURN_R_R_HLP 5 //右转 右占空比
#define TURN1_15_TIME 50 //转弯方式1 转15度的时间
#define TURN2_15_TIME 50 //转弯方式2 转15度的时间
sbit pwml0=P1^1;//左轮驱动
sbit pwml1=P1^0;
sbit pwmr0=P1^3;//右轮驱动
sbit pwmr1=P1^2;
uchar l_hlp,r_hlp;//左右轮占空比
bit l_dire,r_dire;//左右轮方向标志
sbit zhail=P3^5;//红外障碍物检测
sbit zhair=P3^4;
sbit blackl=P1^6;//黑线检测
sbit blackr=P1^7;
bit back;
#define B_ON 0
#define B_OFF 1
sbit speedl=P1^4;//车速检测
sbit speedr=P1^5;
#define MIN_DIS 17
int left_sss=0,right_sss=0;
uchar guang[3],gi,gi_min;//光源检测
uchar min_ddd;
uchar direct,direct0;
#define ss 1
#define ll 2
#define rr 3
#define tl 4
#define bs 5
#define bl 6
#define br 7
#define tr 8
bit stop;
bit this_time,make_ac;
bit zhl,zhr,blal,blar,spel,sper;
bit state_zhai,state_guang,state_t,state_speed;//检测与决策状态标志
bit far_bla,with_bla;
//----------------------------------计时器一些参数
#define Time_Unit 180
bit t0f=0;
int2char pwm_dtime;//计数器初值
uchar num_unit=0;//计数单元次数
uint num100unit=0;//100个计数单元的次数
void Final_Test();
void Start_Car(uchar lp, uchar rp,bit ld,bit rd)//小车启动,
//4个参数为左占空比,右占空比,左右方向
{
l_hlp=lp;
r_hlp=rp;
l_dire=ld;
r_dire=rd;
TR0=0;
TMOD=0x11;
TH0=0xff;
TL0=0xe0;
TR0=1;
IE=0x02;
EA=1;
num_unit=0;
pwml0=0;
pwml1=0;
pwmr0=0;
pwmr1=0;
}
void Stop_Car()//停车,关中断
{
stop=1;
pwml0=0;
pwml1=0;
pwmr0=0;
pwmr1=0;
l_dire=(!l_dire);
r_dire=(!r_dire);
l_hlp=6;
r_hlp=6;
}
void Int_T0()interrupt 1 using 2 //ie0x02//TMOD=0x11;
{
EA=0;
TR0=0;
pwm_dtime.time=0xffff-Time_Unit;
TH0=pwm_dtime.hl[0];
TL0=pwm_dtime.hl[1];//定时us
TR0=1;//开定时器0
t0f=1;//标志置1
if(num_unit==0)//置高电平
{
if(l_dire)pwml0=1;
else pwml1=1;
if(r_dire)pwmr0=1;
else pwmr1=1;
}
if(num_unit==l_hlp)//左轮置低
{
if(l_dire)pwml0=0;
else pwml1=0;
}
if(num_unit==r_hlp)//右轮置低
{
if(r_dire)pwmr0=0;
else pwmr1=0;
}
num_unit++;
if(num_unit==100)
{
num_unit=0;
num100unit++;
}
EA=1;
}
//============================== 传感器检测程序===================
void Bla_Test()//黑线检测
{
blackl=1;
blackr=1;
blal=blackl;
blar=blackr;
}
void S_Test()//位移检测
{
speedl=1;
speedr=1;
if(speedl!=spel)
{
if(l_dire)left_sss++;
else left_sss--;
spel=speedl;
}
if(speedr!=sper)
{
if(r_dire)right_sss++;
else right_sss--;
sper=speedr;
}
}
void Zhai_Test()//障碍物检测
{
zhail=1;
zhair=1;
zhl=zhail;
zhr=zhair;
}
void Guang_Test()//光源检测
{
guang[gi]=AD_Read(gi+1);
if(guang[gi]<guang[gi_min])gi_min=gi;
gi++;
if(gi==3)
{
gi=0;
}
AD_Start(gi+1);
}
//=================================检测结果的判决程序=====
//判决输出为小车的direct ,l_hlp,r_hlp,和min_ddd
void Gowith_Bla()//沿黑线行驶
{
if((blal==B_OFF)&&(blar==B_OFF))//左右都不在黑线上,按原方向返回
{
this_time=1;
if(back)
{
return;
}
back=1;
direct=direct+4;
if(direct>8)direct-=8;
}
else if(blal==B_OFF)//左边不在黑线,右拐
{
direct=rr;
this_time=1;
back=0;
}
else if(blar==B_OFF)//右边不在黑线,左拐
{
direct=ll;
this_time=1;
back=0;
}
}
void Away_Bla()//避免走到黑线
{
if((blal==B_ON)&&(blar==B_ON))//左右都在黑线上,按原方向返回
{
this_time=1;
if(back)
{
return;
}
back=1;
direct=direct+4;
if(direct>8)direct-=8;
}
else if(blal==B_ON)//左边在黑线上,反左
{
direct=bl;
this_time=1;
back=0;
}
else if(blar==B_ON)//右边在黑线上,反右
{
direct=br;
this_time=1;
back=0;
}
}
void Away_Zhai()//躲避障碍物
{
if((zhl==1)&&(zhr==1))//左右都有障碍物,按原方向返回
{
direct=direct+4;
if(direct>8)direct-=8;
this_time=1;
}
else if(zhl==1)//左边有障碍物,左反
{
direct=bl;
this_time=1;
}
else if(zhr==1)//右边有障碍,右反
{
direct=br;
this_time=1;
}
}
void Go_Guang()
{
if(gi_min==0)//左边光强,左拐
{
direct=ll;
this_time=1;
}
else if(gi_min==2)//右边光强,右拐
{
direct=rr;
this_time=1;
}
//else direct=ss;//中间光强,直走
}
//--------------------------------------应用层程序--------------------
//===========================小车转向程序=======================
void Run_Str(bit f)//直走,f 1表示向前,0向后
{
uint time;
time=num100unit;
Start_Car(RUN_STR_L_HLP,RUN_STR_R_HLP,f,f);
while(1)
{
if(t0f)
{
Final_Test();
}
if(num100unit==(time+min_ddd))return;
}
}
//--------------------------------------转弯方式1,左右轮同向--------
void Turn_L(bit f)//左转,f 1表示向前,0向后
{
uint time;
time=num100unit;
Start_Car(TURN_L_L_HLP,TURN_L_R_HLP,f,f);
while(1)
{
if(t0f)
{
Final_Test();
}
if(num100unit==(time+min_ddd))return;
}
}
void Turn_R(bit f)//右转,f 1表示向前,0向后
{
uint time;
time=num100unit;
Start_Car(TURN_R_L_HLP,TURN_R_R_HLP,f,f);
while(1)
{
if(t0f)
{
Final_Test();
}
if(num100unit==(time+min_ddd))return;
}
}
//-------------------------------------转弯方式2,左右轮异向--------
void Turn(bit f)// f 0左转 1右转
{
uint time;
time=num100unit;
Start_Car(50,50,f,!f);
while(1)
{
if(t0f)
{
Final_Test();
}
if(num100unit==(time+min_ddd))return;
}
}
//=============================小车检测,判决程序==================
void Final_Test()//综合检测,但不包括光源检测,因为检测时间间隔不一样
{
Bla_Test();
Zhai_Test();
S_Test();
if(state_guang)Guang_Test();
}
void Final_Ac()//综合判决
{
direct0=direct;
if(with_bla)Gowith_Bla();
if(state_guang)Go_Guang();
if(state_zhai)Away_Zhai();
if(far_bla)Away_Bla();
if(this_time)
{
this_time=0;
}
else //这次没有做出方向判断,直走
{
direct=1;
}
if(direct!=direct0)
{
make_ac=1;
Stop_Car();
stop=0;
}
}
void Action()
{
if(stop)return;
if(make_ac)
{
switch (direct)
{
case 1 : Run_Str(0);break;
case 2 : Turn_L(0);break;
case 3 : Turn_R(0);break;
case 4 : Turn(0);break;
case 5 : Run_Str(1);break;
case 6 : Turn_L(1);break;
case 7 : Turn_R(1);break;
case 8 : Turn(1);break;
default : ;
}
make_ac=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -