Ask Your Question
1

spawn model with ROS2 gazebo

asked 2019-02-04 03:42:55 -0600

poonam1120 gravatar image

Hi,

I followed this link and able to spawn the sdf model in gazebo.

https://github.com/ros-simulation/gaz...

But instead of sending sdf model code from command line, can we send just sdf/urdf file ? I tried that but it failed.

When i send complete sdf modem from cmnd lien with spawn_entity, it launched in gazebo. I am trying it on crystal.

Please let me know if anybody is having any clue.

Thanks.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2019-03-13 13:21:02 -0600

clyde gravatar image

Following tfoote's advice, here is an example:

import sys
import rclpy
from gazebo_msgs.srv import SpawnEntity


def request_spawn(xml: str):
    rclpy.init()
    node = rclpy.create_node('spawn_entity')
    client = node.create_client(SpawnEntity, 'spawn_entity')
    if not client.service_is_ready():
        client.wait_for_service()
    request = SpawnEntity.Request()
    request.xml = xml
    future = client.call_async(request)
    rclpy.spin_until_future_complete(node, future)
    if future.result() is not None:
        print('response: %r' % future.result())
    else:
        raise RuntimeError('exception while calling service: %r' % future.exception())
    node.destroy_node()
    rclpy.shutdown()


if len(sys.argv) < 2:
    print('usage: ros2 run my_package my_node.py -- example.urdf')
    sys.exit(1)

f = open(sys.argv[1], 'r')
request_spawn(f.read())
edit flag offensive delete link more
1

answered 2019-02-04 11:30:17 -0600

tfoote gravatar image

updated 2019-03-13 15:50:06 -0600

The examples are using generic command line tools. If you want to send from a file you can probably set it up to pipe the arguments into the rosservice call using xargs. But more likely I'd recommend writing the few line rclpy script to load the file and call the service call. If you'd like to contribute a generic version of that helper script to the gazebo_ros_pkgs that would be even better.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

Asked: 2019-02-04 03:42:55 -0600

Seen: 300 times

Last updated: Mar 13