From 82fe3c4012a2525b34c28c1460800052e0bf2dcc Mon Sep 17 00:00:00 2001 From: Dominic Reber Date: Wed, 12 Mar 2025 06:42:25 +0100 Subject: [PATCH 1/2] Try catch RTDE setup --- src/rtde/rtde_client.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/rtde/rtde_client.cpp b/src/rtde/rtde_client.cpp index 8667d466..d4f94f87 100644 --- a/src/rtde/rtde_client.cpp +++ b/src/rtde/rtde_client.cpp @@ -92,10 +92,12 @@ bool RTDEClient::init(const size_t max_connection_attempts, const std::chrono::m unsigned int attempts = 0; while (attempts < max_initialization_attempts) { - setupCommunication(max_connection_attempts, reconnection_timeout); - if (client_state_ == ClientState::INITIALIZED) + try { + setupCommunication(max_connection_attempts, reconnection_timeout); + } catch (const UrException&) {} + if (client_state_ == ClientState::INITIALIZED) { return true; - + } if (++attempts < max_initialization_attempts) { URCL_LOG_ERROR("Failed to initialize RTDE client, retrying in %d seconds", initialization_timeout.count() / 1000); From c081cc8a2730055ff5ada2024fa70ab67b7b674f Mon Sep 17 00:00:00 2001 From: Felix Exner Date: Mon, 24 Mar 2025 09:40:33 +0100 Subject: [PATCH 2/2] Print error of caught exception in init --- src/rtde/rtde_client.cpp | 16 +++++++++++----- tests/test_rtde_client.cpp | 7 ++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/rtde/rtde_client.cpp b/src/rtde/rtde_client.cpp index d4f94f87..c804041a 100644 --- a/src/rtde/rtde_client.cpp +++ b/src/rtde/rtde_client.cpp @@ -90,12 +90,19 @@ bool RTDEClient::init(const size_t max_connection_attempts, const std::chrono::m } unsigned int attempts = 0; + std::stringstream ss; while (attempts < max_initialization_attempts) { - try { + try + { setupCommunication(max_connection_attempts, reconnection_timeout); - } catch (const UrException&) {} - if (client_state_ == ClientState::INITIALIZED) { + } + catch (const UrException& exc) + { + ss << exc.what() << std::endl; + } + if (client_state_ == ClientState::INITIALIZED) + { return true; } if (++attempts < max_initialization_attempts) @@ -104,7 +111,6 @@ bool RTDEClient::init(const size_t max_connection_attempts, const std::chrono::m std::this_thread::sleep_for(initialization_timeout); } } - std::stringstream ss; ss << "Failed to initialize RTDE client after " << max_initialization_attempts << " attempts"; throw UrException(ss.str()); } @@ -711,4 +717,4 @@ std::vector RTDEClient::splitVariableTypes(const std::string& varia return result; } } // namespace rtde_interface -} // namespace urcl +} // namespace urcl \ No newline at end of file diff --git a/tests/test_rtde_client.cpp b/tests/test_rtde_client.cpp index 2831d6b4..cd87c235 100644 --- a/tests/test_rtde_client.cpp +++ b/tests/test_rtde_client.cpp @@ -363,13 +363,14 @@ TEST_F(RTDEClientTest, connect_non_running_robot) client_.reset( new rtde_interface::RTDEClient("192.168.56.123", notifier_, resources_output_recipe_, resources_input_recipe_)); auto start = std::chrono::system_clock::now(); - EXPECT_THROW(client_->init(2, std::chrono::milliseconds(500)), UrException); + EXPECT_THROW(client_->init(2, std::chrono::milliseconds(500), 1), UrException); auto end = std::chrono::system_clock::now(); auto elapsed = end - start; // This is only a rough estimate, obviously. // Since this isn't done on the loopback device, trying to open a socket on a non-existing address // takes considerably longer. - EXPECT_LT(elapsed, 2 * comm::TCPSocket::DEFAULT_RECONNECTION_TIME); + EXPECT_LT(std::chrono::duration_cast(elapsed).count(), + 2 * comm::TCPSocket::DEFAULT_RECONNECTION_TIME.count()); } TEST_F(RTDEClientTest, check_all_rtde_output_variables_exist) @@ -426,4 +427,4 @@ int main(int argc, char* argv[]) } return RUN_ALL_TESTS(); -} +} \ No newline at end of file