【发布时间】:2021-05-25 15:38:22
【问题描述】:
我正在使用 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;
}
【问题讨论】: