Skip to content

Commit 173e3c3

Browse files
authored
Fixed auto reconnection to the RTDE server. (#384)
When reconnecting to RTDE it requires you to set the input and output recipes again, this is now done properly.
1 parent a258cb8 commit 173e3c3

File tree

7 files changed

+330
-156
lines changed

7 files changed

+330
-156
lines changed

include/ur_client_library/comm/pipeline.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,9 @@ class Pipeline
366366
*/
367367
void stop()
368368
{
369-
if (!running_)
370-
return;
371-
372369
URCL_LOG_DEBUG("Stopping pipeline! <%s>", name_.c_str());
373370

374371
running_ = false;
375-
376372
producer_.stopProducer();
377373
if (pThread_.joinable())
378374
{

include/ur_client_library/comm/producer.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class URProducer : public IProducer<T>
4343
URStream<T>& stream_;
4444
Parser<T>& parser_;
4545
std::chrono::seconds timeout_;
46+
std::function<void()> on_reconnect_cb_;
4647

4748
bool running_;
4849

@@ -124,9 +125,21 @@ class URProducer : public IProducer<T>
124125
if (!running_)
125126
return true;
126127

128+
if (stream_.getState() == SocketState::Connected)
129+
{
130+
continue;
131+
}
132+
127133
if (stream_.closed())
128134
return false;
129135

136+
if (on_reconnect_cb_)
137+
{
138+
URCL_LOG_WARN("Failed to read from stream, invoking on reconnect callback and stopping the producer");
139+
on_reconnect_cb_();
140+
return false;
141+
}
142+
130143
URCL_LOG_WARN("Failed to read from stream, reconnecting in %ld seconds...", timeout_.count());
131144
std::this_thread::sleep_for(timeout_);
132145

@@ -140,6 +153,18 @@ class URProducer : public IProducer<T>
140153

141154
return false;
142155
}
156+
157+
/*!
158+
* \brief Sets the reconnection callback. Use this to configure a reconnection callback instead of connecting directly
159+
* to the stream again. This is needed for RTDE as it requires setting up the communication again upon reconnection it
160+
* is not enough to just reconnect to the stream.
161+
*
162+
* \param on_reconnect_cb Callback to be invoked when connection is lost to the stream.
163+
*/
164+
void setReconnectionCallback(std::function<void()> on_reconnect_cb)
165+
{
166+
on_reconnect_cb_ = on_reconnect_cb;
167+
}
143168
};
144169
} // namespace comm
145170
} // namespace urcl

include/ur_client_library/rtde/rtde_client.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ class RTDEClient
231231
comm::INotifier notifier_;
232232
std::unique_ptr<comm::Pipeline<RTDEPackage>> pipeline_;
233233
RTDEWriter writer_;
234+
std::atomic<bool> reconnecting_;
235+
std::atomic<bool> stop_reconnection_;
236+
std::mutex reconnect_mutex_;
237+
std::thread reconnecting_thread_;
234238

235239
VersionInformation urcontrol_version_;
236240

@@ -249,12 +253,13 @@ class RTDEClient
249253
// the robot is booted.
250254
std::vector<std::string> ensureTimestampIsPresent(const std::vector<std::string>& output_recipe) const;
251255

252-
void setupCommunication(const size_t max_num_tries = 0,
256+
bool setupCommunication(const size_t max_num_tries = 0,
253257
const std::chrono::milliseconds reconnection_time = std::chrono::seconds(10));
254-
bool negotiateProtocolVersion(const uint16_t protocol_version);
255-
void queryURControlVersion();
256-
void setupOutputs(const uint16_t protocol_version);
257-
void setupInputs();
258+
uint16_t negotiateProtocolVersion();
259+
bool queryURControlVersion();
260+
void setTargetFrequency();
261+
bool setupOutputs(const uint16_t protocol_version);
262+
bool setupInputs();
258263
void disconnect();
259264

260265
/*!
@@ -288,6 +293,12 @@ class RTDEClient
288293
* \returns A vector of variable variable_names
289294
*/
290295
std::vector<std::string> splitVariableTypes(const std::string& variable_types) const;
296+
297+
/*!
298+
* \brief Reconnects to the RTDE interface and set the input and output recipes again.
299+
*/
300+
void reconnect();
301+
void reconnectCallback();
291302
};
292303

293304
} // namespace rtde_interface

include/ur_client_library/rtde/rtde_writer.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ class RTDEWriter
6161

6262
~RTDEWriter()
6363
{
64-
running_ = false;
65-
if (writer_thread_.joinable())
66-
{
67-
writer_thread_.join();
68-
}
64+
stop();
6965
}
7066

7167
/*!
@@ -88,6 +84,11 @@ class RTDEWriter
8884
*/
8985
void run();
9086

87+
/*!
88+
* \brief Stops the writer thread loop.
89+
*/
90+
void stop();
91+
9192
/*!
9293
* \brief Creates a package to request setting a new value for the speed slider.
9394
*

0 commit comments

Comments
 (0)