Robotics StackExchange | Archived questions

How to put ros subscribers in a for loop?

I have multiple edges from where I'm collecting data from. I currently have only 3 edges, but wish to expand their number in future. In order to adopt that flexibility, how can I make a for loop for multiple rostopics which are indexed e1/ .. e2/... e3/.. and so on

current running code is below that I want to put in a for loop and have a single callback function.

    self.e1_sub = rospy.Subscriber('/e1/edge_data', Edgedata, self.callback_e1)
    self.e2_sub = rospy.Subscriber('/e2/edge_data', Edgedata, self.callback_e2)
    self.e3_sub = rospy.Subscriber('/e3/edge_data', Edgedata, self.callback_e3)

Asked by Bulldog24 on 2022-09-21 13:35:52 UTC

Comments

Answers

I think you can get a lot of info + examples from this question and my answer here (it was for ROS 2, but it should work similarly with ROS1):

In general, self.create_subscription and self.create_publisher can use a parameterized topic name (e.g. '/robot'+str(i)+'/odom') so it can be easily written in a for loop.

Asked by ljaniec on 2022-09-21 18:14:11 UTC

Comments