ros / ros_tutorials

Code used in tutorials found on ROS wiki
http://wiki.ros.org/ros_tutorials
805 stars 540 forks source link

Update turtlesim mimic tutorial node. #65

Closed hidmic closed 5 years ago

hidmic commented 5 years ago

This pull request refactors the mimic node, a turtlesim tutorial, as it does not currently behave as it ought to. Mainly, rclcpp::Node was used as if it were a ROS1 NodeHandle equivalent -- variable naming suggests so.

I also took the liberty to push it closer to the rclcpp::Node subclassing idiom.

dirk-thomas commented 5 years ago

Is there a reason to change the class to only use a single node rather than 2 nodes as it is in the ROS 1 code?

hidmic commented 5 years ago

rather than 2 nodes as it is in the ROS 1 code?

IIUC that isn't what's happening in ROS1. A NodeHandle is a RAII initializer for the sole global node that one initializes in ROS1, plus a convenient way to namespace interfaces. As far as I can see, it was being used for the latter.

In ROS2, one does not get any namespacing and instantiating nodes like this currently brings up one DDS Participant for each. Also, we're not spinning those nodes.

dirk-thomas commented 5 years ago

Can you please share some steps how / what you tested. Thanks.

hidmic commented 5 years ago

Yes! Sorry about that, got caught in something else. I ran into this while trying to migrate the roslaunch section in ROS1 Using rqt_console and roslaunch tutorial. With this patch and the following roughly equivalent ROS2 Python launch file I was able to get it working:

from  launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='turtlesim', node_namespace='turtlesim1',
            node_executable='turtlesim_node', node_name='sim'
        ),
        Node(
            package='turtlesim', node_namespace='turtlesim2',
            node_executable='turtlesim_node', node_name='sim'
        ),
        Node(
            package='turtlesim', node_executable='mimic', node_name='mimic',
            remappings=[
                ('/input/pose', '/turtlesim1/turtle1/pose'),
                ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
            ]
        )
    ])

Just in case you wonder, I'm using Python instead of XML because support for the latter has not been backported to Dashing (nor it will be AFAIK).

To reproduce, drop above snippet into a turtlesim_mimic_launch.py file. Then run:

ros2 launch turtlesim_mimic_launch.py

Two turtlesim windows should pop up. In another terminal, run:

ros2 topic pub -r 1 /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist '{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}'

You should now see both turtles describing a circular trajectory.

dirk-thomas commented 5 years ago

Works well for me. Thanks.