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

📄 testdlg.cpp

📁 运动控制卡的详细开发资料
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// testDlg.cpp : implementation file
//

#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include "ParaStruct.h"
#include "adt8940.h"

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

#define NORUN    0
#define RUNNING  1

class ParaStruct;
ParaStruct g_Para;
#define PARAFILE "pf8940.dat";
CString ParaPath;
BOOL ifInit=FALSE;
BOOL ifRel=FALSE;

int g_WorkStatus=NORUN;
BOOL ifHand=FALSE;
int g_Axis[4]={0,0,0,0};

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#define IDC_MAIN_TIMER 10000

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()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDlg)
	m_fFactPosW = 0.0f;
	m_fFactPosX = 0.0f;
	m_fFactPosY = 0.0f;
	m_fFactPosZ = 0.0f;
	m_fLogPosW = 0.0f;
	m_fLogPosX = 0.0f;
	m_fLogPosY = 0.0f;
	m_fLogPosZ = 0.0f;
	m_fObjW = 0.0;
	m_fObjX = 0.0;
	m_fObjY = 0.0;
	m_fObjZ = 0.0;
	m_fSpeedW = 0.0f;
	m_fSpeedX = 0.0f;
	m_fSpeedY = 0.0f;
	m_fSpeedZ = 0.0f;
	m_Hard = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	DDX_Control(pDX, IDC_BUTTON_NEGTIVE, m_btnNegitive);
	DDX_Control(pDX, IDC_BUTTON_POSITIVE, m_btnPositive);
	DDX_Text(pDX, IDC_EDIT_FACTPOSW, m_fFactPosW);
	DDX_Text(pDX, IDC_EDIT_FACTPOSX, m_fFactPosX);
	DDX_Text(pDX, IDC_EDIT_FACTPOSY, m_fFactPosY);
	DDX_Text(pDX, IDC_EDIT_FACTPOSZ, m_fFactPosZ);
	DDX_Text(pDX, IDC_EDIT_LOGPOSW, m_fLogPosW);
	DDX_Text(pDX, IDC_EDIT_LOGPOSX, m_fLogPosX);
	DDX_Text(pDX, IDC_EDIT_LOGPOSY, m_fLogPosY);
	DDX_Text(pDX, IDC_EDIT_LOGPOSZ, m_fLogPosZ);
	DDX_Text(pDX, IDC_EDIT_OBJPOSW, m_fObjW);
	DDX_Text(pDX, IDC_EDIT_OBJPOSX, m_fObjX);
	DDX_Text(pDX, IDC_EDIT_OBJPOSY, m_fObjY);
	DDX_Text(pDX, IDC_EDIT_OBJPOSZ, m_fObjZ);
	DDX_Text(pDX, IDC_EDIT_SPEEDW, m_fSpeedW);
	DDX_Text(pDX, IDC_EDIT_SPEEDX, m_fSpeedX);
	DDX_Text(pDX, IDC_EDIT_SPEEDY, m_fSpeedY);
	DDX_Text(pDX, IDC_EDIT_SPEEDZ, m_fSpeedZ);
	DDX_Text(pDX, IDC_STATIC_HARD, m_Hard);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_INIT, OnButtonInit)
	ON_BN_CLICKED(IDC_BUTTON_BACKZERO, OnButtonBackzero)
	ON_BN_CLICKED(IDC_BUTTON_PMOVE, OnButtonPmove)
	ON_BN_CLICKED(IDC_CHECK_ABSREL, OnCheckAbsrel)
	ON_BN_CLICKED(IDC_BUTTON_INP, OnButtonInp)
	ON_BN_CLICKED(IDC_BUTTON_CONTINUE, OnButtonContinue)
	ON_BN_CLICKED(IDC_BUTTON_INPARC, OnButtonInparc)
	ON_BN_CLICKED(IDC_BUTTON_MOVEFREE, OnButtonMovefree)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CHECK_AXISX, OnCheckAxisx)
	ON_BN_CLICKED(IDC_CHECK_AXISY, OnCheckAxisy)
	ON_BN_CLICKED(IDC_CHECK_AXISZ, OnCheckAxisz)
	ON_BN_CLICKED(IDC_CHECK_AXISW, OnCheckAxisw)
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

