|
| 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-07 |
| 34 | + * |
| 35 | + * |
| 36 | + * |
| 37 | + * |
| 38 | + */ |
| 39 | +//---------------------------------------------------------------------- |
| 40 | + |
| 41 | +#ifndef UR_CONTROLLERS__TOOL_CONTACT_CONTROLLER_HPP_ |
| 42 | +#define UR_CONTROLLERS__TOOL_CONTACT_CONTROLLER_HPP_ |
| 43 | + |
| 44 | +#include <chrono> |
| 45 | +#include <vector> |
| 46 | +#include <memory> |
| 47 | + |
| 48 | +#include <controller_interface/controller_interface.hpp> |
| 49 | +#include "std_msgs/msg/bool.hpp" |
| 50 | +#include <rclcpp_action/server.hpp> |
| 51 | +#include <rclcpp_action/create_server.hpp> |
| 52 | +#include <rclcpp/rclcpp.hpp> |
| 53 | +#include <rclcpp_action/server_goal_handle.hpp> |
| 54 | +#include <rclcpp/duration.hpp> |
| 55 | + |
| 56 | +#include <realtime_tools/realtime_buffer.hpp> |
| 57 | +#include <realtime_tools/realtime_server_goal_handle.hpp> |
| 58 | + |
| 59 | +#include <ur_msgs/action/tool_contact.hpp> |
| 60 | +#include "ur_controllers/tool_contact_controller_parameters.hpp" |
| 61 | + |
| 62 | +namespace ur_controllers |
| 63 | +{ |
| 64 | +class ToolContactController : public controller_interface::ControllerInterface |
| 65 | +{ |
| 66 | +public: |
| 67 | + ToolContactController() = default; |
| 68 | + ~ToolContactController() override = default; |
| 69 | + |
| 70 | + controller_interface::CallbackReturn on_init() override; |
| 71 | + |
| 72 | + controller_interface::InterfaceConfiguration command_interface_configuration() const override; |
| 73 | + |
| 74 | + controller_interface::InterfaceConfiguration state_interface_configuration() const override; |
| 75 | + |
| 76 | + controller_interface::CallbackReturn on_configure(const rclcpp_lifecycle::State& previous_state) override; |
| 77 | + |
| 78 | + controller_interface::CallbackReturn on_activate(const rclcpp_lifecycle::State& previous_state) override; |
| 79 | + |
| 80 | + controller_interface::CallbackReturn on_deactivate(const rclcpp_lifecycle::State& previous_state) override; |
| 81 | + |
| 82 | + controller_interface::CallbackReturn on_shutdown(const rclcpp_lifecycle::State& previous_state) override; |
| 83 | + |
| 84 | + controller_interface::return_type update(const rclcpp::Time& time, const rclcpp::Duration& period) override; |
| 85 | + |
| 86 | +private: |
| 87 | + using RealtimeGoalHandle = realtime_tools::RealtimeServerGoalHandle<ur_msgs::action::ToolContact>; |
| 88 | + using RealtimeGoalHandlePtr = std::shared_ptr<RealtimeGoalHandle>; |
| 89 | + using RealtimeGoalHandleBuffer = realtime_tools::RealtimeBuffer<RealtimeGoalHandlePtr>; |
| 90 | + |
| 91 | + RealtimeGoalHandleBuffer rt_active_goal_; ///< Currently active action goal, if any. |
| 92 | + ur_msgs::action::ToolContact::Feedback::SharedPtr feedback_; ///< preallocated feedback |
| 93 | + rclcpp::TimerBase::SharedPtr goal_handle_timer_; ///< Timer to frequently check on the running goal |
| 94 | + |
| 95 | + // non-rt function that will be called with action_monitor_period to monitor the rt action |
| 96 | + rclcpp::Duration action_monitor_period_ = rclcpp::Duration::from_seconds(0.05); |
| 97 | + void action_handler(); |
| 98 | + |
| 99 | + rclcpp_action::GoalResponse goal_received_callback(const rclcpp_action::GoalUUID& /*uuid*/, |
| 100 | + std::shared_ptr<const ur_msgs::action::ToolContact::Goal> goal); |
| 101 | + |
| 102 | + void |
| 103 | + goal_accepted_callback(std::shared_ptr<rclcpp_action::ServerGoalHandle<ur_msgs::action::ToolContact>> goal_handle); |
| 104 | + |
| 105 | + rclcpp_action::CancelResponse goal_canceled_callback( |
| 106 | + const std::shared_ptr<rclcpp_action::ServerGoalHandle<ur_msgs::action::ToolContact>> goal_handle); |
| 107 | + |
| 108 | + std::atomic<bool> tool_contact_enable_ = false; |
| 109 | + std::atomic<bool> tool_contact_active_ = false; |
| 110 | + std::atomic<bool> tool_contact_abort_ = false; |
| 111 | + std::atomic<bool> change_requested_ = false; |
| 112 | + std::atomic<bool> logged_once_ = false; |
| 113 | + std::atomic<bool> should_reset_goal = false; |
| 114 | + |
| 115 | + std::optional<std::reference_wrapper<hardware_interface::LoanedStateInterface>> tool_contact_result_interface_; |
| 116 | + std::optional<std::reference_wrapper<hardware_interface::LoanedStateInterface>> major_version_state_interface_; |
| 117 | + std::optional<std::reference_wrapper<hardware_interface::LoanedStateInterface>> tool_contact_state_interface_; |
| 118 | + std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> tool_contact_set_state_interface_; |
| 119 | + |
| 120 | + rclcpp_action::Server<ur_msgs::action::ToolContact>::SharedPtr tool_contact_action_server_; |
| 121 | + |
| 122 | + std::shared_ptr<tool_contact_controller::ParamListener> tool_contact_param_listener_; |
| 123 | + tool_contact_controller::Params tool_contact_params_; |
| 124 | + |
| 125 | + static constexpr double TOOL_CONTACT_STANDBY = 1.0; |
| 126 | + static constexpr double TOOL_CONTACT_WAITING_BEGIN = 2.0; |
| 127 | + static constexpr double TOOL_CONTACT_EXECUTING = 3.0; |
| 128 | + static constexpr double TOOL_CONTACT_FAILURE_BEGIN = 4.0; |
| 129 | + static constexpr double TOOL_CONTACT_WAITING_END = 5.0; |
| 130 | + static constexpr double TOOL_CONTACT_SUCCESS_END = 6.0; |
| 131 | + static constexpr double TOOL_CONTACT_FAILURE_END = 7.0; |
| 132 | +}; |
| 133 | +} // namespace ur_controllers |
| 134 | + |
| 135 | +#endif // UR_CONTROLLERS__TOOL_CONTACT_CONTROLLER_HPP_ |
0 commit comments