ROS2 的一点学习笔记

  1. ros2中 launch文件是一个.py文件,而ros1中是一个 .launch文件。
  2. 如何将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更新

  1. 当前遇到模型更新需要:关闭gazebo->重新启动launch.py->重启gazebo的问题
    解决方案:
    cd ~/dev_ws
    . install/setup.zsh 或者 source install/setup.zsh
    ros2 launch autocar_test box_launch.py
    不过,会报错:
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值