Home > Net >  Why Rospy does not work ros2 python environment?
Why Rospy does not work ros2 python environment?

Time:08-16

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

CodePudding user response:

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()
  • Related