can a node listen to two other nodes?
Hey forum members, greetings to you all!
I am new to ROS, I was wondering if it is possible to create a node which will listen to a topic where two other nodes are publishing information.
Let's say I have even_number_node that generates even numbers and odd_number_node that generates odd numbers, they both are publishing let's say std_msgs::UInt64 data to a mathTopic, this third node adder_node will subscribe to the mathTopic and will get odd and even numbers generated by previous nodes, how can I identify which data is sent from which node and find sum of them?
I actually attempted to make it work based on the talker-listener nodes provided in ROS tutorials but messed everything up. Yes, I can create such a thing with service-client node but I want to break it down to be able to understand the details.
I know this sounds pretty much a dumb questions to experts, so thanks for the help!
Update1: Got it to work! Please see update2 below!
I was playing with the code and now it says: build system error and takes me to the following line: message(FATAL_ERROR "\nAssertion failed: file '${FILENAME}' does not exist. Message: ${MESSAGE}\n")
// CMakeLists.txt includes:
add_message_files(
FILES
twoData.msg)
// twoData.msg file includes:
std_msgs/UInt64 data1
std_msgs/UInt64 data2
evenNumberGenerator node, oddNumberGenerator node is the same with minor changes described below in comments:
#include "ros/ros.h"
#include "std_msgs/UInt64.h"
#include "mathematics/msg/twoData.h"
int main(int argc, char **argv)
{
// initialize evenNumberGenerator
ros::init(argc, argv, "evenNumberGenerator"); //oddNumberGenerator node for the other node
ros::NodeHandle nodeObject;
// publish to mathTopic
ros::Publisher publisherObject = nodeObject.advertise<twoData::UInt64>("mathTopic", 1);
ros::Rate loop_rate(1);
int i = 1;
while(ros::ok())
{
twoData::UInt64 number;
while(ros::ok())
{
number.data2 = 2*i; //this is number.data1 = 2*i-1; for oddNumberGenerator node
i++;
ROS_INFO("%lli", number.data2); // number.data1 for the other node
loop_rate.sleep();
publisherObject.publish(number);
}
}
return 0;
}
Update2: I got it to work. As @ahendrix said each node sends a complete message, not part of it, like:
node1 sends:
data1: 1 # odd numbers
data2: 0
---------------
data1: 3
data2: 0
--------------
...
and node2 sends:
data1: 0
data2: 2 # even numbers
---------------
data1: 0
data2: 4
--------------
...
and the third node which is supposed to listen to both of them at the same topic returns:
I believe oddNumberGenerator says: 1 # data from oddNumberGenerator
I believe evenNumberGenerator says: 0
------------------------------------------------------------
I believe oddNumberGenerator says: 0 # data from evenNumberGenerator
I believe evenNumberGenerator says: 2
------------------------------------------------------------
I believe oddNumberGenerator says: 3 # data from oddNumberGenerator
I believe evenNumberGenerator says: 0
------------------------------------------------------------
I believe oddNumberGenerator says: 0 # data from evenNumberGenerator
I believe evenNumberGenerator says: 4
------------------------------------------------------------
I believe oddNumberGenerator says: 5 # data from oddNumberGenerator
I believe evenNumberGenerator says: 0
------------------------------------------------------------
I believe oddNumberGenerator says: 0 # data from evenNumberGenerator
I believe evenNumberGenerator says: 6
------------------------------------------------------------
...
As expected, so I decided to go with @ahendrix's advise on Time Synchronizer and modified my code as it is below:
#include "ros/ros.h"
#include "mathematics/TwoData.h"
#include "message_filters ...
I think you question is rather how you can see which node sent a specific message instead of what you wrote in the title, right?
Well, I think that's what came to my mind when I was thinking about the problem. But yes, how can I see which node outputs what without having to look at the code?