ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Use a common code for multiple NODES with one variable value change [python]

asked 2017-03-09 12:35:42 -0600

vvyogi gravatar image

updated 2017-03-09 12:37:54 -0600

I need to control multiple differential drive robots.

I wrote a NODE in python which is able to control the robot i.e. publishes velocity and steering of robot using some sensor feedback.

Now I am copying the same code again and again with different names eg controller1.py controller2.py etc
Inside the codes the change is just one variable (name='bot_0' or 'bot_1' or 'bot_2' and so on).

Thus, I was thinking if there is some way in which I can replicate the node from roslaunch and pass one argument. This will make code more scalable and manageable.

Kindly provide your inputs.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-03-09 13:57:43 -0600

ahendrix gravatar image

You can use ROS parameters to change the configuration values of your nodes at run time; have a look at the rospy parameters tutorial for how to retrieve parameters. The roslaunch XML reference explains the tags that are available in roslaunch.

If you use a private parameter to configure your node, your launch file would look something like:

<launch>
  <node pkg="my_controller_package" type="controller.py" name="controller1">
    <param name="name" value="bot_1"/>
  </node>
  <node pkg="my_controller_package" type="controller.py" name="controller2">
    <param name="name" value="bot_2"/>
  </node>
</launch>

Assuming that your node is called controller.py in a package named my_controller_package; this sets the private parameter name for each node, which you can retrieve in rospy with rospy.get_param("~name")

edit flag offensive delete link more

Comments

1

The ability to reuse nodes and run multiple instances simultaneously with different configurations via parameters is one of ROS's biggest strengths. This answer is an excellent example of this.

jayess gravatar image jayess  ( 2017-03-26 01:20:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-09 12:35:42 -0600

Seen: 473 times

Last updated: Mar 09 '17