Search code examples
ros

Import ROS .msg from C++ package into python


I'm new to ROS and I'm having some trouble. I found a ROS package 'video_stream_opencv' on GitHub that I want to use, and I'd like to write some python code that subscribes to one of the image topics that package creates.

How do I import the message definition into my python code? I can't find any *.msg files in that package.

However, if I run:

    rosmsg show sensor_msgs/Image

I get the message definition:

    std_msgs/Header header
      uint32 seq
      time stamp
      string frame_id
    uint32 height
    uint32 width
    string encoding
    uint8 is_bigendian
    uint32 step
    uint8[] data

I can then use that info to create my own .msg file, right? But then how do I import that into python?


Solution

  • sensor_msgs is a package that should be automatically included in your ros distribution. Since I'm not familiar with python syntax here's how you would include the correct header in a roscpp node:

    #include "sensor_msgs/Image.h"
    

    According to a short google research the corresponding python syntax would be something like

    from sensor_msgs.msg import Image
    

    Additionally you will need to let CMake know where to search the package by adding sensor_msgs-package in your packages CMakeLists.txt like this:

    find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg sensor_msgs)
    

    I can't currently try this out, so please give me feedback if everything worked fine.