Ask Your Question
0

Ros2 publisher/subscriber same node

asked 2021-03-11 11:45:04 -0500

Bulun gravatar image

Hi There! I’m new to ROS2 and I’m struggling in coding a node acting as both subscriber and publisher to exchange data with other nodes. I’ve tried to modify the python code in the ROS2 Tutorial but I still don't get how to make it work. Any suggestions?

Below I'm attaching the modified Publisher and Subscriber codes.

Many thanks! Alex


Publisher

import rclpy from rclpy.node import Node

from std_msgs.msg import String

class MinimalPublisher(Node):

def __init__(self):
    super().__init__('minimal_publisher')
    self.publisher_ = self.create_publisher(String, 'topic', 10)
    timer_period = 0.5  # seconds
    self.timer = self.create_timer(timer_period, self.timer_callback)
    self.i = 0

    self.subscription = self.create_subscription(
        String,
        'return',
        self.listener_callback,
        10)
    self.subscription

def listener_callback( self, msg):
    self.get_logger().info(' Returned msg: I heard: "%s"' % msg.data)

def timer_callback(self):
    msg = String()
    msg.data = 'Hello World: %d' % self.i
    self.publisher_.publish(msg)
    self.get_logger().info('Publishing: "%s"' % msg.data)
    self.i += 1

def main(args=None): rclpy.init(args=args)

minimal_publisher = MinimalPublisher()

rclpy.spin(minimal_publisher)

minimal_publisher.destroy_node()
rclpy.shutdown()

if __name__ == '__main__': main()


Subscriber

import rclpy from rclpy.node import Node

from std_msgs.msg import String

class MinimalSubscriber(Node):

def __init__(self):
    super().__init__('minimal_subscriber')
    self.subscription = self.create_subscription(
        String,
        'topic',
        self.listener_callback,
        10)
    self.subscription  # prevent unused variable warning

    self.publisher_ = self.create_publisher( String, 'return', 10)
    time_period = 0.5
    self_timer = self_create_timer(time_period, self.timer_callback)
    self.i = 0

def listener_callback(self, msg):
    self.get_logger().info('I heard: "%s"' % msg.data)

def timer_callback(self)
  msg = String()
  msg.data = 'Received' %d ' % self.i
  self.publisher_.publish(msg)
  self.get_logger().info('Publishing return msg: "%s"' % msg.data)
  self.i += 1

def main(args=None): rclpy.init(args=args)

minimal_subscriber = MinimalSubscriber()

rclpy.spin(minimal_subscriber)

minimal_subscriber.destroy_node()
rclpy.shutdown()

if __name__ == '__main__': main()

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2021-03-12 08:25:43 -0500

Bulun gravatar image

IT - COULD - WORK! I found the solution!!!! ah ah ah...

edit flag offensive delete link more

Comments

I am facing a similar issue. Could you tell what was the issue you found and the solution to it? Thanks .

nd23 gravatar image nd23  ( 2021-07-23 21:16:05 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-03-11 11:45:04 -0500

Seen: 616 times

Last updated: Mar 11 '21