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 -0600

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 -0600

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 -0600 )edit
1

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 -0600 )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 -0600

Seen: 684 times

Last updated: Nov 07 '19