www.pudn.com > VC6.0.rar > Demo02Dlg.cpp, change:2004-03-17,size:6666b


// Demo02Dlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Demo02.h" 
#include "Demo02Dlg.h" 
# include	"dmc1000.h"		//包含库头文件 
//定义四个轴的常量 
const int XCH = 0; 
const int YCH = 1; 
const int ZCH = 2; 
const int UCH = 3; 
 
/******************************************** 
	雷赛DMC1000控制卡编程示例之二 
 
?-主要演示功能: 
	1. 单轴回原点操作 
	2. 多轴回原点操作 
 
!-关键函数: 
	回原点函数 
		d1000_home_move			//单轴 
		d1000_home_move_all		//多轴 
 
	查询原点输入信号 
		d1000_get_axis_status 
 
	 
#-注意事项: 
		查询原点信号:因d1000_get_axis_status返回多个输入信号在 
	同一个整数内,而原点状态为2位,故其状态查询为: 
 
		status = (d1000_get_axis_status( 0 )&0x04 )==0x04; 
 
	此时: 
		status 的值只有0和1两种 
 
 
	Writen by LEISAI-SUPPORT 
	FAX: 0755 - 26402718 
	TEL: 0755 - 26434329 
	EMAIL: support@leisai.com 
	HTTP: www.leisai.com	 
********************************************/ 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDemo02Dlg dialog 
 
CDemo02Dlg::CDemo02Dlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CDemo02Dlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDemo02Dlg) 
	m_bDirX = TRUE; 
	m_bDirY = TRUE; 
	m_bDirZ = TRUE; 
	m_bDirU = TRUE; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CDemo02Dlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDemo02Dlg) 
	DDX_Check(pDX, IDC_CHECK_X, m_bDirX); 
	DDX_Check(pDX, IDC_CHECK_Y, m_bDirY); 
	DDX_Check(pDX, IDC_CHECK_Z, m_bDirZ); 
	DDX_Check(pDX, IDC_CHECK_U, m_bDirU); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CDemo02Dlg, CDialog) 
	//{{AFX_MSG_MAP(CDemo02Dlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_TIMER() 
	ON_WM_DESTROY() 
	ON_BN_CLICKED(IDC_BUTTON_X, OnButtonX) 
	ON_BN_CLICKED(IDC_BUTTON_Y, OnButtonY) 
	ON_BN_CLICKED(IDC_BUTTON_Z, OnButtonZ) 
	ON_BN_CLICKED(IDC_BUTTON_U, OnButtonU) 
	ON_BN_CLICKED(IDC_BUTTON_ALL, OnButtonAll) 
	ON_BN_CLICKED(IDC_CHECK_X, OnCheckX) 
	ON_BN_CLICKED(IDC_CHECK_Y, OnCheckY) 
	ON_BN_CLICKED(IDC_CHECK_Z, OnCheckZ) 
	ON_BN_CLICKED(IDC_CHECK_U, OnCheckU) 
	ON_BN_CLICKED(ID_STOP, OnStop) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDemo02Dlg message handlers 
 
BOOL CDemo02Dlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// 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 
	 
	if( d1000_board_init() <= 0 )//控制卡初始化 
		MessageBox("未找到控制卡!","警告"); 
 
	UpdateControl(); 
 
	SetTimer( IDC_TIMER, 100, NULL ); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
// 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 CDemo02Dlg::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 CDemo02Dlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CDemo02Dlg::OnTimer(UINT nIDEvent)  
{ 
	static CString string; 
	static const int ORGIN = (1<<2);	//第二位为原点信号 
 
	string.Format( "原点状态:%d,%d,%d,%d",  
		(ORGIN&d1000_get_axis_status( XCH)) == ORGIN, 
		(ORGIN&d1000_get_axis_status( YCH)) == ORGIN, 
		(ORGIN&d1000_get_axis_status( ZCH)) == ORGIN, 
		(ORGIN&d1000_get_axis_status( UCH)) == ORGIN); 
 
	GetDlgItem( IDC_STATIC_STATUS )->SetWindowText( string ); 
 
	CDialog::OnTimer(nIDEvent); 
} 
 
