Questions about 4.3 Topics Quiz

Good afternoon:
At the time of solving the exercise “4.3 Topics Quiz” I have managed to publish in the /cmd_vel topic and read the data from the laser sensor in the appropriate positions as a subscriber of the /kobuki/laser/scan topic but I am not able to relate both scripts. Could you help me by giving me some kind of advice. Should I create some kind of own message?
I also don’t know how to configure the launch file so that the node acts both as a publisher and as a subscriber. I thought in the following way but it turned out wrong:

Un saludo y muchas gracias de antemano !

Your best bet it to create the publisher and the subscriber in the same script. You can use Python classes for some added benefits.

With this, you only need to launch one node.

Hi,

So have you completed the quiz? What do you mean by relating both scripts?

One thing to take into account is that you can create a publisher and a subscriber in the same node. This allows you to say use the laser data to send velocities to the robot.

In the launch file you created, you can see that the name you are giving your nodes is illegal, and it also seems like it can’t find the package you have put your file in. Remember to compile your packages and source the workspace they are in before you launch files within them.

Hello,

In ROS, it’s possible and common to have both a publisher and a subscriber within the same Python file. You can achieve this using the rospy library, which provides the necessary functionalities for publishing and subscribing to ROS topics.

Specifically to this session you mentioned, the node’s name was defined, but you can select any name for the python file you want.

Hi,
As mentioned in the other comments, you can declare a publisher and a subcriber in the same node. This can be useful, for example, for using the subcriber callback data and republishing according to what is received (eg: scan and cmd_vel).
Here’s a simple example of a basic node in Python that uses a publisher and a subcriber:

#!/usr/bin/env python3

import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class ExamplePubSub(Node):
    def __init__(self):
        super().__init__('my_node')
        
        # Publisher
        self.publisher = self.create_publisher(String, 'value_republished', 10)
        
        # Subscriber
        self.subscription = self.create_subscription(
            String,
            'input_topic',
            self.subscriber_callback,
            10)
        
        
        # Timer
        self.timer = self.create_timer(1.0, self.timer_callback)
        
        # Variable to store updated value from the subscriber
        self.updated_value = None

    def subscriber_callback(self, msg):
        self.get_logger().info(f'Received: {msg.data}')
        
        # Update the variable with the received value
        self.updated_value = msg.data

    def timer_callback(self):
        if self.updated_value is not None:
            self.get_logger().info(f'Timer callback using updated value: {self.updated_value}')
            # Do something with the updated value, for example, publish it
            # you can also modify it or just use it for a condition
            self.publisher.publish(String(data=self.updated_value))
def main(args=None):
    rclpy.init(args=args)
    node = ExamplePubSub()
    try:
        rclpy.spin(node)
    except KeyboardInterrupt:
        pass
    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()