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

Publisher Subscriber not working

asked 2015-03-19 03:37:24 -0600

Ashesh Goswami gravatar image

updated 2015-03-19 04:06:49 -0600

kokirits gravatar image

Hi everyone,

I have been using ROS for quite a while but am suddenly stuck with a very weird problem. In a part of my code, the subscriber to a publisher is not working within the code segment. If I try to use rostopic echo, it works fine but as soon as I try running the subscriber node, not only does it give no result but also after disconnecting the subscriber node if I try to perform rostopic echo it does not work anymore. This is a very strange problem and any help would be very much appreciated. I am attaching the subscriber publisher nodes (much simplified versions)

#include <ros/ros.h>
#include <ros/callback_queue.h>
#include <visualization_msgs/Marker.h>
#include <std_msgs/String.h>
#include <sensor_msgs/LaserScan.h>
#include <nav_msgs/Odometry.h>
#include <fstream>
#include <sstream>
#include <string> 
#include <stdlib.h>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <math.h>
#include <std_msgs/MultiArrayLayout.h>
#include <std_msgs/MultiArrayDimension.h>
#include <std_msgs/Float32MultiArray.h>
#include <std_msgs/Int8.h>
#include "list_velocity_tracking.h"

#define PI 3.14159265
using namespace std;
std_msgs::Int8 lane_indicator;

void laneCallback(const std_msgs::Int8::ConstPtr& lc);

int main(int argc, char** argv)
{

    ros::init(argc, argv, "velocit_estimation_list");
    ros::NodeHandle n;
    ros::NodeHandle n1;
    pub2 = n.advertise<std_msgs::Int8>("lane_change_indicator", 5);
    int count = 0;
    lane_indicator.data = 0;

    while(ros::ok())
    {
        pub2.publish(lane_indicator);
    }
}

and the subscriber node

#include <ros/ros.h>
#include <ros/callback_queue.h>
#include <visualization_msgs/Marker.h>
#include <std_msgs/String.h>
#include <sensor_msgs/LaserScan.h>
#include <nav_msgs/Odometry.h>
#include <fstream>
#include <sstream>
#include <string> 
#include <stdlib.h>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <math.h>
#include <std_msgs/MultiArrayLayout.h>
#include <std_msgs/MultiArrayDimension.h>
#include <std_msgs/Float32MultiArray.h>
#include <std_msgs/Int8.h>
#include "list_velocity_tracking.h"

#define PI 3.14159265
using namespace std;

void laneCallback(const std_msgs::Int8::ConstPtr& lc);

int main(int argc, char** argv)
{

    ros::init(argc, argv, "velocit_estimation_list");
    ros::NodeHandle n;
    ros::NodeHandle n1;
    lane_lock = n.subscribe("lane_change_indicator", 10, laneCallback);
    int count = 0;

    while(ros::ok())
    {
        ros::spinOnce();
    }
}

void laneCallback(const std_msgs::Int8::ConstPtr& lc)
{
    cout<<"inside callback\n";
}

and yes, I have included lane_lock in the header file as a ROS subscriber. thanks a lot in advance

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2015-03-19 04:52:45 -0600

Ashesh Goswami gravatar image

thanks a lot. i ran the rqt_graph and figured out that the problem was with an inactive node, i.e. i was running this node through another inactive node and hence the topic didn't have a clear path from the publisher to the subscriber..the problem is solved now

edit flag offensive delete link more
1

answered 2015-03-19 03:42:59 -0600

pexison gravatar image

I think that maybe you are not subscribing to the right topic, have your tried "/lane_change_indicator" instead of "lane_change_indicator"?

edit flag offensive delete link more

Comments

It does not work if I put "/lane_change_indicator"

Ashesh Goswami gravatar image Ashesh Goswami  ( 2015-03-19 04:11:54 -0600 )edit
1

check it with rosrun rqt_graph rqt_graph

pexison gravatar image pexison  ( 2015-03-19 04:16:17 -0600 )edit

you are welcome!, please mark the answer like correct and close the post. :)

pexison gravatar image pexison  ( 2015-03-19 05:53:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-03-19 03:37:24 -0600

Seen: 2,896 times

Last updated: Mar 19 '15