Skip to content

Commit 524e564

Browse files
Made sendRobotProgram robust to secondary stream disconnected
1 parent b623b00 commit 524e564

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

include/ur_client_library/ur/ur_driver.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,14 @@ class UrDriver
489489

490490
private:
491491
static std::string readScriptFile(const std::string& filename);
492+
/*!
493+
* \brief Reconnects the secondary stream used to send program to the robot.
494+
*
495+
* Only for use in headless mode, as it replaces the use of the URCaps program.
496+
*
497+
* \returns true of on successful reconnection, false otherwise
498+
*/
499+
bool reconnectSecondaryStream();
492500

493501
int rtde_frequency_;
494502
comm::INotifier notifier_;

src/ur/ur_driver.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,8 @@ bool UrDriver::sendScript(const std::string& program)
542542
{
543543
if (secondary_stream_ == nullptr)
544544
{
545-
throw std::runtime_error("Sending script to robot requested while there is no primary interface established. "
546-
"This "
547-
"should not happen.");
545+
throw std::runtime_error("Sending script to robot requested while there is no secondary interface established. "
546+
"This should not happen.");
548547
}
549548

550549
// urscripts (snippets) must end with a newline, or otherwise the controller's runtime will
@@ -562,6 +561,16 @@ bool UrDriver::sendScript(const std::string& program)
562561
return true;
563562
}
564563
URCL_LOG_ERROR("Could not send program to robot");
564+
565+
URCL_LOG_INFO("Reconnecting secondary stream to retry sending program...");
566+
secondary_stream_->close();
567+
if (secondary_stream_->connect() && secondary_stream_->write(data, len, written))
568+
{
569+
URCL_LOG_DEBUG("Sent program to robot:\n%s", program_with_newline.c_str());
570+
return true;
571+
}
572+
URCL_LOG_ERROR("Retry sending program failed!");
573+
565574
return false;
566575
}
567576

@@ -578,6 +587,19 @@ bool UrDriver::sendRobotProgram()
578587
}
579588
}
580589

590+
bool UrDriver::reconnectSecondaryStream()
591+
{
592+
URCL_LOG_DEBUG("Closing secondary stream...");
593+
secondary_stream_->close();
594+
if (secondary_stream_->connect())
595+
{
596+
URCL_LOG_DEBUG("Secondary stream connected");
597+
return true;
598+
}
599+
URCL_LOG_ERROR("Failed to reconnect secodary stream!");
600+
return false;
601+
}
602+
581603
std::vector<std::string> UrDriver::getRTDEOutputRecipe()
582604
{
583605
return rtde_client_->getOutputRecipe();

tests/test_ur_driver.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,19 @@ TEST_F(UrDriverTest, target_outside_limits_pose)
359359
waitForProgramNotRunning(1000);
360360
}
361361

362+
TEST_F(UrDriverTest, send_robot_program_retry_on_failure)
363+
{
364+
// Start robot program
365+
g_ur_driver_->sendRobotProgram();
366+
EXPECT_TRUE(waitForProgramRunning(1000));
367+
368+
// Check that sendRobotProgram is robust to the secondary stream being disconnected. This is what happens when
369+
// switching from Remote to Local and back to Remote mode for example.
370+
g_ur_driver_->secondary_stream_->close();
371+
372+
EXPECT_TRUE(g_ur_driver_->sendRobotProgram());
373+
}
374+
362375
// TODO we should add more tests for the UrDriver class.
363376

364377
int main(int argc, char* argv[])

0 commit comments

Comments
 (0)