使用機器人操作系統ROS 2和仿真軟件Gazebo 9環境綜合測試教程(三)
在完成教程(一)搭建機器人和(二)命令遙控可視化后,將仿真機器人用于更為逼真的環境,可以測試如SLAM,區域覆蓋以及場館巡邏算法,這里環境均采用aws提供模型,分別為smallhouse和bookstore,環境適用于ROS2和ROS1全部案例,但ROS1內容不做講解,這里只簡要敘述一下ROS2中調試和使用的過程。
ROS2和Gazebo9中mobot室內環境仿真測試
在環境中添加機器人模型:
在launch中添加機器人需要注意如上差異,如添加一個urdf格式機器人可參考如下命令:
ros2 service call /spawn_entity 'gazebo_msgs/SpawnEntity' '{name: "urdf_ball", xml: "
更多內容參考:https://github.com/ros-simulation/gazebo_ros_pkgs/wiki/ROS-2-Migration:-Spawn-and-delete
如果需要在制定位置插入機器人,需要查閱spawn_entity.py源碼:
# Encode xml object back into string for service call
entity_xml = ElementTree.tostring(xml_parsed)
# Form requested Pose from arguments
initial_pose = Pose()
initial_pose.position.x = float(self.args.x)
initial_pose.position.y = float(self.args.y)
initial_pose.position.z = float(self.args.z)
q = quaternion_from_euler(self.args.R, self.args.P, self.args.Y)
initial_pose.orientation.w = q[0]
initial_pose.orientation.x = q[1]
initial_pose.orientation.y = q[2]
initial_pose.orientation.z = q[3]
注意這里的initial_pose和表中參數的對應情況,不再贅述。
mobot加入smallhouse場景如下所示,支持多機器人!后續補充:
可以在此環境中復習,前2課所學過的全部內容。
啟動文件smallhouse.launch.py代碼如下:
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import ExecuteProcess, DeclareLaunchArgument
from launch.actions import IncludeLaunchDescription
from launch_ros.actions import Node
from launch.conditions import IfCondition
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import LaunchConfiguration
# this is the function launch system will look for
def generate_launch_description():
robot_name = 'mobot'
world_file_name = 'smallhouse.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', 'mobot.urdf')
# read urdf contents because to spawn an entity in
# gazebo we need to provide entire urdf as string on command line
#small_house = launch.actions.IncludeLaunchDescription(
# launch.launch_description_sources.PythonLaunchDescriptionSource(
# os.path.join(
# get_package_share_directory('aws_robomaker_small_house_world'),
# 'launch',
# 'small_house.launch.py')))
# Spawn mobot
spawn_mobot = Node(
package='gazebo_ros',
node_executable='spawn_entity.py',
node_name='spawn_entity',
#node_namespace=namespace_,
output='screen',
#emulate_tty=True,
arguments=['-entity',
'mobot',
'-x', '3.5', '-y', '1.0', '-z', '0.1',
'-file', urdf
]
)
#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: \"mobot\", 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', world, '-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'),
#small_house,
spawn_mobot,
])
機器人初始位置為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~
機器人
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。