Gazebo | Ignition | Community
Ask Your Question

Connect to a running Gazebo server from c++

asked 2015-06-24 12:20:50 -0600

mehdi gravatar image

updated 2015-06-25 03:57:39 -0600

In almost examples in the gazebo repository, a new gazebo server is initiated by the standalone code by loading a world file. How to connect to an existing gazebo server on localhost from a standalone cpp code? This is my code for creating a joint between two models

#include <sdf/sdf.hh>
#include "gazebo/gazebo.hh"
#include "gazebo/common/Plugin.hh"
#include "gazebo/msgs/msgs.hh"
#include "gazebo/transport/transport.hh"

#include <gazebo/gazebo.hh>
#include <gazebo/common/common.hh>
#include <gazebo/physics/physics.hh>

/// \example examples/plugins/
/// This example creates a WorldPlugin, initializes the Transport system by
/// creating a new Node, and publishes messages to alter gravity.
int main(int _argc, char **_argv)
    gazebo::physics::WorldPtr world = gazebo::physics::get_world("default");
    gazebo::physics::ModelPtr package = world->GetModel("my_package");
    gazebo::physics::LinkPtr package_link = package->GetLink("simple_box");
    gazebo::physics::ModelPtr maru = world->GetModel("maru");
    gazebo::physics::LinkPtr gripper_rot_link = maru->GetLink("gripper_rot");
    gazebo::physics::JointPtr newJoint;
    newJoint = world->GetPhysicsEngine()->CreateJoint("fixed", maru);
    newJoint->Load(package_link, gripper_rot_link, gripper_rot_link->GetWorldPose());
    newJoint->Attach(package_link, gripper_rot_link);
    newJoint->SetAxis(0,  gazebo::math::Vector3(0.0f,0.0f,1.0f) );
    if( maru->GetJoint("package_joint") == NULL )
        std::cout<<"Model GetJoint : FALSE \n"<<std::endl;
        std::cout<<"Model GetJoint : OK \n"<<std::endl;
    return 0;

i get the following error even though I launched Gazebo with

"roslaunch gazebo_ros empty_world.launch"

where the world's name is default.

Error [] Unable to find world by name in physics::getworld[default] Exception [] Unable to find world by name in physics::getworld(world_name)

After digging into the source code I found out that the only function that can be used to connect to Gazebo is "gazebo::setupServer". This function doesn't have a parameter for choosing to either create a new master or connect to an existing one. It always create a new master

  g_master = new gazebo::Master();

Does it mean that it is impossible for the moment to connect to a running Gazebo master from cpp?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-06-25 02:26:36 -0600

AndreiHaidu gravatar image

You could control a running server via messages, or you start your simulation via a custom main.

edit flag offensive delete link more


Any details about the messages, is it possible to spawn joints using them? some example code would be nice. and for custom main as it is now, it won't help me because my Gazebo is running already and I just need to run my code to create or delete a joint dynamically.

mehdi gravatar imagemehdi ( 2015-06-25 03:19:08 -0600 )edit

you can write a plugin (which is started with gazebo) to listen to some given topic to which you can then send a message to create/delete a joint

AndreiHaidu gravatar imageAndreiHaidu ( 2015-06-26 06:20:23 -0600 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-06-24 12:20:50 -0600

Seen: 1,148 times

Last updated: Jun 25 '15