Skip to content

Commit

Permalink
Timestamp is now added to the output recipe correctly
Browse files Browse the repository at this point in the history
Added test to verify the addition of timestamp

Only disconnect if client is initialized
  • Loading branch information
urmahp committed Aug 23, 2023
1 parent 5b06798 commit 34a76f2
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion include/ur_client_library/rtde/rtde_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class RTDEClient
constexpr static const double CB3_MAX_FREQUENCY = 125.0;
constexpr static const double URE_MAX_FREQUENCY = 500.0;

std::vector<std::string> readRecipe(const std::string& recipe_file);
std::vector<std::string> readRecipe(const std::string& recipe_file, bool output_recipe = false);

void setupCommunication();
bool negotiateProtocolVersion(const uint16_t protocol_version);
Expand Down
40 changes: 27 additions & 13 deletions src/rtde/rtde_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ namespace rtde_interface
RTDEClient::RTDEClient(std::string robot_ip, comm::INotifier& notifier, const std::string& output_recipe_file,
const std::string& input_recipe_file, double target_frequency)
: stream_(robot_ip, UR_RTDE_PORT)
, output_recipe_(readRecipe(output_recipe_file))
, input_recipe_(readRecipe(input_recipe_file))
, output_recipe_(readRecipe(output_recipe_file, true))
, input_recipe_(readRecipe(input_recipe_file, false))
, parser_(output_recipe_)
, prod_(stream_, parser_)
, pipeline_(prod_, PIPELINE_NAME, notifier, true)
Expand Down Expand Up @@ -241,13 +241,6 @@ void RTDEClient::setupOutputs(const uint16_t protocol_version)
size_t written;
uint8_t buffer[4096];
URCL_LOG_INFO("Setting up RTDE communication with frequency %f", target_frequency_);
// Add timestamp to rtde output recipe, used to check if robot is booted
const std::string timestamp = "timestamp";
auto it = std::find(output_recipe_.begin(), output_recipe_.end(), timestamp);
if (it == output_recipe_.end())
{
output_recipe_.push_back(timestamp);
}
if (protocol_version == 2)
{
size = ControlPackageSetupOutputsRequest::generateSerializedRequest(buffer, target_frequency_, output_recipe_);
Expand Down Expand Up @@ -384,9 +377,12 @@ void RTDEClient::setupInputs()
void RTDEClient::disconnect()
{
// If communication is started it should be paused before disconnecting
sendPause();
pipeline_.stop();
stream_.disconnect();
if (client_state_ > ClientState::UNINITIALIZED)
{
sendPause();
pipeline_.stop();
stream_.disconnect();
}
client_state_ = ClientState::UNINITIALIZED;
}

Expand Down Expand Up @@ -548,7 +544,7 @@ bool RTDEClient::sendPause()
throw UrException(ss.str());
}

std::vector<std::string> RTDEClient::readRecipe(const std::string& recipe_file)
std::vector<std::string> RTDEClient::readRecipe(const std::string& recipe_file, bool output_recipe)
{
std::vector<std::string> recipe;
std::ifstream file(recipe_file);
Expand All @@ -559,11 +555,29 @@ std::vector<std::string> RTDEClient::readRecipe(const std::string& recipe_file)
URCL_LOG_ERROR("%s", msg.str().c_str());
throw UrException(msg.str());
}

if (file.peek() == std::ifstream::traits_type::eof())
{
std::stringstream msg;
msg << "The recipe '" << recipe_file << "' file is empty exiting ";
URCL_LOG_ERROR("%s", msg.str().c_str());
throw UrException(msg.str());
}

std::string line;
while (std::getline(file, line))
{
recipe.push_back(line);
}

// Add timestamp to rtde output recipe, used to check if robot is booted
const std::string timestamp = "timestamp";
auto it = std::find(recipe.begin(), recipe.end(), timestamp);
if (it == recipe.end() && output_recipe == true)
{
recipe.push_back(timestamp);
}

return recipe;
}

Expand Down
1 change: 0 additions & 1 deletion tests/resources/empty.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

27 changes: 27 additions & 0 deletions tests/resources/rtde_output_recipe_without_timestamp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
actual_q
actual_qd
speed_scaling
target_speed_fraction
runtime_state
actual_TCP_force
actual_TCP_pose
actual_digital_input_bits
actual_digital_output_bits
standard_analog_input0
standard_analog_input1
standard_analog_output0
standard_analog_output1
analog_io_types
tool_mode
tool_analog_input_types
tool_analog_input0
tool_analog_input1
tool_output_voltage
tool_output_current
tool_temperature
robot_mode
safety_mode
robot_status_bits
safety_status_bits
actual_current
tcp_offset
21 changes: 15 additions & 6 deletions tests/test_rtde_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@ TEST_F(RTDEClientTest, empty_recipe)
{
std::string output_recipe = "resources/empty.txt";
std::string input_recipe = "resources/empty.txt";
client_.reset(new rtde_interface::RTDEClient(ROBOT_IP, notifier_, output_recipe, input_recipe));

EXPECT_THROW(client_->init(), UrException);
EXPECT_THROW(client_.reset(new rtde_interface::RTDEClient(ROBOT_IP, notifier_, output_recipe, input_recipe)),
UrException);

// Only input recipe is empty
client_.reset(new rtde_interface::RTDEClient(ROBOT_IP, notifier_, output_recipe_, input_recipe));

EXPECT_THROW(client_->init(), UrException);
EXPECT_THROW(client_.reset(new rtde_interface::RTDEClient(ROBOT_IP, notifier_, output_recipe_, input_recipe)),
UrException);
}

TEST_F(RTDEClientTest, invalid_target_frequency)
Expand Down Expand Up @@ -296,6 +294,17 @@ TEST_F(RTDEClientTest, write_rtde_data)
client_->pause();
}

TEST_F(RTDEClientTest, output_recipe_without_timestamp)
{
std::string output_recipe = "resources/rtde_output_recipe_without_timestamp.txt";
client_.reset(new rtde_interface::RTDEClient(ROBOT_IP, notifier_, output_recipe, input_recipe_));

std::vector<std::string> actual_output_recipe = client_->getOutputRecipe();
const std::string timestamp = "timestamp";
auto it = std::find(actual_output_recipe.begin(), actual_output_recipe.end(), timestamp);
EXPECT_FALSE(it == actual_output_recipe.end());
}

int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit 34a76f2

Please sign in to comment.