c++ - 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, 1000, &callback_node::callback2, &callback_obj);
ros::Subscriber sub3 = nh.subscribe(SUB_TOPIC_3, 1000, &callback_node::callback3, &callback_obj);
ros::Subscriber sub4 = nh.subscribe(SUB_TOPIC_4, 1000, &callback_node::callback4, &callback_obj);
// Spin
ros::spin();
// Success
return 0;
}
解决方案
立即引起我注意的一件事是,您实际上std_msgs::String::ConstPtr
已将主题类型设置为主题类型,但主题不应该是类型/ecu_pcl
,而是应该具有不同的数据类型,我假设为点云,例如. 因此,您必须将回调修改为正确的数据类型,例如./velodyne_points
std_msgs::String
sensor_msgs::PointCloud2
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
和package.xml
!
推荐阅读
- ionic4 - 无法设置未定义的属性“webkitTransition”?
- coldfusion - ColdFusion 从 CF 8 升级到 CF2018 后经常崩溃
- sql - SQL 以 5 结束价格
- c# - 找不到具有 CLSID {91493441-5A91-11CF-8700-00AA0060263B} 的 Powerpoint COM 对象来设置权限
- excel - 在匹配excel中的特定单元格后尝试引用可变行中的数据
- javafx - 使用插入 toString 的静态嵌套类
- jquery - ($(window).width() > 768) 在 768 像素处不中断
- angularjs - 由于以下原因无法实例化模块 dataAccessService:错误:[$injector:nomod]
- java - 我有一些代码可以打印出给定数组的摘要,但想知道如何打印位置而不是总和中的值?
- html - 如何使用 flex-wrap 让两侧的 div 具有相同的空间?- 弹性盒