ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

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

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

zhenli gravatar image

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

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

Lorenz gravatar image

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

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

Robert Krug gravatar image

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

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 image zhenli  ( 2012-02-27 10:19:48 -0600 )edit

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

Robert Krug gravatar image Robert Krug  ( 2012-02-27 22:29:03 -0600 )edit

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

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

zhenli gravatar image

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

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 image hsu  ( 2012-03-22 15:47:59 -0600 )edit

Question Tools


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

Seen: 5,220 times

Last updated: Mar 22 '12