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 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 a Disconnect, otherwise it functions as a 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 is used to start the thread.
Dim T_LinkStart = New Thread(AddressOf 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 Sub LinkStart()
.........
End Sub
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.
KS_API
– an instance of the Api class.Private Command As KsCommandStatus = Nothing
– 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 useCommand
to get the state of a method.Private Code As KsError = KsError.errNoError
– KsError is an enum type that contains all error codes of KINGSTAR. We useCode
to receive the return value of a method.errNoError
means the method runs successfully. IfCode
is 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.
'KS API Start
Code = KS_API.Create(0, 0)
If Code <> KsError.errNoError Then
KS_API.Destroy()
Return
End If
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 Then
KS_API.Destroy()
Return
End If
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 Then
KS_API.Destroy()
Return
End If
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 Then
KS_API.Destroy()
Return
End If
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 Then
KS_API.Destroy()
Return
End If
Code = KS_API.EnableAxisOutput(True)
If Code <> KsError.errNoError Then
KS_API.Destroy()
Return
End If
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.
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 Not Command.Done Then
KS_API.Destroy()
Return
End If
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 Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
If KSMStatus.State = EthercatState.ecatOP Then
'Stops the EtherCAT network and the KINGSTAR Subsystem.
KS_API.Stop()
btnConnect.Text = "Connect"
Return
End If
.........
End Sub