Why Rospy does not work ros2 python environment?

Question:

In the python virtual environment, I can not import rospy. Why? I need it to publish and to subscribe the image_raw.

Asked By: tgr

||

Answers:

With ros2, you’d have to use rclpy instead of rospy.
rospy does not exist anymore with ros2, so you also cannot import it. rclpy is the new client library that builds on top of ros2′ rcl. See here for further information.

Generally, ros2 is well documented with many demos and tutorials. See here for a simple subscriber/publisher tutorial.

Here is also a quick example of how to subscribe and publish on an image topic:

#! /usr/bin/env python3

import rclpy
from rclpy.node import Node

from sensor_msgs.msg import Image
    
class SimplePubSub(Node):
    def __init__(self):
        super().__init__('simple_pub_sub')
        self.publisher = self.create_publisher(Image, '/image_processed', 10)
        self.subscription = self.create_subscription(
            Image, '/image_raw', self.img_callback, 10)

    def img_callback(self, msg):
        self.get_logger().info('processing image....')

        # msg = ......

        self.publisher.publish(msg)


def main(args=None):
    rclpy.init(args=args)
    simple_pub_sub = SimplePubSub()
    rclpy.spin(simple_pub_sub)
    simple_pub_sub.destroy_node()
    rclpy.shutdown()
Answered By: Lukas
#! /usr/bin/env python3

import rclpy
from rclpy.node import Node 
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2 
    
class SimplePubSub(Node):
    def __init__(self):
        super().__init__('simple_pub_sub')

        topic_name= 'video_frames'

        self.publisher_ = self.create_publisher(Image, topic_name , 10)
        self.timer = self.create_timer(0.1, self.timer_callback)

        self.cap = cv2.VideoCapture(0)
        self.br = CvBridge()

        self.subscription = self.create_subscription(Image, topic_name, self.img_callback, 10)
        self.subscription 
        self.br = CvBridge()


    def timer_callback(self):
        ret, frame = self.cap.read()     
        if ret == True:
            self.publisher_.publish(self.br.cv2_to_imgmsg(frame))
        self.get_logger().info('Publishing video frame')


    def img_callback(self, data):
        self.get_logger().info('Receiving video frame')
        current_frame = self.br.imgmsg_to_cv2(data)
        cv2.imshow("camera", current_frame)   
        cv2.waitKey(1)


def main(args=None):
    rclpy.init(args=args)
    simple_pub_sub = SimplePubSub()
    rclpy.spin(simple_pub_sub)
    simple_pub_sub.destroy_node()
    rclpy.shutdown()

  
if __name__ == '__main__':
  main()
Answered By: Bharath Kumar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.