|
| 1 | +// Copyright 2024, 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 | +#ifndef UR_ROBOT_DRIVER__ROBOT_STATE_HELPER_HPP_ |
| 30 | +#define UR_ROBOT_DRIVER__ROBOT_STATE_HELPER_HPP_ |
| 31 | + |
| 32 | +#include <chrono> |
| 33 | +#include <memory> |
| 34 | + |
| 35 | +#include "rclcpp/rclcpp.hpp" |
| 36 | +#include "rclcpp_action/create_server.hpp" |
| 37 | +#include "std_msgs/msg/bool.hpp" |
| 38 | +#include "std_srvs/srv/trigger.hpp" |
| 39 | + |
| 40 | +#include "ur_dashboard_msgs/action/set_mode.hpp" |
| 41 | +#include "ur_dashboard_msgs/msg/safety_mode.hpp" |
| 42 | +#include "ur_dashboard_msgs/msg/robot_mode.hpp" |
| 43 | +#include "ur_client_library/ur/datatypes.h" |
| 44 | + |
| 45 | +namespace ur_robot_driver |
| 46 | +{ |
| 47 | +class RobotStateHelper |
| 48 | +{ |
| 49 | +public: |
| 50 | + using SetModeGoalHandle = rclcpp_action::ServerGoalHandle<ur_dashboard_msgs::action::SetMode>; |
| 51 | + |
| 52 | + explicit RobotStateHelper(const rclcpp::Node::SharedPtr& node); |
| 53 | + RobotStateHelper() = delete; |
| 54 | + virtual ~RobotStateHelper() = default; |
| 55 | + |
| 56 | +private: |
| 57 | + rclcpp::Node::SharedPtr node_; |
| 58 | + |
| 59 | + void robotModeCallback(ur_dashboard_msgs::msg::RobotMode::SharedPtr msg); |
| 60 | + void safetyModeCallback(ur_dashboard_msgs::msg::SafetyMode::SharedPtr msg); |
| 61 | + |
| 62 | + void updateRobotState(); |
| 63 | + |
| 64 | + bool recoverFromSafety(); |
| 65 | + bool doTransition(const urcl::RobotMode target_mode); |
| 66 | + bool jumpToRobotMode(const urcl::RobotMode target_mode); |
| 67 | + |
| 68 | + bool safeDashboardTrigger(rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr srv); |
| 69 | + |
| 70 | + bool stopProgram(); |
| 71 | + |
| 72 | + void setModeAcceptCallback(const std::shared_ptr<SetModeGoalHandle> goal_handle); |
| 73 | + rclcpp_action::GoalResponse setModeGoalCallback(const rclcpp_action::GoalUUID& uuid, |
| 74 | + std::shared_ptr<const ur_dashboard_msgs::action::SetMode::Goal> goal); |
| 75 | + rclcpp_action::CancelResponse setModeCancelCallback(const std::shared_ptr<SetModeGoalHandle> goal_handle); |
| 76 | + |
| 77 | + void setModeExecute(const std::shared_ptr<SetModeGoalHandle> goal_handle); |
| 78 | + |
| 79 | + bool headless_mode_; |
| 80 | + |
| 81 | + std::shared_ptr<ur_dashboard_msgs::action::SetMode::Result> result_; |
| 82 | + std::shared_ptr<ur_dashboard_msgs::action::SetMode::Feedback> feedback_; |
| 83 | + std::shared_ptr<const ur_dashboard_msgs::action::SetMode::Goal> goal_; |
| 84 | + std::shared_ptr<SetModeGoalHandle> current_goal_handle_; |
| 85 | + |
| 86 | + std::atomic<urcl::RobotMode> robot_mode_; |
| 87 | + std::atomic<urcl::SafetyMode> safety_mode_; |
| 88 | + std::atomic<bool> error_ = false; |
| 89 | + std::atomic<bool> in_action_; |
| 90 | + std::atomic<bool> program_running_; |
| 91 | + std::mutex goal_mutex_; |
| 92 | + |
| 93 | + rclcpp_action::Server<ur_dashboard_msgs::action::SetMode>::SharedPtr set_mode_as_; |
| 94 | + |
| 95 | + rclcpp::CallbackGroup::SharedPtr robot_mode_sub_cb_; |
| 96 | + |
| 97 | + rclcpp::Subscription<ur_dashboard_msgs::msg::RobotMode>::SharedPtr robot_mode_sub_; |
| 98 | + rclcpp::Subscription<ur_dashboard_msgs::msg::SafetyMode>::SharedPtr safety_mode_sub_; |
| 99 | + rclcpp::Subscription<std_msgs::msg::Bool>::SharedPtr program_running_sub; |
| 100 | + |
| 101 | + rclcpp::CallbackGroup::SharedPtr service_cb_grp_; |
| 102 | + |
| 103 | + rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr unlock_protective_stop_srv_; |
| 104 | + rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr restart_safety_srv_; |
| 105 | + rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr power_on_srv_; |
| 106 | + rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr power_off_srv_; |
| 107 | + rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr brake_release_srv_; |
| 108 | + rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr stop_program_srv_; |
| 109 | + rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr play_program_srv_; |
| 110 | + rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr resend_robot_program_srv_; |
| 111 | +}; |
| 112 | +} // namespace ur_robot_driver |
| 113 | + |
| 114 | +#endif // UR_ROBOT_DRIVER__ROBOT_STATE_HELPER_HPP_ |
0 commit comments