The Enable button: enable an 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.
The method of this button is btnENABLE_Click. You need to enable an axis before you start any motion. First we check whether the EtherCAT link state is ecatOP
, and then we declare the variable Mindex
to receive the index of an axis.
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, the text of the Connect button is changed to "Connect."
To know whether the third bit is true, we use the bitwise AND operator (&). The condition "if (((KSMServo[Mindex].Value & 0x00000004)>>2 == 0x00000001) == false)
" is described below:
- Perform a logical AND operation for the value of the selected axis and the hex value 0x00000004.
- 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 the hex value 0x00000001. If it is false, run the following code.
private void btnENABLE_Click(object sender, EventArgs e)
{
if (KSMStatus.State == EthercatState.ecatOP)
{
int Mindex = lbMList.SelectedIndex;
if (((KSMServo[Mindex].Value & 0x00000004) >> 2 == 0x00000001) == false)
{
//Enables or disables the operation of an axis.
Command = KS_API.WaitForCommand(1, false, motion.PowerAxis(Mindex, true, true, true));
if (!Command.Done)
{
lbErrorStatus.Invoke((MethodInvoker)delegate { lbErrorStatus.Text = "Failed to enable the axis: " + Command.ErrorId.ToString("X"); });
return;
}
}
else
{
//Enables or disables the operation of an axis.
Command = KS_API.WaitForCommand(1, false, motion.PowerAxis(Mindex, false, true, true));
if (!Command.Done)
{
lbErrorStatus.Invoke((MethodInvoker)delegate { lbErrorStatus.Text = "Failed to enable the axis: " + Command.ErrorId.ToString("X"); });
return;
}
}
}
}
private void t_Staus_Tick(object sender, EventArgs e)
{
if (Mindex >= 0)
...........
{
if (((KSMServo[Mindex].Value & 0x00000004) >> 2 == 0x00000001) == true)
{
btnENABLE.Invoke((MethodInvoker)delegate { btnENABLE.Text = "Disable"; });
}
else
{
btnENABLE.Invoke((MethodInvoker)delegate { btnENABLE.Text = "Enable"; });
}
}
else
{
btnConnect.Invoke((MethodInvoker)delegate { btnConnect.Text = "Connect"; });
}
}