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

Creating a service server in a constructor

asked 2021-08-15 00:06:04 -0600

njkong gravatar image

updated 2021-08-17 05:20:15 -0600

Delb gravatar image

I'm trying to start a service server in the constructor of a class. I'm using ROS Noetic on Ubuntu 20.04. I've tried several methods like this and I've tried just matching the basic example from the wiki. Here are the cpp, hpp files and the error message.

#include <test_services/TestServices.hpp>

namespace test_services {

TestServices::TestServices(ros::NodeHandle& nodeHandle)
{
  // This is the constructor

  // Add a service server
  service_ = nodeHandle.advertiseService("service_name",TestServices::service_callback);
}

TestServices::~TestServices()
{ // This is the Destructor.
}



bool TestServices::service_callback(std_srvs::SetBool::Request &request,std_srvs::SetBool::Response &response)
{
  // This is the callback
return true;
}

void TestServices::spin() {
  // Ros spin function

  int update_rate_ = 100;
  ros::Rate r(update_rate_);

  // plan_timestamp_ = ros::Time::now();

  while (ros::ok()) {
    ros::spinOnce();
    // ROS_INFO_STREAM("Sleep");
    r.sleep();

  }
}
} /* namespace */

The hpp file

#pragma once

#include <ros/ros.h>
#include <std_srvs/SetBool.h>
// #include <cmath.h>

namespace test_services {

/*!
 * Class containing the Husky Highlevel Controller
 */
class TestServices {
public:
    /*!
     * Constructor.
     */
    TestServices(ros::NodeHandle& nodeHandle);

    /*!
     * Destructor.
     */
    virtual ~TestServices();

    // Ros Spin function
    void spin();

private:
    bool service_callback(const std_srvs::SetBool::Request &request,const std_srvs::SetBool::Response &response);
    ros::NodeHandle nodeHandle_;
    ros::ServiceServer service_;
};

} /* namespace */

The Error message

Errors     << test_services:make /home/ros/Workspaces/smb_ws/logs/test_services/build.make.011.log                                                                       
/home/ros/Workspaces/smb_ws/src/test_services/src/TestServices.cpp: In constructor ‘test_services::TestServices::TestServices(ros::NodeHandle&)’:
/home/ros/Workspaces/smb_ws/src/test_services/src/TestServices.cpp:10:71: error: invalid use of non-static member function ‘bool test_services::TestServices::service_callback(const Request&, const Response&)’
   10 |   service_ = nodeHandle.advertiseService("service_name",TestServices::service_callback);
      |                                                         ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
In file included from /home/ros/Workspaces/smb_ws/src/test_services/src/TestServices.cpp:1:
/home/ros/Workspaces/smb_ws/src/test_services/include/test_services/TestServices.hpp:28:7: note: declared here
   28 |  bool service_callback(const std_srvs::SetBool::Request &request,const std_srvs::SetBool::Response &response);
      |       ^~~~~~~~~~~~~~~~
/home/ros/Workspaces/smb_ws/src/test_services/src/TestServices.cpp: At global scope:
/home/ros/Workspaces/smb_ws/src/test_services/src/TestServices.cpp:19:6: error: no declaration matches ‘bool test_services::TestServices::service_callback(std_srvs::SetBool::Request&, std_srvs::SetBool::Response&)’
   19 | bool TestServices::service_callback(std_srvs::SetBool::Request &request,std_srvs::SetBool::Response &response)
      |      ^~~~~~~~~~~~
In file included from /home/ros/Workspaces/smb_ws/src/test_services/src/TestServices.cpp:1:
/home/ros/Workspaces/smb_ws/src/test_services/include/test_services/TestServices.hpp:28:7: note: candidate is: ‘bool test_services::TestServices::service_callback(const Request&, const Response&)’
   28 |  bool service_callback(const std_srvs::SetBool::Request &request,const std_srvs::SetBool::Response &response);
      |       ^~~~~~~~~~~~~~~~
/home/ros/Workspaces/smb_ws/src/test_services/include/test_services/TestServices.hpp:12:7: note: ‘class test_services::TestServices’ defined here
   12 | class TestServices {
      |       ^~~~~~~~~~~~
make[2]: *** [CMakeFiles/test_services.dir/build.make:76: CMakeFiles/test_services.dir/src/TestServices.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:1147: CMakeFiles/test_services.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
cd /home/ros/Workspaces/smb_ws/build/test_services; catkin build --get-env test_services | catkin env -si  /usr/bin/make --jobserver-auth=3,4; cd -
.........................................................................................................................................................................
Failed     << test_services:make                   [ Exited with code 2 ]                                                                                                
Failed    <<< test_services                        [ 1.7 seconds ]                                                                                                       
[build] Summary: 4 of 5 packages succeeded.                                                                                                                              
[build]   Ignored:   6 packages were skipped or are blacklisted ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2021-08-17 05:19:35 -0600

Delb gravatar image

There are two issues in your code :

  • The first one generates the following error : error: invalid use of non-static member function ‘bool test_services::TestServices::service_callback(const Request&, const Response&)’

It says that you tried to pass a non-static member function to advertiseService, which means you can't use it without an instaniated object of the class its contained in. If you look at the function prototype in the documentation :

ServiceServer ros::NodeHandle::advertiseService (   const std::string &     service,
bool(T::*)(MReq &, MRes &)  srv_func,
T *     obj 
)   
 \param service Service name to advertise on
 \param srv_func Member function pointer to call when a message has arrived
 \param obj Object to call srv_func on

So the second parameter must be a function pointer, and the third one must be an object of your class, like this :

  service_ = nodeHandle.advertiseService("service_name", &TestServices::service_callback, this);
  • The second issue is stated error : error: no declaration matches ‘bool test_services::TestServices::service_callback(std_srvs::SetBool::Request&, std_srvs::SetBool::Response&)’

In your hpp file you didn't declare the fonction correctly :

bool service_callback(const std_srvs::SetBool::Request &request,const std_srvs::SetBool::Response &response);

You have declared the parameters as const, you need to remove them.

edit flag offensive delete link more

Comments

Great, thanks! This builds now.

njkong gravatar image njkong  ( 2021-08-17 09:08:30 -0600 )edit

Question Tools

Stats

Asked: 2021-08-15 00:06:04 -0600

Seen: 249 times

Last updated: Aug 17 '21