ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How to put ros subscribers in a for loop?

asked 2022-09-21 13:35:52 -0600

Bulldog24 gravatar image

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)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-09-21 18:14:11 -0600

ljaniec gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools



Asked: 2022-09-21 13:35:52 -0600

Seen: 65 times

Last updated: Sep 21 '22