【Gazebo】ROS2加载URDF到Gazebo11

原文链接:【Gazebo】ROS2加载URDF到Gazebo11

ros2 加载urdf到gazebo11

图片

图片

笔记:

下载gazebo_model 解压到   ~/.bazebo/models目录下。  

killall gzserver  //第二次运行launch时关闭服务器  

killall gzclient//构建,获取安装,启动colcon build --packages-select irb52urdf . install/setup.bash
//启动前将irb52urdf包拷贝到models目录下ros2 launch irb52urdf gazebo.launch.py

不拷贝包到models目录下报错

 拷贝到这里

 

launch.py文件:


import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, ExecuteProcess
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
from launch.launch_description_sources import PythonLaunchDescriptionSource
from scripts import GazeboRosPaths

def generate_launch_description():

  use_sim_time = LaunchConfiguration('use_sim_time', default='false')
  urdf_file_name = 'urdf/irb52urdf.urdf'#

  print("urdf_file_name : {}".format(urdf_file_name))

  urdf = os.path.join(
      get_package_share_directory('irb52urdf'),
      urdf_file_name)

  return LaunchDescription([

        DeclareLaunchArgument(
            'use_sim_time',
            default_value='false',
            description='Use simulation (Gazebo) clock if true'),

        ExecuteProcess(
            cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so'],
            output='screen'),

        Node(
            package='robot_state_publisher',
            executable='robot_state_publisher',
            name='robot_state_publisher',
            output='screen',
            parameters=[{'use_sim_time': use_sim_time}],
            arguments=[urdf]),

        Node(
            package='joint_state_publisher',
            executable='joint_state_publisher',
            name='joint_state_publisher',
            output='screen',
            parameters=[{'use_sim_time': use_sim_time}]
            ),

        Node(
            package='gazebo_ros',
            executable='spawn_entity.py',
            name='urdf_spawner',
            output='screen',
            arguments=["-topic", "/robot_description", "-entity", "irb52urdf", "-x", "0.0", "-y", "0.0", "-z", "0.0"],
            #arguments=["-topic", "robot_description", "-entity", "robot", "-x", "0.0", "-y", "0.0", "-z", "0.0"],
            #arguments=["-entity","irb52urdf","-b","-file", urdf,],
            
            )
  ])

参考:

http://gazebosim.org/tutorials

https://github.com/osrf/gazebo_models

https://www.ncnynl.com/archives/202110/4811.html

http://www.lxshaw.com/tech/ros/2021/05/22/gazebo%e5%85%a5%e9%97%a8%ef%bc%88%e4%b9%9d%ef%bc%89%ef%bc%9a%e4%b8%80%e4%b8%aa%e6%9b%b4%e5%a5%bd%e7%9a%84ros2-gazebo%e6%8f%92%e4%bb%b6%e6%a8%a1%e6%9d%bf/

<think>好的,我需要帮助用户在ROS2中通过Gazebo加载和显示URDF文件。首先,根据用户提供的引用内容,我需要整理出正确的步骤。 用户提到的引用[1]指出,Gazebo需要gazebo_ros插件来与ROS2交互。所以第一步应该是安装这个插件包。然后引用[3]提到了编写launch文件,这说明用户需要创建一个launch文件来启动Gazebo加载URDF。另外,引用[2]提到在ROS2中,使用sdf文件作为world文件,可能需要生成sdf或者在world文件中包含URDF。 接下来,我需要确保URDF文件正确,包括添加必要的Gazebo插件,比如传输和控制器插件。然后,用户需要将URDF转换为sdf,可能使用gz命令。不过,在launch文件中直接调用Gazebo并传入URDF文件路径也是一个方法,可能不需要手动转换。 然后,编写launch文件时,需要使用gazebo_ros包的启动脚本,例如Gazebo.launch.py,并指定world文件。同时,需要启动robot_state_publisher来发布机器人状态,可能需要加载控制器配置。 最后,用户可能需要运行launch文件后,使用Rviz2Gazebo的界面来查看模型。需要提醒用户检查URDF是否正确,是否有必要的插件,以及可能遇到的常见问题,比如模型位置不正确或者缺少依赖包。 可能的问题包括:URDF未包含Gazebo插件导致模型不可见,launch文件路径错误,或者缺少依赖包。需要指导用户逐步排查这些问题。</think>在ROS2中通过Gazebo加载和显示URDF文件需按照以下步骤实现: ### 一、环境准备 1. 安装必要软件包: ```bash sudo apt install ros-humble-gazebo-ros-pkgs ros-humble-robot-state-publisher ``` 此安装包含GazeboROS2交互的核心插件[^1] ### 二、URDF文件配置 1. 在`<robot>`标签内添加Gazebo插件: ```xml <!-- 添加颜色定义 --> <gazebo reference="base_link"> <material>Gazebo/Orange</material> </gazebo> <!-- 添加传动插件 --> <transmission name="transmission_name"> <type>transmission_interface/SimpleTransmission</type> <joint name="joint_name"/> <actuator name="motor"> <mechanicalReduction>1</mechanicalReduction> </actuator> </transmission> <!-- 添加控制器插件 --> <gazebo> <plugin name="gazebo_ros_control" filename="libgazebo_ros_control.so"> <robotNamespace>/</robotNamespace> </plugin> </gazebo> ``` ### 三、创建launch文件 创建`robot_display.launch.py`: ```python from launch import LaunchDescription from launch_ros.actions import Node from launch.actions import IncludeLaunchDescription from launch.launch_description_sources import PythonLaunchDescriptionSource from ament_index_python.packages import get_package_share_directory import os def generate_launch_description(): pkg_path = get_package_share_directory('your_package') urdf_path = os.path.join(pkg_path, 'urdf/robot.urdf') return LaunchDescription([ # 启动Gazebo IncludeLaunchDescription( PythonLaunchDescriptionSource([ os.path.join(get_package_share_directory('gazebo_ros'), 'launch', 'gazebo.launch.py') ]), launch_arguments={'world': 'empty'}.items() ), # 发布机器人状态 Node( package='robot_state_publisher', executable='robot_state_publisher', name='robot_state_publisher', output='screen', arguments=[urdf_path] ), # 加载URDFGazebo Node( package='gazebo_ros', executable='spawn_entity.py', arguments=['-entity', 'my_robot', '-file', urdf_path], output='screen' ) ]) ``` ### 四、运行与验证 1. 编译工作空间: ```bash colcon build --packages-select your_package source install/setup.bash ``` 2. 启动launch文件: ```bash ros2 launch your_package robot_display.launch.py ``` 3. 验证方法: - 在Gazebo界面中查看模型渲染 - 使用`ros2 topic list`检查`/gazebo/model_states`话题 - 通过Rviz2添加RobotModel显示 §§注意事项§§ 1. URDF必须包含有效的惯性参数`<inertial>`和碰撞属性`<collision>` 2. 若出现模型"漂浮"现象,需检查质量(mass)和惯性矩阵设置 3. Gazebo版本需与ROS2发行版匹配(Humble推荐Gazebo Fortress)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值