I am using a rosbag to publish on various topics and i am trying to get my sample program to allow one node to subscribe to those topics via class method functions. But nothing is being printed out on console for the subscribers. I tried roswtf and i got
WARNING The following node subscriptions are unconnected:
* /roscpp_pcl_example:
* /camera/depth/points
Here is my program code and i am not sure where the problem lies. These are my reference for the 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;
}
One thing that immediately jumps to my eye is that you have actually put std_msgs::String::ConstPtr
as the topic type but have topics /ecu_pcl
and /velodyne_points
which should not be of type std_msgs::String
but instead should have a different data types, point clouds I assume, so something like sensor_msgs::PointCloud2
. Therefore you would have to modify the callbacks to their correct data types, e.g. sensor_msgs::PointCloud2::ConstPtr
.
What surprises me as well is the message roswtf
gives you:
WARNING The following node subscriptions are unconnected:
* /roscpp_pcl_example:
* /camera/depth/points
This means that your node roscpp_pcl_example
is subscribed to a topic /camera/depth/points
nobody is actually publishing. Yet your code snippet does not mention /camera/depth/points
. (Are you only showing part of the node's source code or have you forgotten to recompile the code with catkin build
?)
Anyways in order to debug it try the following:
$ source devel/setup.bash
in each console you open for the following stepsrosbag
that you want to play and execute $ rosbag info <filename.bag>
and check in the output. Make sure that all the topics you need for your node have actually been recorded!$ rostopic play <filename.bag>
.$ rostopic list
.$ rostopic info <topicname>
.CMakeLists.txt
and package.xml
accordingly!