- ros2中 launch文件是一个.py文件,而ros1中是一个 .launch文件。
-
如何将urdf导入现有的工程
背景:新建package,如何写urdf并在gazebo中显示?
答:1. 新建urdf文件夹,在package的下一层目录,和src,include等文件夹在一层。并把urdf文件写出来或者粘过来。
2.改写cmakelist.txt文件
在
末尾
添加 include代码
作用:配置编译过程安装urdf、worlds和meshes目录到install文件夹中。
因为所有的软件包都将有ROS环境中的安装目录提供,所以在编译软件包时创建该目录。
install(DIRECTORY urdf
DESTINATION share/${PROJECT_NAME})
3.编写launch文件
要在Gazebo中看到机器人,需要创建启动(launch)文件夹和启动文件
在启动文件中,将做两件事。
- 使用预定义的设置和启动Gazebo mobot_room.world
- 调用gazebo提供的spawn_entity服务以生成mobot.urdf在世界上定义的机器人。
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import ExecuteProcess, DeclareLaunchArgument
from launch_ros.actions import Node
from launch.substitutions import LaunchConfiguration
# this is the function launch system will look for
def generate_launch_description():
robot_name = 'autocar_test'
#world_file_name = 'mobot_room.world'
# full path to urdf and world file
#world = os.path.join(get_package_share_directory(robot_name), 'worlds', world_file_name)
urdf = os.path.join(get_package_share_directory(robot_name), 'urdf', 'box.urdf')
# read urdf contents because to spawn an entity in
# gazebo we need to provide entire urdf as string on command line
xml = open(urdf, 'r').read()
# double quotes need to be with escape sequence
xml = xml.replace('"', '\\"')
# this is argument format for spwan_entity service
spwan_args = '{name: \"autocar_test\", xml: \"' + xml + '\" }'
# create and return launch description object
return LaunchDescription([
# start gazebo, notice we are using libgazebo_ros_factory.so instead of libgazebo_ros_init.so
# That is because only libgazebo_ros_factory.so contains the service call to /spawn_entity
ExecuteProcess(
cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so'],
output='screen'),
# tell gazebo to spwan your robot in the world by calling service
ExecuteProcess(
cmd=['ros2', 'service', 'call', '/spawn_entity', 'gazebo_msgs/SpawnEntity', spwan_args],
output='screen'),
])
(该段代码参考张瑞雷老师博客
https://www.guyuehome.com/6788
https://zhangrelay.blog.csdn.net/article/details/104296249)
4. 再次改写cmakelist文件,实现编译后安装到启动文件夹
install(DIRECTORY launch
DESTINATION share/${PROJECT_NAME})
5.编译等步骤
cd ~/dev_ws
colcon build
. install/setup.zsh 或者 source install/setup.zsh
ros2 launch autocar_test box_launch.py
20201116更新
-
当前遇到模型更新需要:关闭gazebo->重新启动launch.py->重启gazebo的问题
解决方案:
cd ~/dev_ws
. install/setup.zsh 或者 source install/setup.zsh
ros2 launch autocar_test box_launch.py
不过,会报错:
版权声明:本文为chch2010523原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。