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

📄 communicate.cpp

📁 本程序是2005年参加中国机器人大赛的比赛程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//新的串口
#include "stdafx.h"
#include "Robocup.h"
#include "Object.h"
#include "afx.h"
#include "robocupdoc.h"
#include "robocupview.h"
#include "debugdlg.h"
#include "Global.h"
#include "object.h"
#include "memory.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern DWORD timestart;
void Player::Set_m_Angle( double angle )
{//调整角度在-180到180之间 
	
	if(angle>0 )
		angle= ((int)angle%360<180) ? (int)angle%360 : (int)angle%360-360;
	if(angle<0 )
		angle=(int)(-angle)%360<180 ? (-(int)(-angle)%360) : 360-((int)(-angle)%360);
	m_Angle = angle; 
}

double Player::DistFromPointToLine(Vector point,Vector pt1, Vector pt2)
{
	double temp = (pt2.x - pt1.x);
	double A, B, C;
	double dist;

	if (fabs(temp) < 0.001) 
	{
		A = 1;
		B = 0;
	}
	else 
	{
		double m = (pt2.y - pt1.y) / temp;
		A = -m;
		B = 1;
	}  
	C = -(A * pt2.x + B * pt2.y);

	dist = fabs((A*point.x+B*point.y+C)/sqrt(A*A+B*B));

	return dist;
}

int Player::InitComm(int Comm_num, int baud)
{
	CString strCommNum;

	if(1 == Comm_num)
		strCommNum = "COM1";
	else
		strCommNum = "COM2";

	m_hComm = CreateFile( strCommNum,  
						  GENERIC_READ | GENERIC_WRITE, 
						  0, 
						  0, 
						  OPEN_EXISTING,
						  FILE_FLAG_OVERLAPPED,
						  0);
	if (m_hComm == INVALID_HANDLE_VALUE)//)
	{
		AfxMessageBox("串口初始化失败");
		return -1;
	}

	// error opening port; abort	
	DCB dcb;

	FillMemory(&dcb, sizeof(dcb), 0);//FileMemory很好用,MemSet
	dcb.DCBlength = sizeof(dcb);
	
	CString str;
	CString str1;

	str1.Format("%d", baud);
	str = str1 + ",n,8,1";
	BOOL b = BuildCommDCB(str, &dcb);

	if (!b) 
	{   
		AfxMessageBox("buildcommdcb failed!");
		return -1;
	}

	if(!SetCommState(m_hComm, &dcb))
	{
		AfxMessageBox("串口初始化失败!");
		return -1;
	}

	return 1;
}

void Player::SendCommandofAll()
{//通讯系统接口

}




BOOL Player::WriteABuffer(char * lpBuf, DWORD dwToWrite)// 把从lpBbuf开始的dwToWrite个字节写入h_mComm
{
	OVERLAPPED osWrite = {0};
	DWORD dwWritten;
	BOOL fRes;
//	FILE *pFile;
//	pFile=fopen("writebuffer.txt","a+");
//	DWORD t;
//	t=GetTickCount();
//	fprintf(pFile,"\n\n");
	// Create this writes OVERLAPPED structure hEvent.
	osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);// 第二个参数
	if (osWrite.hEvent == NULL)
	{
	// Error creating overlapped event handle.
		
		return FALSE;
	}
	// Issue write.
	if (!WriteFile(m_hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) //dwWritten????
	{
		if (GetLastError() != ERROR_IO_PENDING) //没有调用
		{ 
		// WriteFile failed, but it isn't delayed. Report error and abort.
			fRes = FALSE;
			AfxMessageBox("fail1");
		}
		else
		{
			// Write is pending.
			if (!GetOverlappedResult(m_hComm, &osWrite, &dwWritten, TRUE))//未调用
			{
				fRes = FALSE;
				AfxMessageBox("fail2");
			}
			else
			{	
			//	fprintf(pFile,"writebuffer return true time=%u\n",GetTickCount()-t);
			//	fclose(pFile);// Write operation completed successfully.
				fRes = TRUE;
			}
		}
	}
	else//没有调用过
	// WriteFile completed .
	{
		fRes = TRUE;
	}

	CloseHandle(osWrite.hEvent);

	return fRes;
}


