Search code examples
pythonlinuxopencvraspberry-piros

How to add sensor_msgs.msg type in my ros system?


I am working with ROS Kinetic on Raspberry Pi 3 Model B which contains Raspbian Stretch Lite. Well, when I create a package in a specific ws to make streaming video from Raspberry Pi to a Linux remote Pc I get an import error about the msg type named sensor_msgs.msg. When I run the same code on my linux pc, it works. But it is not works on the Raspberry Pi because of this error(sensor_msgs.msg).

I want to build a publisher on the raspberry pi using ros. What can I do to solve this error? I can't found this message type in my rosmsg list, when I use this command $rosmsg list | grep sensor_msgs.

This is my code scrip which is on the Raspberry Pi

#!/usr/bin/env python

import cv2
import rospy
from sensor_msgs.msg **import** Image
from cv_bridge import CvBridge, CvBridgeError

def main():
    cap = cv2.VideoCapture(0)
    rospy.init_node('stream_video',anonymous=False)
    front_pub = rospy.Publisher('/image_front',Image,queue_size=1)
    rate = rospy.Rate(30)
    bridge = CvBridge()

    while not rospy.is_shutdown():

        try:
            ret, frame = cap.read()
            if ret == True:
                img = bridge.cv2_to_imgmsg(frame, 'bgr8')
                front_pub.publish(img)

        except KeyboardInterrupt:
            break
        rate.sleep()

if __name__ == '__main__':
    main()

**pi@raspberrypi**:**~/selfdriving_ws/src/camera_stream/src $** rosrun camera_stream stream_video.py 

Traceback (most recent call last):
  File "/home/pi/selfdriving_ws/src/camera_stream/src/stream_video.py", line 5, in <module>
    from sensor_msgs.msg import Image
**ImportError: No module named sensor_msgs.msg**

Solution

  • I never run it on RPI. In normal PC, there wasn`t such issue. BTW there shouldnt be *

    >>> from sensor_msgs.msg import Image
    >>> 
    

    There could be several possible causes e.g CMAKE file issue. sensor_msgs not installed/not compatible, config error etc

    from your word " I can't found this message type in my rosmsg list, when I use this command $rosmsg list | grep sensor_msgs." I guess it because you didn`t install it or installed a non-compatible version.

    Try to install using the wstools to get it. RPI require some special touch.

    rosinstall_generator sensor_msgs --rosdistro indigo --deps -wet-only --tar > indigo-sensor_msgs-wet.rosinstall
    wstool init src indigo-sensor_msgs-wet.rosinstall