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

How to change topic name in function call?

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

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)
    client.wait_for_server()

Any idea?

edit retag flag offensive close merge delete

Comments

1

You could pass ns as a string and concatenate /move_base

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

Yes, that worked, thanks.

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

2 Answers

Sort by ยป oldest newest most voted
1

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

WarTurtle gravatar image

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

I made it work doing this

def movebase_client(ns, x, y):

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

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)
client.wait_for_server()

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

  <node pkg="okg" name="name" type="name.py" 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"/>
  </node>

Thanks, osilva and Geoff.

edit flag offensive delete link more

Comments

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

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

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

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

Question Tools

1 follower

Stats

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

Seen: 141 times

Last updated: Nov 17 '21