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

How to change topic name in function call?

asked 2021-11-16 04:06:11 -0500

WarTurtle gravatar image

Hello, I am trying to run the same code on different robots, and they have different tf prefixes. So instead of making n files, I want to just change the prefix in the function, here called ns

def movebase_client(ns, x, y):

    client = actionlib.SimpleActionClient('ns/move_base', MoveBaseAction)

Any idea?

edit retag flag offensive close merge delete



You could pass ns as a string and concatenate /move_base

osilva gravatar image osilva  ( 2021-11-16 04:46:25 -0500 )edit

Yes, that worked, thanks.

WarTurtle gravatar image WarTurtle  ( 2021-11-16 06:32:28 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2021-11-16 06:33:25 -0500

WarTurtle gravatar image

updated 2021-11-17 07:29:37 -0500

I made it work doing this

def movebase_client(ns, x, y):

    client = actionlib.SimpleActionClient(ns + '/move_base', MoveBaseAction)

and then when you call the function,

movebase_client('Bot1', 4.0, 4.0)

This, however, does not allow me to change the values in the launch file. For this, I had to use the param server. In the python file, I wrote this:

x = rospy.get_param('~x_position')
y = rospy.get_param('~y_position')

client = actionlib.SimpleActionClient('move_base', MoveBaseAction)

and then in the launch file I could then change the values doing this:

  <node pkg="okg" name="name" type="" output="screen">
      <remap from="move_base"       to="Bot1/move_base"/>
      <param name="~x_position"      type="double"      value="2.0"/>
      <param name="~y_position"      type="double"      value="2.0"/>

Thanks, osilva and Geoff.

edit flag offensive delete link more


Great. You can accept your answer now by clicking on the checkmark button.

osilva gravatar image osilva  ( 2021-11-16 06:36:54 -0500 )edit

answered 2021-11-16 17:56:30 -0500

Geoff gravatar image

I'm not 100% clear on what you are trying to do, but the usual way to handle this sort of problem in ROS is to use remapping of topics. You can also place an entire collection of nodes/topics in a namespace by putting them in a group in your launch file and setting the ns attribute of the group.

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

1 follower


Asked: 2021-11-16 04:06:11 -0500

Seen: 76 times

Last updated: Nov 17 '21