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

📄 motordlgdebg.cpp

📁 一个用于无刷直流电动机的驱动与控制程序.里面所用的采集卡的型号为MC08.
💻 CPP
字号:
// MotorDlgDebg.cpp : implementation file
//

#include "stdafx.h"
#include "TestForm.h"
#include "MotorDlgDebg.h"

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

/////////////////////////////////////////////////////////////////////////////
// MotorDlgDebg dialog


MotorDlgDebg::MotorDlgDebg(CWnd* pParent /*=NULL*/)
	: CDialog(MotorDlgDebg::IDD, pParent)
{
	//{{AFX_DATA_INIT(MotorDlgDebg)
	m_dModlgdebSpeed = 320;		//设定电机转速
	m_dModlgdebTangle = 5;		//设定电机转角
	m_dModlgdebRealtangle = 0.0;//反馈电机转角及实际转角
	//}}AFX_DATA_INIT
	m_ModlgdebPps=0;		//控制卡发送脉冲速率
	m_lModlgdebPlus=0;		//控制卡发送脉冲个数
	m_nModlgdebDirect=0;	//电机转向,反转-1,正转-0
	m_nModlgdebDirectTem=0;	//电机转向暂存变量
	m_nModlgdebMtype=0;		//电机运动类型,点位-1,连续-0
	m_nModlgdebRtype=0;		//电机速度类型,常速-1,快速-0
	m_nModlgdebStype=0;		//电机制动类型,立即-1,光滑-0
	m_nModlgdebAcc=5;		//电机加速度选择值为1/8最大转速
	m_nModlgdebDec=5;		//电机减速度选择值为1/8最大转速

	m_bSelfchekflag =FALSE;//设备自检未完成
	
}


void MotorDlgDebg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(MotorDlgDebg)
	DDX_Control(pDX, IDC_COMODLGDEB_DEC, m_cModlgdebDec);
	DDX_Control(pDX, IDC_COMODLGDEB_ACC, m_cModlgdebAcc);
	DDX_Control(pDX, IDC_COMODLGDEB_STYPE, m_cModlgdebStype);
	DDX_Control(pDX, IDC_COMODLGDEB_RTYPE, m_cModlgdebRtype);
	DDX_Control(pDX, IDC_COMODLGDEB_MTYPE, m_cModlgdebMtype);
	DDX_Control(pDX, IDC_COMODLGDEB_DIRECT, m_cModlgdebDirect);
	DDX_Text(pDX, IDC_EDMODLGDEB_SPEED, m_dModlgdebSpeed);
	DDX_Text(pDX, IDC_EDMODLGDEB_TANGLE, m_dModlgdebTangle);
	DDX_Text(pDX, IDC_EDMODLGDEB_REALTANGLE, m_dModlgdebRealtangle);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(MotorDlgDebg, CDialog)
	//{{AFX_MSG_MAP(MotorDlgDebg)
	ON_BN_CLICKED(IDC_BUMODLGDEB_RUN, OnBumodlgdebRun)
	ON_BN_CLICKED(IDC_BUMODLGDEB_PAUSE, OnBumodlgdebPause)
	ON_BN_CLICKED(IDC_BUMODLGDEB_REALTANGLE, OnBumodlgdebRealtangle)
	ON_BN_CLICKED(IDC_BUMODLGDEB_STOP, OnBumodlgdebStop)
	ON_CBN_SELENDOK(IDC_COMODLGDEB_MTYPE, OnSelendokComodlgdebMtype)
	ON_BN_CLICKED(IDC_BUMODLGDEB_INI, OnBumodlgdebIni)
	ON_BN_CLICKED(IDC_BUMODLGDEB_EXIT, OnBumodlgdebExit)
	ON_CBN_SELENDOK(IDC_COMODLGDEB_RTYPE, OnSelendokComodlgdebRtype)
	ON_CBN_SELENDOK(IDC_COMODLGDEB_STYPE, OnSelendokComodlgdebStype)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MotorDlgDebg message handlers
