ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A
Ask Your Question

How can I use the C++ program to add , delete object in ros gazebo

asked 2012-02-26 08:53:17 -0500

zhenli gravatar image

updated 2014-01-28 17:11:29 -0500

ngrennan gravatar image

Dear All. While read the following documentation, I can not know how to use it to create c++ program to add or delete object in ros gazebo automatically by program.

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2012-02-26 21:33:19 -0500

Lorenz gravatar image

updated 2012-02-27 04:49:44 -0500

This shows which services to call and how and this shows how to call a service in C++.

edit flag offensive delete link more

answered 2012-02-27 09:17:33 -0500

Robert Krug gravatar image

updated 2012-02-27 10:06:10 -0500

You can use either the /gazebo/spawn_urdf_model service (for .urdf files) or the /gazebo/spawn_gazebo_model service (for gazebo .xml files). In your code you need a service client for the service, e.g., for urdf models:

ros::ServiceClient gazebo_spawn_clt_ = node_handle_.serviceClient< gazebo_msgs::SpawnModel> ("/gazebo/spawn_urdf_model");

Then you need to read the contents of the urdf file into the model_xml field of a gazebo_msgs::SpawnModel service. The generated service then can be used to call your client:;

edit flag offensive delete link more


Can you give me a detail example for URDF file.

zhenli gravatar imagezhenli ( 2012-02-27 10:19:48 -0500 )edit

I added a very simple example of a node spawning a urdf model on pastebin

Robert Krug gravatar imageRobert Krug ( 2012-02-27 22:29:03 -0500 )edit

But I use .xacro file rather than .urdf file. How to use it for the model_xml field in c++ code? Thanks. gravatar ( 2017-04-21 12:17:30 -0500 )edit

answered 2012-02-27 10:31:36 -0500

zhenli gravatar image

updated 2012-03-22 15:44:53 -0500

hsu gravatar image

I use the following function to create box in the gazebo world. It is here, but why it is not visible? I can not see it, but sure it is in the gazebo world with name and position information. The step: 1.I download the object.urdf 2. I use : rosrun gazebo urdf2model -f object.urdf -o object.model 3. Based on the talker program package, I add the gazebo package

#include <gazebo/SpawnModel.h>  
#include <gazebo/urdf2gazebo.h>

#include <tf/transform_datatypes.h>

The in the function I define the following:

gazebo::SpawnModel sm;

    std::ifstream ifs;"object.model");

    sm.request.model_name = "box";
    ifs >> sm.request.model_xml;

    sm.request.initial_pose.position.x = 4.0;
    sm.request.initial_pose.position.y = 4.0;
    sm.request.initial_pose.position.z = 1.0;

    sm.request.initial_pose.orientation = tf::createQuaternionMsgFromYaw(0.0);

    sm.request.reference_frame = "";

    ros::service::call( "/gazebo/spawn_gazebo_model", sm );

So I do not why we can not see it but it is in the gazebo world?

edit flag offensive delete link more


which object.model are you using? please post the xml. thanks.

hsu gravatar imagehsu ( 2012-03-22 15:47:59 -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


Asked: 2012-02-26 08:53:17 -0500

Seen: 2,344 times

Last updated: Mar 22 '12