啟用多應用程式時的 KINGSTAR 子系統狀態檢查
KINGSTAR 支援多應用程式同時連結,您可以開發多款應用程式,並將這些應用程式連結到同一個 KINGSTAR 子系統,這些應用程式可以使用不同的程式語言進行開發,比如 C/C++ 和 Windows .NET。由於 KINGSTAR 支援 OPC UA 通訊協定,您亦可將遠端電腦的應用程式透過 OPC UA 連結到 KINGSTAR 子系統。
注意:自 KINGSTAR 4.4 開始,KINGSTAR 動態連結函式庫 (DLLs) 中的 .NET API 及 .NET Class 介面可供遠端電腦使用。
下圖展示了在不同電腦開發的多款應用程式可同時連結到同一個 KINGSTAR 子系統。在此我們將有安裝 KINGSTAR Runtime 的電腦稱為本機電腦;而另一台電腦(沒有安裝 KINGSTAR Runtime)稱為遠端電腦。由於本機電腦已安裝了 KINGSTAR Runtime,透過核心的分配將電腦區分為 RTX64 及 Windows 兩個系統,而 KINGSTAR 子系統運作在 RTX64 系統上。假設我們在 RTX64 及 Windows 系統分別開發了兩個應用程式,App 1 與 App 2 (RTX64);App 3 與 App 4 (Windows),且在遠端電腦上開發了 App 5 與 App 6,所有的應用程式,都可同時連結到本機電腦上的 KINGSTAR 子系統。
當我們將應用程式連結到 KINGSTAR 子系統時,會先執行一些程序,比如啟動 RTX64 的 RTSS (Real-Time Subsystem) 子系統,接著啟動 KINGSTAR 子系統 或 EtherCAT 網路。假設同時有三個應用程式 (App 1, App 2, App 3) 欲連結到 KINGSTAR 子系統,此時 App 1 首先連結到 KINGSTAR 子系統,並啟動了 RTSS 子系統與 KINGSTAR 子系統,當 App 2 要連結到 KINGSTAR 子系統時,就不需要再做一次啟動 RTSS 子系統與 KINGSTAR 子系統的動作,同理,當 App 3 要連結到 KINGSTAR 子系統時,也不需要做啟動 RTSS 子系統與 KINGSTAR 子系統的動作。
這就是為何我們需要為應用程式設定一個機制,即在連結到 KINGSTAR 子系統前,先行檢查 KINGSTAR 子系統的目前狀態,應用程式才能知道接下來可執行什麼動作。而這樣的機制,可以透過使用 KINGSTAR API 來達成。您可以使用 KINGSTAR API 取得 KINGSTAR 全域記憶體中的所有變數,包含 KINGSTAR 子系統的狀態。KINGSTAR 全域記憶體由 KINGSTAR 子系統控制,可供使用不同程式語言開發的應用程式彼此溝通。
以下範例展示了連接到 KINGSTAR 子系統的基本過程,同時示範了如何使用 API 來配置子系統和控制 EtherCAT 網路與運動控制。在呼叫 Create 時,它將自動檢查 KINGSTAR 子系統是否已啟動;您可使用 GetStatus 來檢查 EtherCAT 的狀態。
範例
//////////////////////////////////////////////////////////////////
//
// This code snippet demonstrates a basic flow of how to link to
// KINGSTAR subsystem and use the APIs to configure subsystem and
// control the EtherCAT network and motion devices.
//
//////////////////////////////////////////////////////////////////
KsError nRet = errNoError;
KsCommandStatus Command = { 0 };
SubsystemStatus Subsystem = { ecatOffline, ecatOffline, 0, 0, 0, {ecatOffline}, {ecatOffline}, {axisOffline} };
// Link to the KINGSTAR subsystem.
// If the KINGSTAR subsystem does not exist, it would create a new KINGSTAR subsystem.
// You have to call this API first before you use other KINGSTAR functions.
nRet = Create(0, 0);
// Check if the subsystem is started.
// A new KINGSTAR subsystem is not started by default.
nRet = GetStatus(&Subsystem, NULL);
if (Subsystem.State == ecatOP)
{
RtPrintf("EtherCAT already started: %x\n", nRet);
}
else if (nRet == errNoError && Subsystem.State == ecatOffline)
{
// When the subsystem state is ecatOffline, you can use below APIs to configure settings:
//
// Subsystem Configuration APIs
// Axis Variable APIs
// AddModuleConfiguration()
// RemoveModuleConfiguration()
// SetConfigurationAxseCount()
// SetConfigurationIoCount()
//
// If you just want to use the default setting, skip this part and start the subsystem.
// Start the subsystem. It would change the subsystem state to ecatOP.
// This function is asynchronous so the network is not started yet when it returns.
// Use WaitForCommand() for synchronization.
Command = WaitForCommand(30, TRUE, Start());
}
// Now the subsystem state is ecatOP, you can use below APIs to control the subsystem and devices:
//
// Subsystem Control
// Slave Control
// Axis Control
// IO Control
// Heartbeat
// COM API
// Motion API
//
// You can also change the subsystem state or slave state.
// Please check "Usable EtherCAT states" section and the API pages to find out
// which functions you can use in different states.
// Stop the EtherCAT network before destroying KINGSTAR Subsystem or before re-configuration.
Command = WaitForCommand(5, FALSE, Stop());
if (Command.Error)
{
RtPrintf("Stop Failed: %d\n", Command.ErrorId);
}
// Terminates the KINGSTAR Subsystem if there is no other application connected to it.
nRet = Destroy();
if (nRet != errNoError)
{
RtPrintf("Destroy Failed: %x\n", nRet);
}