ros2_control - kilted
Loading...
Searching...
No Matches
external_wrench_plugin.hpp
1// Copyright 2026 PAL Robotics S.L.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef MUJOCO_ROS2_CONTROL_PLUGINS__EXTERNAL_WRENCH_PLUGIN_HPP_
16#define MUJOCO_ROS2_CONTROL_PLUGINS__EXTERNAL_WRENCH_PLUGIN_HPP_
17
18#include <mutex>
19#include <queue>
20#include <string>
21#include <vector>
22
23#include <mujoco_ros2_control_msgs/srv/apply_external_wrench.hpp>
24#include <rclcpp/rclcpp.hpp>
25#include <realtime_tools/realtime_publisher.hpp>
26#include <visualization_msgs/msg/marker_array.hpp>
27
28#include "mujoco_ros2_control_plugins/mujoco_ros2_control_plugins_base.hpp"
29
31{
32
89{
90public:
91 ExternalWrenchPlugin() = default;
92 ~ExternalWrenchPlugin() override = default;
93
94 bool init(rclcpp::Node::SharedPtr node, const mjModel* model, mjData* data) override;
95 void pre_step(mjData* data) override;
96 void cleanup() override;
97
112 void publish_markers(visualization_msgs::msg::MarkerArray& markers) const;
113
114private:
115 using ApplyExternalWrench = mujoco_ros2_control_msgs::srv::ApplyExternalWrench;
116 using MarkerArray = visualization_msgs::msg::MarkerArray;
117
119 struct ActiveWrench
120 {
121 int body_id{ -1 };
122 std::string link_name;
123 mjtNum force[3]{ 0.0, 0.0, 0.0 };
124 mjtNum torque[3]{ 0.0, 0.0, 0.0 };
125 mjtNum application_point[3]{ 0.0, 0.0, 0.0 };
126 rclcpp::Time end_time{ 0, 0, RCL_ROS_TIME };
127 rclcpp::Duration ramp_down_duration{ 0, 0 };
128 double current_scale{ 1.0 };
129 };
130
132 void handleApplyWrench(const ApplyExternalWrench::Request::SharedPtr request,
133 ApplyExternalWrench::Response::SharedPtr response);
134
136 void publish_markers();
137
138 // ROS interfaces
139 rclcpp::Node::SharedPtr node_;
140 rclcpp::Logger logger_{ rclcpp::get_logger("ExternalWrenchPlugin") };
141 rclcpp::Service<ApplyExternalWrench>::SharedPtr service_;
142
143 rclcpp::Publisher<MarkerArray>::SharedPtr marker_pub_raw_;
144 std::unique_ptr<realtime_tools::RealtimePublisher<MarkerArray>> marker_pub_;
145
146 // Model pointer (const, valid for simulation lifetime)
147 const mjModel* model_{ nullptr };
148
149 // Pending queue written by service callback, drained in pre_step()
150 std::mutex pending_mutex_;
151 std::queue<ActiveWrench> pending_wrenches_;
152
153 // Active wrenches — only modified inside pre_step()
154 std::vector<ActiveWrench> active_wrenches_;
155
156 // Body IDs whose xfrc_applied slots were written in the previous pre_step() call.
157 // Zeroed at the start of the next pre_step() before re-accumulating.
158 std::vector<int> prev_written_body_ids_;
159
160 // Marker visualization scaling
162 double force_arrow_scale_{ 0.01 };
164 double torque_arrow_scale_{ 0.1 };
165
166 std::atomic_bool service_requested_{ false };
167};
168
169} // namespace mujoco_ros2_control_plugins
170
171#endif // MUJOCO_ROS2_CONTROL_PLUGINS__EXTERNAL_WRENCH_PLUGIN_HPP_
Plugin that applies one or more external wrenches (force + torque) to named MuJoCo bodies for configu...
Definition external_wrench_plugin.hpp:89
bool init(rclcpp::Node::SharedPtr node, const mjModel *model, mjData *data) override
Initialize the plugin.
Definition external_wrench_plugin.cpp:27
void publish_markers(visualization_msgs::msg::MarkerArray &markers) const
Append RViz arrow markers for all currently active wrenches to markers.
Definition external_wrench_plugin.cpp:190
void cleanup() override
Cleanup the plugin.
Definition external_wrench_plugin.cpp:177
void pre_step(mjData *data) override
Called immediately before every simulation step on the physics thread. Default does nothing.
Definition external_wrench_plugin.cpp:61
Base class for MuJoCo ROS 2 control plugins.
Definition mujoco_ros2_control_plugins_base.hpp:52
Definition mujoco_ros2_control_plugins_base.hpp:22