启用多应用程式时的 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);
}