2727// ----------------------------------------------------------------------
2828
2929#include < regex>
30+ #include < thread>
3031#include < unistd.h>
3132#include < ur_client_library/log.h>
3233#include < ur_client_library/ur/dashboard_client.h>
3334#include < ur_client_library/exceptions.h>
3435
36+ using namespace std ::chrono_literals;
37+
3538namespace urcl
3639{
3740DashboardClient::DashboardClient (const std::string& host) : host_(host), port_(DASHBOARD_SERVER_PORT)
@@ -131,14 +134,15 @@ bool DashboardClient::sendRequest(const std::string& command, const std::string&
131134 return ret;
132135}
133136
134- bool DashboardClient::waitForReply (const std::string& command, const std::string& expected, double timeout)
137+ bool DashboardClient::waitForReply (const std::string& command, const std::string& expected,
138+ const std::chrono::duration<double > timeout)
135139{
136- const unsigned int TIME_STEP_SIZE_US ( 100000 ); // 100ms
140+ const std::chrono::duration< double > wait_period = 100ms;
137141
138- double count = 0 ;
142+ std::chrono::duration< double > time_done ( 0 ) ;
139143 std::string response;
140144
141- while (count < timeout)
145+ while (time_done < timeout)
142146 {
143147 // Send the request
144148 response = sendAndReceive (command + " \n " );
@@ -150,8 +154,8 @@ bool DashboardClient::waitForReply(const std::string& command, const std::string
150154 }
151155
152156 // wait 100ms before trying again
153- usleep (TIME_STEP_SIZE_US );
154- count = count + ( 0.000001 * TIME_STEP_SIZE_US) ;
157+ std::this_thread::sleep_for (wait_period );
158+ time_done += wait_period ;
155159 }
156160
157161 URCL_LOG_WARN (" Did not got the expected \" %s\" respone within the timeout. Last respone was: \" %s\" " ,
@@ -161,20 +165,20 @@ bool DashboardClient::waitForReply(const std::string& command, const std::string
161165
162166bool DashboardClient::retryCommand (const std::string& requestCommand, const std::string& requestExpectedResponse,
163167 const std::string& waitRequest, const std::string& waitExpectedResponse,
164- unsigned int timeout)
168+ const std::chrono::duration<double > timeout,
169+ const std::chrono::duration<double > retry_period)
165170{
166- const double RETRY_EVERY_SECOND (1.0 );
167- unsigned int count (0 );
171+ std::chrono::duration<double > time_done (0 );
168172 do
169173 {
170174 sendRequest (requestCommand, requestExpectedResponse);
171- count++ ;
175+ time_done += retry_period ;
172176
173- if (waitForReply (waitRequest, waitExpectedResponse, RETRY_EVERY_SECOND ))
177+ if (waitForReply (waitRequest, waitExpectedResponse, retry_period ))
174178 {
175179 return true ;
176180 }
177- } while (count < timeout);
181+ } while (time_done < timeout);
178182 return false ;
179183}
180184
@@ -183,7 +187,7 @@ bool DashboardClient::commandPowerOff()
183187 return sendRequest (" power off" , " Powering off" ) && waitForReply (" robotmode" , " Robotmode: POWER_OFF" );
184188}
185189
186- bool DashboardClient::commandPowerOn (unsigned int timeout)
190+ bool DashboardClient::commandPowerOn (const std::chrono::duration< double > timeout)
187191{
188192 return retryCommand (" power on" , " Powering on" , " robotmode" , " Robotmode: IDLE" , timeout);
189193}
0 commit comments