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

Problem declaring a service server within a class [closed]

asked 2018-11-11 12:47:44 -0500

RobinB gravatar image

updated 2018-11-11 14:48:04 -0500

Hi everyone, I am a beginner in c++ especially with classes, I am trying to declare a service server within a class but it doesn't work, I have already had the same problem declaring a subscriber within a class, I tried applying the same thing to service server declaration without success.

#include "ros/ros.h"
#include "geometry_msgs/Vector3Stamped.h" //Message type for the torque values
#include "std_msgs/Float32.h"
#include "std_srvs/Empty.h"

class Torque
{
private:
    std_msgs::Float32 totalTorque;
    ros::NodeHandle n;
    ros::Subscriber subscription_torque;
    ros::Publisher publisher_torque;
    ros::ServiceServer service_server_torque;
public:

  void Torque(){
    this->service_server_torque = n.advertiseService("/omniROS/resetTotalTorque",&Torque::resetService,this);
    this->subscription_torque = this->n.subscribe("/omniROS/torque", 100,&Torque::callbackTorque,this);
    this->publisher_torque = this->n.advertise<std_msgs::Float32>("/omniROS/totalTorque", 100);
  }

  void callbackTorque(const geometry_msgs::Vector3Stamped::ConstPtr& torque)
  {
    this->totalTorque.data += std::abs(torque->vector.x);
    this->publisher_torque.publish(this->totalTorque);
  }

  void resetService(std_srvs::Empty::Request &req,std_srvs::Empty::Response &res){
    //This is a service server that will reset the totalTorque, it's a function that takes a void as input and returns a void as an output
    this->totalTorque.data=0.0;
  }
};

int main(int argc, char **argv)
{
  ros::init(argc, argv, "total_torque_node");
  Torque torque;
  // torque.initialisation();
  while (ros::ok())
   {
       ros::spin();
   }
  return 0;
}

And i have the following error :

/home/user/omniROS/omniROS_ws/src/total_torque/src/totalTorque.cpp: In member function ‘void Torque::initialisation()’:
/home/user/omniROS/omniROS_ws/src/total_torque/src/totalTorque.cpp:17:108: error: no matching function for call to ‘ros::NodeHandle::advertiseService(const char [26], void (Torque::*)(std_srvs::Empty::Request&, std_srvs::Empty::Response&), Torque*)’
     this->service_server_torque = n.advertiseService("/omniROS/resetTotalTorque",&Torque::resetService,this);
                                                                                                            ^
In file included from /opt/ros/kinetic/include/ros/ros.h:45:0,
                 from /home/user/omniROS/omniROS_ws/src/total_torque/src/totalTorque.cpp:1:
/opt/ros/kinetic/include/ros/node_handle.h:879:17: note: candidate: template<class T, class MReq, class MRes> ros::ServiceServer ros::NodeHandle::advertiseService(const string&, bool (T::*)(MReq&, MRes&), T*)
   ServiceServer advertiseService(const std::string& service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
                 ^
/opt/ros/kinetic/include/ros/node_handle.h:879:17: note:   template argument deduction/substitution failed:
/home/user/omniROS/omniROS_ws/src/total_torque/src/totalTorque.cpp:17:108: note:   mismatched types ‘bool’ and ‘void’
     this->service_server_torque = n.advertiseService("/omniROS/resetTotalTorque",&Torque::resetService,this);
                                                                                                            ^
In file included from /opt/ros/kinetic/include/ros/ros.h:45:0,
                 from /home/user/omniROS/omniROS_ws/src/total_torque/src/totalTorque.cpp:1:
/opt/ros/kinetic/include/ros/node_handle.h:924:17: note: candidate: template<class T, class MReq, class MRes> ros::ServiceServer ros::NodeHandle::advertiseService(const string&, bool (T::*)(ros::ServiceEvent<MReq, MRes>&), T*)
   ServiceServer advertiseService(const std::string& service, bool(T::*srv_func)(ServiceEvent<MReq, MRes>&), T *obj)
                 ^
/opt/ros/kinetic/include/ros/node_handle.h:924:17: note:   template argument deduction/substitution failed:
/home/user/omniROS/omniROS_ws/src/total_torque/src/totalTorque.cpp:17:108: note:   mismatched types ‘bool’ and ‘void’
     this->service_server_torque = n.advertiseService("/omniROS/resetTotalTorque",&Torque::resetService,this);
                                                                                                            ^
In file included from /opt/ros/kinetic/include/ros ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by allenh1
close date 2018-11-12 13:01:14.233787

Comments

1

Can you tell my what I have done wrong ?

well, you haven't told us what "doesn't work" ..

gvdhoorn gravatar image gvdhoorn  ( 2018-11-11 13:06:27 -0500 )edit

Sorry I forgot to paste the error message , i just edited it

RobinB gravatar image RobinB  ( 2018-11-11 14:47:13 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2018-11-11 15:53:31 -0500

Dimitar Rakov gravatar image

The callback function has to be bool instead of void.

edit flag offensive delete link more

Comments

This is indeed the problem.

gvdhoorn gravatar image gvdhoorn  ( 2018-11-12 00:48:56 -0500 )edit

Indeed, thank you very much, I will pay attention to this for later

RobinB gravatar image RobinB  ( 2018-11-12 02:15:34 -0500 )edit

@RobinB: please accept the answer by @Dimitar Rakov by clicking the checkmark to the left of his answer.

gvdhoorn gravatar image gvdhoorn  ( 2018-11-12 02:27:32 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-11-11 12:47:44 -0500

Seen: 1,421 times

Last updated: Nov 12 '18