Skip to content

Commit d57df32

Browse files
committed
Allow configuring the connection and initialization attempts for the UrDriver's RTDE client
1 parent d3e91d9 commit d57df32

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

include/ur_client_library/rtde/rtde_client.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ namespace rtde_interface
5353
{
5454
static const uint16_t MAX_RTDE_PROTOCOL_VERSION = 2;
5555
static const unsigned MAX_REQUEST_RETRIES = 5;
56-
static const unsigned MAX_INITIALIZE_ATTEMPTS = 10;
5756

5857
enum class UrRtdeRobotStatusBits
5958
{
@@ -131,14 +130,21 @@ class RTDEClient
131130
* \brief Sets up RTDE communication with the robot. The handshake includes negotiation of the
132131
* used protocol version and setting of input and output recipes.
133132
*
134-
* \param max_num_tries Maximum number of connection attempts before counting the connection as
133+
* \param max_connection_attempts Maximum number of (socket) connection attempts before counting the connection as
135134
* failed. Unlimited number of attempts when set to 0.
136-
* \param reconnection_time time in between connection attempts to the server
135+
* \param reconnection_timeout Time in between connection attempts to the socket
136+
* \param max_initialization_attempts Maximum number of initialization attempts before counting
137+
* the initialization as failed. Initialization can
138+
* fail given an established socket connection e.g. when the connected socket does not implement
139+
* an RTDE interface.
140+
* \param initialization_timeout Time in between initialization attempts of the RTDE interface
137141
*
138142
* \returns Success of the handshake
139143
*/
140-
bool init(const size_t max_num_tries = 0,
141-
const std::chrono::milliseconds reconnection_time = std::chrono::seconds(10));
144+
bool init(const size_t max_connection_attempts = 0,
145+
const std::chrono::milliseconds reconnection_timeout = comm::TCPSocket::DEFAULT_RECONNECTION_TIME,
146+
const size_t max_initialization_attempts = 3,
147+
const std::chrono::milliseconds initialization_timeout = std::chrono::seconds(1));
142148
/*!
143149
* \brief Triggers the robot to start sending RTDE data packages in the negotiated format.
144150
*

include/ur_client_library/ur/ur_driver.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#ifndef UR_CLIENT_LIBRARY_UR_UR_DRIVER_H_INCLUDED
2929
#define UR_CLIENT_LIBRARY_UR_UR_DRIVER_H_INCLUDED
3030

31+
#include <chrono>
3132
#include <functional>
3233

3334
#include "ur_client_library/rtde/rtde_client.h"
@@ -107,6 +108,30 @@ struct UrDriverConfiguration
107108
*/
108109
double servoj_lookahead_time = 0.03;
109110

111+
/*!
112+
* \brief Number of attempts to reconnect to sockets such as the primary or RTDE interface.
113+
*
114+
* If set to 0, the driver will try to reconnect indefinitely.
115+
*/
116+
size_t socket_reconnect_attempts = 0;
117+
118+
/*!
119+
* \brief Time in between connection attempts to sockets such as the primary or RTDE interface.
120+
*/
121+
std::chrono::milliseconds socket_reconnection_timeout = std::chrono::seconds(10);
122+
123+
/*!
124+
* \brief Number of attempts to initialize (given a successful socket connection) the RTDE interface.
125+
*
126+
* If set to 0, the driver will try to initialize the RTDE interface indefinitely.
127+
*/
128+
size_t rtde_initialization_attempts_ = 3;
129+
130+
/*!
131+
* \brief Time in between initialization attempts of the RTDE interface.
132+
*/
133+
std::chrono::milliseconds rtde_initialization_timeout_ = std::chrono::seconds(5);
134+
110135
bool non_blocking_read = false;
111136

112137
// TODO: Remove on 2027-05
@@ -873,6 +898,12 @@ class UrDriver
873898
std::unique_ptr<comm::URStream<primary_interface::PrimaryPackage>> primary_stream_;
874899
std::unique_ptr<comm::URStream<primary_interface::PrimaryPackage>> secondary_stream_;
875900

901+
size_t socket_connection_attempts_ = 0;
902+
std::chrono::milliseconds socket_reconnection_timeout_ = std::chrono::milliseconds(10000);
903+
904+
size_t rtde_initialization_attempts_ = 0;
905+
std::chrono::milliseconds rtde_initialization_timeout_ = std::chrono::milliseconds(10000);
906+
876907
double force_mode_gain_scale_factor_ = 0.5;
877908
double force_mode_damping_factor_ = 0.025;
878909

src/rtde/rtde_client.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,34 @@ RTDEClient::~RTDEClient()
7676
disconnect();
7777
}
7878

79-
bool RTDEClient::init(const size_t max_num_tries, const std::chrono::milliseconds reconnection_time)
79+
bool RTDEClient::init(const size_t max_connection_attempts, const std::chrono::milliseconds reconnection_timeout,
80+
const size_t max_initialization_attempts, const std::chrono::milliseconds initialization_timeout)
8081
{
82+
if (max_initialization_attempts <= 0)
83+
{
84+
throw UrException("The number of initialization attempts has to be greater than 0.");
85+
}
86+
8187
if (client_state_ > ClientState::UNINITIALIZED)
8288
{
8389
return true;
8490
}
8591

8692
unsigned int attempts = 0;
87-
while (attempts < MAX_INITIALIZE_ATTEMPTS)
93+
while (attempts < max_initialization_attempts)
8894
{
89-
setupCommunication(max_num_tries, reconnection_time);
95+
setupCommunication(max_connection_attempts, reconnection_timeout);
9096
if (client_state_ == ClientState::INITIALIZED)
9197
return true;
9298

93-
if (++attempts < MAX_INITIALIZE_ATTEMPTS)
99+
if (++attempts < max_initialization_attempts)
94100
{
95101
URCL_LOG_ERROR("Failed to initialize RTDE client, retrying in 10 seconds");
96-
std::this_thread::sleep_for(std::chrono::seconds(10));
102+
std::this_thread::sleep_for(initialization_timeout);
97103
}
98104
}
99105
std::stringstream ss;
100-
ss << "Failed to initialize RTDE client after " << MAX_INITIALIZE_ATTEMPTS << " attempts";
106+
ss << "Failed to initialize RTDE client after " << max_initialization_attempts << " attempts";
101107
throw UrException(ss.str());
102108
}
103109

src/ur/ur_driver.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void UrDriver::init(const UrDriverConfiguration& config)
6060
servoj_lookahead_time_ = config.servoj_lookahead_time;
6161
handle_program_state_ = config.handle_program_state;
6262
in_headless_mode_ = config.headless_mode;
63+
socket_connection_attempts_ = config.socket_reconnect_attempts;
64+
socket_reconnection_timeout_ = config.socket_reconnection_timeout;
6365

6466
URCL_LOG_DEBUG("Initializing urdriver");
6567
URCL_LOG_DEBUG("Initializing RTDE client");
@@ -691,7 +693,7 @@ void UrDriver::resetRTDEClient(const std::string& output_recipe_filename, const
691693

692694
void UrDriver::initRTDE()
693695
{
694-
if (!rtde_client_->init())
696+
if (!rtde_client_->init(socket_connection_attempts_, socket_reconnection_timeout_))
695697
{
696698
throw UrException("Initialization of RTDE client went wrong.");
697699
}

0 commit comments

Comments
 (0)