Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ We should include:
</joint>


Using force-torque sensors in simulation
----------------------------------------

To use ``force-torque`` sensors in *gz_ros2_control* you should define its parameters in your URDF or SDF (see the `SDF specification <http://sdformat.org/spec?ver=1.12&elem=sensor#sensor_force_torque>`__)

.. code-block:: xml

<sensor name="force_torque_sensor" type="force_torque">
<update_rate>10.0</update_rate>
<always_on>true</always_on>
<visualize>true</visualize>
<topic>force_torque_sensor</topic>
</sensor>

It is important to add this as ``reference`` sensor in the ``<gazebo>`` tag in your URDF file.

Add the gz_ros2_control plugin
==========================================

Expand Down
107 changes: 106 additions & 1 deletion gz_ros2_control/src/gz_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "gz_ros2_control/gz_system.hpp"

#include <array>
#include <cstddef>
#include <limits>
#include <map>
#include <memory>
Expand All @@ -23,10 +25,12 @@

#ifdef GZ_HEADERS
#include <gz/msgs/imu.pb.h>
#include <gz/msgs/wrench.pb.h>

#include <gz/physics/Geometry.hh>
#include <gz/sim/components/AngularVelocity.hh>
#include <gz/sim/components/Imu.hh>
#include <gz/sim/components/ForceTorque.hh>
#include <gz/sim/components/JointAxis.hh>
#include <gz/sim/components/JointForceCmd.hh>
#include <gz/sim/components/JointPosition.hh>
Expand All @@ -48,10 +52,12 @@
#define GZ_VECTOR_DOT dot
#else
#include <ignition/msgs/imu.pb.h>
#include <ignition/msgs/wrench.pb.h>

#include <ignition/math/Vector3.hh>
#include <ignition/gazebo/components/AngularVelocity.hh>
#include <ignition/gazebo/components/Imu.hh>
#include <ignition/gazebo/components/ForceTorque.hh>
#include <ignition/gazebo/components/JointAxis.hh>
#include <ignition/gazebo/components/JointForceCmd.hh>
#include <ignition/gazebo/components/JointPosition.hh>
Expand Down Expand Up @@ -122,6 +128,35 @@ struct MimicJoint
std::vector<std::string> interfaces_to_mimic;
};

class ForceTorqueData
{
public:
/// \brief force torque sensor's name.
std::string name{};

/// \brief force torque sensor's topic name.
std::string topicName{};

/// \brief handles to the force torque from within Gazebo
sim::Entity sim_ft_sensors_ = sim::kNullEntity;

/// \brief An array per FT
std::array<double, 6> ft_sensor_data_;

/// \brief callback to get the Force Torque topic values
void OnForceTorque(const GZ_MSGS_NAMESPACE Wrench & _msg);
};

void ForceTorqueData::OnForceTorque(const GZ_MSGS_NAMESPACE Wrench & _msg)
{
this->ft_sensor_data_[0] = _msg.force().x();
this->ft_sensor_data_[1] = _msg.force().y();
this->ft_sensor_data_[2] = _msg.force().z();
this->ft_sensor_data_[3] = _msg.torque().x();
this->ft_sensor_data_[4] = _msg.torque().y();
this->ft_sensor_data_[5] = _msg.torque().z();
}

class ImuData
{
public:
Expand Down Expand Up @@ -170,9 +205,12 @@ class gz_ros2_control::GazeboSimSystemPrivate
/// \brief vector with the joint's names.
std::vector<struct jointData> joints_;

/// \brief vector with the imus .
/// \brief vector with the imus.
std::vector<std::shared_ptr<ImuData>> imus_;

/// \brief vector with the force torque sensors.
std::vector<std::shared_ptr<ForceTorqueData>> ft_sensors_;

/// \brief state interfaces that will be exported to the Resource Manager
std::vector<hardware_interface::StateInterface> state_interfaces_;

Expand Down Expand Up @@ -521,6 +559,55 @@ void GazeboSimSystem::registerSensors(
this->dataPtr->imus_.push_back(imuData);
return true;
});

