Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Problem of building ros server in Gazebo model plugin

Hello,

I am now building a ros server node in a gazebo plugin and got an error:

error: no matching function for call to ‘ros::NodeHandle::advertiseService(const char [12], bool (gazebo::JointController::*)(gazebo_tutorials::mysrv::Request&, gazebo_tutorials::mysrv::Response&))’ this->my_service = this->rosNode->advertiseService("/add_server", &JointController::addd);

I think this is because I did not set up the argument types correctly, I google for a long time and found nothing similar to my code. Any help would be a life saver for me, thanks in advanced!

here's some pieces of my code:

class JointController : public ModelPlugin
{
/// \brief Constructor
public: JointController() {}

public: virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
  // Store the model pointer for convenience.
  this->model = _model;

  // Initialize ros, if it has not already bee initialized.
  if (!ros::isInitialized())
  {
    int argc = 0;
    char **argv = NULL;
    ros::init(argc, argv, "gazebo_client",
        ros::init_options::NoSigintHandler);
  }

  // Create our ROS node. This acts in a similar manner to
  // the Gazebo node
  this->rosNode.reset(new ros::NodeHandle("gazebo_client"));
  //ros::NodeHandle n;

  //FITNESS publisher
  this->fitness_pub =this->rosNode->advertise<std_msgs::Float64>("/fitness", 100);

  //Service Node
  this->my_service = this->rosNode->advertiseService("/add_server", &JointController::addd);

  // Spin up the queue helper thread.
  this->rosQueueThread =
    std::thread(std::bind(&JointController::QueueThread, this));
}

public: bool addd(gazebo_tutorials::mysrv::Request& req,
         gazebo_tutorials::mysrv::Response& res)
{
  res.sum = req.a + req.b;
  ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
  ROS_INFO("sending back response: [%ld]", (long int)res.sum);
  return true;
}
}