如何解决ROS 节点订阅未连接
我正在使用 rosbag 发布各种主题,并且我试图让我的示例程序允许一个节点通过类方法函数订阅这些主题。但是在控制台上没有为订阅者打印任何内容。我试过 roswtf,我得到了
WARNING The following node subscriptions are unconnected:
* /roscpp_pcl_example:
* /camera/depth/points
这是我的程序代码,我不确定问题出在哪里。这些是我对 API https://wiki.ros.org/roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks 的参考 http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29
// Include the ROS library
#include <ros/ros.h>
// Include pcl
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
// Include PointCloud2 message
#include <sensor_msgs/PointCloud2.h>
#include <std_msgs/String.h>
// Topics
static const std::string SUB_TOPIC_1 = "/ecu_pcl";
static const std::string SUB_TOPIC_2 = "/velodyne_back/velodyne_points";
static const std::string SUB_TOPIC_3 = "/velodyne_center/velodyne_points";
static const std::string SUB_TOPIC_4 = "/velodyne_front/velodyne_points"
//define class with member functions for callback usage
//allows for subscription of varIoUs topics in the same node
class callback_node{
public:
void callback1(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO_STREAM(msg->data.c_str());
}
void callback2(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO_STREAM(msg->data.c_str());
}
void callback3(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO_STREAM(msg->data.c_str());
}
void callback4(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO_STREAM(msg->data.c_str());
}
};
int main (int argc,char** argv)
{
// Initialize the ROS Node "roscpp_pcl_example"
ros::init (argc,argv,"roscpp_pcl_example");
ros::NodeHandle nh;
callback_node callback_obj;
// Print "Hello" message with node name to the terminal and ROS log file
ROS_INFO_STREAM("Hello from ROS Node: " << ros::this_node::getName());
// Create ROS Subscribers to SUB_TOPIC with a queue_size of 1000 and a callback function via class methods
ros::Subscriber sub1 = nh.subscribe(SUB_TOPIC_1,1000,&callback_node::callback1,&callback_obj);
ros::Subscriber sub2 = nh.subscribe(SUB_TOPIC_2,&callback_node::callback2,&callback_obj);
ros::Subscriber sub3 = nh.subscribe(SUB_TOPIC_3,&callback_node::callback3,&callback_obj);
ros::Subscriber sub4 = nh.subscribe(SUB_TOPIC_4,&callback_node::callback4,&callback_obj);
// Spin
ros::spin();
// Success
return 0;
}
解决方法
立即引起我注意的一件事是,您实际上已将 std_msgs::String::ConstPtr
作为主题类型,但主题 /ecu_pcl
和 /velodyne_points
不应属于 {{1} 类型而是应该有不同的数据类型,我假设点云,所以像sensor_msgs::PointCloud2
。因此,您必须将回调修改为正确的数据类型,例如std_msgs::String
。
令我惊讶的是 sensor_msgs::PointCloud2::ConstPtr
给您的信息:
roswtf
这意味着您的节点 WARNING The following node subscriptions are unconnected:
* /roscpp_pcl_example:
* /camera/depth/points
订阅了一个主题 roscpp_pcl_example
,实际上没有人发布。但是您的代码段没有提到 /camera/depth/points
。 (您是只展示了节点源代码的一部分还是忘记使用 /camera/depth/points
重新编译代码?)
无论如何,为了调试它,请尝试以下操作:
- 在为以下步骤打开的每个控制台中执行
catkin build
,确保您获得了正确的工作区 - 打开控制台并浏览您要播放的
$ source devel/setup.bash
并执行rosbag
并检查输出。确保您的节点所需的所有主题都已实际记录! - 如果是这种情况,请继续使用
$ rosbag info <filename.bag>
。 - 现在,通过打开一个新控制台、找到工作区并输入
$ rostopic play <filename.bag>
来检查是否所有主题都已正确发布。 - 然后使用
$ rostopic list
查看此列表中每个主题的类型。 - 最后确保您为订阅者使用正确的主题类型。您可能需要相应地更新您的
$ rostopic info <topicname>
和CMakeLists.txt
!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。