|
| 1 | +// Copyright 2025, 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 Jacob Larsen [email protected] |
| 33 | + * \date 2025-01-17 |
| 34 | + * |
| 35 | + * |
| 36 | + * |
| 37 | + * |
| 38 | + */ |
| 39 | +//---------------------------------------------------------------------- |
| 40 | + |
| 41 | +#ifndef UR_ROBOT_DRIVER__TRAJECTORY_UNTIL_NODE_HPP_ |
| 42 | +#define UR_ROBOT_DRIVER__TRAJECTORY_UNTIL_NODE_HPP_ |
| 43 | + |
| 44 | +#include <memory> |
| 45 | +#include <string> |
| 46 | +#include <variant> |
| 47 | + |
| 48 | +#include <rclcpp/rclcpp.hpp> |
| 49 | +#include <std_msgs/msg/bool.hpp> |
| 50 | +#include <rclcpp_action/server.hpp> |
| 51 | +#include <rclcpp_action/rclcpp_action.hpp> |
| 52 | +#include <rclcpp_action/create_server.hpp> |
| 53 | +#include <rclcpp_action/server_goal_handle.hpp> |
| 54 | +#include <rclcpp/duration.hpp> |
| 55 | +#include <ur_msgs/action/tool_contact.hpp> |
| 56 | +#include <ur_msgs/action/follow_joint_trajectory_until.hpp> |
| 57 | +#include <control_msgs/action/follow_joint_trajectory.hpp> |
| 58 | + |
| 59 | +namespace ur_robot_driver |
| 60 | +{ |
| 61 | +class TrajectoryUntilNode : public rclcpp::Node |
| 62 | +{ |
| 63 | +public: |
| 64 | + explicit TrajectoryUntilNode(const rclcpp::NodeOptions& options = rclcpp::NodeOptions()); |
| 65 | + ~TrajectoryUntilNode(); |
| 66 | + |
| 67 | +private: |
| 68 | + using TrajectoryUntilAction = ur_msgs::action::FollowJointTrajectoryUntil; |
| 69 | + |
| 70 | + rclcpp_action::Server<TrajectoryUntilAction>::SharedPtr action_server_; |
| 71 | + rclcpp_action::Client<control_msgs::action::FollowJointTrajectory>::SharedPtr trajectory_action_client_; |
| 72 | + std::string trajectory_action_uri_; |
| 73 | + |
| 74 | + // Add new until types here, when available |
| 75 | + using TCClient = rclcpp_action::Client<ur_msgs::action::ToolContact>::SharedPtr; |
| 76 | + std::variant<TCClient> until_action_client_variant; |
| 77 | + std::string until_action_uri_; |
| 78 | + |
| 79 | + rclcpp::CallbackGroup::SharedPtr server_callback_group; |
| 80 | + rclcpp::CallbackGroup::SharedPtr clients_callback_group; |
| 81 | + |
| 82 | + std::shared_ptr<rclcpp_action::ServerGoalHandle<TrajectoryUntilAction>> server_goal_handle_; |
| 83 | + |
| 84 | + template <class ActionType, class ClientType> |
| 85 | + void send_until_goal(std::shared_ptr<const TrajectoryUntilAction::Goal> goal); |
| 86 | + |
| 87 | + template <class T> |
| 88 | + void until_response_callback(const typename rclcpp_action::ClientGoalHandle<T>::SharedPtr& goal_handle); |
| 89 | + |
| 90 | + template <class T> |
| 91 | + void until_result_callback(const typename rclcpp_action::ClientGoalHandle<T>::WrappedResult& result); |
| 92 | + |
| 93 | + void cancel_until_goal(); |
| 94 | + |
| 95 | + void trajectory_response_callback( |
| 96 | + const rclcpp_action::ClientGoalHandle<control_msgs::action::FollowJointTrajectory>::SharedPtr& goal_handle); |
| 97 | + void trajectory_feedback_callback( |
| 98 | + const rclcpp_action::ClientGoalHandle<control_msgs::action::FollowJointTrajectory>::SharedPtr& goal_handle, |
| 99 | + const std::shared_ptr<const control_msgs::action::FollowJointTrajectory::Feedback> feedback); |
| 100 | + void trajectory_result_callback( |
| 101 | + const rclcpp_action::ClientGoalHandle<control_msgs::action::FollowJointTrajectory>::WrappedResult& result); |
| 102 | + |
| 103 | + bool assign_until_action_client(std::shared_ptr<const TrajectoryUntilAction::Goal> goal); |
| 104 | + |
| 105 | + rclcpp_action::GoalResponse goal_received_callback(const rclcpp_action::GoalUUID& uuid, |
| 106 | + std::shared_ptr<const TrajectoryUntilAction::Goal> goal); |
| 107 | + void |
| 108 | + goal_accepted_callback(const std::shared_ptr<rclcpp_action::ServerGoalHandle<TrajectoryUntilAction>> goal_handle); |
| 109 | + rclcpp_action::CancelResponse |
| 110 | + goal_cancelled_callback(const std::shared_ptr<rclcpp_action::ServerGoalHandle<TrajectoryUntilAction>> goal_handle); |
| 111 | + |
| 112 | + void send_trajectory_goal(std::shared_ptr<const TrajectoryUntilAction::Goal> goal); |
| 113 | + |
| 114 | + void report_goal(rclcpp_action::ClientGoalHandle<control_msgs::action::FollowJointTrajectory>::WrappedResult result); |
| 115 | + |
| 116 | + template <typename UntilResult> |
| 117 | + void report_goal(UntilResult result); |
| 118 | + |
| 119 | + void reset_node(); |
| 120 | + |
| 121 | + rclcpp_action::ClientGoalHandle<control_msgs::action::FollowJointTrajectory>::SharedPtr |
| 122 | + current_trajectory_goal_handle_; |
| 123 | + rclcpp_action::ClientGoalHandle<ur_msgs::action::ToolContact>::SharedPtr current_until_goal_handle_; |
| 124 | + |
| 125 | + std::shared_ptr<TrajectoryUntilAction::Result> prealloc_res_; |
| 126 | + |
| 127 | + std::shared_ptr<TrajectoryUntilAction::Feedback> prealloc_fb_; |
| 128 | + |
| 129 | + std::atomic<bool> trajectory_accepted_; |
| 130 | + std::atomic<bool> until_accepted_; |
| 131 | + |
| 132 | + std::condition_variable cv_until_; |
| 133 | + std::condition_variable cv_trajectory_; |
| 134 | + std::mutex mutex_until; |
| 135 | + std::mutex mutex_trajectory; |
| 136 | +}; |
| 137 | + |
| 138 | +} // namespace ur_robot_driver |
| 139 | + |
| 140 | +#endif // UR_ROBOT_DRIVER__TRAJECTORY_UNTIL_NODE_HPP_ |
0 commit comments