GAZEBO 插件
用ROS2 的 python脚本在GAZEBO中生成机器人,需要用到GAZEBO的系统插件,打开一个命令窗口,输入:
gazebo --verbose -s libgazebo_ros_factory.so
在另一个命令窗口,用下面的命令查看服务
ros2 service list
在最下面,我们可以看到一个 /spawn_entity 服务,只需要将ROS2的客户端和这个服务相连,就能生成机器人,接下来先用命令行简单生成一个机器人。
使用ROS2 命令行生成一个机器人
ros2 service call /spawn_entity 'gazebo_msgs/SpawnEntity' '{name: "sdf_ball", xml: "<?xml version=\"1.0\" ?><sdf version=\"1.5\"><model name=\"will_be_ignored\"><static>true</static><link name=\"link\"><visual name=\"visual\"><geometry><sphere><radius>1.0</radius></sphere></geometry></visual></link></model></sdf>"}'
命令解析
ros2 service call <服务的名字> <服务的消息类型> <具体的服务内容>
ros2 通过命令行向服务发送请求,GAZEBO 将会在接受请求并相应生成机器人。
使用ROS2 脚本生成多个机器人
进入工作空间,创建一个包(具体创建过程省略,可以参考 http://docs.ros.org/en/rolling/ ),编写脚本。
import os
import sys
import rclpy
import time
from ament_index_python.packages import get_package_share_directory
from gazebo_msgs.srv import SpawnEntity
def main():
""" Main for spwaning turtlebot node """
# Get input arguments from user
# Start node
rclpy.init()
node = rclpy.create_node("entity_spawner")
node.get_logger().info(
'Creating Service client to connect to `/spawn_entity`')
client = node.create_client(SpawnEntity, "/spawn_entity")
node.get_logger().info("Connecting to `/spawn_entity` service...")
if not client.service_is_ready():
client