0
$\begingroup$

Rosanswers logo

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;
}

Originally posted by bobpop on ROS Answers with karma: 5 on 2017-04-18

Post score: 0


Original comments

Comment by arunavanag on 2017-04-19:
Can you write this in a bit readable way? ROS answers have tools for quoting your code?

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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!


Originally posted by troski with karma: 26 on 2017-04-19

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by bobpop on 2017-04-19:
I did rostopic echo and found that my publisher didn't work.I guess that there are something wrong in my cmakelists.

$\endgroup$
0
$\begingroup$

Rosanswers logo

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??


Originally posted by ΦXocę 웃 Пepeúpa ツ with karma: 424 on 2017-04-19

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by bobpop on 2017-04-19:
yeah,I found the my topic name is wrong.It should be same,and the first one is not the fatal error.Thank you.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.