Link states and progress bar

When we click Connect, the KINGSTAR Subsystem starts to link to hardware. To display the link state, we use a Label and Progress Bar. The label's name is statusLabel and the progress bar's is statusProgress.

Label: link states

An EtherCAT link has four states: Init, Pre-Operational (Pre-Op), Safe-Operational (Safe-Op), Operational (Op). When the state is Op, the connection has been created. To display these states during linking, we use getecState to get the state of the EtherCAT link. After we click Connect, the program checks whether the EtherCAT state is Op. statusLabel reflects the corresponding the state through setText.

The following code is in QtGui.cpp:

void QtGui::btnConnectClicked()
{
   int Timeout = 0;

   ui->btnConnect->setEnabled(false);

   if (ks->getwkState() == disconnected)
   {
      statusProgress->setMaximum(0);
      statusProgress->setMinimum(0);

      while (ks->getecState() != ecatOP && Timeout < 2000)
      {
         ...........

         if (ks->getecState() == ecatInit) statusLabel->setText("EC state: Init");

         if (ks->getecState() == ecatPreOP) statusLabel->setText("EC state: PreOP");

         if (ks->getecState() == ecatSafeOP) statusLabel->setText("EC state: SafeOP");

         Timeout++;

         Sleep(100);
      }
      ...........
      statusLabel->setText("EC state: OP");
      ...........
   }
   else
   {
      ...........
   }
   ...........
}

Progress Bar: the linking progress

A progress bar shows a process that requires long time to finish. In Qt, you can customize its border, chunk, and text-align using setStyleSheet, determine whether the text of the completed percentage should be displayed using setTextVisible, and set the maximum and minimum steps of the progress bar using the setMaximum and setMinimum functions.

The following code is in QtGui.cpp:

statusProgress->setTextVisible(false);
statusProgress->setMaximum(100);
statusProgress->setMinimum(0);
statusProgress->setStyleSheet(          \
   "QProgressBar {                      \
      border: 2px solid grey;           \
      border-radius: 5px;               \
      text-align: center;               \
   }                                    \
                                        \
   QProgressBar::chunk {                \
      background-color: #16A0C0;        \
      width: 10px;                      \
      margin: 0.5px;                    \
}");

After we click Connect, getwkState will detect the connection state by returning wkState. In ksworker.h, the connection state is defined in the workerState enum. ks is a ksWorker object. If the connection state is disconnected, the maximum and minimum steps of the progress bar are zero.

The following code is in ksworker.cpp:

workerState ksWorker::getwkState()
{
   return wkState;
}

The following code is in QtGui.cpp:

void QtGui::btnConnectClicked()
{
   ...........
   if (ks->getwkState() == disconnected)
   {
      statusProgress->setMaximum(0);
      statusProgress->setMinimum(0);
   }
   ...........
}

After the EtherCAT state becomes Op, the maximum step of the progress bar is set to 100. We use setValue to set the current value of the progress bar to 100.

The following code is in QtGui.cpp:

statusLabel->setText("EC state: OP");
statusProgress->setMaximum(100);
statusProgress->setValue(100);

If the EtherCAT state is not Op, the current value of the progress bar is set to zero.

else
{
   ...........
   statusProgress->setValue(0);
   ...........
}