Listening to Publisher in Python from C++ Subscriber: Problem
Hello all,
I have a publisher in Python that I want publishing an Integer value from 0-2. I want to subscribe to that publisher from C++ and obtain that value. Below is the relevant code:
PYTHON:
def capturePointCloud(tracker):
pub1 = rospy.Publisher('PointCloudView', Int8, queue_size=1)
pub1.publish(tracker)
print "Made it"
where the tracker parameter is 0, 1 or 2. This is published 3 times through 3 capturePointCloud calls. I'd like for it to only be published once, and then when it's received on the C++ subscriber be stopped publishing, but for now I just want this working.
C++
int main (int argc, char** argv)
{
ros::init (argc, argv, "my_pcl_tutorial");
ros::NodeHandle nh2;
ROS_INFO("message is");
while(true)
{
sub4 = nh2.subscribe<std_msgs::Int8>("/PointCloudView", 1, saveFiles);
if(tracker == 2)
break;
}
}
void saveFiles(std_msgs::Int8 msg)
{
ROS_INFO("message is");
tracker = msg.data;
....
I have not finished the saveFiles callback function because it is irrelevant. For some reason, the callback function in C++ is never reached. I have used rostopic info /PointCloudView and I get:
Type: std_msgs/Int8
Publishers:
* /BAXTER (http://localhost:37366/)
Subscribers:
* /my_pcl_tutorial (http://localhost:38479/)
* /rostopic_8283_1451517068960 (http://localhost:59099/)
/PointCloudView even shows up on rostopic list, so what am I doing wrong here? The code looks fine to me, and I cannot figure out why the subscriber is not listening to the publisher. C++ listeners should be able to access Python publishers and what I'm doing is fairly simple, so why is this not working? I'd be very grateful for any help. Thank you all.
EDIT:
Thanks to @gvdhoorn, I was able to fix my problem as well as get a better understanding of subscribers/publisher control. Below is the updated code:
Python:
rospy.init_node('BAXTER', anonymous=True)
pub1 = rospy.Publisher('PointCloudView', String, queue_size=10)
pub2 = rospy.Publisher('PointCloudView', String, queue_size=10)
pub3 = rospy.Publisher('PointCloudView', String, queue_size=10)
....
pub1.publish(str(0))
...
pub2.publish(str(1))
...
pub3.publish(str(2))
C++:
ros::init (argc, argv, "my_pcl_tutorial");
ros::NodeHandle nh2;
sub4 = nh2.subscribe("/PointCloudView", 10, saveFiles);
ros::spin();
Please don't use answers to post updates, edit your original question for that (use the edit link/button below it). I've already merged your update into your post, but please keep it in mind for future questions.
You don't need multiple publishers to publish the same message to the same topic. A single one will suffice. You can publish any number of messages with a single publisher (but they have to be of the same type). Also: it's fine if you do, but normally you don't need multiple
NodeHandle
s either.And a suggestion if I may: try to pick up a book (or two) about ROS. That will very quickly allow you to gain some insight into why things are done the way they are, moving away from 'magic incantations'. See wiki/Books (A Gentle Introduction to ROS is nice, and free).
Will do! I'll change my code up as well, thank you.