/////////////////////////////
BOOL MotorDlgDebg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_cModlgdebDirect.SetCurSel(0);		//设定默认转向,正转
	m_cModlgdebMtype.SetCurSel(0);		//设定默认运动类型,连续运动
	m_cModlgdebRtype.SetCurSel(0);		//设定默认速度方式,快速方式
	m_cModlgdebStype.SetCurSel(0);		//设定默认制动方式,光滑制动

	m_cModlgdebAcc.SetCurSel(5);		//设定默认加速度
	m_cModlgdebDec.SetCurSel(5);		//设定默认减速度

	CEdit *ed1=(CEdit*) GetDlgItem(IDC_EDMODLGDEB_TANGLE);
	CEdit *ed2=(CEdit*) GetDlgItem(IDC_EDMODLGDEB_REALTANGLE);
	CEdit *ed3=(CEdit*) GetDlgItem(IDC_BUMODLGDEB_REALTANGLE);

	ed1->EnableWindow(FALSE);   //电机转角编辑框无效
	ed2->EnableWindow(FALSE);   //电机实际转角编辑框无效
	ed3->EnableWindow(FALSE);   //电机实际转角按钮无效

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//电机开始运行
void MotorDlgDebg::OnBumodlgdebRun() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if (m_bSelfchekflag)
	{
		motordebgDlg.m_bMotorDebugflag=TRUE;//调试电机

		CEdit *ed3=(CEdit*) GetDlgItem(IDC_BUMODLGDEB_REALTANGLE);	
		ed3->EnableWindow(TRUE);   //电机实际转角按钮有效
		motordebgDlg.m_dMotorSpeed = m_dModlgdebSpeed;	//电机调试速度		
		motordebgDlg.m_nMotorDirectTem=m_cModlgdebDirect.GetCurSel();//获得电机转向

		m_nModlgdebMtype=m_cModlgdebMtype.GetCurSel();//获得电机运动类型
		m_nModlgdebRtype=m_cModlgdebRtype.GetCurSel();//获得电机速度类型
		m_nModlgdebStype=m_cModlgdebStype.GetCurSel();//获得电机制动方式

		if (m_nModlgdebMtype==1)
		{
			motordebgDlg.m_bMotorVmove=FALSE;//点位运动
			motordebgDlg.m_dMotorTangle = m_dModlgdebTangle;//电机转角
		}
		else
		{
			motordebgDlg.m_bMotorVmove=TRUE;//连续运动
		}

		if (m_nModlgdebRtype==1)
		{
			motordebgDlg.m_bMotorRtype=FALSE;//常速运动
		}
		else
		{
			motordebgDlg.m_dMotorAcc= GetAccrate();//获得升速加速度
			motordebgDlg.m_bMotorRtype=TRUE;//快速运动
		}	
		
		motordebgDlg.MotorRun();
	}
	else
		MessageBox("请先进行设备自检操作!");
}

//暂停电机
void MotorDlgDebg::OnBumodlgdebPause() 
{
	// TODO: Add your control notification handler code here
	CEdit *ed2=(CEdit*) GetDlgItem(IDC_EDMODLGDEB_REALTANGLE);
	// CEdit *ed3=(CEdit*) GetDlgItem(IDC_BUMODLGDEB_REALTANGLE);

	ed2->EnableWindow(FALSE);   //电机实际转角编辑框无效
	// ed3->EnableWindow(FALSE);   //电机实际转角按钮无效

	if (m_nModlgdebStype==1)
		{
			motordebgDlg.m_bMotorStype=FALSE;//立即制动
		}
		else
		{
			motordebgDlg.m_dMotorDec= GetDecrate();//获得降速加速度
			motordebgDlg.m_bMotorStype=TRUE;//光滑制动
		}

	motordebgDlg.MotorStop();
	
}

//获得电机实际转角
void MotorDlgDebg::OnBumodlgdebRealtangle() 
{
	// TODO: Add your control notification handler code here
	CEdit *ed2=(CEdit*) GetDlgItem(IDC_EDMODLGDEB_REALTANGLE);
	ed2->EnableWindow(TRUE);   //电机实际转角编辑框有效
	m_dModlgdebRealtangle = motordebgDlg.MotorRealtangle();//获得实际转角值
	UpdateData(FALSE);//将角度显示在编辑框内
}

//急停电机
void MotorDlgDebg::OnBumodlgdebStop() 
{
	// TODO: Add your control notification handler code here
	CEdit *ed2=(CEdit*) GetDlgItem(IDC_EDMODLGDEB_REALTANGLE);
	// CEdit *ed3=(CEdit*) GetDlgItem(IDC_BUMODLGDEB_REALTANGLE);

	ed2->EnableWindow(FALSE);   //电机实际转角编辑框无效
	// ed3->EnableWindow(FALSE);   //电机实际转角按钮无效
	
	motordebgDlg.m_bMotorStype=FALSE;//立即制动
	motordebgDlg.MotorStop();
	
}

