ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

How to use sensor_msgs/Image message?

asked 2016-10-31 07:26:55 -0500

anonymous user

Anonymous

Hi

  • I'm a beginner on ROS.
  • I want to use a topic "/usb_cam/image_raw". (this topic is usb_cam package)
  • So I made a package, and a Subscriber.
  • However, there is still error in the catkin_make process.
  • How can I write CMakeList ?

main source

#include "ros/ros.h"
#include "sensor_msgs/Image.h"

void msgCallback(const sensor_msgs::Image::ConstPtr& msg)
{
        ROS_INFO("height = %d, width = %d",msg->height, msg->width);
}
int main(int argc, char** argv)
{
        ros::init(argc,argv,"cam_data");
        ros::NodeHandle nh;

        ros::Subscriber cam_sub = nh.subscribe("/usb_cam/image_raw",100,msgCallback);
        ros::spin();

        return 0;
}

CMakeList

cmake_minimum_required(VERSION 2.8.3)
project(test_cam)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  roscpp
  sensor_msgs
)


 generate_messages(
   DEPENDENCIES
   sensor_msgs
 )

catkin_package(
#  INCLUDE_DIRS include
  LIBRARIES test_cam
  CATKIN_DEPENDS roscpp sensor_msgs
  DEPENDS system_lib
)

include_directories(
  ${catkin_INCLUDE_DIRS}
)

add_executable(receive_cam src/receive_cam.cpp)
#target_link_libraries(test_cam ${catkin_LIBRARIES})
#add_dependencies(receive_cam test_cam_cpp)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-10-31 07:44:53 -0500

updated 2016-10-31 07:45:15 -0500

You have several problems:

  1. If you are going to use the generate_msgs() macro to ensure message dependencies are built, you'll need a find_package call for message_generation, you'll also need to list message_runtime as a CATKIN_DEPENDS in your catkin_package macro. You'll also need to add message_generation as a build dependency and message_runtime as a run dependency in your package.xml. Documentation here.
  2. You have what appear to be incorrectly specified dependencies in your catkin_package macro. Is system_lib actually a CMake project containing libraries that you depend on? Are you providing the test_cam library for use with other catkin packages? Documentation here
  3. You need to link your executable against libraries that your package depends on with target_link_libraries.

Here's a version that should work:

cmake_minimum_required(VERSION 2.8.3)
project(test_cam)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  sensor_msgs
  message_generation
)

generate_messages(
  DEPENDENCIES sensor_msgs
 )

catkin_package(
  INCLUDE_DIRS include
  CATKIN_DEPENDS roscpp message_runtime sensor_msgs
)

include_directories(
  ${catkin_INCLUDE_DIRS}
)

add_executable(receive_cam src/receive_cam.cpp)
target_link_libraries(receive_cam ${catkin_LIBRARIES})
edit flag offensive delete link more

Comments

1

Note: @breeze6478 is writing a subscriber. generate_message(..) is typically not needed/used in that case, as he depends onsensor_msgs. I think the only problem (but this is a guess) was that generate_messages(..) is there. It's not needed. And of course the test_cam etc.

gvdhoorn gravatar image gvdhoorn  ( 2016-10-31 08:56:30 -0500 )edit

That is for sure correct, unless the package is defining custom messages, no call to generate_messages() should be needed. Without more info on the package, it was tough to tell whether I should delete the call or fix the call.

jarvisschultz gravatar image jarvisschultz  ( 2016-10-31 10:36:20 -0500 )edit

If he is only writing a subscriber, I should have probably just deleted the call.

jarvisschultz gravatar image jarvisschultz  ( 2016-10-31 10:36:48 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-31 07:26:55 -0500

Seen: 12,339 times

Last updated: Oct 31 '16