?? motiondemodlg.cpp
字號:
// 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 + -