啟動鈕:啟動軸

啟動 (Enable) 鈕的功能為啟用 (Enable)停用 (Disable) 鈕依照軸為啟動或停用狀態而定,當軸停用時,點選 Enable 以啟用;當軸已啟用,則點選 Disable 以停用。

此方法的按鈕為 btnENABLE_Click,開始任何運動前需先啟動軸,首先檢查 EtherCAT 連結狀態是否為 ecatOP,然後宣告變數 Mindex 以接收軸的索引。

接著檢查軸狀態,在 StatusWord 物件中,第三位元(位元 2)為 " 操作已啟用 (operation enabled)",若此位元為 true 則軸已啟用。在此案例中,當 Mindex 大於或等於零,且第三位元為 true,則代表至少有一個軸在列表中並為啟動狀態,因此此鈕為 Disable;若第三位元為 false,則鈕為 Enable,而 PowerAxis 用來啟用與停用軸;而當 Mindex 小於零,代表清單中沒有軸,則 Connect 的文字將變為 "Connect"。

欲知第三位元是否為 true,使用位元 AND 運算子 (&),條件 "if (((KSMServo[Mindex].Value & 0x00000004)>>2 == 0x00000001) == false)" 如下描述:

  1. 對選定軸的值和十六進制值 0x00000004 執行邏輯 AND 運算。
  2. 取 AND 運算的結果並將值的位元向右移動兩個位置。
  3. 移動位元後,檢查其是否等於十六進制值 0x00000001,若為 false,則執行下列代碼。
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"; });
   }
}