連接鈕
連接 (Connect) 鈕功能為 Connect 或 Disconnect 鈕依硬體連接狀態而定, 點擊 Connect 以將 KINGSTAR 子系統連接至硬體;若硬體已連接,擇點選 Disconnect 以斷開連結。
此鈕的函式為 OnBnClickedBtnConnect,連結至硬體前,檢查 KSMStatus.State,KSMStatus 為 SubsystemStatus 類型之實例。我們想知道 EtherCAT 連接是否已建立 (ecatOP),若已建立,則按鈕函式為 Disconnect,否則為 Connect。在對話方框顯示前, OnInitDialog 用來初始化所有 MFC 控制,其呼叫 ID 為 60 的計時器,而 OnTimer 為在指定時間間隔呼叫的函式,在此範例中,每 5 毫秒呼叫一次該計時器,呼叫後將擷取 List Box 的項目並獲取 KINGSTAR 子系統的狀態。
BOOL CMFC_GUIDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetTimer(60, 5, NULL); //nIDEvent=60, interval=5
}void CMFC_GUIDlg::OnTimer(UINT_PTR nIDEvent)
{
.........
if (nIDEvent == 60) //update timer ID
{
int Mindex = m_MotorList.GetCurSel();
//Gets the state of the EtherCAT link.
::GetStatus(&KSMStatus, NULL);
if (KSMStatus.State == ecatOffline)
{
GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"Offline");
}
if (KSMStatus.State == ecatInit)
{
GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"Init");
}
if (KSMStatus.State == ecatPreOP)
{
GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"PreOP");
}
if (KSMStatus.State == ecatSafeOP)
{
GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"SafeOP");
}
if (KSMStatus.State == ecatOP)
{
GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"OP");
GetDlgItem(IDC_BTN_CONNECT)->SetWindowText(L"Disconnect");
.........
}
.........
}
}void CMFC_GUIDlg::OnBnClickedBtnConnect()
{
if (KSMStatus.State == ecatOP)
{
::Stop(); //Stops the EtherCAT network and the KINGSTAR Subsystem.
SetDlgItemText(IDC_BTN_CONNECT, L"Connect");
return;
}
...........
}
連接 (Connect)
作為連接 (Connect) 鈕,我們設定了計時器以監測連接狀態,在進度列 (progress bar) 中呼叫此計時器,其每 1000 毫秒被觸發一次,接著我們使用一系列 KINGSTAR 函式來初始化 EtherCAT 和運動控制設定,然後建立及開啟 EtherCAT 連結,旗標 OPFlag 代表連結的狀態。
SetTimer(55, 100, NULL); //nIDEvent = 55, interval, progress bar
OPFlag = false;
初始化 KINGSTAR 子系統之連結
首先呼叫 Create,準備連接應用程式至 KINGSTAR 子系統,開始任何動作前,必須最先呼叫 Create。
Code = ::Create(0, 0);
if (Code != errNoError)
{
//RtPrintf("Failed to create: 0x%x\n", Code);
Str_Error.Format(_T("Failed to stop EtherCAT: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
::Destroy();
return;
}
設定 EtherCAT 循環時間
SetCycleTime設定 EtherCAT 循環時間(秒),欲使用低於 1 毫秒的循環時間,須備有高速計時器套件。非所有軸皆可支援快速循環時間,若選擇了不支援的循環時間,則每個軸的更新時間會自動且各自延長,欲使用快速循環時間,請確保電腦上的網卡可使用,只有具有低延遲的網卡才可支持快速循環。
//EtherCAT cycle time in seconds.
Code = ::SetCycleTime(cycle1000);
if (Code != errNoError)
{
Str_Error.Format(_T("Failed to set SetCycleTime: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
::Destroy();
return;
}
停用 RTX64 伺服主控台 (Server Console) 上的記錄
EnableServerLog啟用或停用 RTX64 Server Console 上的即時訊息,若將其停用,則主控台將只顯示 KINGSTAR 訊息,在此範例中我們選擇將其啟用。
//Disable logs in the RTX64 server console.
Code = ::EnableServerLog(true);
if (Code != errNoError)
{
Str_Error.Format(_T("Failed to EnableServerLog: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
::Destroy();
return;
}
設定存取模式
SetAxisAccessMode設定 EtherCAT 驅動器之資料傳送模式,存取模式決定驅動器可用的控制模式,存取模式可在 KsAccessMode 類型中選擇,預設之存取模式為 accessVelPos,
//Configure the different modes used with the axes.
Code = ::SetAxisAccessMode(accessVelPos);
if (Code != errNoError)
{
Str_Error.Format(_T("Failed to SetAxisAccessMode: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
::Destroy();
return;
}
啟用軸之數位輸入
EnableAxisInput啟用或停用軸之數位輸入的存取,首三個輸入位元為負超程 (Overtravel)、正超程與原點復歸感測器,若輸入啟用後可使用超程位元。
Code = ::EnableAxisInput(TRUE);
if (Code != errNoError)
{
Str_Error.Format(_T("Failed to EnableAxisInput: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
::Destroy();
return;
}
設定模擬軸的數量
SetConfiguredAxesCount設定模擬軸的數量,我們將數量設為一,若沒有真實軸,將為 EtherCAT 網路建立一個模擬軸。
//Set Configured Axes Count
::SetConfiguredAxesCount(1);
啟用實際速度
EnableActualVelocity讀取軸的實際速度。
//Enable to read ActualVelocity
::EnableActualVelocity(true);
開啟 KINGSTAR 子系統
Start開啟 KINGSTAR 子系統與 EtherCAT 網路。
//Start Ethercat Link
::Start();
斷開連結 (Disconnect)
作為斷連 (Disconnect) 鈕,Stop 用來停止 EtherCAT 連接和 KINGSTAR 子系統,SetDlgItemText 用來更改該鈕的顯示名稱。
void CMFC_GUIDlg::OnBnClickedBtnConnect()
{
if (KSMStatus.State == ecatOP)
{
::Stop(); //Stops the EtherCAT network and the KINGSTAR Subsystem.
SetDlgItemText(IDC_BTN_CONNECT, L"Connect");
return;
}
...........
}