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

📄 robot_test.cpp

📁 FIRA 3对3 机器人足球PC端控制程序。
💻 CPP
字号:
// robot_test.cpp : implementation file
//

#include "stdafx.h"
#include "Vision.h"
#include "robot_test.h"
#include "comm.h"
BYTE Command[8];
CComm	 Rs232_port;

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

/////////////////////////////////////////////////////////////////////////////
// Crobot_test dialog


Crobot_test::Crobot_test(CWnd* pParent /*=NULL*/)
	: CDialog(Crobot_test::IDD, pParent)
{
	//{{AFX_DATA_INIT(Crobot_test)
	m_robot1_left = 0;
	m_robot2_left = 0;
	m_robot1_right = 0;
	m_robot2_right = 0;
	m_robot3_left = 0;
	m_robot3_right = 0;
	//}}AFX_DATA_INIT
}


void Crobot_test::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(Crobot_test)
	DDX_Control(pDX, IDC_SCROLLBAR6, m_srobot3_right);
	DDX_Control(pDX, IDC_SCROLLBAR5, m_srobot3_left);
	DDX_Control(pDX, IDC_SCROLLBAR4, m_srobot2_right);
	DDX_Control(pDX, IDC_SCROLLBAR3, m_srobot2_left);
	DDX_Control(pDX, IDC_SCROLLBAR2, m_srobot1_right);
	DDX_Control(pDX, IDC_SCROLLBAR1, m_srobot1_left);
	DDX_Text(pDX, IDC_ROBOT1_L_EDIT, m_robot1_left);
	DDX_Text(pDX, IDC_ROBOT2_L_EDIT, m_robot2_left);
	DDX_Text(pDX, IDC_ROBOT1_R_EDIT, m_robot1_right);
	DDX_Text(pDX, IDC_ROBOT2_R_EDIT, m_robot2_right);
	DDX_Text(pDX, IDC_HGOALIE_L_EDIT, m_robot3_left);
	DDX_Text(pDX, IDC_HGOALIE_R_EDIT, m_robot3_right);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(Crobot_test, CDialog)
	//{{AFX_MSG_MAP(Crobot_test)
	ON_BN_CLICKED(IDC_STOP_BUTTON, OnStopButton)
	ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton)
	ON_BN_CLICKED(IDC_SEND_BUTTON, OnSendButton)
	ON_BN_CLICKED(IDC_MINUS_BUTTON, OnMinusButton)
	ON_BN_CLICKED(IDC_VEL_INVERS_BUTTON, OnVelInversButton)
	ON_BN_CLICKED(IDC_TURN_BUTTON, OnTurnButton)
	ON_WM_HSCROLL()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_ZERO_BUTTON, OnZeroButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Crobot_test message handlers

void Crobot_test::OnStopButton() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	Command[0] = 0x0a;
	Command[1] = 0x7f;
	Command[2] = 0x7f;
	Command[3] = 0x7f;
	Command[4] = 0x7f;
	Command[5] = 0x7f;
	Command[6] = 0x7f;
	Command[7] = 0x0a;
	
	Rs232_port.WriteCommBlock((LPSTR)Command, 8);
}

BOOL Crobot_test::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	UpdateData(true);
	m_srobot1_left.SetScrollRange(-127,127);
	m_srobot1_right.SetScrollRange(-127,127);
	m_srobot2_left.SetScrollRange(-127,127);
	m_srobot2_right.SetScrollRange(-127,127);
	m_srobot3_left.SetScrollRange(-127,127);
	m_srobot3_right.SetScrollRange(-127,127);

	m_srobot1_left.SetScrollPos(0);
	m_srobot1_right.SetScrollPos(0);
	m_srobot2_left.SetScrollPos(0);
	m_srobot2_right.SetScrollPos(0);
	m_srobot3_left.SetScrollPos(0);
	m_srobot3_right.SetScrollPos(0);

	
	Rs232_port.SetComPort(1,19200,8,0,0);
	Rs232_port.CreateCommInfo();
	Rs232_port.OpenComPort();
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void Crobot_test::OnOK() 
{
	// TODO: Add extra validation here
	OnStopButton();
	OnStopButton();
	Rs232_port.DestroyComm();
	
	CDialog::OnOK();
}