this->dataPtr->ecm->Each<sim::components::ForceTorque,
sim::components::Name>(
[&](const sim::Entity & _entity,
const sim::components::ForceTorque *,
const sim::components::Name * _name) -> bool
{
auto ftData = std::make_shared<ForceTorqueData>();
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "Loading sensor: " << _name->Data());

auto sensorTopicComp = this->dataPtr->ecm->Component<
sim::components::SensorTopic>(_entity);
if (sensorTopicComp) {
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "Topic name: " << sensorTopicComp->Data());
}

RCLCPP_INFO_STREAM(
this->nh_->get_logger(), "\tState:");
ftData->name = _name->Data();
ftData->sim_ft_sensors_ = _entity;

hardware_interface::ComponentInfo component;
for (auto & comp : sensor_components_) {
if (comp.name == _name->Data()) {
component = comp;
}
}

static const std::map<std::string, size_t> interface_name_map = {
Comment thread
BartlomiejK2 marked this conversation as resolved.
{"force.x", 0},
{"force.y", 1},
{"force.z", 2},
{"torque.x", 3},
{"torque.y", 4},
{"torque.z", 5},
};

for (const auto & state_interface : component.state_interfaces) {
RCLCPP_INFO_STREAM(this->nh_->get_logger(), "\t\t " << state_interface.name);

size_t data_index = interface_name_map.at(state_interface.name);
this->dataPtr->state_interfaces_.emplace_back(
ftData->name,
state_interface.name,
&ftData->ft_sensor_data_[data_index]);
}
this->dataPtr->ft_sensors_.push_back(ftData);
return true;
});
}

CallbackReturn
Expand Down Expand Up @@ -639,6 +726,24 @@ hardware_interface::return_type GazeboSimSystem::read(
}
}
}

for (unsigned int i = 0; i < this->dataPtr->ft_sensors_.size(); ++i) {
if (this->dataPtr->ft_sensors_[i]->topicName.empty()) {
auto sensorTopicComp = this->dataPtr->ecm->Component<
sim::components::SensorTopic>(this->dataPtr->ft_sensors_[i]->sim_ft_sensors_);
if (sensorTopicComp) {
this->dataPtr->ft_sensors_[i]->topicName = sensorTopicComp->Data();
RCLCPP_INFO_STREAM(
this->nh_->get_logger(), "ForceTorque " << this->dataPtr->ft_sensors_[i]->name <<
" has a topic name: " << sensorTopicComp->Data());

this->dataPtr->node.Subscribe(
this->dataPtr->ft_sensors_[i]->topicName, &ForceTorqueData::OnForceTorque,
this->dataPtr->ft_sensors_[i].get());
}
}
}

return hardware_interface::return_type::OK;
}

Expand Down
1 change: 1 addition & 0 deletions gz_ros2_control_demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ install(DIRECTORY
launch
config
urdf
worlds
DESTINATION share/${PROJECT_NAME}/
)

Expand Down
23 changes: 23 additions & 0 deletions gz_ros2_control_demos/config/cart_controller_ft_sensor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
controller_manager:
ros__parameters:
update_rate: 1000 # Hz

joint_state_broadcaster:
type: joint_state_broadcaster/JointStateBroadcaster

joint_trajectory_controller:
ros__parameters:
type: joint_trajectory_controller/JointTrajectoryController
joints:
- slider_to_cart
command_interfaces:
- position
state_interfaces:
- position
- velocity

force_torque_sensor_broadcaster:
ros__parameters:
type: force_torque_sensor_broadcaster/ForceTorqueSensorBroadcaster
sensor_name: "force_torque_sensor"
frame_id: "slider_to_cart"
140 changes: 140 additions & 0 deletions gz_ros2_control_demos/launch/cart_example_ft_sensor.launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Copyright 2025 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription
from launch.actions import RegisterEventHandler
from launch.event_handlers import OnProcessExit
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import Command, FindExecutable, LaunchConfiguration, PathJoinSubstitution