void Player::SendSingleCommand(unsigned char command1,unsigned char command2,unsigned char command3)
{
	TCHAR FrameHead1;
	TCHAR FrameHead2;
	TCHAR FrameHead3;
	FrameHead1=(TCHAR)0x55;
	FrameHead2=(TCHAR)0xff;
	FrameHead3 = (TCHAR)0x41;
	
	WriteABuffer(&FrameHead1, 1);
	WriteABuffer(&FrameHead1, 1);
	WriteABuffer(&FrameHead1, 1);
	WriteABuffer(&FrameHead1, 1);

	WriteABuffer(&FrameHead2, 1);
	WriteABuffer(&FrameHead2, 1);

	WriteABuffer(&FrameHead3, 1);	
	WriteABuffer(&FrameHead3, 1);

	WriteABuffer((TCHAR*)&command1, 1);
	WriteABuffer((TCHAR*)&command2, 1);//命令字
	WriteABuffer((TCHAR*)&command3, 1);

	WriteABuffer(&FrameHead2, 1);
	WriteABuffer(&FrameHead2, 1);

}

void Player::SendCommand(DWORD time)
{	unsigned char command1,command2=0,command3=0;//command1为小车编号,command2和command3为左右轮必须初始化为0;
	unsigned char temp=0;
	//command1

	if(time>=1 && time<10)
	{
		time=10;
	}
	if(time>310)
	{
		time=310;
	}
	unsigned int time_code;
	unsigned int num_code;
	time_code=(time+4)/10;
	num_code=(this->Get_Num()==5) ? 0 :(this->Get_Num());
	if ( num_code >=6  )
	{
		MessageBox( NULL, "Player's Number is 1--5!", NULL, NULL );
		return;
	}	

	command1=num_code<<5 | time_code ;

	//command2&&command3
	for(unsigned char j=0;j<=1;j++)//j=0为右轮,j=1为左轮
	{
//		unsigned char wheel;
//		wheel=j<<7;
//		temp=wheel;//第8位左右轮
		if(j==0)
		{
		
			(*this).Get_vR()<0 ? command3=command3|0x80 : command3=command3 & 0x7f;
		}
		else //j==1
		{
			(*this).Get_vL()<0 ? command2=command2|0x80 : command2=command2 & 0x7f;
		}   

/*		int m_floor=0;//0-127
		if(j==0)//分别设置速度,低6位
		{
			m_floor=(int)floor((fabs((*this).Get_vR())/(MAX_VELOCITY/256.0))-0.5);//0-255
			if(m_floor>=128)
			m_floor=m_floor+(int)(diff[m_floor-128].delta_R);                    
			m_floor=m_floor-128;
			if(m_floor>=128) m_floor=127;
			if(m_floor<0) m_floor=0;
			
			command2=command2 | m_floor;
		}
		else//j==1
		{
			m_floor=(int)floor((fabs((*this).Get_vL())/(MAX_VELOCITY/256.0))-0.5);//0-255
			if(m_floor>=128)
			m_floor=m_floor+(int)(diff[m_floor-128].delta_L);
			m_floor=m_floor-128;
			if(m_floor>=128) m_floor=127;
			if(m_floor<0) m_floor=0;
			command3=command3 | m_floor;
		}
		
	}
*/
	}	 
	command2=command2 | int((*this).Get_vL());
	command3=command3 | int((*this).Get_vR());
	
//	CString str;
//	str.Format("command2=%d,command3=%d",command2,command3);
//	AfxMessageBox(str);
	SendSingleCommand(command1,command2,command3);
}

void Player::BasicAct(double R, BOOL bIsRightHand, BOOL bIsLine, double V, DWORD time)
{
	//R为转弯半径(只为正),bIsRightHand为是否满足右手定则,
 //bIsLine为R是否为无穷大, V为速度(可正可负)
	if(fabs(V) < 0.1)//要加绝对值
		V = 0;
	if(bIsLine)//不管R是多少,直线行走
	{//R为无穷大
		Set_vL(V);
		Set_vR(V);
	}

	else
	{
		if(bIsRightHand)//右手定则(即右手画圆),如果v>0向左转,如果v<0后左传
		{
			double vL, vR;
			vR = V;
			vL = (2*R-m_Length)*vR/(2*R+m_Length);
			Set_vL(vL);
			Set_vR(vR);
		//	fprintf(pFile,"vL=%2f,vR=%2f",this->Get_vL(),this->Get_vR());
		}
		else
		{
			double vL, vR;
			vL = V;
			vR = (2*R-m_Length)*vL/(2*R+m_Length);
			Set_vL(vL);
			Set_vR(vR);
		//	fprintf(pFile,"vL=%2f,vR=%2f",this->Get_vL(),this->Get_vR());
		}
	}
	SendCommand(time);
//	SetMMTimer((unsigned)fabs(time),DWORD(this),MMProc_Stop);
}


