Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Gazebo plugin: unable to use world ptr in service

I am trying to make a plugin which creates a service that can run Gazebo for a specific number of physics loops.

So, I made this:

#include <rl_gazebo/run_physics.h>

#include <std_msgs/String.h>

namespace gazebo {

GazeboRunPhysicsPlugin::GazeboRunPhysicsPlugin() { }

GazeboRunPhysicsPlugin::~GazeboRunPhysicsPlugin() {
  node_handle_->shutdown();
  delete node_handle_;
}

void GazeboRunPhysicsPlugin::Load(physics::WorldPtr _world, sdf::ElementPtr _sdf) {
  // Make sure the ROS node for Gazebo has already been initialized
  if (!ros::isInitialized()) {
    ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
      << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
    return;
  }

  world_ = _world;
  node_handle_ = new ros::NodeHandle;
  service_ = node_handle_->advertiseService("run_physics_loops", &GazeboRunPhysicsPlugin::RunPhysicsLoops, this);
}

bool RunPhysicsLoops(rl_msgs::RunPhysicsLoops::Request &req,
                     rl_msgs::RunPhysicsLoops::Response &res) {
  int nloops = req.num_loops;
  world_->RunBlocking(nloops); // ERROR Here
  return true;
}

// Register this plugin with the simulator
GZ_REGISTER_WORLD_PLUGIN(GazeboRunPhysicsPlugin)

} // namespace gazebo

I get a compilation error at the point world_->RunBlocking(nloops); which says:

.../src/run_physics.cc: In function ‘bool gazebo::RunPhysicsLoops(rl_msgs::RunPhysicsLoops::Request&, rl_msgs::RunPhysicsLoops::Response&)’:
.../src/run_physics.cc:36:3: error: ‘world_’ was not declared in this scope
   world_->RunBlocking(nloops);
   ^

What i am trying to do ...

I have a ros node which has it's own main loop - and in each oop of that I want to run gazebo's physics loop 10 (for example) times. I was using the ros services/topics to communicate with gazebo because I am using hector_quadrotor - which requires gazebo to be run using gazebo_ros.

Hence, I cannot make a gazebo server inside my rosnode - and need it to be run separately. So, I need a ros-service which should be able to call the line world->runBlocking(10). After a little digging I found a plugin can do this and now am trying to make one.