void CDemo02Dlg::OnDestroy()  
{ 
	KillTimer( IDC_TIMER );	//在关闭控制卡之前先关闭定时器 
	d1000_board_close();	//再次强调,一定调用此函数 
	CDialog::OnDestroy(); 
	 
	 
} 
 
void CDemo02Dlg::OnOK()  
{ 
//	CDialog::OnOK(); 
} 
 
void CDemo02Dlg::OnCancel()  
{ 
	 
	CDialog::OnCancel(); 
} 
 
void CDemo02Dlg::OnButtonX()  
{ 
	if( !UpdateData( TRUE ) )  
		return; 
	d1000_home_move( XCH, 1600, m_bDirX?6400:-6400, 0.1 ); 
	 
} 
 
void CDemo02Dlg::OnButtonY()  
{ 
	if( !UpdateData( TRUE ) )  
		return; 
	d1000_home_move( YCH, 1600, m_bDirY?3200:-3200, 0.1 ); 
	 
} 
 
void CDemo02Dlg::OnButtonZ()  
{ 
	if( !UpdateData( TRUE ) )  
		return; 
	d1000_home_move( ZCH, 1600, m_bDirZ?12800:-12800, 0.1 ); 
	 
} 
 
void CDemo02Dlg::OnButtonU()  
{ 
	if( !UpdateData( TRUE ) )  
		return; 
	d1000_home_move( UCH, 1600, m_bDirU?1600:-1600, 0.1 ); 
	 
} 
 
void CDemo02Dlg::OnButtonAll()  
{ 
	if( !UpdateData( TRUE ) )  
		return; 
 
	const int MAX_AXIS = 4; 
	//配置参数 
	int axis[MAX_AXIS]			= { XCH, YCH, ZCH, UCH }; 
	long start[MAX_AXIS]		= { 1600, 1600, 1600, 1600 }; 
	long speed[MAX_AXIS]		= { 3200*(m_bDirX?1:-1),  
									6400*(m_bDirY?1:-1),  
									12800*(m_bDirZ?1:-1),  
									1600*(m_bDirU?1:-1) }; 
	double accel[MAX_AXIS]		= { 0.1, 0.1, 0.1, 0.1 }; 
 
	for( int i(0); i<MAX_AXIS; i++) 
		d1000_home_move( i, start[i], speed[i], accel[i]); 
} 
 
void CDemo02Dlg::UpdateControl() 
{ 
	GetDlgItem( IDC_CHECK_X )->SetWindowText( m_bDirX?"X轴原点方向:正":"X轴原点方向:反"); 
	GetDlgItem( IDC_CHECK_Y )->SetWindowText( m_bDirY?"Y轴原点方向:正":"Y轴原点方向:反"); 
	GetDlgItem( IDC_CHECK_Z )->SetWindowText( m_bDirZ?"Z轴原点方向:正":"Z轴原点方向:反"); 
	GetDlgItem( IDC_CHECK_U )->SetWindowText( m_bDirU?"U轴原点方向:正":"U轴原点方向:反"); 
 
} 
 
void CDemo02Dlg::OnCheckX()  
{ 
	UpdateData( TRUE );	 
	UpdateControl(); 
} 
 
void CDemo02Dlg::OnCheckY()  
{ 
	UpdateData( TRUE );	 
	UpdateControl(); 
	 
} 
 
void CDemo02Dlg::OnCheckZ()  
{ 
	UpdateData( TRUE );	 
	UpdateControl(); 
	 
} 
 
void CDemo02Dlg::OnCheckU()  
{ 
	UpdateData( TRUE );	 
	UpdateControl(); 
	 
} 
 
void CDemo02Dlg::OnStop()  
{//停止 
	//当减速时间为0时,为急停效果 
	d1000_decel_stop( XCH ); 
	d1000_decel_stop( YCH ); 
	d1000_decel_stop( ZCH ); 
	d1000_decel_stop( UCH ); 
	return; 
	 
	 
}