在何处查看发布者发送的输出数据

Where to see the output data send by publisher?

本文关键字:输出 数据 发布者 在何处      更新时间:2023-10-16

我正在ROS上做一个程序,其中发布者是订阅者的回调函数,除了我看不到数据在哪里打印之外,一切都进展顺利。

我编写的代码如下所示:

    #include <ros/ros.h>
    #include <std_msgs/Int16.h>
    class pubsub
    {
    private:
        ros::NodeHandle nh;
        ros::Publisher pub;
        ros::Subscriber sub;
    public:
        void callback(const std_msgs::Int16::ConstPtr& msg) 
        {
            ROS_INFO("I heard this data: [%d]", msg->data);
            std_msgs::Int16 msg2;
            msg2.data = msg->data;
            ROS_INFO_STREAM(" I got some data");
            pub.publish(msg2);
        }
        pubsub()
        {
            pub = nh.advertise<std_msgs::Int16>("just",100);
            sub = nh.subscribe<std_msgs::Int16>("just",100,&pubsub::callback,this);
        }
    };

int main(int argc, char **argv){
    ros::init(argc,argv,"node");
    pubsub ps;
    ros::spin();
    return 0;
}

程序正在正确编译。执行时,只等待数据,不给终端任何输出。

命令 rostopic echo/just 即使我在运行代码后输入整数,也没有任何显示。

我哪里做错了?

永远不会

调用回调,因为发布者在回调内。事实上,你没有发布任何东西。请从 main 或构造函数发布初始消息,然后您将获得预期的消息循环。

就像 nayab 的回答中已经描述的那样,如果收到消息,您正在发布一条消息。由于不会发送任何有关此主题的消息,因此您永远不会发送任何消息。您需要在其他地方发布消息以接收它。因此,只需将一个新方法(如 pub(添加到pubsub类中即可。例如,在带有 10 Hz ros::ratemain环路内部。不要忘记添加ros::spinOnce()以允许回调处理。以下是改进的代码:

#include <ros/ros.h>
#include <std_msgs/Int16.h>
class pubsub
{
private:
    ros::NodeHandle nh;
    ros::Publisher pub;
    ros::Subscriber sub;
public:
    void callback(const std_msgs::Int16::ConstPtr& msg) 
    {
        ROS_INFO("I heard this data: [%d]", msg->data);
    }
    void pub(int number)
    {
        std_msgs::Int16 msg;
        msg.data = number;
        ROS_INFO_STREAM("Publishing data");
        pub.publish(msg);
    }
    pubsub()
    {
        pub = nh.advertise<std_msgs::Int16>("just",100);
        sub = nh.subscribe<std_msgs::Int16>("just",100,&pubsub::callback,this);
    }
};
int main(int argc, char **argv)
{
    ros::init(argc,argv,"node");
    pubsub ps;
    ros::Rate r(10);
    int number = 0;
    while (ros::ok())
    {
       ps.pub(number++);
       ros::spinOnce();
       r.sleep();
    }
    return 0;
}
#include <ros/ros.h>
#include <std_msgs/Int16.h>
class pubsub
{
private:
    ros::NodeHandle nh;
    ros::Publisher pub;
    ros::Subscriber sub;
public:
    void callback(const std_msgs::Int16::ConstPtr& msg) 
    {
        ROS_INFO("I heard this data: [%d]", msg->data);
    }
    void pub(int number)
    {
        std_msgs::Int16 msg;
        msg.data = number;
        ROS_INFO_STREAM("Publishing data");
        pub.publish(msg);
    }
    pubsub()
    {
        pub = nh.advertise<std_msgs::Int16>("just",100);
        sub = nh.subscribe<std_msgs::Int16>("just",100,&pubsub::callback,this);
    }
};
int main(int argc, char **argv)
{
    ros::init(argc,argv,"node");
    pubsub ps;
    ros::Rate r(10);
    int number = 0;
    while (ros::ok())
    {
       ps.pub(number++);
       ros::spinOnce();
       r.sleep();
    }
    return 0;`ahah sala...`enter code here`
}