机器人智能躲开障碍物

来源:互联网 发布:日语翻译软件下载 编辑:程序博客网 时间:2024/05/01 03:32

/***********************************************************
功能:机器人能够智能躲避障碍物
说明:P1_0接左电机,P1_1接右电机
时间:2012.12.15
作者:荆轩道
************************************************************/
#include<BoeBot.h>
#include<uart.h>
#include <intrins.h>

#define LeftIR        P1_5   //左边红外接收连接到P15
#define RightIR       P3_6   //右边红外接收连接到P36
#define LeftLaunch    P1_4   //左边红外发射连接到P14
#define RightLaunch   P3_3   //右边红外发射连接到P33

int IRLaunch(unsigned char IR)
{
 int counter;
 if(IR=='L')
   for(counter=0;counter<38;counter++)//左边发射
    {
     LeftLaunch=1;
     _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
     _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
     LeftLaunch=0;
     _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
     _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
    }
  if(IR=='R')
    for(counter=0;counter<38;counter++)//右边发射
    {
     RightLaunch=1;
     _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
     _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
     RightLaunch=0;
     _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
     _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
    }
}
int main(void)
{
 int  i,pulseCount;
 int  pulseLeft,pulseRight;
 bit  irDetectLeft,irDetectRight;
 uart_Init();
 printf("Program Running!\n");    
 do
 {
   IRLaunch('R');
   irDetectRight = RightIR; //右边接收
   IRLaunch('L');
   irDetectLeft = LeftIR; //左边接收
  if((irDetectLeft==0)&&(irDetectRight==0)) //向前走
  {
   pulseCount=1;
   pulseLeft=1700;
   pulseRight=1300;
  }
  else if((irDetectLeft==1)&&(irDetectRight==0)) //右转
  {
   pulseCount=10;
   pulseLeft=1300;
   pulseRight=1300;
  }
  else if((irDetectLeft==0)&&(irDetectRight==1)) //左转
  {
   pulseCount=10;
   pulseLeft=1700;
   pulseRight=1700;
  }
  else //后退
  {
   pulseCount=15;
   pulseLeft=1300;                       
   pulseRight=1700;
  }
  for(i=0;i<pulseCount;i++)
  {
    P1_1=1;
    delay_nus(pulseLeft);
    P1_1=0;

    P1_0=1;
    delay_nus(pulseRight);
    P1_0=0;
    delay_nms(20);
  }
 }
 while(1);
}

 

原创粉丝点击