基本信息
源码名称:电机控制平台示例(c++源码)
源码大小:7.20M
文件格式:.zip
开发语言:C/C++
更新时间:2020-04-06
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

// 电机控制平台Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "电机控制平台.h"
#include "电机控制平台Dlg.h"
#include "ges.h"
#include "windows.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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_kd = 10;
	m_ki = 10;
	m_Vzhuzhou = 2000;
	m_kp = 20;
	m_Bisa = 0;
	m_MaxVel = 500;
	m_StartVel =1;
	m_StopAcc = 6;
	m_WorkVel = 15;
	m_WorkAcc = 1;
	m_X = 10000;
	m_Y = 20000;
	m_Z = 0.0;
	m_yuanxinX = 0.0;
	m_yuanxinY = 0.0;
	m_angle = 360;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Text(pDX, IDC_EDIT3, m_kd);
	DDX_Text(pDX, IDC_EDIT2, m_ki);
	DDX_Text(pDX, IDC_EDIT4, m_Vzhuzhou);
	DDX_Text(pDX, IDC_EDIT1, m_kp);
	DDX_Text(pDX, IDC_EDIT5, m_Bisa);
	DDX_Text(pDX, IDC_EDIT8, m_MaxVel);
	DDX_Text(pDX, IDC_EDIT6, m_StartVel);
	DDX_Text(pDX, IDC_EDIT9, m_StopAcc);
	DDX_Text(pDX, IDC_EDIT7, m_WorkVel);
	DDX_Text(pDX, IDC_EDIT10, m_WorkAcc);
	DDX_Text(pDX, IDC_EDIT11, m_X);
	DDX_Text(pDX, IDC_EDIT12, m_Y);
	DDX_Text(pDX, IDC_EDIT13, m_Z);
	DDX_Text(pDX, IDC_EDIT14, m_yuanxinX);
	DDX_Text(pDX, IDC_EDIT15, m_yuanxinY);
	DDX_Text(pDX, IDC_EDIT16, m_angle);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnCloseCard)
	ON_BN_CLICKED(IDC_BUTTON3, Onstart1)
	ON_BN_CLICKED(IDC_BUTTON4, OnClose1)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON6, OnArcXY)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
	ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
	ON_BN_CLICKED(IDC_BUTTON12, OnButton12)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::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 CMyDlg::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();
	}
}
	unsigned short limit,limit1;
// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


short rtn;
void error(short r)
{
	switch(r)
		
	{
	case -1:
		AfxMessageBox("communication error");break;
	case 0:
		break;
	case 1:
		AfxMessageBox("command error");break;
	case 2:
		AfxMessageBox("radius or chord is 0");break;
	case 3:
		AfxMessageBox("length is 0 or overflow");break;
	case 4:
		AfxMessageBox("velocity or acceleration is less than 0");break;
	case 5:
		AfxMessageBox("chord is greater than diameter");break;
	case 7:
		AfxMessageBox("parameter error");break;
	default:
		AfxMessageBox("Error Code = %d ",rtn);break;

	}
}

void CMyDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	rtn=GT_Open();  error(rtn);
	rtn=GT_Reset();  error(rtn);
	rtn=GT_HomeSns(0); error(rtn);//各轴原点下降沿触发


	//初始化伺服电机
	rtn=GT_SetKp(2,m_kp);   //设置PID
	rtn=GT_SetKi(2,m_ki);
	rtn=GT_SetKd(2,m_kd);
	error(rtn);
	rtn=GT_Update(2);    
	error(rtn);
	rtn=GT_EncOff(2);error(rtn);
	rtn=GT_EncOn(2);error(rtn);
	
	rtn=GT_EncSns(1);error(rtn);   //设置旋转编码器
	//GT_AlarmOff(3);
	
	rtn=GT_SetMtrBias(2,m_Bisa);//静差补偿
	rtn=GT_Update(2);
	error(rtn);
	rtn=GT_AxisOn(2);
	error(rtn);
	

	
	rtn=GT_CtrlMode(1,1);  error(rtn);
	rtn=GT_StepDir(1);error(rtn);
	rtn=GT_AxisOn(2);error(rtn);
	rtn=GT_AxisOn(1);error(rtn);

	UpdateData(true);
	
	rtn=GT_SetStrtVel(m_StartVel);//启动速度
	error(rtn);
	rtn=GT_SetMaxVel(m_MaxVel);//最大速度
	error(rtn);
	rtn=GT_SetStpAcc(m_StopAcc);//急停加速度
	error(rtn);
	rtn=GT_SetSynVel(m_WorkVel);
	error(rtn);
	rtn=GT_SetSynAcc(m_WorkAcc);
	error(rtn);
	
	Sleep(20);
	
}

void CMyDlg::OnCloseCard() 
{
	// TODO: Add your control notification handler code here
	GT_AxisOff(1);
	GT_AxisOff(2);
	rtn = GT_Close(); error(rtn);   //关闭运动控制器
	
}

void CMyDlg::Onstart1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	GT_LmtsOn(4);
	rtn=GT_SetSpindleVel(m_Vzhuzhou);
	SetTimer(1,1,NULL);
	error(rtn);
	
	
}

void CMyDlg::OnClose1() 
{
	// TODO: Add your control notification handler code here
	rtn=GT_CloseSpindle();
	error(rtn);
}

void CMyDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
	GT_LnXY(m_X,m_Y);
	error(rtn);

}



void CMyDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnTimer(nIDEvent);
	switch(nIDEvent)
	{
	case 1:
		GT_GetLmtSwt(&limit);
		if(limit&0x80||limit&0x40)
			GT_CloseSpindle();
		KillTimer(1);
		break;
	case 2:
		GT_GetLmtSwt(&limit1);
		if (limit1&0x1||limit1&0x2||limit1&0x4||limit1&0x8)
		{
			rtn=GT_EStpMtn();error(rtn);
		}

	}
	
			


}


void CMyDlg::OnArcXY()//圆弧插补
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	rtn=GT_ArcXY(m_yuanxinX,m_yuanxinY,m_angle);
	error(rtn);
	
}

void CMyDlg::OnButton7() //设置运动轨迹
{
	// TODO: Add your control notification handler code here
	rtn=GT_StrtList();error(rtn);
	rtn=GT_MvXY(0,0,m_WorkVel,m_WorkAcc);error(rtn);

	
}

void CMyDlg::OnButton8() //完成设置运动轨迹
{
	// TODO: Add your control notification handler code here
	rtn=GT_EndList();error(rtn);
	
}

void CMyDlg::OnButton9() //开始连续轨迹运动
{
	// TODO: Add your control notification handler code here
	rtn=GT_StrtMtn();error(rtn);
	SetTimer(2,1,NULL);
	
}

void CMyDlg::OnButton10() //平滑停止
{
	// TODO: Add your control notification handler code here
	rtn=GT_StpMtn();error(rtn);
}

void CMyDlg::OnButton11() //急停
{
	// TODO: Add your control notification handler code here
	rtn=GT_EStpMtn();error(rtn);
	
}

void CMyDlg::OnButton12() //继续
{
	// TODO: Add your control notification handler code here
	rtn=GT_RestoreMtn();error(rtn);
	
}