📄 zhangzhiguangview.cpp
字号:
// zhangzhiguangView.cpp : implementation of the CZhangzhiguangView class
//
#include "stdafx.h"
#include "zhangzhiguang.h"
#include "zhangzhiguangDoc.h"
#include "zhangzhiguangView.h"
#include "math.h"
#include "iostream.h"
/*
#include "dos.h"
#include "conio.h"
#include "graphics.h" //窃以为delay函数通不过是由于头函数的原因,可惜不是
*/
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CZhangzhiguangView
IMPLEMENT_DYNCREATE(CZhangzhiguangView, CFormView)
BEGIN_MESSAGE_MAP(CZhangzhiguangView, CFormView)
//{{AFX_MSG_MAP(CZhangzhiguangView)
ON_WM_MOUSEMOVE()
ON_BN_CLICKED(IDC_RADIO1, OnRengongshichang)
ON_BN_CLICKED(IDC_RADIO2, OnShenjingwangluo)
ON_BN_CLICKED(IDC_BEGINTOCOMPUTE, OnBegintocompute)
ON_BN_CLICKED(IDC_STOPCOMPUTING, OnStopcomputing)
ON_WM_TIMER()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CZhangzhiguangView construction/destruction
CZhangzhiguangView::CZhangzhiguangView()
: CFormView(CZhangzhiguangView::IDD)
{
//{{AFX_DATA_INIT(CZhangzhiguangView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
which_method=0;
/* int temp1=25;
int temp2=475;
int i=0;
route_x=0;
route_y=0; */
// GoalGain=1.0;
GoalGain=1;
ObstacleGain=5e7;//需要初步计算后进行估计,而且改变其值可是影响路径的振荡程度。
fGoalForce_x=0.0;
fGoalForce_y=0.0;
Mid_Pos_x=Orig_Pos_x;
Mid_Pos_y=Orig_Pos_y;
// Obstacle = new Obstacle_Coor_R[3];
// Obstacle={{130,130,40,0.0,0.0},{260,260,40,0.0,0.0},{400,360,30,0.0,0.0}};
// struct Obstacle_Coor_R Obstacle[3]={{130,130,40,0.0,0.0},{260,260,40,0.0,0.0},{400,360,30,0.0,0.0}};
//如果采取上面初始化的方式,则不能够使得参数正常传递;
//然而,采取下面的方式则过于繁琐,还没有弄到一种比较简便易行的方式。
Obstacle[0].Obstacle_x=130;
Obstacle[0].Obstacle_y=130;
Obstacle[0].Obstacle_R=40;
Obstacle[0].Force_Obs_x=0.0;
Obstacle[0].Force_Obs_y=0.0;
Obstacle[1].Obstacle_x=260;
Obstacle[1].Obstacle_y=260;
Obstacle[1].Obstacle_R=40;
Obstacle[1].Force_Obs_x=0.0;
Obstacle[1].Force_Obs_y=0.0;
Obstacle[2].Obstacle_x=400;
Obstacle[2].Obstacle_y=360;
Obstacle[2].Obstacle_R=30;
Obstacle[2].Force_Obs_x=0.0;
Obstacle[2].Force_Obs_y=0.0;
Obstacle[3].Obstacle_x=320;
Obstacle[3].Obstacle_y=110;
Obstacle[3].Obstacle_R=35;
Obstacle[3].Force_Obs_x=0.0;
Obstacle[3].Force_Obs_y=0.0;
Obstacle[4].Obstacle_x=430;
Obstacle[4].Obstacle_y=190;
Obstacle[4].Obstacle_R=30;
Obstacle[4].Force_Obs_x=0.0;
Obstacle[4].Force_Obs_y=0.0;
}
CZhangzhiguangView::~CZhangzhiguangView()
{
}
void CZhangzhiguangView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CZhangzhiguangView)
DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
DDX_Control(pDX, IDC_Y, m_Y);
DDX_Control(pDX, IDC_X, m_X);
//}}AFX_DATA_MAP
}
BOOL CZhangzhiguangView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CZhangzhiguangView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// CZhangzhiguangView printing
BOOL CZhangzhiguangView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CZhangzhiguangView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CZhangzhiguangView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CZhangzhiguangView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CZhangzhiguangView diagnostics
#ifdef _DEBUG
void CZhangzhiguangView::AssertValid() const
{
CFormView::AssertValid();
}
void CZhangzhiguangView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CZhangzhiguangDoc* CZhangzhiguangView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CZhangzhiguangDoc)));
return (CZhangzhiguangDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CZhangzhiguangView message handlers
void CZhangzhiguangView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CFormView::OnMouseMove(nFlags, point);
CString str;
CDC *pDC;
pDC=GetDC();
// CRect rect;
// pDC=GetClient(&rect);
// TODO: Add your message handler code here and/or call default
// CFormView::OnMouseMove(nFlags, point); //显示鼠标坐标值
//显示X坐标
m_X.ResetContent();
str.Format("%3d",point.x);
m_X.AddString(str);
//显示Y坐标
m_Y.ResetContent();
str.Format("%3d",point.y);
m_Y.AddString(str);
}
void CZhangzhiguangView::OnRengongshichang()
{
// TODO: Add your control notification handler code here
which_method=0;
}
void CZhangzhiguangView::OnShenjingwangluo()
{
// TODO: Add your control notification handler code here
which_method=1;
}
void CZhangzhiguangView::OnBegintocompute()
{
// TODO: Add your control notification handler code here
// SetTimer(1,10,NULL);
/*/---------------------------- 下面一段程序是用来测试用--------------------//
if(which_method==0)
{ //人造势场法规划路径
// CClientDC *pDC;
// CRect rect(200,200,300,300);
// pDC->Ellipse(&rect);
int temp1=25;
int temp2=475;
for(int i=0;i<=400;i++)
{
route_x=i+1;
route_x=27+route_x;
route_y=i+1;
route_y=475-route_y;
// void CRobotView::OnDraw(CDC* pDC);
// void Invalidate(BOOL bErase=FALSE);
// Invalidate();
CDC* pDC;
pDC=GetDC();
CPen penSolid(PS_DASH,1,RGB(255,0,0));
CPen *pOldPen=NULL;
pOldPen=pDC->SelectObject(&penSolid);
pDC->MoveTo(temp1,temp2);
pDC->LineTo(route_x,route_y);
// CRect Circle(route_x-0.5,route_y-0.5,route_x+0.5,route_y+0.5);
// pDC->Ellipse(&Circle);
pDC->SelectObject(pOldPen);
temp1=route_x;
temp2=route_y;
// Invalidate();
// delay(10); //为什么加上它就编译通不过呢?
}
}
else
{ //神经网络法规划路径
}
*///----------------------------------------------------------//
//---------------------正式利用所述方法来规划-------------//
//起始点坐标:(50,450),终点坐标:(440,75)//
int temp1=Orig_Pos_x;
int temp2=Orig_Pos_y;
if(which_method==0)
{ //人造势场法规划路径
int TotalStep=1000;
m_Progress.SetRange(0,TotalStep);//设置进度条范围
for(int j=0;j<TotalStep;j++)
{
m_Progress.StepIt(); //进度条显示
if((Mid_Pos_x!=Goal_Pos_x)||(Mid_Pos_y!=Goal_Pos_y))
{
double Force_Total_x=0,Force_Total_y=0;
double fTotalForce;
for(int i=0;i<5;i++) //计算障碍物势力
{
Force_Obstacle(Mid_Pos_x,Mid_Pos_y,Obstacle+i);
Force_Total_x=Force_Total_x+Obstacle[i].Force_Obs_x;
Force_Total_y=Force_Total_y+Obstacle[i].Force_Obs_y;
}
Force_Goal(Mid_Pos_x,Mid_Pos_y); //计算目标势力
Force_Total_x=fGoalForce_x+Force_Total_x; //计算合力
Force_Total_y=fGoalForce_y+Force_Total_y;
fTotalForce=sqrt(pow(Force_Total_x,2)+pow(Force_Total_y,2));
int nReal_x, nReal_y; //确定运动方向
if((Force_Total_x!=0) || (Force_Total_y!=0))
{
nReal_x = (int) (Robot_Speed *Force_Total_x/fTotalForce);
nReal_y = (int) (Robot_Speed *Force_Total_y/fTotalForce);
}
else
{
if((Mid_Pos_x!=Goal_Pos_x)||(Mid_Pos_y!=Goal_Pos_y))
{
nReal_x = 1; //在还没有到达终点时,但遇上合力为0的情况,
nReal_y = 1; //人为地加上一个干扰,这一个象素的干扰
//可以在半径里考虑上,不至于有可能碰上障碍物
}
else
{
nReal_x = 0;
nReal_y = 0;
}
}
Mid_Pos_x=Mid_Pos_x+nReal_x;
Mid_Pos_y=Mid_Pos_y-nReal_y;
CDC* pDC; //画轨迹线
pDC=GetDC();
CPen penSolid(PS_DASH,1,RGB(255,0,0));
CPen *pOldPen=NULL;
pOldPen=pDC->SelectObject(&penSolid);
pDC->MoveTo(temp1,temp2);
pDC->LineTo(Mid_Pos_x,Mid_Pos_y);
pDC->SelectObject(pOldPen);
temp1=Mid_Pos_x;
temp2=Mid_Pos_y;
// for(int m=0;m<5e6;m++)
// {}
}
}
}
else
{ //神经网络法规划路径
}
}
void CZhangzhiguangView::OnStopcomputing()
{
// TODO: Add your control notification handler code here
//KillTimer(1);
exit(1);
}
void CZhangzhiguangView::OnDraw(CDC* pDC)
{
// TODO: Add your specialized code here and/or call the base class
// pDC->SetViewportOrg(27,478);
//----------------------------------------// 画三个圆作为障碍物
// 圆心坐标和半径分别为:
// (130,130) R1=40; (260,260) R2=40; (400,360) R3=30 --------//
CRect Circle1(90,90,170,170);
pDC->Ellipse(&Circle1);
CRect Circle2(370,330,430,390);
pDC->Ellipse(&Circle2);
CRect Circle3(220,220,300,300);
pDC->Ellipse(&Circle3);
CRect Circle6(285,75,355,145);
pDC->Ellipse(&Circle6);
CRect Circle7(400,160,460,220);
pDC->Ellipse(&Circle7);
//-----------------------------------------// 画起始点和目标点
CRect Circle4(Orig_Pos_x-3,Orig_Pos_y-3,Orig_Pos_x+3,Orig_Pos_y+3);
pDC->Ellipse(&Circle4);
CRect Circle5(Goal_Pos_x-3,Goal_Pos_y-3,Goal_Pos_x+3,Goal_Pos_y+3);
pDC->Ellipse(&Circle5);
}
void CZhangzhiguangView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
/*CClientDC dc(this);
BeginDrawRobot(&dc);
*/
CFormView::OnTimer(nIDEvent);
}
void CZhangzhiguangView::BeginDrawRobot(CClientDC *pDC)
{
// for(int i=0;i<=100;i++)
// {
/*
// route_x=i+1;
route_x=temp1+100;
// route_y=i+1;
route_y=temp2-100;
// void CRobotView::OnDraw(CDC* pDC);
// void Invalidate(BOOL bErase=FALSE);
// Invalidate();
// CDC* pDC;
// pDC=GetDC();
CPen penSolid(PS_DASH,1,RGB(255,0,0));
CPen *pOldPen=NULL;
pOldPen=pDC->SelectObject(&penSolid);
pDC->MoveTo(temp1,temp2);
pDC->LineTo(route_x,route_y);
// CRect Circle(route_x-0.5,route_y-0.5,route_x+0.5,route_y+0.5);
// pDC->Ellipse(&Circle);
pDC->SelectObject(pOldPen);
temp1=route_x;
temp2=route_y;
// }
*/
}
void CZhangzhiguangView::Force_Obstacle(int fMid_Pos_x,int fMid_Pos_y,struct Obstacle_Coor_R *PointerObstacle)
{ //计算障碍物产生的力的大小
POINT TempPoint;
double fDistance=Sensor_Max_Range;
double fTemp;
double fTemp_x=0.0, fTemp_y=0.0, fTempDistance=0.0;
double fForce=0.0;
double fTempForce_x=0.0;
double fTempForce_y=0.0;
double fSensorMaxRange=Sensor_Max_Range;
// TempPoint.x=fMid_Pos_x-Obstacle[i].Obstacle_x;
// TempPoint.y=-(fMid_Pos_y-Obstacle[i].Obstacle_y);
TempPoint.x=fMid_Pos_x-PointerObstacle->Obstacle_x;
TempPoint.y=-(fMid_Pos_y-PointerObstacle->Obstacle_y);
fTempDistance=sqrt(pow(TempPoint.x,2) + pow(TempPoint.y,2));
if((fTempDistance-PointerObstacle->Obstacle_R)<fDistance)//考虑半径大小
{
fDistance = fTempDistance-PointerObstacle->Obstacle_R;
fTemp_x = TempPoint.x;
fTemp_y = TempPoint.y;
}
else
{
fTemp_x =0;
fTemp_y =0;
}
fTemp = (1.0/fDistance) - (1.0/fSensorMaxRange);
fForce = ObstacleGain * fTemp /pow(fDistance,2); //障碍物斥力与距离的指数关系可以选择和优化。
// fForce = ObstacleGain * fTemp /fDistance;
fTempForce_x = fForce * fTemp_x / fDistance;
fTempForce_y = fForce * fTemp_y / fDistance;
PointerObstacle->Force_Obs_x=fTempForce_x;
PointerObstacle->Force_Obs_y=fTempForce_y;
// return true;
}
bool CZhangzhiguangView::Force_Goal(int fMid_Pos_x, int fMid_Pos_y)
{ //计算目标产生的力的大小
POINT TempPoint;
double fDistance=0.0;
double fForce;
TempPoint.x =Goal_Pos_x-fMid_Pos_x;
TempPoint.y =-(Goal_Pos_y-fMid_Pos_y);
if((fMid_Pos_x!=Goal_Pos_x)||(fMid_Pos_y!=Goal_Pos_y)) //还没有达到目标位置
{
fDistance = sqrt(pow(TempPoint.x,2) + pow(TempPoint.y,2));
fForce = GoalGain * fDistance;
fGoalForce_x = fForce * TempPoint.x / fDistance;
fGoalForce_y = fForce * TempPoint.y / fDistance;
return true;
}
else
{
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -