0

What I expect: A ros node is run, it has a publisher and a subscriber coded into it, and another dynamic subscriber The publisher publishes data about the node to another master node, which then publishes to the subscriber in the node a) a topic b) type of message from that topic Taking this information, the node must assign the topic and message type to the dynamic subscriber The node, same code, will be run multiple times, but with different data that is being published to the master node so they all get their topics from the master node individually which might or might not be same

To be honest, I am pretty new to ros and I havent tried much, and I am unsure where to start too

Vibhav
  • 1

1 Answers1

0

I think It's not good practice and I would recommend not doing i, but here's how you can: Let's take the " slave " node

  1. you need to publish info about this node, let's assume we will publish the node name node1. So you will create a publisher that publishes message type String.
  2. creat a subscriber that receives a message type String your node needs to wait for this subscriber to receive a message with the name of the the new topic and it's message type (seperated by a comma). you can seperate them into "topic and type
  3. you will run into the problem that you need to import this " new message type" that changes from node to node which is actually doable for example see this answer

now your code should be something like this (not a running version):

#!/usr/bin/env python
import rospy
from std_msgs.msg import String


class Nodes(object):
    def __init__(self, name):
        rospy.init_node('node_name', anonymous=True)
        self.Name = name 
        self.pub = rospy.Publisher('nodes_info', String, queue_size=10)
        self.pub.publish(self.Name)
        self.sub_once = rospy.Subscriber('master_topic', String, self.sub_callback, queue_size=10)
        self.rate = 10      
        self.sub_msg = None
        self.topic = None
        self.type = None

    def sub_callback(self, msg):
        self.sub_msg = msg.data
        chunks = self.sub_msg.split(',')
        self.topic = chunks[0]
        self.type = chunks[1]
        ##
        # DO dynamic import here 
        ##
        self.sub_once.unregister()
    
    def callback(self,data) 
        # DO SOMETHING ( you main callback) 
        d = data 
    
    
    def spin(self):
        r = rospy.Rate(self.rate)
        while self.sub_msg == None:
            r.sleep()
        while not rospy.is_shutdown():
            rospy.Subscriber(topic, type, self.callback)
            print(self.sub_msg) 
            r.sleep()

if __name__ == "__main__":
    SC = Nodes("node1")
    SC.spin()
Baza
  • 1
  • 2