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

service not visible when launched inside class.

asked 2020-08-05 13:32:40 -0500

LukeAI gravatar image

I'm attempting to implement a service in which the callback is a member function of a class (with reference to the tutorial)

My code, as below, builds and runs without error, outputting the expected "service launched!". Without using static in the callback signature I get error: invalid use of non-static member function pointsCallback);

But when I run rosservice list "test_service" or "X/test_service" isn't present. Why isn't my service visible?

#include <stdio.h>
#include <string>
#include <ros/ros.h>

#include "image_transform/TransformPoints.h"

class ImageTransform {
 public:
    ImageTransform() {
        ros::NodeHandle private_nh("~");
        ros::ServiceServer service = private_nh.advertiseService("test_service",
                                                                 pointsCallback);
        std::cout << "service launched!" << std::endl;
    }

    static bool pointsCallback(image_transform::TransformPoints::Request &req,
                               image_transform::TransformPoints::Response &res) {
        std::cout << "service called" << std::endl;
        return true;
    }
};

int main(int argc, char **argv) {
    // ROS NODE INIT
    ros::init(argc, argv, "image_tranform");
    ImageTransform image_transform;
    ros::spin();
    return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-08-05 16:11:24 -0500

jdlangs gravatar image

Your service object is a local variable in the constructor. When the constructor ends it will be destroyed and the service will no longer be provided by the node. It should instead be a member variable of the ImageTransform class.

edit flag offensive delete link more

Comments

ah yeah, school boy error. thankyou.

LukeAI gravatar image LukeAI  ( 2020-08-06 04:59:47 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2020-08-05 13:32:40 -0500

Seen: 367 times

Last updated: Aug 05 '20