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

📄 mrpathplanningview.cpp

📁 一个关于在VC下进行智能移动机器人路径规划与控制的仿真环境建立方法。
💻 CPP
字号:
// MRpathPlanningView.cpp : implementation of the CMRpathPlanningView class
//

#include "stdafx.h"
#include "MRpathPlanning.h"

#include "MRpathPlanningDoc.h"
#include "MRpathPlanningView.h"
#include "Math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMRpathPlanningView

IMPLEMENT_DYNCREATE(CMRpathPlanningView, CView)

BEGIN_MESSAGE_MAP(CMRpathPlanningView, CView)
	//{{AFX_MSG_MAP(CMRpathPlanningView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMRpathPlanningView construction/destruction

CMRpathPlanningView::CMRpathPlanningView()
{
	// TODO: add construction code here

}

CMRpathPlanningView::~CMRpathPlanningView()
{
}

BOOL CMRpathPlanningView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMRpathPlanningView drawing

void CMRpathPlanningView::OnDraw(CDC* pDC)
{
	CMRpathPlanningDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CClientDC dc(this);
	CPen penMR(PS_SOLID,1,RGB(255,0,0));
	dc.SelectObject(&penMR);

	dc.Ellipse(Sx-10,Sy-10,Sx+10,Sy+10);
	dc.Ellipse(Gx-10,Gy-10,Gx+10,Gy+10);

}

/////////////////////////////////////////////////////////////////////////////
// CMRpathPlanningView printing

BOOL CMRpathPlanningView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMRpathPlanningView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMRpathPlanningView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMRpathPlanningView diagnostics

#ifdef _DEBUG
void CMRpathPlanningView::AssertValid() const
{
	CView::AssertValid();
}

void CMRpathPlanningView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMRpathPlanningDoc* CMRpathPlanningView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMRpathPlanningDoc)));
	return (CMRpathPlanningDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMRpathPlanningView message handlers

int CMRpathPlanningView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetTimer(1,100,0);
	Sx=300, Sy=300;
	Gx=100, Gy=100;
	MRu=0, MRw=0;
	
	MRx=Sx;
	MRy=Sy;
	MRa=0;

	return 0;
}

void CMRpathPlanningView::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

void CMRpathPlanningView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(1 == nIDEvent)
	{
		MRx=MRx+MRu*2*cos(MRa*3.1415926/180);
   	    MRy=MRy+MRu*2*sin(MRa*3.1415926/180);
	    MRa=MRa+MRw*2;

        if(MRa>180)
        MRa=MRa-360;
        if(MRa<-180)
        MRa=MRa+360;
		
		DrawMR(MRx,MRy,MRa);

		if(Gx>MRx)
		    PLa=180*atan((Gy-MRy)/(Gx-MRx))/3.1415926;
		else if(Gy>=MRy)
			PLa=180+180*atan((Gy-MRy)/(Gx-MRx))/3.1415926;
		else
			PLa=-180+180*atan((Gy-MRy)/(Gx-MRx))/3.1415926;
		Ea=PLa-MRa;

		MRw=0.1*Ea;
		MRu=0.01*sqrt((Gy-MRy)*(Gy-MRy)+(Gx-MRx)*(Gx-MRx));
	}
	CView::OnTimer(nIDEvent);
}

BOOL CMRpathPlanningView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	//return true;
	return CView::OnEraseBkgnd(pDC);
}

void CMRpathPlanningView::DrawMR(float robotX, float robotY, float robotA)
{
	CClientDC dc(this);
	CPen penMR(PS_SOLID,1,RGB(255,0,0));
	dc.SelectObject(&penMR);

	float Amrx,Amry;
	int i;
	for(i=0;i<180;i+=1)
	{
		Amrx=robotX+100*cos(3.1415926*(robotA+i-90)/180);
		Amry=robotY+100*sin(3.1415926*(robotA+i-90)/180);
		dc.MoveTo(robotX,robotY);
        dc.LineTo(Amrx,Amry);

		dc.Ellipse(robotX-10,robotY-10,robotX+10,robotY+10);
	}
	dc.Ellipse(Gx-10,Gy-10,Gx+10,Gy+10);
}

⌨️ 快捷键说明

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