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

📄 motiondemodlg.cpp

📁 基于AS-R的避障小车 由南京航空航天大学编制
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MotionDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MotionDemo.h"
#include "MotionDemoDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMotionDemoDlg dialog
BOOL CMotionDemoDlg::BalkFlag=false;
BOOL CMotionDemoDlg::BalkFlagr=false;
BOOL CMotionDemoDlg::BalkFlagL=false;
BOOL CMotionDemoDlg::BalkFlag1=false;

CMotionDemoDlg::CMotionDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMotionDemoDlg::IDD, pParent)
{
	
	//{{AFX_DATA_INIT(CMotionDemoDlg)
	m_distance=0.0f;
	m_distance1=0.0f;
	m_PsdSafeDis=45.0f;
	m_SonarSafeDis=0.9f;
	m_SpeedL=250.0f;
	m_SpeedR=250.0f;
	m_Psd1=0.0f;
	m_Psd2=0.0f;
	m_Psd3=0.0f;
	m_Psd4=0.0f;
	m_Sonar1=0.0f;
	m_Sonar2=0.0f;
	m_Sonar3=0.0f;
	m_Sonar4=0.0f;
	m_Sonar5=0.0f;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pRobot = NULL;   //为了正确判别指针的有效性,务必使其初始化为NULL。
    m_pMotion = NULL;  //为了正确判别指针的有效性,务必使其初始化为NULL。
	m_pPsd = NULL; 
	m_pSonar = NULL;  
}

void CMotionDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMotionDemoDlg)
	DDX_Control(pDX, IDC_SONAR_GRAPH, m_graph);
	DDX_Text(pDX, IDC_EDIT1, m_PsdSafeDis);
	DDX_Text(pDX, IDC_EDIT2, m_SonarSafeDis);
	DDX_Text(pDX, IDC_EDIT3, m_SpeedL);
	DDX_Text(pDX, IDC_EDIT4, m_SpeedR);
	DDX_Text(pDX, IDC_PSD1, m_Psd1);
	DDX_Text(pDX, IDC_PSD2, m_Psd2);
	DDX_Text(pDX, IDC_PSD3, m_Psd3);
	DDX_Text(pDX, IDC_PSD4, m_Psd4);
	DDX_Text(pDX, IDC_SONAR1, m_Sonar1);
	DDX_Text(pDX, IDC_SONAR2, m_Sonar2);
	DDX_Text(pDX, IDC_SONAR3, m_Sonar3);
	DDX_Text(pDX, IDC_SONAR4, m_Sonar4);
	DDX_Text(pDX, IDC_SONAR5, m_Sonar5);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMotionDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CMotionDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_PAUSE, OnPause)
	ON_BN_CLICKED(IDC_END, OnEnd)
	ON_BN_CLICKED(IDC_FRONT, OnFront)
	ON_BN_CLICKED(IDC_BACK, OnBack)
	ON_BN_CLICKED(IDC_CLOCKWISE, OnClockwise)
	ON_BN_CLICKED(IDC_REVENSION, OnRevension)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_MSG_SONAR_UPDATEDATA,UpdateMyData)
	ON_MESSAGE(WM_MSG_PSD_UPDATEDATA,UpdateMyData)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMotionDemoDlg message handlers

BOOL CMotionDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	if(!BuildSystem())   //程序启动时,库函数开始被调用

	{

          PostQuitMessage(0);

            return FALSE;  //如初始化失败,返回FALSE,则对话框创建失败,程序退出。

      }
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMotionDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMotionDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMotionDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL  CMotionDemoDlg::BuildSystem()

{

      //get the logical robot 

//获得系统的一个实例。所有的设备都必须通过系统开放的接口来创建与管理。

    m_pRobot = CASRSystem::GetInstance(); 
	IASRProcess* pProcess = NULL;
 
    PROCESS_DESC proc_desc;
    proc_desc.nType = SEQUENCE;
    m_pRobot->CreateProcess(proc_desc,pProcess);  //创建流程
    IASRDevice* pDevice = NULL; 

//用于创建运动设备,其中CreateDevic方法创建设备对象,

//ASSERT(pDevice);用于检查创建的设备对象是否有效。 

   m_pRobot->CreateDevice("MOTION", pDevice);
   ASSERT(pDevice);

//用于查询设备开放的基本控制接口,QueryInterface用于查询接口

    pDevice ->QueryInterface(ASR_IID_MOTION,(LPVOID*)&m_pMotion);
    ASSERT(m_pMotion);
//init the motion device 
	
    m_pRobot->CreateDevice("SENSOR_PSD",pDevice);
	ASSERT(pDevice);
	pDevice->QueryInterface(ASR_IID_SENSOR_PSD,(LPVOID*)&m_pPsd);
	ASSERT(m_pPsd);
    m_pRobot->AddToProcess(pProcess,pDevice);     //添加设备到流程
	HERROR hres;
	PSD_DESC desc;                               
	desc.nTime= 20; //设置采样周期
	//下面打开1~4通道的红外设备。
   desc.nIndex = PSD_1 | PSD_2 | PSD_3 | PSD_4;  
//	desc.nIndex = PSD_1; 
    m_pPsd->SetParam(desc);                             
	m_pPsd->SetCallback(PsdProc,(LONG)this);		
	hres=m_pPsd->Init();								
	if (ASRFAIL(hres))
	{
		CASRSystem::ShowErrorMessage(hres);
		return FALSE;
	}
    
  	
	m_pRobot->CreateDevice("SENSOR_SONAR",pDevice);
	ASSERT(pDevice);
	pDevice->QueryInterface(ASR_IID_SENSOR_SONAR,(LPVOID*)&m_pSonar);
	ASSERT(m_pSonar);
    m_pRobot->AddToProcess(pProcess,pDevice);       //添加设备到流程
//	HERROR hres;
	SONAR_DESC descsonar;  //参数信息
	strcpy(descsonar.szMode,"1,2,3,4,5");      //使用1,3,5路超声
  //  descsonar.nIndex = 0;
    descsonar.nMaxRange= 7;                               //设置最大工作范围
	descsonar.nTime= 20;                                  //设置采样周期
	m_pSonar->SetParam(descsonar);                        //设置设备参数信息
	m_pSonar->SetCallback(SonarProc,(LONG)this);	 //设置回调函数
	hres=m_pSonar->Init();							 //初始化设备环境
	if (ASRFAIL(hres))
	{
		CASRSystem::ShowErrorMessage(hres);
		return FALSE;
	}
	//m_pSonar->Start();
  
//	HERROR  hres;

    hres = m_pMotion->Init();
    if(ASRFAIL(hres))

     {

        CASRSystem::ShowErrorMessage(hres); //显示系统出错信息

        return FALSE;

     }

	return TRUE;

}  

void  CMotionDemoDlg::DestroySystem()

{

     if(m_pMotion)

     {

          m_pMotion->End();             //停止使用运动设备

          m_pMotion->Release();          //释放基本运动控制接口

          m_pMotion = NULL;

     }

⌨️ 快捷键说明

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