The Connect button
This button functions as a Connect or Disconnect button, depending on the current 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 (MainWindow.xaml.cs). When Connect or Disconnect is clicked, we call the Connect method (MainWindowViewModel.cs), which checks whether an EtherCAT connection has been established. If it has, this button functions as Disconnect, otherwise it functions as Connect.
private async void btnConnect_Click(object sender, RoutedEventArgs e)
{
try
{
await ViewModel.Connect();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Connect
In the Connect method, we use ISubsystem.Started to check the connection state. The Subsystem class implements the ISubsystem interface, so we can use all the methods and properties declared in ISubsystem. The property Started checks whether the KINGSTAR Subsystem is started. If it is, the method ISubsystem.Stop is called. If not, the program will start to create an EtherCAT connection and start the KINGSTAR Subsystem. Because we didn't create any EtherCAT link, Started is false, so the button functions as Connect. The else clause is run.
public async Task Connect()
{
if (Subsystem.Started)
{
await Subsystem.Stop().WaitAsync(30000, true);
}
else
{
......
}
}
Set an access mode
Before we start the KINGSTAR Subsystem, we need to configure a few settings. First we use the ISubsystem.Access property to set the data transfer mode to accessPosVel. The list of the access modes can be found in the KsAccessMode enum. An access mode affects the available control modes. For example, accessPosVel allows modeManual and all Pos modes (modeXxxPos), and can be changed to Vel modes (modeXxxVel) later. The list of the control modes can be found in the McControlMode enum. You need to select an access mode and control mode that works best for your axes.
Start the KINGSTAR Subsystem
Next, we set the variable IsConnecting to true, which is a flag representing the state of a connection. We then use the ISubsystem.Start method to establish an EtherCAT connection and start the KINGSTAR Subsystem. Because the Subsystem needs some time to start, we use WaitAsync to wait for it to be completed. We give Start 30,000 milliseconds to finish its job. If it is not done within the time span, the task will be cancelled.
public async Task Connect()
{
if (Subsystem.Started)
{
......
}
else
{
//Use position modes by default but can be switched to velocity modes.
Subsystem.Access = KsAccessMode.accessPosVel;
IsConnecting = true;
//Establish an EtherCAT connection.
await Subsystem.Start().WaitAsync(30000, true);
......
}
}
The initialization settings
After the KINGSTAR Subsystem has been started, we need to initialize a few settings before the EtherCAT connection is established. We use a foreach loop to convert each axis' IAxis.Unit to degrees, enable IAxis. UnitConversion, and set their IAxis.MotionProfile and IAxis.VelocityPid properties to 360 based on the degrees per revolution. After the configuration is done, the first axis in the axis list is assigned to the SelectedAxis variable, which represents the selected axis. Then, we set IsConnecting to false.
public async Task Connect()
{
if (Subsystem.Started)
{
......
}
else
{
......
foreach (var axis in Subsystem.Axes)
{
/*Convert the unit. The distance unit of the axis is degree
instead of count.*/
axis.Unit = new UnitConversion()
{
Numerator = axis.Resolution,
Denominator = 360 /* 360 degrees */
};
axis.UnitConversion = true;
axis.MotionProfile = McProfileSettings.Default(360);
axis.VelocityPid = McPidSettings.DefaultVelocityPid(360);
}
SelectedAxis = Subsystem.Axes.First();
IsConnecting = false;
}
}
Disconnect
When an EtherCAT link has been created, ISubsystem.Start is true. The button functions as Disconnect. We use ISubsystem.Stop to stop the EtherCAT connection and the KINGSTAR Subsystem. Because the Subsystem needs some time to stop, we give Stop 30,000 milliseconds to finish its job. If it is not done within the time span, the task will be cancelled. When the Subsystem is stopped, the text of the Disconnect button is changed to Connect.
public async Task Connect()
{
if (Subsystem.Started)
{
await Subsystem.Stop().WaitAsync(30000, true);
}
else
{
......
}
}