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

How can I modify the code for the turtlesim package ?

asked 2018-04-16 03:42:41 -0500

aks gravatar image

Let us say that i ran a turtlesim_node rosrun turtlesim turtlesim_node and then ran another node with a different name rosrun turtlesim turtlesim_node __name:=my_turtle. Now i ran the teleop node : rosrun turtlesim turtle_teleop_key. Now when i move the teleop key, i am publishing messages to bothe the turtlesim nodes. I do not want this but i guess by default it is so that the teleop node will publish the messages to all the turtlesim_nodes present.

  1. How can i have multiple turtlesim_node running but publish only to 1.
  2. Using rosservice, how can i clear the content of both the turtlesim nodes at the same time ? using rosservice call /clear clears the default node.
  3. Is it possible to modify its code or there is some understanding problem from my side ?
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-04-16 03:58:17 -0500

gvdhoorn gravatar image

updated 2018-04-16 03:58:51 -0500

Now when i move the teleop key, i am publishing messages to bothe the turtlesim nodes. I do not want this but i guess by default it is so that the teleop node will publish the messages to all the turtlesim_nodes present.

No, that is not how this works. The teleop node publishes to the turtle1/command_velocity topic (here), to which all instances of turtlesim_node subscribe. That is why they all receive the same message.

1 . How can i have multiple turtlesim_node running but publish only to 1.

Technically, you can't, as publications are not "to" a certain node, but to a topic. Any node can subscribe to that topic, and publications are anonymous. You have no way to specify a recipient.

What you could do however, is use remapping to effectively 'rewire' a node to publish or subscribe to a different topic. You could use this to make one turtlesim_node subscribe to turtle1/command_velocity (the default) and the other to turtle2/command_velocity. Now if turtle_teleop publishes to turtle1/command_velocity, only the first turtlesim_node will receive the messages.

This effectively achieves what you are asking, but do please note the difference: you're still not sending messages "to" a specific turtlesim_node, you're just making sure that only one of them "tunes in" on the data stream sent out by turtle_teleop.

2 . Using rosservice, how can i clear the content of both the turtlesim nodes at the same time ? using rosservice call /clear clears the default node.

You can't.

In contrast to messages, service invocations are directed to one particular recipient, namely the service provider (or server). In this case most likely the last turtlesim_node that came up 'claims' the name clear and responds to calls to the service.

Remapping can help you here as well, but only to make sure the clear services are reachable for both (or all) instances.

3 . Is it possible to modify its code or there is some understanding problem from my side ?

From the above, it would seem there is no 'need' to modify the code, as remapping seems to solve issue 1, and issue 2 is not possible, even if you'd modify the source.

If you'd still like to take a look, see ros/ros_tutorials/turtlesim. Clone it into a Catkin workspace, build it and be sure to source the devel/setup.bash.

edit flag offensive delete link more

Comments

#q203129 might be interesting as some background on how coupled nodes are when they use the different communication patterns supported by ROS.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-16 04:13:00 -0500 )edit

@gvdhoorn Thanks for such a quick and effective response. I tried this and this works. Just one thing : How can i remap the topics of the node my_turtle. By default it always changes that of the turtlesim_node.

rosrun turtlesim my_turtle /turtle1/cmd_vel:=/turtle2/cmd_vel
aks gravatar image aks  ( 2018-04-16 04:30:39 -0500 )edit

this says couldnt find an executable named my_turtle. When I use the default i.e. rosrun turtlesim turtlesim_node /turtle1/cmd_vel:=/turtle2/cmd_vel it works for the turtlesim_node

aks gravatar image aks  ( 2018-04-16 04:32:20 -0500 )edit

I would recommend you start using launch files. rosrun can be used -- also with remapping -- but launch files will make this much easier.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-16 09:16:16 -0500 )edit

Also: try to avoid speccing absolute topic names (ie: remove the leading /).

gvdhoorn gravatar image gvdhoorn  ( 2018-04-16 09:16:39 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-16 03:42:41 -0500

Seen: 1,956 times

Last updated: Apr 16 '18