⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 robot.cpp

📁 吸尘机器人全覆盖算法仿真源码,希望对大家有用
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		for(j=0;(j<40)&&(map[j][i]!=1);j++);
		if(j>0) map[j-1][i]=4;
	}

	for(i=0;i<40;i++){
		for(j=39;(j>=0)&&(map[j][i]!=1);j--);
		map[j+1][i]=4;
	}
	ofstream out("map\\map.txt");
	for(i=0;i<40;i++){
		for(j=0;j<40;j++)
			out<<map[i][j]<<"-";
		out<<endl;
	}
	out<<endl;
	out<<CurrentPointInEM.x<<"____________"<<CurrentPointInEM.y;
	return 1;

}//markmap
//////////////////MoveTo///////////////////////////
int Robot::MoveTo(int x,int y){
	//Move to (x,y) from current point.
	//This method is a simple form of MoveTo method,
	//and is implemented only on condition that 
	//y is equivalent to CurrentPointInEM.y
	if(!bMoveToFlag_start){
		FindIdealpath(CurrentPointInEM.x,CurrentPointInEM.y,x,y);
	//	MessageBox(GetActiveWindow(),"before","",MB_OK);
		PreMove();
	//	MessageBox(GetActiveWindow(),"after","",MB_OK);
		ofstream out("map\\idealpath.txt");
		for(int i=0;i<idealpath.size();i++){
			out<<"x:"<<idealpath[i].x<<","<<"y:"<<idealpath[i].y<<endl;
		}
		bMoveToFlag_start=true;
	}
    int sz=idealpath.size();sz--;
    int result1;int result2;
		
	if(nMoveToFlag_curr<=sz){
		  if(!bMoveToFlag_rotating){
		          if(!DetectA2(nMoveToFlag_curr)) bMoveToFlag_seeObstacle=false;
				  else bMoveToFlag_seeObstacle=true;
                  
		          if(!(result1=MoveTo2(idealpath[nMoveToFlag_curr].x,idealpath[nMoveToFlag_curr].y))){
                            bMoveToFlag_rotating=true;
			                if(!bMoveToFlag_seeObstacle){//unmarked obstacle.
					                   //moving around obstacle clockwise by default
					                   nMoveToFlag_flag=0;
							}else{//marked obstacle.
					        //first of all,evaluating to determine moving clockwise or vice versa.
								if(!bMoveToFlag_evaluated) {
									int flag=Evaluate();
									bMoveToFlag_evaluated=true;
									//flag=(!flag)?1:2;//flag=1,anticlockwise flag=2,clockwise
								    nMoveToFlag_flag=(!flag)?1:2;
								}
							}//else
				  }//if
	           	  else if(result1==3){
					  nMoveToFlag_curr=0;
					  bMoveToFlag_start=false;
					  bMoveToFlag_evaluated=false;
					  bMoveToFlag_rotating=false;
					  bMoveToFlag_temp=false;
					  return 0;
				  }else if(result1==1) nMoveToFlag_curr++;
				  return 2;
		  }else{
			  if(nMoveToFlag_flag==0){
				   if(((nMoveToFlag_curr=CrossingIP())==-1)||!bMoveToFlag_temp){
                                 if(!(result2=Move())) AnticlockwiseTurn();
								 else if(result2==1){
									      bMoveToFlag_temp=true;
									      if(!Detect(NextDirection(1))) ClockwiseTurn();
								 }
				   }else{ bMoveToFlag_temp=false;bMoveToFlag_rotating=false;bMoveToFlag_evaluated=false;}
					                  
			  }else if(nMoveToFlag_flag==1){
				  if(((nMoveToFlag_curr=CrossingIP())==-1)||!bMoveToFlag_temp){
					             if(!(result2=Move())) ClockwiseTurn();
								 else if(result2==1){
									      bMoveToFlag_temp=true;
					                      if(!DetectA3(NextDirectionInEM(3))) AnticlockwiseTurn();
								 }
				  }else{ bMoveToFlag_temp=false;bMoveToFlag_rotating=false;bMoveToFlag_evaluated=false;}
			  }else if(nMoveToFlag_flag==2){
                  if(((nMoveToFlag_curr=CrossingIP())==-1)||!bMoveToFlag_temp){
						          if(!(result2=Move())) AnticlockwiseTurn();
								  else if(result2==1){
									       bMoveToFlag_temp=true;
						                   if(!DetectA3(NextDirectionInEM(1))) ClockwiseTurn();
								  }
				  }else{ bMoveToFlag_temp=false;bMoveToFlag_rotating=false;bMoveToFlag_evaluated=false;}
			  }//else if
              return 2;
		  }//else
	}else{
		nMoveToFlag_curr=0;
		bMoveToFlag_start=false;
		bMoveToFlag_evaluated=false;
		bMoveToFlag_rotating=false;
		bMoveToFlag_temp=false;
		nMoveToFlag_flag=0;//new added
		return 1;
	}
}
///////////////////////////////////////////////////
int Robot::MoveTo2(int x,int y){
	//Move to (x,y)(coordinate in EM).
	//(x,y) must be one of the eight points adjacent to CurrentPointInEM
    if((x>(CurrentPointInEM.x+1))||
	   (x<(CurrentPointInEM.x-1))||
	   (y>(CurrentPointInEM.y+1))||
	   (y<(CurrentPointInEM.y-1))) return 3;
	if((x==CurrentPointInEM.x)&&(y==CurrentPointInEM.y))
	{       nMoveTo2Flag_phase=1;
			nMoveTo2Flag_scheme=0;
			return 1;
	}
	int rtemp;
	if(nMoveTo2Flag_phase==1){
		    
	        if((x<CurrentPointInEM.x)&&(y<CurrentPointInEM.y)){
		            TurnToD3();
		            if(Detect(CurrentDirection)) {
			            TurnToD4();
			            if(Detect(CurrentDirection)) return 0;
			            else {nMoveTo2Flag_scheme=2;nMoveTo2Flag_phase=2;return 2;}
					}
					nMoveTo2Flag_phase=2;
					nMoveTo2Flag_scheme=1;
					return 2;
			}//if
	
			if((x<CurrentPointInEM.x)&&(y==CurrentPointInEM.y)){
		             TurnToD4();
		             if(Detect(CurrentDirection)){ return 0;
					   }else{ nMoveTo2Flag_scheme=3;nMoveTo2Flag_phase=2;return 2;}
			}//if
	        
			if((x<CurrentPointInEM.x)&&(y>CurrentPointInEM.y)){
		             TurnToD1();
		             if(Detect(CurrentDirection)) {
                            TurnToD4();
							if(Detect(CurrentDirection)) return 0;
							else{nMoveTo2Flag_scheme=5;nMoveTo2Flag_phase=2;return 2;}
					 }
					 nMoveTo2Flag_scheme=4;
					 nMoveTo2Flag_phase=2;
					 return 2;
			}
	        
            if((x==CurrentPointInEM.x)&&(y>CurrentPointInEM.y)){
		             TurnToD1();
					 if(Detect(CurrentDirection)) return 0;
					 else{ nMoveTo2Flag_scheme=6;nMoveTo2Flag_phase=2;return 2;}
			}
	
			if((x>CurrentPointInEM.x)&&(y>CurrentPointInEM.y)){
                     TurnToD1();
		             if(Detect(CurrentDirection)){
			                 TurnToD2();
							 if(Detect(CurrentDirection)) return 0;
							 else{nMoveTo2Flag_scheme=8;nMoveTo2Flag_phase=2;return 2;}
					 }
					 nMoveTo2Flag_scheme=7;
					 nMoveTo2Flag_phase=2;
				     return 2;
			}
	        if((x>CurrentPointInEM.x)&&(y==CurrentPointInEM.y)){
		             TurnToD2();
					 if(Detect(CurrentDirection)) return 0;
					 else{ nMoveTo2Flag_scheme=9;nMoveTo2Flag_phase=2;return 2;}
			}
	        if((x>CurrentPointInEM.x)&&(y<CurrentPointInEM.y)){
		             TurnToD3();
					 if(Detect(CurrentDirection)){
			                   TurnToD2();
							   if(Detect(CurrentDirection)) return 0;
							   else{nMoveTo2Flag_scheme=11;nMoveTo2Flag_phase=2;return 2;}
					 }
					 nMoveTo2Flag_scheme=10;
					 nMoveTo2Flag_phase=2;
		             return 2;
			}
	        if((x==CurrentPointInEM.x)&&(y<CurrentPointInEM.y)){
		            TurnToD3();
					if(Detect(CurrentDirection)) return 0;
					else{ nMoveTo2Flag_scheme=12;nMoveTo2Flag_phase=2;return 2;}
			}
			
	}else if(nMoveTo2Flag_phase==2){
		switch(nMoveTo2Flag_scheme){
		case 1:
			if(Move()==1){
				TurnToD2();
				if(Detect(CurrentDirection)){
                   nMoveTo2Flag_phase=1;
				   nMoveTo2Flag_scheme=0;
				   return 0;
				}else{
					nMoveTo2Flag_phase=3;
					return 2;
				}
			}
			break;
		case 2:
			if(Move()==1){
				TurnToD3();
				if(Detect(CurrentDirection)){
                   nMoveTo2Flag_phase=1;
				   nMoveTo2Flag_scheme=0;
				   return 0;
				}else{
					nMoveTo2Flag_phase=3;
					return 2;
				}
			}
			break;
		case 3:
            if(Move()==1){
				nMoveTo2Flag_phase=1;
				nMoveTo2Flag_scheme=0;
				return 1;
			}
			break;
		case 4:
			if(Move()==1){
				TurnToD4();
				if(Detect(CurrentDirection)){
                   nMoveTo2Flag_phase=1;
				   nMoveTo2Flag_scheme=0;
				   return 0;
				}else{
					nMoveTo2Flag_phase=3;
					return 2;
				}
			}
			break;
		case 5:
			if(Move()==1){
				TurnToD1();
				if(Detect(CurrentDirection)){
                   nMoveTo2Flag_phase=1;
				   nMoveTo2Flag_scheme=0;
				   return 0;
				}else{
					nMoveTo2Flag_phase=3;
					return 2;
				}
			}
			break;
		case 6:
			if(Move()==1){
				nMoveTo2Flag_phase=1;
				nMoveTo2Flag_scheme=0;
				return 1;
			}
			break;
		case 7:
			if(Move()==1){
				TurnToD2();
			    if(Detect(CurrentDirection)){
                      nMoveTo2Flag_phase=1;
				      nMoveTo2Flag_scheme=0;
				      return 0;
				}else{
					nMoveTo2Flag_phase=3;
					return 2;
				}
			}
			break;
		case 8:
			if(Move()==1){
				TurnToD1();
				if(Detect(CurrentDirection)){
                   nMoveTo2Flag_phase=1;
				   nMoveTo2Flag_scheme=0;
				   return 0;
				}else{
					nMoveTo2Flag_phase=3;
					return 2;
				}
			}
			break;
		case 9:
			if(Move()==1){
				nMoveTo2Flag_phase=1;
				nMoveTo2Flag_scheme=0;
				return 1;
			}
			break;
		case 10:
			if(Move()==1){
				TurnToD2();
				if(Detect(CurrentDirection)){
                   nMoveTo2Flag_phase=1;
				   nMoveTo2Flag_scheme=0;
				   return 0;
				}else{
					nMoveTo2Flag_phase=3;
					return 2;
				}
			}
			break;
		case 11:
			if(Move()==1){
				TurnToD3();
				if(Detect(CurrentDirection)){
                   nMoveTo2Flag_phase=1;
				   nMoveTo2Flag_scheme=0;
				   return 0;
				}else{
					nMoveTo2Flag_phase=3;
					return 2;
				}
			}
			break;
		case 12:
			if(Move()==1){
				nMoveTo2Flag_phase=1;
				nMoveTo2Flag_scheme=0;
				return 1;
			}
			break;
		default:
			nMoveTo2Flag_phase=1;
			nMoveTo2Flag_scheme=0;
			return 3;
		}
    }else if(nMoveTo2Flag_phase=3){
		switch(nMoveTo2Flag_scheme){
		case 1:
		case 2:
		case 4:
		case 5:
		case 7:
		case 8:
		case 10:
		case 11:
			if(Move()==1){
				nMoveTo2Flag_phase=1;
				nMoveTo2Flag_scheme=0;
				return 1;
			}
			break;
		default:
			nMoveTo2Flag_phase=1;
			nMoveTo2Flag_scheme=0;
			return 3;
		}
	}
	return 2;
}
//////////////////ToAndFro//////////////////////////
int Robot::ToAndFroA(){
	//Advanced version of method which control robot 
	//moving to and fro and avoiding obstacles automatically.
	int i;

	if(nTFAFlag_j<21){//cloume
		switch(nTFAFlag_phase){
		case 1:
		    if(bTFAFlag_to){
			   for(i=39;(i>0)&&(map[i][nTFAFlag_j]!=2);i--);
			}else if(bTFAFlag_fro){
			   for(i=0;(i<39)&&(map[i][nTFAFlag_j]!=2);i++);
			}//else
		    if(1==MoveTo(i,nTFAFlag_j)){
			    nTFAFlag_phase=2;
				ClearPath();
			}//move to start point
		    break;
		case 2:
		    if(bTFAFlag_to){
			   for(i=0;(i<39)&&(map[i][nTFAFlag_j]!=2);i++);
			}else if(bTFAFlag_fro){
			   for(i=39;(i>0)&&(map[i][nTFAFlag_j]!=2);i--);
			}//else 
			if(1==MoveTo(i,nTFAFlag_j)){
				nTFAFlag_phase=3;
				ClearPath();
			}//move to end point
		    break;
		case 3:
		    if(bTFAFlag_to){bTFAFlag_to=false;bTFAFlag_fro=true;
			}else{bTFAFlag_to=true;bTFAFlag_fro=false;}
			nTFAFlag_j++;
			nTFAFlag_phase=1;
			if(nTFAFlag_j==21){
				nTFAFlag_j=1;
				bTFAFlag_to=true;
				bTFAFlag_fro=false;
			    return 1;
			}
			
		}//switch
	}//if
	return 2;
}
/////////////////Evalute///////////////////////////
int Robot::Evaluate(){
	int cstep=0;int astep=0;bool start=false;bool exit=false;
    int xtemp=CurrentPointInEM.x;int ytemp=CurrentPointInEM.y;
	int result=0;
	for(int j=0;j<idealpath.size();j++)
		if((idealpath[j].x==xtemp)&&(idealpath[j].y==ytemp)) result=j;
	int direction=CurrentDirectionInEM;int rtemp;
	while((!exit)){
		for(int i=0;i<idealpath.size();i++){
			if((xtemp==idealpath[i].x)&&(ytemp==idealpath[i].y)&&start){
				if((cstep>1)&&(i>result)){
					if((map[idealpath[i-1].x][idealpath[i-1].y]==1)) exit=true;
				}//if
			}//if
		}
		if(!exit){
		   if((rtemp=VDetect(&xtemp,&ytemp,direction))==1) {
			   direction=(direction+3)%4;
		       direction=(direction==0)?4:direction;}
		   else if(rtemp==2) {
			   if(!start){
				   direction=(direction+3)%4;
				   direction=(direction==0)?4:direction;
				   if(VDetect(&xtemp,&ytemp,direction)==2){
                    cstep=-1;exit=true;
                   }else{
					   VMove(&xtemp,&ytemp,direction);cstep++;start=true;}
			   }else{cstep=-1;exit=true;}
		   }else {VMove(&xtemp,&ytemp,direction);cstep++;start=true;}
		   if(!VDetect(&xtemp,&ytemp,(direction+1)%4)){
			   direction=(direction+1)%4;
		       direction=(direction==0)?4:direction;}
		  }	//if
		}//while
    start=false;exit=false;direction=CurrentDirectionInEM;
	xtemp=CurrentPointInEM.x;ytemp=CurrentPointInEM.y;
	while((!exit)){
		for(int i=0;i<idealpath.size();i++){
			if((xtemp==idealpath[i].x)&&(ytemp==idealpath[i].y)&&start){
				if((astep>1)&&(i>result))
					if((map[idealpath[i-1].x][idealpath[i-1].y]==1)) exit=true;
			}//if
		}//for
		if(!exit){
		   if((rtemp=VDetect(&xtemp,&ytemp,direction))==1) {
			   direction=(direction+1)%4;
			   direction=(direction==0)?4:direction;}
		   else if(rtemp==2) {
			   if(!start){
				   direction=(direction+1)%4;
				   direction=(direction==0)?4:direction;
				   if(VDetect(&xtemp,&ytemp,direction)==2){
                      astep=-1;exit=true;
                   }else{
					   VMove(&xtemp,&ytemp,direction);astep++;start=true;}
			   }else{astep=-1;exit=true;}
		   }else {VMove(&xtemp,&ytemp,direction);astep++;start=true;}
		   if(!VDetect(&xtemp,&ytemp,(direction+3)%4)){
			   direction=(direction+3)%4;
		       direction=(direction==0)?4:direction;}
		}//if
		
	}//while
    if((cstep==-1)&&(astep==-1)) return 2;
	if((cstep==-1)||((cstep>=astep)&&(astep>=0))) return 0;
	if((astep==-1)||((astep>cstep)&&(cstep>=0))) return 1;

}
bool Robot::VMove(int* x,int* y,int direction){
	switch(direction){
	case 1:
		(*y)++;return true;
	case 2:
        (*x)++;return true;
	case 3:
		(*y)--;
		return true;
	case 4:
		(*x)--;return true;
	default:
		return false;
	}
}
int Robot::VDetect(int *x,int *y,int direction){
	direction=(direction==0)?4:direction;
	switch(direction){
	case 1:
		if(map[*x][(*y)+1]==1){
			if((map[*x][*y+2]==4)||(*y+2)>21) return 2;
			else return 1;
		}else return 0;
	case 2:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -