Ask Your Question
3

What is the correct way to spawn a model to Gazebo using a Python script?

asked 2019-11-07 05:00:02 -0500

vitsensei gravatar image

Hi, I'm kinda fuzzy on this part. Here's my attempt:

rospy.wait_for_service("/gazebo/spawn_urdf_model")

try:
    spawner = rospy.ServiceProxy("/gazebo/spawn_urdf_model", SpawnModel)
    spawner(rover_name, str(open("/home/anh/catkin_ws/src/rover_project/rover_description/urdf/rover.urdf",'r')), "/rover", Pose(position= Point(0,0,2),orientation=Quaternion(0,0,0,0)),"world")

except rospy.ServiceException as e:
    print("Service call failed: ",e)

However, it leads to this result:

('Service call failed: ', ServiceException('service [/gazebo/spawn_urdf_model] responded with an error: ',))

Now, I'm pretty sure I didn't pass the urdf file the correct way. So if you guys can give me a full example on how to do it in Python, that would be awesome!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2019-11-07 17:18:56 -0500

vitsensei gravatar image

Alright guys, I figured it out.

spawner(rover_name, str(open("/home/anh/catkin_ws/src/rover_project/rover_description/urdf/rover.urdf",'r')), "/rover", Pose(position= Point(0,0,2),orientation=Quaternion(0,0,0,0)),"world")

should be

spawner(rover_name, open("/home/anh/catkin_ws/src/rover_project/rover_description/urdf/rover.urdf",'r').read(), "/rover", Pose(position= Point(0,0,2),orientation=Quaternion(0,0,0,0)),"world")

instead!

edit flag offensive delete link more

Comments

1

Hey can you please share the entire code of this file. I am kind of stuck in the same problem. It would be a great help !!

mkb_10062949 gravatar image mkb_10062949  ( 2020-01-03 14:52:28 -0500 )edit
2

Thanks! didn't work without the open( ).read() ! Full code:

from gazebo_msgs.srv import SpawnModel
spawn_model_client = rospy.ServiceProxy('/gazebo/spawn_sdf_model', SpawnModel)
spawn_model_client(
    model_name='ground_plane',
    model_xml=open('/usr/share/gazebo-9/models/ground_plane/model.sdf', 'r').read(),
    robot_namespace='/foo',
    initial_pose=Pose(),
    reference_frame='world'
)
ljburtz gravatar image ljburtz  ( 2020-08-16 01:45:51 -0500 )edit

Slightly modified this answer to include imports to make this executable as is:

from gazebo_msgs.srv import SpawnModel
import rospy
from geometry_msgs.msg import Pose
spawn_model_client = rospy.ServiceProxy('/gazebo/spawn_sdf_model', SpawnModel)
spawn_model_client(
    model_name='ground_plane',
    model_xml=open('/usr/share/gazebo-9/models/ground_plane/model.sdf', 'r').read(),
    robot_namespace='/foo',
    initial_pose=Pose(),
    reference_frame='world'
)
mfe7 gravatar image mfe7  ( 2022-01-24 13:27:53 -0500 )edit

Hello everyone the code everyone shared seems to work well, but I found a problem I couldn't solve. After launching the model with the codes above, I searched for the model's odometry and geometry by typing rostopic list but I couldn't find anything and whenever I manually press on the model in the gazebo simulation, the program crashes. Has anyone solved this problem?

Sindorim_Bear gravatar image Sindorim_Bear  ( 2022-02-02 23:51:07 -0500 )edit

A node should be added to work properly.

Another modification:

from gazebo_msgs.srv import SpawnModel
import rospy
from geometry_msgs.msg import Pose
rospy.init_node('insert_object',log_level=rospy.INFO)
spawn_model_client = rospy.ServiceProxy('/gazebo/spawn_sdf_model', SpawnModel)
spawn_model_client(
model_name='ground_plane',
    model_xml=open('/usr/share/gazebo-9/models/ground_plane/model.sdf', 'r').read(),
    robot_namespace='/foo',
    initial_pose=Pose(),
    reference_frame='world'
)
fegonzalez gravatar image fegonzalez  ( 2022-05-16 12:42:19 -0500 )edit

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-11-07 05:00:02 -0500

Seen: 1,982 times

Last updated: Nov 07 '19