void Crobot_test::OnCancel() 
{
	// TODO: Add extra cleanup here
	OnStopButton();
	OnStopButton();
	Rs232_port.DestroyComm();
	
	CDialog::OnCancel();
}

void Crobot_test::OnAddButton() 
{
	// TODO: Add your control notification handler code here
	if (m_robot1_left <127)	m_robot1_left += 10;
		else m_robot1_left = 127;
	if (m_robot1_right <127)	m_robot1_right += 10;
		else m_robot1_right = 127;
	if (m_robot2_left <127)	m_robot2_left += 10;
		else m_robot1_left = 127;
	if (m_robot2_right <127)	m_robot2_right += 10;
		else m_robot2_right = 127;
	if (m_robot3_left <127)	m_robot3_left += 10;
		else m_robot1_left = 127;
	if (m_robot3_right <127)	m_robot3_right += 10;
		else m_robot1_right = 127;
	
	m_srobot1_left.SetScrollPos(m_robot1_left);
	m_srobot1_right.SetScrollPos(m_robot1_right);
	m_srobot2_left.SetScrollPos(m_robot2_left);
	m_srobot2_right.SetScrollPos(m_robot2_right);
	m_srobot3_left.SetScrollPos(m_robot3_left);
	m_srobot3_right.SetScrollPos(m_robot3_right);
	UpdateData(false);
}

void Crobot_test::OnSendButton() 
{
	// TODO: Add your control notification handler code here
	

	UpdateData(TRUE);

	Command[0] = 0x00;
	Command[1] = -(unsigned char)m_robot1_left +127;
	Command[2] = -(unsigned char)m_robot1_right + 127;
	Command[3] = -(unsigned char)m_robot2_left+127;
	Command[4] = -(unsigned char)m_robot2_right+127;
	Command[5] = -(unsigned char)m_robot3_left+127;
	Command[6] = -(unsigned char)m_robot3_right+127;
	Command[7] = 0x00;


	Rs232_port.WriteCommBlock ((LPSTR)Command, 8);

}

void Crobot_test::OnMinusButton() 
{
	// TODO: Add your control notification handler code here
	if (m_robot1_left > -127)	m_robot1_left -= 10;
		else m_robot1_left = -127;
	if (m_robot1_right >-127)	m_robot1_right -= 10;
		else m_robot1_right = -127;
	if (m_robot2_left > -127)	m_robot2_left -= 10;
		else m_robot2_left = -127;
	if (m_robot2_right >-127)	m_robot2_right -= 10;
		else m_robot2_right = -127;
	if (m_robot3_left > -127)	m_robot3_left -= 10;
		else m_robot3_left = -127;
	if (m_robot3_right >-127)	m_robot3_right -= 10;
		else m_robot3_right = -127;
	m_srobot1_left.SetScrollPos(m_robot1_left);
	m_srobot1_right.SetScrollPos(m_robot1_right);
	m_srobot2_left.SetScrollPos(m_robot2_left);
	m_srobot2_right.SetScrollPos(m_robot2_right);
	m_srobot3_left.SetScrollPos(m_robot3_left);
	m_srobot3_right.SetScrollPos(m_robot3_right);
	UpdateData(false);
}