/*
void Player::BasicAct(double R, BOOL bIsRightHand, BOOL bIsLine, double V)
{//R为转弯半径(只为正),bIsRightHand为是否满足右手定则,
 //bIsLine为R是否为无穷大, V为速度(可正可负)
	if(fabs(V) < 0.1)//要加绝对值
		V = 0;
	
	if(bIsLine)//不管R是多少,直线行走
	{//R为无穷大
		Set_vL(V);
		Set_vR(V);
	}
	else
	{
		if(bIsRightHand)//右手定则(即右手画圆),如果v>0向左转,如果v<0后左传
		{
			double vL, vR;
			vR = V;
			vL = (2*R-m_Length)*vR/(2*R+m_Length);
			Set_vL(vL);
			Set_vR(vR);
		//	fprintf(pFile,"vL=%2f,vR=%2f",this->Get_vL(),this->Get_vR());
		}
		else
		{
			double vL, vR;
			vL = V;
			vR = (2*R-m_Length)*vL/(2*R+m_Length);
			Set_vL(vL);
			Set_vR(vR);
		//	fprintf(pFile,"vL=%2f,vR=%2f",this->Get_vL(),this->Get_vR());
		}
	}
	SendCommand();
}
*/


void Player::InitMMTimer(UINT accuracy)
{
	TIMECAPS tc;
	if(timeGetDevCaps(&tc,sizeof(TIMECAPS))==TIMERR_NOERROR)
	{
		Player::wAccuracy=min(max(tc.wPeriodMin,accuracy),tc.wPeriodMax);	//调用timeBeginPeriod函数设置定时器的分辨率
		timeBeginPeriod(wAccuracy);	//设置定时器
	}
}


UINT Player::SetMMTimer(UINT nElapse,DWORD dwUser,LPTIMECALLBACK TimerProc,UINT fuEvent,UINT wAccuracy)
{
	return timeSetEvent(nElapse,wAccuracy,(LPTIMECALLBACK)TimerProc,(DWORD)dwUser,fuEvent);
//	CString str;
//	str.Format("set timer of %u eElapse=%u",timerid,nElapse);
//	AfxMessageBox(str);

}
/*
void Player::KillMMTimer(UINT wTimerID)
{
	if(timeKillEvent(wTimerID)==MMSYSERR_INVALPARAM )
	{
		CString str;
		str.Format("timer %u doeson't exist",wTimerID);
	}
//	AfxMessageBox(str);
	
}
*/
/*
void Player::IsMeBeyondTheField(BeyondInfo & Info, CRect para)
{	
	int l,r,t,b;
	l=Player::FieldStruct.m_Field.left;
	r=Player::FieldStruct.m_Field.right;
	t=Player::FieldStruct.m_Field.top;
	b=Player::FieldStruct.m_Field.bottom;
	CRect centerrect=CRect(l+para.left,t-para.top,r-para.right,b+para.bottom);
	CRect leftrect=CRect(l,t,l+para.left,b);
	CRect rightrect=CRect(r-para.right,t,r,b);
	CRect toprect=CRect(l+para.left,t,r-para.right,t-para.top);
	CRect bottomrect=CRect(l+para.left,b+para.bottom,r-para.right,b);
	int x=(int)(this->m_vPos.x);
	int y=(int)(this->m_vPos.y);
	CPoint pt=CPoint(x,y);
	if(centerrect.PtInRect(pt))
	{
		Info.IsBeyond=FALSE;	
		Info.IsForwardOutSide=FALSE;
		Info.where=center;
	}
	else
	{
		Info.IsBeyond=TRUE;
		if(PtInRect(leftrect,pt))
		{
			Info.where=left;
			Info.IsForwardOutSide=BOOL(fabs(this->Get_m_Angle())>90);
		}
		else if(PtInRect(rightrect,pt))
		{
			Info.where=right;
			Info.IsForwardOutSide=BOOL(fabs(this->Get_m_Angle())<=90);
		}
		else if(PtInRect(toprect,pt))
		{
			Info.where=top;
			Info.IsForwardOutSide=BOOL(fabs(this->Get_m_Angle()-90)<90);
		}
		else if(PtInRect(bottomrect,pt))
		{
			Info.where=bottom;
			Info.IsForwardOutSide=BOOL(fabs(this->Get_m_Angle()+90)<90);
		}
	}
	return;

}

void Player::BoundaryProc(CRect para,UINT time)
{
	BeyondInfo Info;
	IsMeBeyondTheField(Info , para);
	if(Info.IsBeyond=FALSE)
	{
		return;

⌨️ 快捷键说明

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