Creating a service server in a constructor
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 ...