Node doesn't use subscribe callback (C++)
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;
}