[ROS2] Tutorial Beginner : Client libraries - Creating a package
Creating a package
Background
package를 구성함으로써 code 공유, 사용을 쉽게 할 수 있다.
ament를 build system으로 사용하고 colcon을 build tool로 사용한다.
Python환경에서 package는 아래 5가지가 필요하다.
- package.xml : package에 대한 meta information을 가짐
- respirce/<package_name> : marker file을 가짐
- setup.cfg : executable을 찾을 수 있게 해줌
- setup.py : package 설치 instruction을 가짐
- <package_name> : package를 찾는데 사용, __init__.py를 가짐
my_package/
package.xml
resource/my_package
setup.cfg
setup.py
my_package/
workspace에는 많은 package가 있는데, CMake, Python 등 여러 build type의 package가 있을 것이다.
이렇게 되면 nested package(package 안에 package를 만드는 작업)을 할 수 없다.
이는 workspace에 src 폴더를 만들어 그 안에 package를 만들어 해결할 수 있다.
workspace_folder/
src/
cpp_package_1/
CMakeLists.txt
include/cpp_package_1/
package.xml
src/
py_package_1/
package.xml
resource/py_package_1
setup.cfg
setup.py
py_package_1/
...
cpp_package_n/
CMakeLists.txt
include/cpp_package_n/
package.xml
src/
Tasks
Create a package
이전에 만들어뒀던 ros2_ws를 이용한다.
새로운 package를 생성한다. 이 때, --node-name을 사용하여 node name도 설정할 수 있다.
cd ~/ros2_ws/src
# ros2 pkg create --build-type ament_python --license Apache-2.0 <package_name>
ros2 pkg create --build-type ament_python --license Apache-2.0 --node-name my_node my_package
Build a package
workspace에 package를 넣어두면 많은 package를 한번에 build할 수 있다.
colcon build
하지만 많은 package가 있어서 build하는데 오랜 시간이 소요될 수 있다.
이를 위해 몇개의 package만 build할 수 있다.
colcon build --packages-select my_package
Source the setup file
source install/local_setup.bash
Use the package
ros2 run my_package my_node
Examine package contents
ros2 pkg create를 할 때 아래의 요소들이 자동으로 생성되었다.
Customize package.xml
package를 만들때 description, license에 TODO가 포함되어 있다.
description, license, maintainer는 자동으로 명시되지 않지만 package를 release하고 싶다면 명시해야 한다.
# package.xml
<?xml version="1.0"?>
<?xml-model
href="http://download.ros.org/schema/package_format3.xsd"
schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>my_package</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="user@todo.todo">user</maintainer>
<license>TODO: License declaration</license>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>
package.xml에서 _depend로 끝나는 tag를 볼 수 있는데 어떤 package에 의존하는지 확인할 수 있다.
또한 setup.py에서 package.xml에서 수정한 description, maintainer, license로 수정해줘야 한다.
# setup.py
from setuptools import setup
package_name = 'my_py_pkg'
setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='TODO',
maintainer_email='TODO',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'my_node = my_py_pkg.my_node:main'
],
},
)
ROS2_Humble Documentation : https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries.html