Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for migrating a Python package from ROS 1 to 2 #4780

Draft
wants to merge 4 commits into
base: rolling
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/How-To-Guides/Migrating-from-ROS1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ If you are new to porting between ROS 1 and ROS 2, it is recommended to read thr
Migrating-from-ROS1/Migrating-Interfaces
Migrating-from-ROS1/Migrating-CPP-Package-Example
Migrating-from-ROS1/Migrating-CPP-Packages
Migrating-from-ROS1/Migrating-Python-Package-Example
Migrating-from-ROS1/Migrating-Python-Packages
Migrating-from-ROS1/Migrating-Launch-Files
Migrating-from-ROS1/Migrating-Parameters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
Migrating a Python Package Example
==================================

.. contents:: Table of Contents
:depth: 2
:local:

This guide shows how to migrate an example Python package from ROS 1 to ROS 2.

Prerequisites
-------------

You need a working ROS 2 installation, such as :doc:`ROS {DISTRO} <../../Installation>`.

The ROS 1 code
--------------

Say you have a ROS 1 package called ``talker_py`` that uses ``rospy`` in one node, called ``talker_py_node``.
This package is in a catkin workspace, located at ``~/ros1_talker``.

Your ROS 1 workspace has the following directory layout:

.. code-block:: bash

$ cd ~/ros1_talker
$ find .
.
src
src/talker_py
src/talker_py/package.xml
src/talker_py/CMakeLists.txt
src/talker_py/src
src/talker_py/src/talker_py
src/talker_py/src/talker_py/__init__.py
src/talker_py/scripts
src/talker_py/scripts/talker_py_node
src/talker_py/setup.py

The files have the following content:

``src/talker_py/package.xml``:

.. code-block:: xml

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
<name>talker_py</name>
<version>1.0.0</version>
<description>The talker_py package</description>
<maintainer email="[email protected]">Brian Gerkey</maintainer>
<license>BSD</license>

<buildtool_depend>catkin</buildtool_depend>

<depend>rospy</depend>
<depend>std_msgs</depend>
</package>

``src/talker_py/CMakeLists.txt``:

.. code-block:: cmake

cmake_minimum_required(VERSION 3.0.2)
project(talker_py)

find_package(catkin REQUIRED)

catkin_python_setup()

catkin_package()

catkin_install_python(PROGRAMS
scripts/talker_py_node
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

``src/talker/src/talker_py/__init__.py``:

.. code-block:: python

import rospy
from std_msgs.msg import String

def main():
pub = rospy.Publisher('chatter', String, queue_size=10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
hello_str = "hello world %s" % rospy.get_time()
rospy.loginfo(hello_str)
pub.publish(hello_str)
rate.sleep()

``src/talker_py/scripts/talker_py_node``:

.. code-block:: python

#!/usr/bin/env python

import talker_py

if __name__ == '__main__':
talker_py.main()

``src/talker_py/setup.py``:

.. code-block:: python

from setuptools import setup
from catkin_pkg.python_setup import generate_distutils_setup

setup_args = generate_distutils_setup(
packages=['talker_py'],
package_dir={'': 'src'}
)

setup(**setup_args)

Migrating to ROS 2
------------------

TODO

Conclusion
----------

You have learned how to migrate an example Python ROS 1 package to ROS 2.
Use the :doc:`Migrating Python Packages reference page <./Migrating-Python-Packages>` to help you migrate your own Python packages from ROS 1 to ROS 2.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Migration-Guide-Python
The-ROS2-Project/Contributing/Migration-Guide-Python

Migrating Python Packages
=========================
Migrating Python Packages Reference
===================================

.. contents:: Table of Contents
:depth: 2
Expand Down