在何处查看发布者发送的输出数据
Where to see the output data send by publisher?
我正在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::rate
的main
环路内部。不要忘记添加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`
}
相关文章:
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 使用 python 或 c ++ 中的 sql 根据输入数据输出输出示例
- 将多个效果与 libSox 链接并读取输出数据的正确方法
- 如何将函数的输出数据存储在文本文件中
- 如何从文本文件输出数据?(学生成绩单计划)
- 如何在 c++ 中正确读取 GRC 块"Random Source"的字节输出数据到自己的 OOT 块中?
- 使用文件和结构读取数据并输出数据
- 在何处查看发布者发送的输出数据
- qDebug - 如何以位(二进制格式)输出数据
- 将所有控制台输出数据保存到新的文本文件C++
- 如何将控制台输出数据获取到命令提示符下
- 在字符串中输出数据
- 从C++文件中输入/输出数据的最佳方法
- C++:读取数据和输出数据
- 如何使用二进制搜索C++从二维数组中输出数据
- 从不同大小的向量输出数据
- 通过两个类时出现奇怪的输出数据
- c++程序输出数据到c#
- 从文本文件中读取数据到结构体中,然后输出数据
- 使用 C++ 从 COM 端口读取输出数据