Ask Your Question
0

my nodes don't send (receive) messages

asked 2012-12-18 03:28:06 -0500

ubisum gravatar image

hello everyone
I created a class stage_listener; then, in an externally defined main() function, an object of class stage_listener is created and its function listen is invoked.
The purpose of this function is forcing the object of class stage_listener to become a listener of certain kinds of messages. Here is the code of function:

void stage_listener::listen(int c, char **v){
    while_flag = 1;
    ROS_INFO("Listening activity begun");
    ros::init(c,v, "listener");
    ros::NodeHandle n_odom, n_scan, n_end;
/*  std_msgs::String s;
    end_loop(s);*/

    //ros::Subscriber sub_odom = n_odom.subscribe("/odom", 1000, &stage_listener::end_loop,this);
    ros::Subscriber sub_odom = n_odom.subscribe("/odom", 1000, &stage_listener::addOdomNode,this);
    ros::Subscriber sub_scan = n_scan.subscribe("/base_scan", 1000, &stage_listener::addScanNode,this);
    ros::Subscriber sub_end = n_end.subscribe("/end", 1000, &stage_listener::end_loop,this);

    while(while_flag){
        //ros::Subscriber sub_odom = n_odom.subscribe("/odom", 1000, &stage_listener::end_loop,this);
        ros::spinOnce();
    }
    ROS_INFO("Exit from while");
}

The function receives, as parameters, the argc and argv of the main() where object has been created, since such parameters are requested by ros::init.
These are the functions used for listening:

void stage_listener::end_loop(const std_msgs::String mes){
    ROS_INFO("Setting flag");
    ROS_INFO("%s",mes.data.c_str());
    while_flag = 0;
}

void stage_listener::addOdomNode (const nav_msgs::Odometry mes){
    geometry_msgs::Pose robot_pose = mes.pose.pose;
    geometry_msgs::Point robot_point = robot_pose.position;

    odom_node *on = new odom_node();
    (*on).xCoord = robot_point.x;
    (*on).yCoord = robot_point.y;
    (*on).frame_id = mes.header.frame_id;

    double orientation = tf::getYaw(robot_pose.orientation);
    (*on).angle = (float)orientation;

    odom_list.push_back(*on);
}

void stage_listener::addScanNode (const sensor_msgs::LaserScan mes){
    scan_node *sn = new scan_node();
    (*sn).scan = mes.ranges;
    (*sn).frame_id = mes.header.frame_id;
    (*sn).cartesian = polar2cart(mes.ranges, mes.angle_min, mes.angle_increment, mes.range_min, mes.range_max);

    scan_list.push_back(*sn);
}

After invocation of function listen, the object created begins to wait for messages. An external node, in another package, prepares and sends an std_msgs::String message:

#include "std_msgs/String.h"
#include "ros/ros.h"

int main(int argc, char **argv){
    ros::init(argc, argv,"end_bag");
    ros::NodeHandle n;

    ros::Publisher chatter_pub = n.advertise<std_msgs::string>("/end",1000);
    ros::Rate loop_rate(10);

    std_msgs::String msg;
    msg.data = "end of loop";

    chatter_pub.publish(msg);

    ROS_INFO("Message sent: %s",msg.data.c_str());

    ros::spinOnce();
    //sleep(3);
    loop_rate.sleep();

    exit(0);
}

The problem is that the message created is never received by the waiting object, since it remains iterating on spinOnce() instruction, never exiting the loop, a result that should be granted by reception of message.

Can you individuate any error in my code causing receiver node not to get any message? thanks for support.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2012-12-18 03:36:14 -0500

dornhege gravatar image

updated 2012-12-18 03:36:28 -0500

Your sender does NOT remain iterating in the spinOnce() instruction. As the name says: It only spins once. Thus, your sender will exit before anyone has connected. Use ros::spin() or put the spinOnce in a while loop to keep the program running.

Alternatively, you can wait until chatter_pub.getNumSubscribers() > 0 to make sure someone has connected before the program quits.

In general, when testing publish/subscribe: Use rostopic echo and rostopic pub in place of a subscriber/publisher to isolate where the error comes from.

edit flag offensive delete link more

Comments

thanks for answer. let's suppose that message sent by node has to be stored into a bag file. the process (started from a shell) waiting for messages to store in bag file, is considered a subscriber? will it increment the number of subscribers of topic?

ubisum gravatar image ubisum  ( 2012-12-18 03:58:53 -0500 )edit
1

Yes and yes.

dornhege gravatar image dornhege  ( 2012-12-18 04:56:59 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-12-18 03:28:06 -0500

Seen: 271 times

Last updated: Dec 18 '12