Skip to content

Commit

Permalink
Made sendRobotProgram robust to secondary stream disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-siffert-ocado committed Nov 4, 2024
1 parent b623b00 commit 524e564
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
8 changes: 8 additions & 0 deletions include/ur_client_library/ur/ur_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@ class UrDriver

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

int rtde_frequency_;
comm::INotifier notifier_;
Expand Down
28 changes: 25 additions & 3 deletions src/ur/ur_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,8 @@ bool UrDriver::sendScript(const std::string& program)
{
if (secondary_stream_ == nullptr)
{
throw std::runtime_error("Sending script to robot requested while there is no primary interface established. "
"This "
"should not happen.");
throw std::runtime_error("Sending script to robot requested while there is no secondary interface established. "
"This should not happen.");
}

// urscripts (snippets) must end with a newline, or otherwise the controller's runtime will
Expand All @@ -562,6 +561,16 @@ bool UrDriver::sendScript(const std::string& program)
return true;
}
URCL_LOG_ERROR("Could not send program to robot");

URCL_LOG_INFO("Reconnecting secondary stream to retry sending program...");
secondary_stream_->close();
if (secondary_stream_->connect() && secondary_stream_->write(data, len, written))
{
URCL_LOG_DEBUG("Sent program to robot:\n%s", program_with_newline.c_str());
return true;
}
URCL_LOG_ERROR("Retry sending program failed!");

return false;
}

Expand All @@ -578,6 +587,19 @@ bool UrDriver::sendRobotProgram()
}
}

bool UrDriver::reconnectSecondaryStream()
{
URCL_LOG_DEBUG("Closing secondary stream...");
secondary_stream_->close();
if (secondary_stream_->connect())
{
URCL_LOG_DEBUG("Secondary stream connected");
return true;
}
URCL_LOG_ERROR("Failed to reconnect secodary stream!");
return false;
}

std::vector<std::string> UrDriver::getRTDEOutputRecipe()
{
return rtde_client_->getOutputRecipe();
Expand Down
13 changes: 13 additions & 0 deletions tests/test_ur_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,19 @@ TEST_F(UrDriverTest, target_outside_limits_pose)
waitForProgramNotRunning(1000);
}

TEST_F(UrDriverTest, send_robot_program_retry_on_failure)
{
// Start robot program
g_ur_driver_->sendRobotProgram();
EXPECT_TRUE(waitForProgramRunning(1000));

// Check that sendRobotProgram is robust to the secondary stream being disconnected. This is what happens when
// switching from Remote to Local and back to Remote mode for example.
g_ur_driver_->secondary_stream_->close();

EXPECT_TRUE(g_ur_driver_->sendRobotProgram());
}

// TODO we should add more tests for the UrDriver class.

int main(int argc, char* argv[])
Expand Down

0 comments on commit 524e564

Please sign in to comment.