|
| 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 | +//---------------------------------------------------------------------- |
| 30 | +/*!\file |
| 31 | + * |
| 32 | + * \author Vincenzo Di Pentima [email protected] |
| 33 | + * \date 2024-09-26 |
| 34 | + */ |
| 35 | +//---------------------------------------------------------------------- |
| 36 | +#ifndef UR_CONTROLLERS__FREEDRIVE_MODE_CONTROLLER_HPP_ |
| 37 | +#define UR_CONTROLLERS__FREEDRIVE_MODE_CONTROLLER_HPP_ |
| 38 | + |
| 39 | +#pragma once |
| 40 | + |
| 41 | +#include <memory> |
| 42 | +#include <string> |
| 43 | +#include <vector> |
| 44 | +#include <thread> |
| 45 | +#include <mutex> |
| 46 | + |
| 47 | +#include <controller_interface/controller_interface.hpp> |
| 48 | +#include <rclcpp/rclcpp.hpp> |
| 49 | +#include <rclcpp_action/server.hpp> |
| 50 | +#include <rclcpp_action/create_server.hpp> |
| 51 | +#include <rclcpp_action/server_goal_handle.hpp> |
| 52 | +#include <rclcpp/time.hpp> |
| 53 | +#include <rclcpp/duration.hpp> |
| 54 | +#include "std_msgs/msg/bool.hpp" |
| 55 | + |
| 56 | +#include "freedrive_mode_controller_parameters.hpp" |
| 57 | + |
| 58 | +namespace ur_controllers |
| 59 | +{ |
| 60 | +enum CommandInterfaces |
| 61 | +{ |
| 62 | + FREEDRIVE_MODE_ASYNC_SUCCESS = 0u, |
| 63 | + FREEDRIVE_MODE_ENABLE = 1, |
| 64 | + FREEDRIVE_MODE_ABORT = 2, |
| 65 | +}; |
| 66 | + |
| 67 | +using namespace std::chrono_literals; // NOLINT |
| 68 | + |
| 69 | +class FreedriveModeController : public controller_interface::ControllerInterface |
| 70 | +{ |
| 71 | +public: |
| 72 | + controller_interface::InterfaceConfiguration command_interface_configuration() const override; |
| 73 | + |
| 74 | + controller_interface::InterfaceConfiguration state_interface_configuration() const override; |
| 75 | + |
| 76 | + // Change the input for the update function |
| 77 | + controller_interface::return_type update(const rclcpp::Time& time, const rclcpp::Duration& period) override; |
| 78 | + |
| 79 | + CallbackReturn on_configure(const rclcpp_lifecycle::State& previous_state) override; |
| 80 | + |
| 81 | + CallbackReturn on_activate(const rclcpp_lifecycle::State& previous_state) override; |
| 82 | + |
| 83 | + CallbackReturn on_cleanup(const rclcpp_lifecycle::State& previous_state) override; |
| 84 | + |
| 85 | + CallbackReturn on_deactivate(const rclcpp_lifecycle::State& previous_state) override; |
| 86 | + |
| 87 | + CallbackReturn on_init() override; |
| 88 | + |
| 89 | +private: |
| 90 | + // Command interfaces: optional is used only to avoid adding reference initialization |
| 91 | + std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> async_success_command_interface_; |
| 92 | + std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> enable_command_interface_; |
| 93 | + std::optional<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> abort_command_interface_; |
| 94 | + |
| 95 | + std::shared_ptr<rclcpp::Subscription<std_msgs::msg::Bool>> enable_freedrive_mode_sub_; |
| 96 | + |
| 97 | + rclcpp::TimerBase::SharedPtr freedrive_sub_timer_; ///< Timer to check for timeout on input |
| 98 | + mutable std::chrono::seconds timeout_interval_; |
| 99 | + void freedrive_cmd_callback(const std_msgs::msg::Bool::SharedPtr msg); |
| 100 | + |
| 101 | + std::shared_ptr<freedrive_mode_controller::ParamListener> freedrive_param_listener_; |
| 102 | + freedrive_mode_controller::Params freedrive_params_; |
| 103 | + |
| 104 | + std::atomic<bool> freedrive_active_; |
| 105 | + std::atomic<bool> change_requested_; |
| 106 | + std::atomic<double> async_state_; |
| 107 | + std::atomic<double> first_log_; |
| 108 | + std::atomic<double> timer_started_; |
| 109 | + |
| 110 | + void start_timer(); |
| 111 | + void timeout_callback(); |
| 112 | + |
| 113 | + std::thread logging_thread_; |
| 114 | + std::atomic<bool> logging_thread_running_; |
| 115 | + std::atomic<bool> logging_requested_; |
| 116 | + std::condition_variable logging_condition_; |
| 117 | + std::mutex log_mutex_; |
| 118 | + void log_task(); |
| 119 | + void start_logging_thread(); |
| 120 | + void stop_logging_thread(); |
| 121 | + |
| 122 | + static constexpr double ASYNC_WAITING = 2.0; |
| 123 | + /** |
| 124 | + * @brief wait until a command interface isn't in state ASYNC_WAITING anymore or until the parameter maximum_retries |
| 125 | + * have been reached |
| 126 | + */ |
| 127 | + bool waitForAsyncCommand(std::function<double(void)> get_value); |
| 128 | +}; |
| 129 | +} // namespace ur_controllers |
| 130 | +#endif // UR_CONTROLLERS__PASSTHROUGH_TRAJECTORY_CONTROLLER_HPP_ |
0 commit comments