The Enable button: enable the axis
The Enable button functions as an Enable or Disable button, depending on whether the axis is enabled or disabled. When an axis is disabled, click Enable to enable it. If an axis is already enabled, click Disable to disable it.
Next, we check the state of the axis. In the StatusWord object, the third bit (bit 2) is "operation enabled." If this bit is true, the axis has been enabled. In this case, when Mindex
is equal to or greater than zero, and the third bit is true, it means there is at least one axis is in the list and the axis has been enabled, so the button is Disable. If the third bit is false, the button is Enable. PowerAxis is used to enable and disable the axis. If the Mindex
is less than zero, it means there is no axis in the list.
To know whether the third bit is true, we use the bitwise AND operator (&). ServoNoFlag
is a macro defined at the beginning of MFC_GUIDlg.cpp
. The condition "ServoNoFlag(KSMServo[Mindex]) == 1
" is described below:
- Perform a logical AND operation for the value of the selected axis and the hex value 0x4.
- Take the result from the AND operation and shift the bits of the value two positions to the right.
- After shifting the bits, check whether it is equal to one. If it is true, run the following code.
define ServoNoFlag(A) ((A & 0x4) >>2) //Get servo on
void 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");
}
}
}