@@ -47,6 +47,7 @@ RTDEClient::RTDEClient(std::string robot_ip, comm::INotifier& notifier, const st
4747 , pipeline_(std::make_unique<comm::Pipeline<RTDEPackage>>(*prod_, PIPELINE_NAME, notifier, true ))
4848 , writer_(&stream_, input_recipe_)
4949 , reconnecting_(false )
50+ , stop_reconnection_(false )
5051 , max_frequency_(URE_MAX_FREQUENCY)
5152 , target_frequency_(target_frequency)
5253 , client_state_(ClientState::UNINITIALIZED)
@@ -71,6 +72,7 @@ RTDEClient::RTDEClient(std::string robot_ip, comm::INotifier& notifier, const st
7172 , pipeline_(std::make_unique<comm::Pipeline<RTDEPackage>>(*prod_, PIPELINE_NAME, notifier, true ))
7273 , writer_(&stream_, input_recipe_)
7374 , reconnecting_(false )
75+ , stop_reconnection_(false )
7476 , max_frequency_(URE_MAX_FREQUENCY)
7577 , target_frequency_(target_frequency)
7678 , client_state_(ClientState::UNINITIALIZED)
@@ -79,7 +81,8 @@ RTDEClient::RTDEClient(std::string robot_ip, comm::INotifier& notifier, const st
7981
8082RTDEClient::~RTDEClient ()
8183{
82- reconnecting_ = false ;
84+ prod_->setReconnectionCallback (nullptr );
85+ stop_reconnection_ = true ;
8386 if (reconnecting_thread_.joinable ())
8487 {
8588 reconnecting_thread_.join ();
@@ -100,7 +103,7 @@ bool RTDEClient::init(const size_t max_connection_attempts, const std::chrono::m
100103 return true ;
101104 }
102105
103- prod_->setRTDEReconnectionCallback ( std::bind (&RTDEClient::reconnectCallback, this ) );
106+ prod_->setReconnectionCallback ( nullptr );
104107
105108 unsigned int attempts = 0 ;
106109 std::stringstream ss;
@@ -121,6 +124,9 @@ bool RTDEClient::init(const size_t max_connection_attempts, const std::chrono::m
121124 // Stop pipeline again
122125 pipeline_->stop ();
123126 client_state_ = ClientState::INITIALIZED;
127+ // Set reconnection callback after we are initialized to ensure that a disconnect during initialization doesn't
128+ // trigger a reconnect
129+ prod_->setReconnectionCallback (std::bind (&RTDEClient::reconnectCallback, this ));
124130 return true ;
125131}
126132
@@ -296,7 +302,6 @@ void RTDEClient::resetOutputRecipe(const std::vector<std::string> new_recipe)
296302
297303 parser_ = RTDEParser (output_recipe_);
298304 prod_ = std::make_unique<comm::URProducer<RTDEPackage>>(stream_, parser_);
299- prod_->setRTDEReconnectionCallback (std::bind (&RTDEClient::reconnectCallback, this ));
300305 pipeline_ = std::make_unique<comm::Pipeline<RTDEPackage>>(*prod_, PIPELINE_NAME, notifier_, true );
301306}
302307
@@ -611,7 +616,6 @@ bool RTDEClient::sendStart()
611616 ss << " Did not receive answer to RTDE start request. Message received instead: " << std::endl
612617 << package->toString ();
613618 URCL_LOG_WARN (" %s" , ss.str ().c_str ());
614- return false ;
615619 }
616620 }
617621 std::stringstream ss;
@@ -644,7 +648,6 @@ bool RTDEClient::sendPause()
644648 }
645649 if (rtde_interface::ControlPackagePause* tmp = dynamic_cast <rtde_interface::ControlPackagePause*>(package.get ()))
646650 {
647- client_state_ = ClientState::PAUSED;
648651 return tmp->accepted_ ;
649652 }
650653 }
@@ -774,7 +777,7 @@ void RTDEClient::reconnect()
774777 }
775778
776779 const std::string reconnecting_stopped_msg = " Reconnecting has been stopped, because the object is being destroyed" ;
777- if (reconnecting_ == false )
780+ if (stop_reconnection_ )
778781 {
779782 URCL_LOG_WARN (reconnecting_stopped_msg.c_str ());
780783 return ;
@@ -806,7 +809,7 @@ void RTDEClient::reconnect()
806809 while (std::chrono::steady_clock::now () - start_time < duration)
807810 {
808811 std::this_thread::sleep_for (std::chrono::milliseconds (250 ));
809- if (reconnecting_ == false )
812+ if (stop_reconnection_ )
810813 {
811814 URCL_LOG_WARN (reconnecting_stopped_msg.c_str ());
812815 return ;
@@ -828,12 +831,13 @@ void RTDEClient::reconnect()
828831 {
829832 pause ();
830833 }
834+ URCL_LOG_INFO (" Done reconnecting to the RTDE interface" );
831835 reconnecting_ = false ;
832836}
833837
834838void RTDEClient::reconnectCallback ()
835839{
836- if (reconnecting_)
840+ if (reconnecting_ || stop_reconnection_ )
837841 {
838842 return ;
839843 }
0 commit comments