The Connect button
The Connect button functions as a Connect or Disconnect button, depending on the hardware connection status. Click Connect to connect the KINGSTAR Subsystem to hardware. If the hardware is already connected, click Disconnect to disconnect it.
The method of this button is btnConnect_Click. Before we connect to hardware, we check KSMStatus.State. KSMStatus is an instance of the SubsystemStatus class. We want to know whether an EtherCAT connection has already been established. If it has, this button functions as Disconnect, otherwise it functions as Connect.
Connect
As a Connect button, we create a new thread named T_LinkStart, to which we pass the method LinkStart, which will execute on the thread. T_LinkStart is run as a background thread. The method T_LinkStart.Start starts the thread.
var T_LinkStart = new Thread(LinkStart);
T_LinkStart.IsBackground = true;
T_LinkStart.Start();
Configure the KINGSTAR Subsystem
LinkStart contains a series of KINGSTAR methods to initialize, create, and start the EtherCAT link. The following variables are used in LinkStart.
private void LinkStart()
{.........}
Variables
The following variables are declared at the beginning of Form1.vb. These variables detect the state of a method or an EtherCAT link. They are used across the sample. LinkStart is where you see them first.
The following variables are used across the sample. They are declared at the beginning of Form1.cs.
KS_API– an instance of the Api class.KsCommandStatus Command = null– KsCommandStatus is a class that contains the states of a method. You can know whether the method is running, completes its work, returns any error, and what the error is. We useCommandto get the state of a method.KsError Code = KsError.errNoError– KsError is an enum type that contains all error codes of KINGSTAR. We useCodeto receive the return value of a method.errNoErrormeans the method runs successfully. IfCodeis noterrNoError, Destroy will be called and the KINGSTAR Subsystem will be terminated.- OPFlag – the flag signals whether an EtherCAT link has been created.
Initialize the link to the KINGSTAR Subsystem
First, we call Create, which prepares to link your application to the KINGSTAR Subsystem. Before you start anything, Create must be the first method to call.
Code = KS_API.Create(0, 0);
if (Code != KsError.errNoError)
{
lbErrorStatus.Invoke((MethodInvoker)delegate { lbErrorStatus.Text = "Failed to create: " + Code.ToString(); });
KS_API.Destroy();
return;
}
Set the EtherCAT cycle time
SetCycleTime sets the EtherCAT cycle time, in seconds. To use a cycle time less than 1 millisecond, High Speed Timer Package is required. Not all axes support fast cycle times. If an unsupported cycle time is selected, the update time for each axis is automatically and independently extended. To use fast cycle times, ensure that the network card on the computer is capable. Only the network cards with low latencies are able to support fast cycles.
Code = KS_API.SetCycleTime(0.001);
if (Code != KsError.errNoError)
{
lbErrorStatus.Invoke((MethodInvoker)delegate { lbErrorStatus.Text = "Failed to set cycle time: " + Code.ToString(); });
KS_API.Destroy();
return;
}
Disable the logs on the RTX64 server console
EnableServerLog enables or disables the real-time messages on the RTX64 server console. If you disable them, the console will display only KINGSTAR messages. In this sample, we choose to disable it.
Code = KS_API.EnableServerLog(false);
if (Code != KsError.errNoError)
{
lbErrorStatus.Invoke((MethodInvoker)delegate { lbErrorStatus.Text = "Failed to set server log: " + Code.ToString(); });
KS_API.Destroy();
return;
}
Set an access mode
SetAxisAccessMode sets the data transfer mode for EtherCAT drives. The access mode determines the control mode your drives can use. An access mode can be selected from the KsAccessMode enum type. By default, the access mode is accessVelPos.
Code = KS_API.SetAxisAccessMode(KsAccessMode.accessVelPos);
if (Code != KsError.errNoError)
{
lbErrorStatus.Invoke((MethodInvoker)delegate { lbErrorStatus.Text = "Failed to set access mode: " + Code.ToString(); });
KS_API.Destroy();
return;
}
Enable the digital input and output of an axis
EnableAxisInput and EnableAxisOutput enables or disables the access to the digital inputs and outputs of an axis. The first three bits of the inputs are Negative Overtravel, Positive Overtravel, and Home Sensor. If you enable the inputs, you can access the Overtravel bits.
Code = KS_API.EnableAxisInput(true);
if (Code != KsError.errNoError)
{
lbErrorStatus.Invoke((MethodInvoker)delegate { lbErrorStatus.Text = "Failed to set servo inputs: " + Code.ToString(); });
KS_API.Destroy();
return;
}
Code = KS_API.EnableAxisOutput(true);
if (Code != KsError.errNoError)
{
lbErrorStatus.Invoke((MethodInvoker)delegate { lbErrorStatus.Text = "Failed to set servo Outputs: " + Code.ToString(); });
KS_API.Destroy();
return;
}
Set a number of simulated axes
SetConfiguredAxesCount sets a number of simulated axes. We use the variable SERVOCOUNT to set the number to one. If there are no real axes, one simulated axis will be created for the EtherCAT network.
Code = KS_API.SetConfiguredAxesCount(SERVOCOUNT);
Enable actual velocity
EnableActualVelocity reads the actual velocity of an axis.
Code = KS_API.EnableActualVelocity(true);
Start the KINGSTAR Subsystem
We use Start to start the KINGSTAR Subsystem and EtherCAT network. To prevent the situation where the Subsystem fails to start and the program waits forever for it to complete, we use WaitForCommand to set a 30-second timeout for Start. If the KINGSTAR Subsystem can't be started, we use Destroy to close the link to the Subsystem and terminate it.
Command = KS_API.WaitForCommand(30, true, KS_API.Start());
if (!Command.Done)
{
lbErrorStatus.Invoke((MethodInvoker)delegate { lbErrorStatus.Text = "Failed to start EtherCAT: " + Command.ErrorId.ToString(); });
KS_API.Destroy();
return;
}
OPFlag = false;
Disconnect
As a Disconnect button, we use Stop to stop the EtherCAT connection and the KINGSTAR Subsystem. When the Subsystem is stopped, the text of the Disconnect button is changed to Connect.
private void btnConnect_Click(object sender, EventArgs e)
{
if (KSMStatus.State == EthercatState.ecatOP)
{
//Stops the EtherCAT network and the KINGSTAR Subsystem.
KS_API.Stop();
btnConnect.Text = "Connect";
return;
}
.........
}