【发布时间】:2018-03-13 08:35:31
【问题描述】:
我在 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 也不显示任何内容。
我哪里出错了?
【问题讨论】: