|
35 | 35 | */ |
36 | 36 | //---------------------------------------------------------------------- |
37 | 37 |
|
38 | | -#include <ur_client_library/comm/stream.h> |
39 | | -#include <ur_client_library/primary/primary_package.h> |
| 38 | +#include <ur_client_library/primary/primary_client.h> |
40 | 39 |
|
41 | | -#include <memory> |
| 40 | +#include <chrono> |
| 41 | +#include <thread> |
| 42 | + |
| 43 | +#include <ur_msgs/action/send_script.hpp> |
42 | 44 |
|
43 | 45 | #include <rclcpp/rclcpp.hpp> |
| 46 | +#include "rclcpp_action/rclcpp_action.hpp" |
44 | 47 | #include <std_msgs/msg/string.hpp> |
45 | 48 |
|
| 49 | +using SendScript = ur_msgs::action::SendScript; |
| 50 | + |
46 | 51 | class URScriptInterface : public rclcpp::Node |
47 | 52 | { |
48 | 53 | public: |
49 | | - URScriptInterface() |
50 | | - : Node("urscript_interface") |
51 | | - , m_script_sub(this->create_subscription<std_msgs::msg::String>( |
52 | | - "~/script_command", 1, [this](const std_msgs::msg::String::SharedPtr msg) { |
53 | | - auto program_with_newline = msg->data + '\n'; |
54 | | - |
55 | | - RCLCPP_INFO_STREAM(this->get_logger(), program_with_newline); |
56 | | - |
57 | | - size_t len = program_with_newline.size(); |
58 | | - const auto* data = reinterpret_cast<const uint8_t*>(program_with_newline.c_str()); |
59 | | - size_t written; |
60 | | - |
61 | | - if (m_secondary_stream->write(data, len, written)) { |
62 | | - URCL_LOG_INFO("Sent program to robot:\n%s", program_with_newline.c_str()); |
63 | | - return true; |
64 | | - } |
65 | | - URCL_LOG_ERROR("Could not send program to robot"); |
66 | | - return false; |
67 | | - })) |
| 54 | + URScriptInterface() : Node("urscript_interface") |
68 | 55 | { |
69 | 56 | this->declare_parameter("robot_ip", rclcpp::PARAMETER_STRING); |
70 | | - m_secondary_stream = std::make_unique<urcl::comm::URStream<urcl::primary_interface::PrimaryPackage>>( |
71 | | - this->get_parameter("robot_ip").as_string(), urcl::primary_interface::UR_SECONDARY_PORT); |
72 | | - m_secondary_stream->connect(); |
73 | | - |
74 | | - auto program_with_newline = std::string("sec urscript_interface_initialization:\ntextmsg(\"urscript_interface " |
75 | | - "connected\")\nend\n"); |
76 | | - size_t len = program_with_newline.size(); |
77 | | - const auto* data = reinterpret_cast<const uint8_t*>(program_with_newline.c_str()); |
78 | | - size_t written; |
79 | | - m_secondary_stream->write(data, len, written); |
| 57 | + this->declare_parameter("retry_on_readonly_interface", true); |
| 58 | + |
| 59 | + primary_client_ = |
| 60 | + std::make_unique<urcl::primary_interface::PrimaryClient>(this->get_parameter("robot_ip").as_string(), notif_); |
| 61 | + |
| 62 | + primary_client_->start(10, std::chrono::seconds(10)); |
| 63 | + |
| 64 | + script_sub_ = create_subscription<std_msgs::msg::String>( |
| 65 | + "~/script_command", 1, std::bind(&URScriptInterface::script_callback, this, std::placeholders::_1)); |
| 66 | + |
| 67 | + send_script_server_ = rclcpp_action::create_server<SendScript>( |
| 68 | + this, "~/execute_script", |
| 69 | + std::bind(&URScriptInterface::handle_goal, this, std::placeholders::_1, std::placeholders::_2), |
| 70 | + std::bind(&URScriptInterface::handle_cancel, this, std::placeholders::_1), |
| 71 | + std::bind(&URScriptInterface::handle_accepted, this, std::placeholders::_1)); |
| 72 | + } |
| 73 | + |
| 74 | + ~URScriptInterface() override |
| 75 | + { |
| 76 | + if (execute_thread_.joinable()) { |
| 77 | + execute_thread_.join(); |
| 78 | + } |
80 | 79 | } |
81 | 80 |
|
82 | 81 | private: |
83 | | - rclcpp::Subscription<std_msgs::msg::String>::SharedPtr m_script_sub; |
84 | | - std::unique_ptr<urcl::comm::URStream<urcl::primary_interface::PrimaryPackage>> m_secondary_stream; |
| 82 | + // Use non-blocking interface |
| 83 | + bool script_callback(const std_msgs::msg::String::SharedPtr msg) |
| 84 | + { |
| 85 | + if (primary_client_ == nullptr) { |
| 86 | + RCLCPP_ERROR(get_logger(), "Primary client not initialized yet"); |
| 87 | + return false; |
| 88 | + } |
| 89 | + if (busy) { |
| 90 | + RCLCPP_ERROR(get_logger(), "Script interface is executing action, ignoring topic"); |
| 91 | + return false; |
| 92 | + } |
| 93 | + if (primary_client_->sendScript(msg->data)) { |
| 94 | + URCL_LOG_INFO("Sent program to robot:\n%s", msg->data.c_str()); |
| 95 | + return true; |
| 96 | + } else { |
| 97 | + URCL_LOG_ERROR("Could not send program to robot"); |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + rclcpp_action::GoalResponse handle_goal(const rclcpp_action::GoalUUID& uuid, |
| 103 | + std::shared_ptr<const SendScript::Goal> goal) |
| 104 | + { |
| 105 | + if (primary_client_ == nullptr) { |
| 106 | + RCLCPP_ERROR(get_logger(), "Primary client not initialized yet"); |
| 107 | + return rclcpp_action::GoalResponse::REJECT; |
| 108 | + } |
| 109 | + |
| 110 | + if (busy) { |
| 111 | + RCLCPP_ERROR(get_logger(), "Action server is busy"); |
| 112 | + return rclcpp_action::GoalResponse::REJECT; |
| 113 | + } |
| 114 | + busy = true; |
| 115 | + |
| 116 | + if (execute_thread_.joinable()) { |
| 117 | + // Should return immediately, as we are not busy |
| 118 | + execute_thread_.join(); |
| 119 | + } |
| 120 | + return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE; |
| 121 | + } |
| 122 | + |
| 123 | + rclcpp_action::CancelResponse |
| 124 | + handle_cancel(const std::shared_ptr<rclcpp_action::ServerGoalHandle<SendScript>> goal_handle) |
| 125 | + { |
| 126 | + if (goal_handle->is_executing()) { |
| 127 | + try { |
| 128 | + primary_client_->commandStop(); |
| 129 | + } catch (const urcl::UrException& exc) { |
| 130 | + RCLCPP_ERROR(get_logger(), "Caught UR exception while trying to cancel goal:"); |
| 131 | + RCLCPP_ERROR(get_logger(), exc.what()); |
| 132 | + } catch (const std::exception& exc) { |
| 133 | + RCLCPP_ERROR(get_logger(), "Caught unexpected exception while trying to cancel goal:"); |
| 134 | + RCLCPP_ERROR(get_logger(), exc.what()); |
| 135 | + } |
| 136 | + return rclcpp_action::CancelResponse::ACCEPT; |
| 137 | + } else { |
| 138 | + RCLCPP_ERROR(get_logger(), "Received cancel request for inactive goal, rejecting"); |
| 139 | + return rclcpp_action::CancelResponse::REJECT; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + void handle_accepted(const std::shared_ptr<rclcpp_action::ServerGoalHandle<SendScript>> goal_handle) |
| 144 | + { |
| 145 | + execute_thread_ = std::thread([this, goal_handle]() { this->execute(goal_handle); }); |
| 146 | + } |
| 147 | + |
| 148 | + void execute(const std::shared_ptr<rclcpp_action::ServerGoalHandle<SendScript>> goal_handle) |
| 149 | + { |
| 150 | + auto goal = goal_handle->get_goal(); |
| 151 | + auto timeout = goal->start_timeout; |
| 152 | + auto chrono_timeout = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 153 | + std::chrono::seconds(timeout.sec) + std::chrono::nanoseconds(timeout.nanosec)); |
| 154 | + // A ROS duration can be negative, would not make sense here |
| 155 | + if (chrono_timeout < std::chrono::milliseconds(0)) { |
| 156 | + chrono_timeout = std::chrono::milliseconds(0); |
| 157 | + } |
| 158 | + |
| 159 | + try { |
| 160 | + primary_client_->sendScriptBlocking(goal->program, goal->script_name, chrono_timeout, goal->fail_on_warnings, |
| 161 | + this->get_parameter("retry_on_readonly_interface").as_bool()); |
| 162 | + } |
| 163 | + |
| 164 | + catch (const urcl::UrException& exc) { |
| 165 | + RCLCPP_ERROR(get_logger(), "Script did not execute successfully. Error message:"); |
| 166 | + RCLCPP_ERROR(get_logger(), exc.what()); |
| 167 | + auto res = std::make_shared<SendScript::Result>(); |
| 168 | + res->success = false; |
| 169 | + res->message = exc.what(); |
| 170 | + goal_handle->abort(res); |
| 171 | + busy = false; |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + catch (const std::exception& exc) { |
| 176 | + RCLCPP_ERROR(get_logger(), "Unexpected exception caught during script execution:"); |
| 177 | + RCLCPP_ERROR(get_logger(), exc.what()); |
| 178 | + auto res = std::make_shared<SendScript::Result>(); |
| 179 | + res->success = false; |
| 180 | + res->message = exc.what(); |
| 181 | + goal_handle->abort(res); |
| 182 | + busy = false; |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + auto res = std::make_shared<SendScript::Result>(); |
| 187 | + if (goal_handle->is_canceling()) { |
| 188 | + res->success = false; |
| 189 | + res->message = "Script execution canceled"; |
| 190 | + goal_handle->canceled(res); |
| 191 | + } else { |
| 192 | + res->success = true; |
| 193 | + res->message = "Script executed successfully"; |
| 194 | + goal_handle->succeed(res); |
| 195 | + } |
| 196 | + busy = false; |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + rclcpp::Subscription<std_msgs::msg::String>::SharedPtr script_sub_; |
| 201 | + rclcpp_action::Server<SendScript>::SharedPtr send_script_server_; |
| 202 | + std::unique_ptr<urcl::primary_interface::PrimaryClient> primary_client_; |
| 203 | + urcl::comm::INotifier notif_; |
| 204 | + std::atomic<bool> busy = false; |
| 205 | + std::thread execute_thread_; |
85 | 206 | }; |
86 | 207 |
|
87 | 208 | int main(int argc, char** argv) |
|
0 commit comments