Hi I'm am very new to both ROS and C++ so this is a very basic question.
The problem boils down to trying to send a char array from one node to another and then being able to process that string after it is read by the other node. I have been trying to pass a char array using publishers and subscribers, but have been failing for about a week (I guess I'm a stubborn SOB). Should I try a service-client model instead?
Whats the best way to do this?
The long story is a have 3 nodes that each read from a different serial port(ttyUSB0-ttyUSB2)format the data they get from that port. Each node then publishes that data to its own respective Topic. This part is easy. I need to get the data from all 3 topics into a 'main' node then format all the data a bit(the data formatting is trivial) and then send the resulting data through an XBee (the sending is easy as well). My problem arises when I try to have a 'main' node subscribe to all 3 topics. Basically the main node gets stuck in an infinite loop, it just keeps doing the callback loop and never progresses further into the program.
How do I write the callback function so that I can just pass the data into an array or string and move on with my program?
EDIT:
I have written a example program that shows the issue (I don't think I explained it very well above).
This programs tries to listen to the "chatter" topic made by talker.cpp from the "Writing a Simple Publisher and Subscriber (C++)" tutorial. The program is simply a modified version of listener from the same tutorial.
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <iostream>
#include <sstream>
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
//ROS_INFO("I heard: [%s]", msg->data.c_str());
printf("\nstuck in the callback loop");
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
while (ros::ok())
{
sleep(3);
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
printf("\nIM Not stuck anymore!\n");
ros::spin();
}
return 0;
}
Basically i want to be able to run commands bellow the:
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
call. If I were able to do this, when I run the program I should see my printf saying "IM Not stuck anymore!"
instead i see this:
IM Not stuck anymore!
stuck in the callback loop
stuck in the callback loop
stuck in the callback loop
stuck in the callback loop
stuck in the callback loop
stuck in the callback loop
The program never reaches the lower printf statement after the callback begins (I'm not sure why it does on the first iteration but whatever). This is problematic as my goal is to read in a string from a topic, then modify it below the read statement. Ultimately I would like to do this with the other 2 topics as well and then send a combined formatted message out the serial port.
Is the method that I am trying even possible or have I just completely misunderstood how publishers and subscribers work? If it's possible what do I need to do?
Thanks in advance for any help.
Originally posted by MikeV on ROS Answers with karma: 1 on 2011-04-13
Post score: 0