from launch_ros.actions import Node
from launch_ros.substitutions import FindPackageShare


def generate_launch_description():
# Launch Arguments
use_sim_time = LaunchConfiguration('use_sim_time', default=True)
gz_args = LaunchConfiguration('gz_args', default='')

# Get URDF via xacro
robot_description_content = Command(
[
PathJoinSubstitution([FindExecutable(name='xacro')]),
' ',
PathJoinSubstitution(
[FindPackageShare('gz_ros2_control_demos'),
'urdf', 'test_cart_ft_sensor.xacro.urdf']
),
]
)
robot_description = {'robot_description': robot_description_content}
robot_controllers = PathJoinSubstitution(
[
FindPackageShare('gz_ros2_control_demos'),
'config',
'cart_controller_ft_sensor.yaml',
]
)

gazebo_world = PathJoinSubstitution(
[
FindPackageShare('gz_ros2_control_demos'),
'worlds',
'empty_ft_sensor.sdf',
]
)

node_robot_state_publisher = Node(
package='robot_state_publisher',
executable='robot_state_publisher',
output='screen',
parameters=[robot_description]
)

gz_spawn_entity = Node(
package='ros_gz_sim',
executable='create',
output='screen',
arguments=['-topic', 'robot_description',
'-name', 'cart', '-allow_renaming', 'true'],
)

joint_state_broadcaster_spawner = Node(
package='controller_manager',
executable='spawner',
arguments=['joint_state_broadcaster'],
)
joint_trajectory_controller_spawner = Node(
package='controller_manager',
executable='spawner',
arguments=[
'joint_trajectory_controller',
'--param-file',
robot_controllers,
],
)
force_torque_sensor_broadcaster = Node(
package='controller_manager',
executable='spawner',
arguments=[
'force_torque_sensor_broadcaster',
'--param-file',
robot_controllers,
],
)

# Bridge
bridge = Node(
package='ros_gz_bridge',
executable='parameter_bridge',
arguments=['/clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock'],
output='screen'
)

return LaunchDescription([
# Launch gazebo environment
IncludeLaunchDescription(
PythonLaunchDescriptionSource(
[PathJoinSubstitution([FindPackageShare('ros_gz_sim'),
'launch',
'gz_sim.launch.py'])]),
launch_arguments=[('gz_args', [gz_args, ' -r -v 4 ', gazebo_world])]),
RegisterEventHandler(
event_handler=OnProcessExit(
target_action=gz_spawn_entity,
on_exit=[joint_state_broadcaster_spawner],
)
),
RegisterEventHandler(
event_handler=OnProcessExit(
target_action=joint_state_broadcaster_spawner,
on_exit=[joint_trajectory_controller_spawner],
)
),
RegisterEventHandler(
event_handler=OnProcessExit(
target_action=joint_trajectory_controller_spawner,
on_exit=[force_torque_sensor_broadcaster],
)
),
bridge,
node_robot_state_publisher,
gz_spawn_entity,
# Launch Arguments
DeclareLaunchArgument(
'use_sim_time',
default_value=use_sim_time,
description='If true, use simulated clock'),
])
1 change: 1 addition & 0 deletions gz_ros2_control_demos/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<exec_depend>control_msgs</exec_depend>
<exec_depend>diff_drive_controller</exec_depend>
<exec_depend>effort_controllers</exec_depend>
<exec_depend>force_torque_sensor_broadcaster</exec_depend>
<exec_depend>gz_ros2_control</exec_depend>
<exec_depend>hardware_interface</exec_depend>
<exec_depend>imu_sensor_broadcaster</exec_depend>
Expand Down
Loading
Loading