ROS消息已发送但未收到 [英] ROS message sent but not received

查看:875
本文介绍了ROS消息已发送但未收到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用ROS,我需要不时发送一条消息。我有这个函数:

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();
}

我在python中有这个:

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()

监听器是python中的一个线程。
我得到的函数 sendMessage (我看到在终端消息发送很多次),但我没有看到python脚本接收

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.

更新:我测试了python回调与 rostopic pub / FirstTaskFilter std_msgs / Stringtest

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

任何想法?

推荐答案

每次广告发布商,然后您立即使用它发布内容。

这是有问题的,因为订阅者需要一些时间订阅新兴的发布商。如果您在订阅者完成此操作之前发布邮件,则这些邮件将不会到达。

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);
    ...
}

c $ c> advertise 确保所有现有订阅者可以在开始发布消息之前订阅。这只是必要的,如果重要的是不要丢失单个消息。在大多数实际情况下,可以省略。

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.

这篇关于ROS消息已发送但未收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