ros2_control - kilted
Loading...
Searching...
No Matches
base_velocity_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__BASE_VELOCITY_PLUGIN_HPP_
16#define MUJOCO_ROS2_CONTROL_PLUGINS__BASE_VELOCITY_PLUGIN_HPP_
17
18#include <limits>
19#include <mutex>
20#include <string>
21
22#include <geometry_msgs/msg/twist.hpp>
23#include <geometry_msgs/msg/twist_stamped.hpp>
24#include <rclcpp/rclcpp.hpp>
25
26#include "mujoco_ros2_control_plugins/mujoco_ros2_control_plugins_base.hpp"
27
29{
30
68{
69public:
70 BaseVelocityPlugin() = default;
71 ~BaseVelocityPlugin() override = default;
72
73 bool init(rclcpp::Node::SharedPtr node, const mjModel* model, mjData* data) override;
74 void pre_step(mjData* data) override;
75 void cleanup() override;
76
77private:
78 void twistCallback(const geometry_msgs::msg::Twist& msg);
79 void twistStampedCallback(const geometry_msgs::msg::TwistStamped& msg);
80 void storeCommand(double vx, double vy, double wz);
81
82 // ROS interfaces
83 rclcpp::Node::SharedPtr node_;
84 rclcpp::Logger logger_{ rclcpp::get_logger("BaseVelocityPlugin") };
85 rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr twist_sub_;
86 rclcpp::Subscription<geometry_msgs::msg::TwistStamped>::SharedPtr twist_stamped_sub_;
87
88 // Model/body/joint lookup
89 const mjModel* model_{ nullptr };
90 int body_id_{ -1 };
91 int qvel_adr_{ -1 };
92
93 // Clamp parameters
94 double max_linear_velocity_{ std::numeric_limits<double>::infinity() };
95 double max_yaw_rate_{ std::numeric_limits<double>::infinity() };
96 rclcpp::Duration cmd_timeout_{ 0, 0 };
97
98 struct CommandState
99 {
100 double vx{ 0.0 };
101 double vy{ 0.0 };
102 double wz{ 0.0 };
103 rclcpp::Time time{ 0, 0, RCL_ROS_TIME };
104 };
105
106 // Latest command, written by the subscription callback (ROS executor thread) under
107 // cmd_mutex_. pre_step() (physics thread) copies it into cached_cmd_ via try_lock, so
108 // it never blocks on the subscription callback.
109 std::mutex cmd_mutex_;
110 CommandState latest_cmd_;
111
112 // Local copy of the command, only ever touched from pre_step() (physics thread).
113 CommandState cached_cmd_;
114};
115
116} // namespace mujoco_ros2_control_plugins
117
118#endif // MUJOCO_ROS2_CONTROL_PLUGINS__BASE_VELOCITY_PLUGIN_HPP_
Drives a mobile/floating-base robot from a commanded planar body velocity (vx, vy,...
Definition base_velocity_plugin.hpp:68
void pre_step(mjData *data) override
Called immediately before every simulation step on the physics thread. Default does nothing.
Definition base_velocity_plugin.cpp:131
bool init(rclcpp::Node::SharedPtr node, const mjModel *model, mjData *data) override
Initialize the plugin.
Definition base_velocity_plugin.cpp:48
void cleanup() override
Cleanup the plugin.
Definition base_velocity_plugin.cpp:171
Base class for MuJoCo ROS 2 control plugins.
Definition mujoco_ros2_control_plugins_base.hpp:52
Definition mujoco_ros2_control_plugins_base.hpp:22