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

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. http://www.ros.org/wiki/simulator_gazebo/Tutorials/SpawningObjectInSimulation

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
5

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
3

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:

gazebo_spawn_clt_.call(spawn_model_srv);

edit flag offensive delete link more

Comments

Can you give me a detail example for URDF file.

zhenli gravatar image zhenli  ( 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 image Robert 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.

jasonwang538@gmail.com gravatar image jasonwang538@gmail.com  ( 2017-04-21 12:17:30 -0500 )edit
0

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<iostream>
#include<fstream>
#include <tf/transform_datatypes.h>

The in the function I define the following:

gazebo::SpawnModel sm;

    std::ifstream ifs;
    ifs.open("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

Comments

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

hsu gravatar image hsu  ( 2012-03-22 15:47:59 -0500 )edit

Question Tools

Stats

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

Seen: 5,268 times

Last updated: Mar 22 '12