使用机器人操作系统ROS 2和仿真软件Gazebo 9环境综合测试教程(三)

举报
zhangrelay 发表于 2021/07/15 03:19:18 2021/07/15
【摘要】 在完成教程(一)搭建机器人和(二)命令遥控可视化后,将仿真机器人用于更为逼真的环境,可以测试如SLAM,区域覆盖以及场馆巡逻算法,这里环境均采用aws提供模型,分别为smallhouse和bookstore,环境适用于ROS2和ROS1全部案例,但ROS1内容不做讲解,这里只简要叙述一下ROS2中调试和使用的过程。 ROS2和Gazebo9中mobot室内环境仿真测...

在完成教程(一)搭建机器人和(二)命令遥控可视化后,将仿真机器人用于更为逼真的环境,可以测试如SLAM,区域覆盖以及场馆巡逻算法,这里环境均采用aws提供模型,分别为smallhouse和bookstore,环境适用于ROS2和ROS1全部案例,但ROS1内容不做讲解,这里只简要叙述一下ROS2中调试和使用的过程。

ROS2和Gazebo9中mobot室内环境仿真测试

在环境中添加机器人模型:

功能 ROS 1 ROS 2
Service names spawn_sdf_model spawn_entity
  spawn_urdf_model spawn_entity
  delete_model delete_entity
  delete_light delete_entity
     
Service types gazebo_msgs/SpawnModel gazebo_msgs/SpawnEntity
  gazebo_msgs/DeleteModel gazebo_msgs/DeleteEntity
  gazebo_msgs/DeleteLight gazebo_msgs/DeleteEntity
     
Fields model_name name (optional)
  light_name name (optional)
  model_xml xml
  robot_namespace (optional) robot_namespace (optional)
  initial_pose (optional) initial_pose (optional)
  reference_frame (optional) reference_frame (optional)

在launch中添加机器人需要注意如上差异,如添加一个urdf格式机器人可参考如下命令:

ros2 service call /spawn_entity 'gazebo_msgs/SpawnEntity' '{name: "urdf_ball", xml: "<?xml version=\"1.0\" ?><robot name=\"will_be_ignored\"><link name=\"link\"><visual><geometry><sphere radius=\"1.0\"/></geometry></visual><inertial><mass value=\"1\"/><inertia ixx=\"1\" ixy=\"0.0\" ixz=\"0.0\" iyy=\"1\" iyz=\"0.0\" izz=\"1\"/></inertial></link></robot>"}'

更多内容参考:https://github.com/ros-simulation/gazebo_ros_pkgs/wiki/ROS-2-Migration:-Spawn-and-delete

如果需要在制定位置插入机器人,需要查阅spawn_entity.py源码:


  
  
  1. # Encode xml object back into string for service call
  2. entity_xml = ElementTree.tostring(xml_parsed)
  3. # Form requested Pose from arguments
  4. initial_pose = Pose()
  5. initial_pose.position.x = float(self.args.x)
  6. initial_pose.position.y = float(self.args.y)
  7. initial_pose.position.z = float(self.args.z)
  8. q = quaternion_from_euler(self.args.R, self.args.P, self.args.Y)
  9. initial_pose.orientation.w = q[0]
  10. initial_pose.orientation.x = q[1]
  11. initial_pose.orientation.y = q[2]
  12. initial_pose.orientation.z = q[3]

注意这里的initial_pose和表中参数的对应情况,不再赘述。

mobot加入smallhouse场景如下所示,支持多机器人!后续补充:

可以在此环境中复习,前2课所学过的全部内容。

启动文件smallhouse.launch.py代码如下:


  
  
  1. import os
  2. from ament_index_python.packages import get_package_share_directory
  3. from launch import LaunchDescription
  4. from launch.actions import ExecuteProcess, DeclareLaunchArgument
  5. from launch.actions import IncludeLaunchDescription
  6. from launch_ros.actions import Node
  7. from launch.conditions import IfCondition
  8. from launch.launch_description_sources import PythonLaunchDescriptionSource
  9. from launch.substitutions import LaunchConfiguration
  10. # this is the function launch system will look for
  11. def generate_launch_description():
  12. robot_name = 'mobot'
  13. world_file_name = 'smallhouse.world'
  14. # full path to urdf and world file
  15. world = os.path.join(get_package_share_directory(robot_name), 'worlds', world_file_name)
  16. urdf = os.path.join(get_package_share_directory(robot_name), 'urdf', 'mobot.urdf')
  17. # read urdf contents because to spawn an entity in
  18. # gazebo we need to provide entire urdf as string on command line
  19. #small_house = launch.actions.IncludeLaunchDescription(
  20. # launch.launch_description_sources.PythonLaunchDescriptionSource(
  21. # os.path.join(
  22. # get_package_share_directory('aws_robomaker_small_house_world'),
  23. # 'launch',
  24. # 'small_house.launch.py')))
  25. # Spawn mobot
  26. spawn_mobot = Node(
  27. package='gazebo_ros',
  28. node_executable='spawn_entity.py',
  29. node_name='spawn_entity',
  30. #node_namespace=namespace_,
  31. output='screen',
  32. #emulate_tty=True,
  33. arguments=['-entity',
  34. 'mobot',
  35. '-x', '3.5', '-y', '1.0', '-z', '0.1',
  36. '-file', urdf
  37. ]
  38. )
  39. #xml = open(urdf, 'r').read()
  40. # double quotes need to be with escape sequence
  41. #xml = xml.replace('"', '\\"')
  42. # this is argument format for spwan_entity service
  43. #spwan_args = '{name: \"mobot\", xml: \"' + xml + '\"}'
  44. # create and return launch description object
  45. return LaunchDescription([
  46. # start gazebo, notice we are using libgazebo_ros_factory.so instead of libgazebo_ros_init.so
  47. # That is because only libgazebo_ros_factory.so contains the service call to /spawn_entity
  48. ExecuteProcess(
  49. cmd=['gazebo', '--verbose', world, '-s', 'libgazebo_ros_factory.so'],
  50. output='screen'),
  51. # tell gazebo to spwan your robot in the world by calling service
  52. # ExecuteProcess(
  53. # cmd=['ros2', 'service', 'call', '/spawn_entity', 'gazebo_msgs/SpawnEntity', spwan_args],
  54. # output='screen'),
  55. #small_house,
  56. spawn_mobot,
  57. ])

机器人初始位置为x: 3.5 y: 1.0 z: 0.1,参考如下文档片段:

Robot Simulation - Initial Position

Do not use (0,0,0) for the initial position as it will collide with the lounge chairs. Instead, a resonable position is (3.5,1.0,0.0).

机器人可以加载已有的ROS2功能包,如导航功能包等。

具体参考:2020年ROS机器人操作系统用户官方调查 文末的介绍。

关于环境综合测试更多截图如下所示:

smallhouse:

navigation2:

bookstore:

navigation2:

下一节将讲解如何制作一个跟随机器人~mobot-follow~


 

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/105707950

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。