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 function of this button is OnBnClickedBtnConnect. Before we connect to the hardware, we check KSMStatus.State. KSMStatus is an instance of the SubsystemStatus type. We want to know whether an EtherCAT connection has already been established (ecatOP
). If it has, this button functions as Disconnect, otherwise it functions as Connect. OnInitDialog is used to initialize all MFC controls before the dialog box is displayed. It calls the timer whose ID is 60. OnTimer is a function called at a specific interval. In this sample, this timer is called every 5 milliseconds. When it is called, it retrieves the items in the List Box and gets the state of the KINGSTAR Subsystem.
BOOL CMFC_GUIDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetTimer(60, 5, NULL); //nIDEvent=60, interval=5
}
void CMFC_GUIDlg::OnTimer(UINT_PTR nIDEvent)
{
.........
if (nIDEvent == 60) //update timer ID
{
int Mindex = m_MotorList.GetCurSel();
//Gets the state of the EtherCAT link.
::GetStatus(&KSMStatus, NULL);
if (KSMStatus.State == ecatOffline)
{
GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"Offline");
}
if (KSMStatus.State == ecatInit)
{
GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"Init");
}
if (KSMStatus.State == ecatPreOP)
{
GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"PreOP");
}
if (KSMStatus.State == ecatSafeOP)
{
GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"SafeOP");
}
if (KSMStatus.State == ecatOP)
{
GetDlgItem(IDC_NET_STATUS)->SetWindowText(L"OP");
GetDlgItem(IDC_BTN_CONNECT)->SetWindowText(L"Disconnect");
.........
}
.........
}
}
void CMFC_GUIDlg::OnBnClickedBtnConnect()
{
if (KSMStatus.State == ecatOP)
{
::Stop(); //Stops the EtherCAT network and the KINGSTAR Subsystem.
SetDlgItemText(IDC_BTN_CONNECT, L"Connect");
return;
}
...........
}
Connect
As a Connect button, we set a timer to monitor the connection status. It is called in the progress bar. The timer is triggered every 100 milliseconds. Then we use a series of KINGSTAR functions to initialize EtherCAT and motion control settings, and create and start an EtherCAT link. The flag OPFlag represents the link status.
SetTimer(55, 100, NULL); //nIDEvent = 55, interval, progress bar
OPFlag = false;
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 function to call.
Code = ::Create(0, 0);
if (Code != errNoError)
{
//RtPrintf("Failed to create: 0x%x\n", Code);
Str_Error.Format(_T("Failed to stop EtherCAT: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
::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.
//EtherCAT cycle time in seconds.
Code = ::SetCycleTime(cycle1000);
if (Code != errNoError)
{
Str_Error.Format(_T("Failed to set SetCycleTime: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
::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 enable it.
//Disable logs in the RTX64 server console.
Code = ::EnableServerLog(true);
if (Code != errNoError)
{
Str_Error.Format(_T("Failed to EnableServerLog: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
::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 type. By default, the access mode is accessVelPos
.
//Configure the different modes used with the axes.
Code = ::SetAxisAccessMode(accessVelPos);
if (Code != errNoError)
{
Str_Error.Format(_T("Failed to SetAxisAccessMode: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
::Destroy();
return;
}
Enable the digital input of an axis
EnableAxisInput enables or disables the access to the digital inputs 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 = ::EnableAxisInput(TRUE);
if (Code != errNoError)
{
Str_Error.Format(_T("Failed to EnableAxisInput: %d\n"), Command.ErrorId);
GetDlgItem(IDC_ERROR_RETURN)->SetWindowText(Str_Error);
::Destroy();
return;
}
Set a number of simulated axes
SetConfiguredAxesCount sets a number of simulated axes. We set the number to one. If there are no real axes, one simulated axis will be created for the EtherCAT network.
//Set Configured Axes Count
::SetConfiguredAxesCount(1);
Enable actual velocity
EnableActualVelocity reads the actual velocity of an axis.
//Enable to read ActualVelocity
::EnableActualVelocity(true);
Start the KINGSTAR Subsystem
Start starts the KINGSTAR Subsystem and EtherCAT network.
//Start Ethercat Link
::Start();
Disconnect
As a Disconnect button, Stop is used to stop the EtherCAT connection and the KINGSTAR Subsystem. SetDlgItemText is used to change the button's display name.
void CMFC_GUIDlg::OnBnClickedBtnConnect()
{
if (KSMStatus.State == ecatOP)
{
::Stop(); //Stops the EtherCAT network and the KINGSTAR Subsystem.
SetDlgItemText(IDC_BTN_CONNECT, L"Connect");
return;
}
...........
}