publishing &subscribing at the same node (no callback)

asked 2018-03-19 03:27:10 -0500

rosmaster gravatar image

updated 2018-03-19 03:59:09 -0500

gvdhoorn gravatar image

I'm trying to publish and subscribe in a package video_stream_opencv

I have used Publisher test_pub to publish a message to another package (turtlebot pkg) and it worked fine.

However when I try to subscribe message sent from turtlebot no error messages occurs but I dont get any callbacks from nh.subscribe("path_topic", 50, pathCallback). Neither ROS_INFO appears nor i changes to 1..

I have also tried to position nh.subscribe("path_topic", 50, pathCallback) in other places but none of them seems to work.

path_topic appears on both computer's rostopic list

rostopic echo /path_topic shows nothing . No errors. it just pauses.

I have no idea what;s wrong. Thanks in advance for your help

imgData.msg file

geometry_msgs/Point[] points
std_msgs/Int32 head_x
std_msgs/Int32 head_y
std_msgs/Int32 tail_x
std_msgs/Int32 tail_y
std_msgs/Int32 count_pixel

BELOW IS THE CODE (video_stream_opencv)

int i=0;

void pathCallback(const video_stream_opencv::imgData::ConstPtr& path_msg)

    ROS_INFO("path sub");

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

        ros::init(argc, argv, "image_publisher");
        ros::NodeHandle nh;
        ros::NodeHandle _nh("~"); // to get the private params

        ros::Publisher test_pub = nh.advertise<video_stream_opencv::imgData>("obstacle_topic",1);

while (nh.ok()) {

      ros::Subscriber sub=nh.subscribe("path_topic", 50, pathCallback);
              cap >> OriFrame;
           if (pub.getNumSubscribers() > 0){

                      if(!OriFrame.empty()) {








keyop CODE

void KeyOpCore::auto_driving()

     ros::NodeHandle n;  
     ROS_INFO("afterpress k, before subscribe");
     ros::Subscriber obstacle_coordinate_subscriber = n.subscribe("/webcam/obstacle_topic", 50,  &KeyOpCore::ReceiveObstacle,this);
     ros::Publisher pub_path = n.advertise<kobuki_keyop::imgData>("path_topic",1);
     if (path_topic_checker)
       path_topic_checker = false;

edit retag flag offensive close merge delete



Don't create ros::Subscribers inside your while loop. Do it right after you create your ros::Publisher.

gvdhoorn gravatar image gvdhoorn  ( 2018-03-19 04:00:04 -0500 )edit

but then I thought I would get to subscribe topic only once.. I have to get it subscribed for every captured frame(by while(nh.ok()) I get captured frame so that it looks like a video!

rosmaster gravatar image rosmaster  ( 2018-03-19 05:53:27 -0500 )edit

No, that is not how ROS works. A subscriber "delivers" incoming msgs to your program via callbacks. It invokes your callback for every msg that is received.

Do not mistake subscribing with "reading". Creating subscribers in while-loops also doesn't result in synchronous 'reading' of topics.

gvdhoorn gravatar image gvdhoorn  ( 2018-03-19 05:55:13 -0500 )edit

I would suggest to search for some literature on event-based systems, and publish-subscribe. The ROS wiki also has a section on concepts - specifically the "computation graph" - that may be helpful.

gvdhoorn gravatar image gvdhoorn  ( 2018-03-19 05:57:14 -0500 )edit

thanks.!! but it still doesnt show anything even after Ive put the subscribing part out of the while loop

rosmaster gravatar image rosmaster  ( 2018-03-19 06:29:09 -0500 )edit

Might be this line: if (pub.getNumSubscribers() > 0){ . Where is pub defined? Did you mean to write test_pub?

stevejp gravatar image stevejp  ( 2018-03-19 07:15:02 -0500 )edit