啟動鈕:啟動軸
啟動 (Enable) 鈕的功能為啟用 (Enable) 或停用 (Disable) 鈕依照軸為啟動或停用狀態而定,當軸停用時,點選 Enable 以啟用;當軸已啟用,則點選 Disable 以停用。
下一步,檢查軸狀態,在 StatusWord 物件中,第三位元(位元 2)為 " 操作已啟用 (operation enabled)",若此位元為 true 則軸已啟用。在此案例中,當 Mindex 大於或等於零,且第三位元為 true,則代表至少有一個軸在列表中並為啟動狀態,因此此鈕為 Disable;若第三位元為 false,則鈕為 Enable,使用 PowerAxis 來啟動或停用軸;若 Mindex 小於零,則代表列表中無軸。
欲知第三位元是否為 true,使用位元 AND 運算子 (&),ServoNoFlag 為在 MFC_GUIDlg.cpp 開頭所定義的巨集,條件 "ServoNoFlag(KSMServo[Mindex]) == 1" 如下描述:
- 對選定軸的值和十六進制值 0x4 執行邏輯 AND 運算。
- 取 AND 運算的結果並將值的位元向右移動兩個位置。
- 移動位元後,檢查其是否等於一,若為 true,則執行下列代碼。
define ServoNoFlag(A) ((A & 0x4) >>2) //Get servo onvoid CMFC_GUIDlg::OnBnClickedBtnEn()
{
if (KSMStatus.State == ecatOP)
{
int Mindex = m_MotorList.GetCurSel();
if (ServoNoFlag(KSMServo[Mindex]) == 0)
{
//Enables or disables the operation of an axis.
Command = WaitForCommand(1, FALSE, ::PowerAxis(Mindex, TRUE, TRUE, TRUE));
if (!Command.Done)
{
Str_Error.Format(_T("Failed to enable the axis: 0x%x\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
}
}
else
{
//Enables or disables the operation of an axis.
Command = WaitForCommand(1, FALSE, ::PowerAxis(Mindex, FALSE, TRUE, TRUE));
if (!Command.Done)
{
Str_Error.Format(_T("Failed to enable the axis: 0x%x\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
return;
}
}
}if (nIDEvent == 60) //update timer ID
{
...........
if (KSMStatus.State == ecatOP)
{
...........
}
else
{
SetDlgItemText(IDC_BTN_CONNECT, L"Connect");
}
if (Mindex >= 0)
{
if (ServoNoFlag(KSMServo[Mindex]) == 1)
{
SetDlgItemText(IDC_BTN_EN, L"Disable");
}
else
{
SetDlgItemText(IDC_BTN_EN, L"Enable");
}
}
}