Robotics StackExchange | Archived questions

Connect to a running Gazebo server from c++

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/world_edit.cc
/// 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->SetName("package_joint");
    newJoint->SetModel(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;
    else
        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 [PhysicsIface.cc:75] Unable to find world by name in physics::getworld[default] Exception [PhysicsIface.cc:77] 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?

Asked by mehdi on 2015-06-24 12:20:50 UTC

Comments

Answers

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

Asked by AndreiHaidu on 2015-06-25 02:26:36 UTC

Comments

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.

Asked by mehdi on 2015-06-25 03:19:08 UTC

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

Asked by AndreiHaidu on 2015-06-26 06:20:23 UTC