|
| 1 | +// Copyright (c) 2026 PickNik LLC |
| 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 | + * \brief Controller for updating the robot's gravity vector via ros2_control. |
| 33 | + * |
| 34 | + * Exposes the ~/set_gravity service (ur_msgs/srv/SetGravity). Incoming gravity directions are |
| 35 | + * transformed into the robot base frame and written to the gravity command interfaces. |
| 36 | + * |
| 37 | + * \author Adam Pettinger adam.l.pettinger@gmail.com |
| 38 | + * \date 2026-03-13 |
| 39 | + * |
| 40 | + */ |
| 41 | +//---------------------------------------------------------------------- |
| 42 | + |
| 43 | +#ifndef UR_CONTROLLERS__GRAVITY_UPDATE_CONTROLLER_HPP_ |
| 44 | +#define UR_CONTROLLERS__GRAVITY_UPDATE_CONTROLLER_HPP_ |
| 45 | + |
| 46 | +#include <memory> |
| 47 | +#include <string> |
| 48 | +#include <unordered_map> |
| 49 | +#include <vector> |
| 50 | + |
| 51 | +#include "controller_interface/controller_interface.hpp" |
| 52 | +#include "ur_msgs/srv/set_gravity.hpp" |
| 53 | +#include "rclcpp/time.hpp" |
| 54 | +#include "rclcpp/duration.hpp" |
| 55 | +#include "realtime_tools/realtime_thread_safe_box.hpp" |
| 56 | +#include "tf2/LinearMath/Quaternion.hpp" |
| 57 | +#include "tf2_geometry_msgs/tf2_geometry_msgs.hpp" |
| 58 | +#include "tf2_ros/buffer.hpp" |
| 59 | +#include "tf2_ros/transform_listener.hpp" |
| 60 | +#include "ur_controllers/gravity_update_controller_parameters.hpp" |
| 61 | + |
| 62 | +namespace ur_controllers |
| 63 | +{ |
| 64 | +enum CommandInterfaces |
| 65 | +{ |
| 66 | + GRAVITY_X, |
| 67 | + GRAVITY_Y, |
| 68 | + GRAVITY_Z, |
| 69 | + GRAVITY_ASYNC_SUCCESS, |
| 70 | +}; |
| 71 | + |
| 72 | +enum StateInterfaces |
| 73 | +{ |
| 74 | + INITIALIZED_FLAG = 69, |
| 75 | +}; |
| 76 | + |
| 77 | +class GravityUpdateController : public controller_interface::ControllerInterface |
| 78 | +{ |
| 79 | +public: |
| 80 | + controller_interface::InterfaceConfiguration command_interface_configuration() const override; |
| 81 | + |
| 82 | + controller_interface::InterfaceConfiguration state_interface_configuration() const override; |
| 83 | + |
| 84 | + controller_interface::return_type update(const rclcpp::Time& time, const rclcpp::Duration& period) override; |
| 85 | + |
| 86 | + CallbackReturn on_configure(const rclcpp_lifecycle::State& previous_state) override; |
| 87 | + |
| 88 | + CallbackReturn on_activate(const rclcpp_lifecycle::State& previous_state) override; |
| 89 | + |
| 90 | + CallbackReturn on_deactivate(const rclcpp_lifecycle::State& previous_state) override; |
| 91 | + |
| 92 | + CallbackReturn on_cleanup(const rclcpp_lifecycle::State& previous_state) override; |
| 93 | + |
| 94 | + CallbackReturn on_init() override; |
| 95 | + |
| 96 | +private: |
| 97 | + void setGravity(const ur_msgs::srv::SetGravity::Request::SharedPtr req, |
| 98 | + ur_msgs::srv::SetGravity::Response::SharedPtr resp); |
| 99 | + |
| 100 | +protected: |
| 101 | + rclcpp::Service<ur_msgs::srv::SetGravity>::SharedPtr set_gravity_srv_; |
| 102 | + |
| 103 | + std::shared_ptr<tf2_ros::Buffer> tf_buffer_; |
| 104 | + std::shared_ptr<tf2_ros::TransformListener> tf_listener_; |
| 105 | + |
| 106 | + // Parameters from ROS for gravity_update_controller |
| 107 | + std::shared_ptr<gravity_update_controller::ParamListener> param_listener_; |
| 108 | + gravity_update_controller::Params params_; |
| 109 | + |
| 110 | + static constexpr double ASYNC_WAITING = 2.0; |
| 111 | + |
| 112 | + realtime_tools::RealtimeThreadSafeBox<std::optional<tf2::Vector3>> cmd_gravity_box_; |
| 113 | + realtime_tools::RealtimeThreadSafeBox<std::optional<double>> async_success_box_; |
| 114 | + |
| 115 | + /** |
| 116 | + * @brief wait until a command interface isn't in state ASYNC_WAITING anymore or until the parameter maximum_retries |
| 117 | + * have been reached |
| 118 | + */ |
| 119 | + bool waitForAsyncCommand(std::function<double(void)> get_value); |
| 120 | +}; |
| 121 | +} // namespace ur_controllers |
| 122 | + |
| 123 | +#endif // UR_CONTROLLERS__GRAVITY_UPDATE_CONTROLLER_HPP_ |
0 commit comments