在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例嵌入式开发 → 电机控制平台示例(c++源码)

电机控制平台示例(c++源码)

嵌入式开发

下载此实例
  • 开发语言:C/C++
  • 实例大小:7.20M
  • 下载次数:43
  • 浏览次数:351
  • 发布时间:2020-04-06
  • 实例类别:嵌入式开发
  • 发 布 人:PZH2020
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 示例 电机 控制

实例介绍

【实例简介】

【实例截图】

from clipboard

from clipboard

【核心代码】

// 电机控制平台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);
	
}

标签: 示例 电机 控制

实例下载地址

电机控制平台示例(c++源码)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警