//运动类型选择响应
void MotorDlgDebg::OnSelendokComodlgdebMtype() 
{
	// TODO: Add your control notification handler code here
	CEdit *ed1=(CEdit*) GetDlgItem(IDC_EDMODLGDEB_TANGLE);	
	m_nModlgdebMtype=m_cModlgdebMtype.GetCurSel();
	if (m_nModlgdebMtype==1)
	{		
		ed1->EnableWindow(TRUE);   //电机转角编辑框有效
		//MessageBox("12");
	}
	else
		// m_pModlgdebTangle->EnableWindow(FALSE);   //电机转角编辑框无效
		ed1->EnableWindow(FALSE);   //电机转角编辑框无效	
}

//设备自检
void MotorDlgDebg::OnBumodlgdebIni() 
{
	// TODO: Add your control notification handler code here
	////////////////////////
	//初始化设备
	motordebgDlg.initBoard();
	////////////////////////
	m_bSelfchekflag = TRUE;
	//m_pModlgdebTangle->EnableWindow(FALSE);   //电机转角编辑框有效
}

//结束调试
void MotorDlgDebg::OnBumodlgdebExit() 
{
	// TODO: Add your control notification handler code here
	motordebgDlg.m_bMotorDebugflag=FALSE;//调试电机结束
}

//获得升速加速度
double MotorDlgDebg::GetAccrate()
{
	double accrate;
	m_nModlgdebAcc=m_cModlgdebAcc.GetCurSel();
	switch(m_nModlgdebAcc)
	{
	case 0:
		accrate=1.0/2;
		break;
	case 1:
		accrate=1.0/3;
		break;
	case 2:
		accrate=1.0/4;
		break;
	case 3:
		accrate=1.0/5;
		break;
	case 4:
		accrate=1.0/6;
		break;
	case 5:
		accrate=1.0/8;
		break;
	case 6:
		accrate=1.0/10;
		break;
	case 7:
		accrate=1.0/20;
		break;	
	default:
		accrate=1.0/8;
		break;
	}
	return accrate;
}

//获得降速加速度
double MotorDlgDebg::GetDecrate()
{
	double decrate;
	m_nModlgdebDec=m_cModlgdebDec.GetCurSel();
	switch(m_nModlgdebAcc)
	{
	case 0:
		decrate=1.0/2;
		break;
	case 1:
		decrate=1.0/3;
		break;
	case 2:
		decrate=1.0/4;
		break;
	case 3:
		decrate=1.0/5;
		break;
	case 4:
		decrate=1.0/6;
		break;
	case 5:
		decrate=1.0/8;
		break;
	case 6:
		decrate=1.0/10;
		break;
	case 7:
		decrate=1.0/20;
		break;	
	default:
		decrate=1.0/8;
		break;
	}
	return decrate;
}

//选择速度方式
void MotorDlgDebg::OnSelendokComodlgdebRtype() 
{
	// TODO: Add your control notification handler code here
	CEdit *ed1=(CEdit*) GetDlgItem(IDC_COMODLGDEB_ACC);	
	m_nModlgdebRtype=m_cModlgdebRtype.GetCurSel();
	if (m_nModlgdebRtype==0)
	{		
		ed1->EnableWindow(TRUE);   //电机升速加速度选择列表有效
		//MessageBox("12");
	}
	else		
		ed1->EnableWindow(FALSE);   //电机升速加速度选择列表无效
}

//选择制动方式
void MotorDlgDebg::OnSelendokComodlgdebStype() 
{
	// TODO: Add your control notification handler code here
	CEdit *ed1=(CEdit*) GetDlgItem(IDC_COMODLGDEB_DEC);	
	m_nModlgdebStype=m_cModlgdebStype.GetCurSel();
	if (m_nModlgdebStype==0)
	{		
		ed1->EnableWindow(TRUE);   //电机降速加速度选择列表有效
		//MessageBox("12");
	}
	else		
		ed1->EnableWindow(FALSE);   //电机降速加速度选择列表无效
}

⌨️ 快捷键说明

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