BOOL CTestDlg::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

	m_btnPositive.SetCh(0x1000,MouseDOWN,MouseUP);
	m_btnNegitive.SetCh(0x1001,MouseDOWN,MouseUP);

	m_btnNegitive.SetIcon(IDI_ICONNEGITIVE);
	m_btnPositive.SetIcon(IDI_ICONPOSITIVE);


	char path[MAX_PATH];
	
	///查找系统目录中是否有参数文件
	::GetSystemDirectory(path, MAX_PATH);
	ParaPath=path;
	ParaPath+='\\';
	ParaPath+=PARAFILE;	
	BOOL result=g_Para.LoadFromFile(ParaPath.GetBuffer(ParaPath.GetLength()));
	if (!result)
	{
		MessageBox("参数文件打开失败!","警告",MB_OK||MB_ICONSTOP);
		return FALSE;			
	}





	SetTimer(IDC_MAIN_TIMER,100,NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTestDlg::OnButtonInit() 
{	
	CString str;
	int m=adt8940_init_board();

	if (m<=0)
	{
		MessageBox("初始化卡失败!","提示",MB_ICONSTOP);
		return;
	}	
	ifInit=TRUE;
	m=get_hardware_ver(0);
	str.Format("硬件版本:V%d",m); 	
	CStatic *lbl=(CStatic*)GetDlgItem(IDC_STATIC_HARD);
	lbl->SetWindowText(str);
	Update();
	
}


void CTestDlg::MouseDOWN(int nID)
{	
	if (!ifInit) return;

	if( g_WorkStatus==RUNNING ) return;
	if (g_Axis[0]==0 && g_Axis[1]==0 && g_Axis[2]==0 && g_Axis[3]==0)
		return;
	g_WorkStatus = TRUE;
	ifHand=TRUE;	
	int axis;
	float distance=0.1;
	if (g_Axis[0]!=0)
		axis=1;
	else if (g_Axis[1]!=0)
		axis=2;
	else if (g_Axis[2]!=0)
		axis=3;
	else if (g_Axis[3]!=0)
		axis=4;
	switch( nID )
	{
		case 0x1000:
			hand_move(axis,0,ActionFunction);
			break;
		case 0x1001:
			hand_move(axis,1,ActionFunction);
			break;	
	}
	Sleep(50);
	g_WorkStatus = NORUN;
	ifHand=FALSE;
}
//鼠标弹起
void CTestDlg::MouseUP()
{
	all_stop(0);
	g_WorkStatus = NORUN ;
	ifHand=FALSE;
}


void CTestDlg::OnButtonBackzero() 
{
	// TODO: Add your control notification handler code here
		if (!ifInit) return;

	if (g_WorkStatus==RUNNING) 	return;
		
	int result;
	BOOL axis[4]={FALSE,FALSE,FALSE,FALSE};
	CButton *btn;
	btn=(CButton*)GetDlgItem(IDC_CHECK_AXISX);
	if (btn->GetCheck()==1) axis[0]=TRUE;
	btn=(CButton*)GetDlgItem(IDC_CHECK_AXISY);
	if (btn->GetCheck()==1) axis[1]=TRUE;
	btn=(CButton*)GetDlgItem(IDC_CHECK_AXISZ);
	if (btn->GetCheck()==1) axis[2]=TRUE;
	btn=(CButton*)GetDlgItem(IDC_CHECK_AXISW);
	if (btn->GetCheck()==1) axis[3]=TRUE;
//	SetTimer(IDC_MAIN_TIMER,100,NULL);
	
	g_WorkStatus=RUNNING;

	if (axis[0] && !axis[1] && !axis[2] && !axis[3] ||
		axis[1] && !axis[0] && !axis[2] && !axis[3] ||
		axis[2] && !axis[0] && !axis[1] && !axis[3] ||
		axis[3] && !axis[0] && !axis[1] && !axis[2] )
	{
		int homeaxis;
		if (axis[0]) homeaxis=1;
		if (axis[1]) homeaxis=2;
		if (axis[2]) homeaxis=3;
		if (axis[3]) homeaxis=4;
		result=home1(homeaxis,ActionFunction);
	}
	else if (axis[0] && axis[1] && !axis[2] && !axis[3] ||
			 axis[0] && axis[2] && !axis[1] && !axis[3] ||
			 axis[0] && axis[3] && !axis[1] && !axis[2] ||
			 axis[1] && axis[2] && !axis[0] && !axis[3] ||
			 axis[1] && axis[3] && !axis[0] && !axis[2] ||
			 axis[2] && axis[3] && !axis[0] && !axis[1] )
	{
		int homeaxis[2];
		if (axis[0] && axis[1]) {homeaxis[0]=1; homeaxis[1]=2;}
		if (axis[0] && axis[2]) {homeaxis[0]=1; homeaxis[1]=3;}
		if (axis[0] && axis[3]) {homeaxis[0]=1; homeaxis[1]=4;}
		if (axis[1] && axis[2]) {homeaxis[0]=2; homeaxis[1]=3;}
		if (axis[1] && axis[3]) {homeaxis[0]=2; homeaxis[1]=4;}
		if (axis[2] && axis[3]) {homeaxis[0]=3; homeaxis[1]=4;}
		result=home2(homeaxis[0],homeaxis[1],ActionFunction);
	}
	else if (axis[0] && axis[1] && axis[2] && !axis[3] ||
			 axis[0] && axis[1] && axis[3] && !axis[2] ||
			 axis[0] && axis[2] && axis[3] && !axis[1] ||
			 axis[1] && axis[2] && axis[3] && !axis[0] )
	{
		int homeaxis[3];
		if (axis[0] && axis[1] && axis[2]) {homeaxis[0]=1; homeaxis[1]=2; homeaxis[2]=3;}
		if (axis[0] && axis[1] && axis[3]) {homeaxis[0]=1; homeaxis[1]=2; homeaxis[2]=4;}
		if (axis[0] && axis[2] && axis[3]) {homeaxis[0]=1; homeaxis[1]=3; homeaxis[2]=4;}
		if (axis[1] && axis[2] && axis[3]) {homeaxis[0]=2; homeaxis[1]=3; homeaxis[2]=4;}
		result=home3(homeaxis[0],homeaxis[1],homeaxis[2],ActionFunction);
	}
	else if (axis[0] && axis[1] && axis[2] && axis[3])
		result=home4(ActionFunction);
	else
	{
		MessageBox("请选定轴!","提示");
//		KillTimer(IDC_MAIN_TIMER);
		g_WorkStatus=NORUN;
		return;
	}

	if (result==-1)
		MessageBox("有禁止轴参与回原点!","错误",MB_OK||MB_ICONSTOP);
	else if (result==-2)
		MessageBox("未找到原点信号,回原点失败!","错误",MB_OK||MB_ICONSTOP);
	else if (result==0)
		MessageBox("回原点终止!","提示",MB_OK||MB_ICONSTOP);
	else if (result==1)
		MessageBox("回原点成功!","提示",MB_OK||MB_ICONINFORMATION);
	
	g_WorkStatus=NORUN;
//	KillTimer(IDC_MAIN_TIMER);
	
}

void CTestDlg::OnButtonPmove() 
{
	// TODO: Add your control notification handler code here
	if (g_WorkStatus==RUNNING) return;
	UpdateData(TRUE);
	int result;
	BOOL axis[4]={FALSE,FALSE,FALSE,FALSE};
	float distance[4];
	CButton *btn;
	btn=(CButton*)GetDlgItem(IDC_CHECK_AXISX);
	if (btn->GetCheck()==1) axis[0]=TRUE;
	btn=(CButton*)GetDlgItem(IDC_CHECK_AXISY);
	if (btn->GetCheck()==1) axis[1]=TRUE;
	btn=(CButton*)GetDlgItem(IDC_CHECK_AXISZ);
	if (btn->GetCheck()==1) axis[2]=TRUE;
	btn=(CButton*)GetDlgItem(IDC_CHECK_AXISW);
	if (btn->GetCheck()==1) axis[3]=TRUE;
	g_WorkStatus=RUNNING;
	if (axis[0] && !axis[1] && !axis[2] && !axis[3] ||
		axis[1] && !axis[0] && !axis[2] && !axis[3] ||
		axis[2] && !axis[0] && !axis[1] && !axis[3] ||
		axis[3] && !axis[0] && !axis[1] && !axis[2] )
	{
		int homeaxis;
		if (axis[0]) {homeaxis=1; distance[0]=m_fObjX;}
		if (axis[1]) {homeaxis=2; distance[0]=m_fObjY;}
		if (axis[2]) {homeaxis=3; distance[0]=m_fObjZ;}
		if (axis[3]) {homeaxis=4; distance[0]=m_fObjW;}
		result=work_move1(homeaxis,distance[0],ifRel?0:1,0,ActionFunction);		
	}
	else if (axis[0] && axis[1] && !axis[2] && !axis[3] ||
			 axis[0] && axis[2] && !axis[1] && !axis[3] ||
			 axis[0] && axis[3] && !axis[1] && !axis[2] ||
			 axis[1] && axis[2] && !axis[0] && !axis[3] ||
			 axis[1] && axis[3] && !axis[0] && !axis[2] ||
			 axis[2] && axis[3] && !axis[0] && !axis[1] )
	{
		int homeaxis[2];
		if (axis[0] && axis[1]) {homeaxis[0]=1; homeaxis[1]=2; distance[0]=m_fObjX; distance[1]=m_fObjY;}
		if (axis[0] && axis[2]) {homeaxis[0]=1; homeaxis[1]=3; distance[0]=m_fObjX; distance[1]=m_fObjZ;}
		if (axis[0] && axis[3]) {homeaxis[0]=1; homeaxis[1]=4; distance[0]=m_fObjX; distance[1]=m_fObjW;}
		if (axis[1] && axis[2]) {homeaxis[0]=2; homeaxis[1]=3; distance[0]=m_fObjY; distance[1]=m_fObjZ;}
		if (axis[1] && axis[3]) {homeaxis[0]=2; homeaxis[1]=4; distance[0]=m_fObjY; distance[1]=m_fObjW;}
		if (axis[2] && axis[3]) {homeaxis[0]=3; homeaxis[1]=4; distance[0]=m_fObjZ; distance[1]=m_fObjW;}
		result=work_move2(homeaxis[0],distance[0],homeaxis[1],distance[1],ifRel?0:1,0,ActionFunction);		
	}
	else if (axis[0] && axis[1] && axis[2] && !axis[3] ||
			 axis[0] && axis[1] && axis[3] && !axis[2] ||
			 axis[0] && axis[2] && axis[3] && !axis[1] ||
			 axis[1] && axis[2] && axis[3] && !axis[0] )
	{
		int homeaxis[3];
		if (axis[0] && axis[1] && axis[2]) 
		{
			homeaxis[0]=1; homeaxis[1]=2; homeaxis[2]=3;
			distance[0]=m_fObjX; distance[1]=m_fObjY;distance[2]=m_fObjZ;
		}
		if (axis[0] && axis[1] && axis[3]) 
		{
			homeaxis[0]=1; homeaxis[1]=2; homeaxis[2]=4;
			distance[0]=m_fObjX; distance[1]=m_fObjY;distance[2]=m_fObjW;

⌨️ 快捷键说明

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