Skip to content

Commit b42da6d

Browse files
authored
Try catch RTDE setup (#285)
In #266, we improved the behavior of the urcl regarding reconnection attempts and timeouts for sockets. However, there are still a few problems (more to come later). I propose to fix one of them here to keep the scope of the PRs small. `RTDEClient::setupCommunication` is called in a while loop, and inside `RTDEClient::setupCommunication` there is a call to `pipeline_->init(...)` which in turn calls `producer_.setupProducer(...)`. `setupProducer` (see [here](https://github.com/UniversalRobots/Universal_Robots_Client_Library/blob/62d34dff101bc460a4d36692405fd076974b8791/include/ur_client_library/comm/producer.h#L67)) throws if the connection of the `stream_` (which is just a tcp socket in my understanding) fails. Hence, if the RTDE sockets are not available, this causes the initialization sequence to terminate immediately and not retry for the desired `max_initialization_attempts`. Now that I have a deeper understanding of the URCL after digging deeper a bit (sorry that I didn't bring that up earlier), I don't fully agree with the `rtde_initialization_attempts` and `rtde_initialization_timeout` anymore since the RTDE sockets are just usual tcp sockets that are already subject to `socket_reconnect_attempts` and `socket_reconnection_timeout`. This means that currently it's just a while loop of connecting tcp sockets in a while loop for the RTDE connection setup which is maybe not really necessary and just creates a lot of delays. But it's also possible that I miss an important detail about RTDE comm so maybe my feeling is wrong here.
1 parent 9d01f09 commit b42da6d

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/rtde/rtde_client.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,27 @@ bool RTDEClient::init(const size_t max_connection_attempts, const std::chrono::m
9090
}
9191

9292
unsigned int attempts = 0;
93+
std::stringstream ss;
9394
while (attempts < max_initialization_attempts)
9495
{
95-
setupCommunication(max_connection_attempts, reconnection_timeout);
96+
try
97+
{
98+
setupCommunication(max_connection_attempts, reconnection_timeout);
99+
}
100+
catch (const UrException& exc)
101+
{
102+
ss << exc.what() << std::endl;
103+
}
96104
if (client_state_ == ClientState::INITIALIZED)
105+
{
97106
return true;
98-
107+
}
99108
if (++attempts < max_initialization_attempts)
100109
{
101110
URCL_LOG_ERROR("Failed to initialize RTDE client, retrying in %d seconds", initialization_timeout.count() / 1000);
102111
std::this_thread::sleep_for(initialization_timeout);
103112
}
104113
}
105-
std::stringstream ss;
106114
ss << "Failed to initialize RTDE client after " << max_initialization_attempts << " attempts";
107115
throw UrException(ss.str());
108116
}
@@ -709,4 +717,4 @@ std::vector<std::string> RTDEClient::splitVariableTypes(const std::string& varia
709717
return result;
710718
}
711719
} // namespace rtde_interface
712-
} // namespace urcl
720+
} // namespace urcl

tests/test_rtde_client.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,14 @@ TEST_F(RTDEClientTest, connect_non_running_robot)
363363
client_.reset(
364364
new rtde_interface::RTDEClient("192.168.56.123", notifier_, resources_output_recipe_, resources_input_recipe_));
365365
auto start = std::chrono::system_clock::now();
366-
EXPECT_THROW(client_->init(2, std::chrono::milliseconds(500)), UrException);
366+
EXPECT_THROW(client_->init(2, std::chrono::milliseconds(500), 1), UrException);
367367
auto end = std::chrono::system_clock::now();
368368
auto elapsed = end - start;
369369
// This is only a rough estimate, obviously.
370370
// Since this isn't done on the loopback device, trying to open a socket on a non-existing address
371371
// takes considerably longer.
372-
EXPECT_LT(elapsed, 2 * comm::TCPSocket::DEFAULT_RECONNECTION_TIME);
372+
EXPECT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count(),
373+
2 * comm::TCPSocket::DEFAULT_RECONNECTION_TIME.count());
373374
}
374375

375376
TEST_F(RTDEClientTest, check_all_rtde_output_variables_exist)
@@ -426,4 +427,4 @@ int main(int argc, char* argv[])
426427
}
427428

428429
return RUN_ALL_TESTS();
429-
}
430+
}

0 commit comments

Comments
 (0)