void Crobot_test::OnVelInversButton() 
{
	// TODO: Add your control notification handler code here
	m_robot1_left = -m_robot1_left;
	m_robot1_right = -m_robot1_right;
	m_robot2_left = -m_robot2_left;
	m_robot2_right = -m_robot2_right;
	m_robot3_left = -m_robot3_left;
	m_robot3_right = -m_robot3_right;
	UpdateData(false);

	Command[0] = 0x00;
	Command[1] = -(unsigned char)m_robot1_left +127;
	Command[2] = -(unsigned char)m_robot1_right + 127;
	Command[3] = -(unsigned char)m_robot2_left+127;
	Command[4] = -(unsigned char)m_robot2_right+127;
	Command[5] = -(unsigned char)m_robot3_left+127;
	Command[6] = -(unsigned char)m_robot3_right+127;
	Command[7] = 0x00;

	m_srobot1_left.SetScrollPos(m_robot1_left);
	m_srobot1_right.SetScrollPos(m_robot1_right);
	m_srobot2_left.SetScrollPos(m_robot2_left);
	m_srobot2_right.SetScrollPos(m_robot2_right);
	m_srobot3_left.SetScrollPos(m_robot3_left);
	m_srobot3_right.SetScrollPos(m_robot3_right);
	Rs232_port.WriteCommBlock ((LPSTR)Command, 8);
}

void Crobot_test::OnTurnButton() 
{
	// TODO: Add your control notification handler code here
	SetTimer(1,50,0);
}

void Crobot_test::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	int pos,nID,index;
    nID	= GetWindowLong(pScrollBar->m_hWnd, GWL_ID);		
	index =nID-IDC_SCROLLBAR1;
	pos = pScrollBar->GetScrollPos();
    switch(nSBCode){
	case  SB_LEFT :
			pos = 0;
			break;
	case  SB_RIGHT :
			pos = 255;
			break;
    case  SB_LINELEFT:
			pos=pos-1;
			break;
	case  SB_LINERIGHT:
			pos=pos+1;
			break;
    case  SB_PAGELEFT:
            pos = pos-5;
			break;
	case  SB_PAGERIGHT:		
			pos = pos +5;
			break;
	case  SB_THUMBPOSITION:
			pos = nPos;
			break;
	case  SB_THUMBTRACK:
			break;
	}
	if (pos < -127) pos = -127;
	if (pos >127) pos = 127;
	pScrollBar->SetScrollPos(pos);
    switch(index){
	case 0 :
		m_robot1_left = pos;
		break;
	case 1 :
		m_robot1_right = pos;
		break;
	case 2 :
		m_robot2_left = pos;
		break;
	case 3 : 
	    m_robot2_right = pos; 
		break;
	case 4 :
		m_robot3_left = pos;
		break;
	case 5 : 
	    m_robot3_right = pos; 
		break;
	}


	UpdateData(FALSE); 
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
int motor_flg;
void Crobot_test::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnTimer(nIDEvent);
	unsigned char command[8];

    motor_flg++;

	UpdateData(TRUE);
    if (motor_flg == 15) {
		motor_flg = 0;
		m_robot1_left = -m_robot1_left;
		m_robot1_right = -m_robot1_right;
		m_robot2_left = -m_robot2_left;
		m_robot2_right = -m_robot2_right;
		m_robot3_left = -m_robot3_left;
		m_robot3_right = -m_robot3_right;
	}
	command[0] = 0x00;
	command[1] = (unsigned char)m_robot1_left+127;
	command[2] = (unsigned char)m_robot1_right+127;
	command[3] = (unsigned char)m_robot2_left+127;
	command[4] = (unsigned char)m_robot2_right+127;
	command[5] = (unsigned char)m_robot3_left+127;
	command[6] = (unsigned char)m_robot3_left+127;
	command[7] = 0x00;

	Rs232_port.WriteCommBlock ((LPSTR)command, 8);

	UpdateData(false);
}

void Crobot_test::OnZeroButton() 
{
	// TODO: Add your control notification handler code here
	m_robot1_left = 0;
	m_robot1_right = 0;
	m_robot2_left = 0;
	m_robot2_right = 0;
	m_robot3_left = 0;
	m_robot3_right = 0;
	m_srobot1_left.SetScrollPos(m_robot1_left);
	m_srobot1_right.SetScrollPos(m_robot1_right);
	m_srobot2_left.SetScrollPos(m_robot2_left);
	m_srobot2_right.SetScrollPos(m_robot2_right);
	m_srobot3_left.SetScrollPos(m_robot3_left);
	m_srobot3_right.SetScrollPos(m_robot3_right);
	UpdateData(false);
}

⌨️ 快捷键说明

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