Skip to content

Commit 607eeeb

Browse files
author
Felix Exner
committed
Modernize timeout interface
This should be more clear to API users what the timeouts actually mean and this should be more platform independent to use std::this_thread::sleep_for instead of usleep.
1 parent f146074 commit 607eeeb

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

include/ur_client_library/ur/dashboard_client.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,30 @@ class DashboardClient : public comm::TCPSocket
9494
*
9595
* \param command Command that will be sent to the server
9696
* \param expected Expected replay
97-
* \param timeout Timeout time in seconds
97+
* \param timeout Timeout to wait before the command is considered failed.
9898
*
9999
* \return True if the reply was as expected within the timeout time
100100
*/
101-
bool waitForReply(const std::string& command, const std::string& expected, double timeout = 30.0);
101+
bool waitForReply(const std::string& command, const std::string& expected,
102+
std::chrono::duration<double> timeout = std::chrono::seconds(30));
102103

103104
/*!
104-
* \brief Keep Sending the requesting Command and wait until it returns the expected answer
105+
* \brief Keep Sending the requesting Command and wait until it returns the expected answer.
105106
*
106107
* \param requestCommand Request command that will be sent to the server
107108
* \param requestExpectedResponse The expected reply to the request
108109
* \param waitRequest The status request
109110
* \param waitExpectedResponse The expected reply on the status
110-
* \param timeout Timeout time in seconds
111+
* \param timeout Timeout before the command is ultimately considered failed
112+
* \param retry_period Retries will be done with this period
111113
*
112114
* \return True when both the requested command was receive with the expected reply as well as the resulting status
113115
* also is as expected within the timeout time
114116
*/
115117
bool retryCommand(const std::string& requestCommand, const std::string& requestExpectedResponse,
116-
const std::string& waitRequest, const std::string& waitExpectedResponse, unsigned int timeout);
118+
const std::string& waitRequest, const std::string& waitExpectedResponse,
119+
const std::chrono::duration<double> timeout,
120+
const std::chrono::duration<double> retry_period = std::chrono::seconds(1));
117121

118122
/*!
119123
* \brief Send Power off command
@@ -125,11 +129,12 @@ class DashboardClient : public comm::TCPSocket
125129
/*!
126130
* \brief Send Power on command
127131
*
128-
* \param timeout Timeout in seconds
132+
* \param timeout Timeout in seconds - The robot might take some time to boot before this call can
133+
* be made successfully.
129134
*
130135
* \return True succeeded
131136
*/
132-
bool commandPowerOn(unsigned int timeout = 1200);
137+
bool commandPowerOn(const std::chrono::duration<double> timeout = std::chrono::seconds(300));
133138

134139
/*!
135140
* \brief Send Brake release command

src/ur/dashboard_client.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
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+
3538
namespace urcl
3639
{
3740
DashboardClient::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

162166
bool 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

Comments
 (0)