Rosanswers logo

I am trying to write a subscriber to obtain the depth information from a zed camera. I have written the following .cpp code:

//#include <iostream>
#include "ros/ros.h"
//#include "std_msgs/String.h"
#include "sensor_msgs/Image.h"

//using namespace std;

void depthCallback(const sensor_msgs::Image& msg)
    ROS_INFO("Depth Info: [%d]", msg->data )
int main(int argc, char **argv)
    //cout << "Hello world!" << endl;
    ros::init(argc, argv, "zed_depth_subscriber")
    ros::NodeHandle n;
    ros::Subscriber sub = n.subscribe("/camera/depth/image_rect_color",1000, depthCallback);
    ros::spin ();
    return 0;

I realize that the line: ROS_INFO("Depth Info: [%d]", msg->data ) may throw an error (I am expecting a matrix, but not really sure how I can handle it in a ROS subscriber). But my current problem is related to CMakeLists.txt and package.xml files.

This subscriber is supposed to obtain depth information from the topic /camera/depth/image_rect_color from the zed wrapper. I am planning to add this .cpp file to


and edit the CMakeLists.txt in the location:


I am looking at the example given in ROS Tutorials which gave me the following:

add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp)

I know that 'listener' should be replaced with 'zed_depth_subscriber'. But how do I identify the dependencies? How will the line add_dependencies(.... change?

Also do I need to edit package.xml residing in the same folder as that of CMakeLists.txt?

Originally posted by skr_robo on ROS Answers with karma: 178 on 2016-07-08

Post score: 0


1 Answer 1


Rosanswers logo

I figured out how to do it. I created a separate package for the subscriber. I have the following depthCallback function now:

void depthCallback(const sensor_msgs::Image::ConstPtr& msg)
    ROS_INFO("Depth Info: [%d]", msg->data[500]);

The main dependency required is sensor_msgs (along with roscpp). The add_dependecies(... line in the CmakeLists.txt would look like:

add_dependencies(zed_depth_subscriber_node sensor_msgs_generate_messages_cpp)

The package.xml should have the sensor_msgs as run and build dependency.

Originally posted by skr_robo with karma: 178 on 2016-07-18

This answer was ACCEPTED on the original site

Post score: 0


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.