|
| 1 | +// Copyright 2023, FZI Forschungszentrum Informatik, Created on behalf of Universal Robots A/S |
| 2 | +// |
| 3 | +// Redistribution and use in source and binary forms, with or without |
| 4 | +// modification, are permitted provided that the following conditions are met: |
| 5 | +// |
| 6 | +// * Redistributions of source code must retain the above copyright |
| 7 | +// notice, this list of conditions and the following disclaimer. |
| 8 | +// |
| 9 | +// * Redistributions in binary form must reproduce the above copyright |
| 10 | +// notice, this list of conditions and the following disclaimer in the |
| 11 | +// documentation and/or other materials provided with the distribution. |
| 12 | +// |
| 13 | +// * Neither the name of the {copyright_holder} nor the names of its |
| 14 | +// contributors may be used to endorse or promote products derived from |
| 15 | +// this software without specific prior written permission. |
| 16 | +// |
| 17 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | +// POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +//---------------------------------------------------------------------- |
| 30 | +/*!\file |
| 31 | + * |
| 32 | + * \author Felix Exner [email protected] |
| 33 | + * \date 2023-06-20 |
| 34 | + * |
| 35 | + */ |
| 36 | +//---------------------------------------------------------------------- |
| 37 | + |
| 38 | +#include <ur_client_library/comm/stream.h> |
| 39 | +#include <ur_client_library/primary/primary_package.h> |
| 40 | + |
| 41 | +#include <memory> |
| 42 | + |
| 43 | +#include <rclcpp/rclcpp.hpp> |
| 44 | +#include <std_msgs/msg/string.hpp> |
| 45 | + |
| 46 | +class URScriptInterface : public rclcpp::Node |
| 47 | +{ |
| 48 | +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 | + })) |
| 68 | + { |
| 69 | + 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 | + ~URScriptInterface() override = default; |
| 75 | + |
| 76 | +private: |
| 77 | + rclcpp::Subscription<std_msgs::msg::String>::SharedPtr m_script_sub; |
| 78 | + std::unique_ptr<urcl::comm::URStream<urcl::primary_interface::PrimaryPackage>> m_secondary_stream; |
| 79 | +}; |
| 80 | + |
| 81 | +int main(int argc, char** argv) |
| 82 | +{ |
| 83 | + rclcpp::init(argc, argv); |
| 84 | + rclcpp::spin(std::make_unique<URScriptInterface>()); |
| 85 | + rclcpp::shutdown(); |
| 86 | + return 0; |
| 87 | +} |
0 commit comments