Ask Your Question
0

Node doesn't use subscribe callback (C++)

asked 2019-01-29 06:57:24 -0500

RosNewbie92 gravatar image

Hi,

I can't wrap my head around this problem I'm facing, where I've made a very bare-bones node that should for the moment being only subscribe to a topic and republish it under a new name. I know that the subscribed topic is indeed published by another node, but the callback function is never called when it receives messages from this topic.

Both the "pubbing" and "subbing" messages are displayed in the terminal window, however never the message in the callback function TestBlock::gnssCB. When I use "rosnode info /test_node" it says it has no subscribers.

I'd be very grateful if someone could point out where I'm wrong! :)

testnode.h:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <std_msgs/Float64.h>
#include "custom_msgs/gnssGGA.h"
#include <sstream>


class TestBlock
{
public:
    explicit TestBlock(ros::NodeHandle nh);
private:
    ros::NodeHandle nh;
    ros::Subscriber gnssGGA_sub;
    ros::Publisher signal_pub;

    void gnssPublish(custom_msgs::gnssGGA& msg);
    void gnssCB(const custom_msgs::gnssGGA& msg);
    void initPublishers();
    void initSubscribers();

    // Constructing gnssGGA msg
    double latitude;
    //string lat_direction
    double longitude;
    //string lon_direction
    double altitude;
    //gnssGGA_status status
};

Test_ros.cpp:

#include "testnode.h"


void TestBlock::initPublishers(){
    ros::Publisher signal_pub = nh.advertise<custom_msgs::gnssGGA>("/GPS/fix_AFTER", 10);
    ROS_WARN("Pubbing");
}

void TestBlock::initSubscribers(){
    ros::Subscriber gnssGGA_sub = nh.subscribe("/GPS/fix_PRE",10,&SigBlock::gnssCB,this);
    ROS_WARN("Subbing");
}

void TestBlock::gnssCB(const custom_msgs::gnssGGA& msg){
   ROS_WARN("Callback");
   latitude = msg.latitude;
   longitude = msg.longitude;
   altitude = msg.altitude;
}

void TestBlock::gnssPublish(custom_msgs::gnssGGA& msg){
    custom_msgs::gnssGGA received_GGA;
    received_GGA.latitude = msg.latitude;
    received_GGA.longitude = msg.longitude;
    received_GGA.altitude = msg.altitude;
    ROS_WARN("Published! %f",msg.longitude);
    signal_pub.publish(received_GGA);
}


TestBlock::TestBlock(ros::NodeHandle nh) : nh(nh)
{
    ROS_WARN("Initing Sig");
    initPublishers();
    initSubscribers();
}

test_node.cpp:

#include "testnode.h"    
int main(int argc, char **argv)
    {
      ros::init(argc, argv, "TestBlock");
      ros::NodeHandle n_sig;
      TestBlock sig_node(n_sig);
      ros::spin();
      return 0;
    }
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-01-29 07:05:48 -0500

gvdhoorn gravatar image

updated 2019-01-29 07:08:08 -0500

This is a C++ problem.

Even though you have a ros::Subscriber gnssGGA_sub and a ros::Publisher signal_pub member in your class, you initialise your subscriber here:

ros::Publisher signal_pub = nh.advertise<..>(..);

and publisher like so:

ros::Subscriber gnssGGA_sub = nh.subscribe(..);

That makes signal_pub and gnssGGA_sub variables local to initPublishers(..) and initSubscribers(..). The specific terminology here is variable shadowing.

As those variables go out of scope at "the end" of those methods, the subscription and the publication will be destroyed, and your callback is "never called".

You'll want to do something like this:

signal_pub = nh.advertise<..>(..);

Note the absence of ros::Publisher.

Note also that there are code styles where member variables are suffixed with an underscore (_). That way, signal_pub and signal_pub_ are two different things (well .. names .. but it's more obvious to a human reading the code what the intent is).

edit flag offensive delete link more

Comments

Thank you.

What would be a solution for this scoping problem? I'd want to avoid having them in the main function, is that possible?

RosNewbie92 gravatar imageRosNewbie92 ( 2019-01-29 07:09:12 -0500 )edit
2

Please read my answer. Especially the last bit.

gvdhoorn gravatar imagegvdhoorn ( 2019-01-29 07:09:51 -0500 )edit

That fixed it, thanks a lot!

RosNewbie92 gravatar imageRosNewbie92 ( 2019-01-29 07:20:12 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-01-29 06:53:41 -0500

Seen: 34 times

Last updated: Jan 29