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

why my listener can't get the published data?

asked 2017-04-18 23:05:42 -0500

bobpop gravatar image

updated 2017-04-19 01:02:27 -0500

Dear friends: I have built two nodes :main and listener. The main nead to read (x,y,z) from a device and publish to the listener. The main code can read the data but the listener can't get the published data now,and i even don't know whether the publish is success,how should i handle it? Thanks for you help. The result of rosrun talker: X=0.013095 Y=-0.0524488 Z=-0.0401042

But the listener is like this: X: 0 Y: 0 Z: 0

The rosmsg show is: dong@dong-H61MHB:~/Project/robot_test$ rosmsg show rosfalcon/falconPos float64 X float64 Y float64 Z The source code of publisher

int main(int argc, char* argv[]) 
   { ros::init(argc,argv, "main");
    if(init_falcon(0))
    { 
        std::cout << "Falcon Initialised Starting ROS Node" <<std::endl;
       m_falconDevice.setFalconKinematic<libnifalcon::FalconKinematicStamper>();
       ros::NodeHandle node;
       ros::Publisher pub = node.advertise<rosfalcon::falconPos>("falconPos",10);
while(node.ok())
     {       boost::array<double, 3> Pos;
            Pos = m_falconDevice.getPosition();
            rosfalcon::falconPos position;
            position.X = Pos[0];
            position.Y = Pos[1];
            position.Z = Pos[2];
            pub.publish(position);
            std::cout << "X="<< position.X<< "  Y="<< position.Y<< "
Z="<< position.Z << std::endl;      
            }
        m_falconDevice.close();
    }   return 0;
    }

And the main part of listener is:

int main(int argc, char *argv[])
{
  ros::init(argc, argv, "listener");
  ros::NodeHandle node;
while(node.ok())
{
  ros::Subscriber sub = node.subscribe("position", 10,&positionCallback);
}
  ros::spin();
  return 0;
}
edit retag flag offensive close merge delete

Comments

Can you write this in a bit readable way? ROS answers have tools for quoting your code?

arunavanag gravatar image arunavanag  ( 2017-04-19 00:06:28 -0500 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2017-04-19 01:04:08 -0500

troski gravatar image

updated 2017-04-19 01:06:15 -0500

your topic names are not matching, the subscriber is listening to "position", and your publisher is publishing to "falconPos" they need to match.

If you wanna check if the publisher is working, you can do "rostopic echo +<topic_name>" on a sourced console, it should display the data from your publisher.

as for the the subscriber, you can check who is subscribed to the topic by "rostopic info +<topic_name>"

I see that you are not using absolute topic names. the subscriber will add the name of this node handle to the topic when you do node.subscribe.(<"topic_name">) try node.subscribe(<"/topic_name">) with the "/" before the topic name.

using absolutes topic names is a bad practice, but this will get you going. after you can do remaps on the launch file and get all fancy.

Good Luck!

edit flag offensive delete link more

Comments

I did rostopic echo and found that my publisher didn't work.I guess that there are something wrong in my cmakelists.

bobpop gravatar image bobpop  ( 2017-04-19 07:20:41 -0500 )edit
1

answered 2017-04-19 05:25:37 -0500

updated 2017-04-19 06:22:31 -0500

Your ros::spin(); is outside of the while loop, not allowing the application to actually invoke the callback

on the other hand, your node1 is publishing to "falconPos" but node2 is subscribed to "position" , who else is publishing to that topics??

edit flag offensive delete link more

Comments

yeah,I found the my topic name is wrong.It should be same,and the first one is not the fatal error.Thank you.

bobpop gravatar image bobpop  ( 2017-04-19 07:06:25 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-04-18 22:58:01 -0500

Seen: 581 times

Last updated: Apr 19 '17