ROS message sent but not received

2019-08-09 10:12发布

I'm using ROS in my project and I need to send one message from time to time. I have this function:

void RosNetwork::sendMessage(string msg, string channel) {
    _mtx.lock();
    ros::Publisher chatter_pub = _n.advertise<std_msgs::String>(channel.c_str(),10);
    ros::Rate loop_rate(10);
    std_msgs::String msgToSend;
    msgToSend.data = msg.c_str();
    chatter_pub.publish(msgToSend);
    loop_rate.sleep();
    cout << "Message Sent" << endl;
    _mtx.unlock();
}

And I have this in python:

def callbackFirst(data):
    #rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
    print("Received message from first filter")

def callbackSecond(data):
    #rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
    print("Received message from second filter")

def listener():
    rospy.Subscriber("FirstTaskFilter", String, callbackFirst)
    print("subscribed to FirstTaskFilter")
    rospy.Subscriber("SecondTaskFilter", String, callbackSecond)
    print("subscribed to SecondTaskFilter")
    rospy.spin()

The listener is a thread in python. I get to the function sendMessage (I see in the terminal "Message Sent" a lot of times) but I don't see that the python script receives the message.

Update: I tested the python callback with rostopic pub /FirstTaskFilter std_msgs/String "test" and this works perfectly.

Any thought?

1条回答
一纸荒年 Trace。
2楼-- · 2019-08-09 11:13

You are re-advertising the publisher every time and then you are immediately using it to publish something.
This is problematic as it needs some time for the subscribers to subscribe to newly emerging publishers. If you are publishing messages before the subscriber has finished with this, these messages will not arrive.

To avoid this problem, do not advertise a new publisher every time, but do it only once in the constructor of your class and store the publisher in a member variable. Your code could look something like this:

RosNetwork() {
    _chatter_pub = _n.advertise<std_msgs::String>(channel.c_str(),10);
    ros::Duration(1).sleep(); // optional, to make sure no message gets lost
}

void RosNetwork::sendMessage(string msg, string channel) {
    ...
    _chatter_pub.publish(msgToSend);
    ...
}

The one-second-sleep after advertise makes sure that all existing subscribers can subscribe before you start publishing messages. This is only necessary, if it is important that not a single message gets lost. In most practical cases it can be omitted.

查看更多
登录 后发表回答