ROS Advent Calendar 2020 This is the article on the 22nd day. This year, ROS2 has become quite popular, and the transition to ROS2 has become realistic, such as support for ROS2 in major libraries. Next year, 2021, the transition to ROS2 is expected to progress further (arbitrary forecast).
I'm finally starting to touch ROS2, but the OS I'm using is 18.04, which is a little old and I can't try the latest foxy. There are ways to build from source or use Docker, but the source needs to be built every time, and Docker needs to be mounted at the time of development, so the authority is a little troublesome.
Therefore,
Aiming for that, I made it possible to install ROS2 with the Wheel package of Python, so I will introduce it.
The confirmed environment is as follows.
#Environment
$ python3 --version
Python 3.6.9
$ python3 -m venv venv
$ . ./venv/bin/activate
(venv) $ pip install -U pip
#Installation
(venv) $ pip install --extra-index-url https://rospypi.github.io/simple2 rclpy std_msgs ros2py-init
...(Abbreviation) ..
#Run only once
(venv) $ ros2py-init
updated
#If you want to use it as it is, deactivate it once and activate it again.
(venv) $ deactivate
$ . ./venv/bin/activate
LD_LIBRARY_PATH=....
Get sample code
git clone https://github.com/ros2/examples.git
Open a new terminal and run Publisher.
$ . ./venv/bin/activate
LD_LIBRARY_PATH=....
(venv) $ python examples/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_member_function.py
[INFO] [1608563828.438826436] [minimal_publisher]: Publishing: "Hello World: 0"
[INFO] [1608563828.931807019] [minimal_publisher]: Publishing: "Hello World: 1"
[INFO] [1608563829.430286609] [minimal_publisher]: Publishing: "Hello World: 2"
[INFO] [1608563829.931504088] [minimal_publisher]: Publishing: "Hello World: 3"
[INFO] [1608563830.430435935] [minimal_publisher]: Publishing: "Hello World: 4"
[INFO] [1608563830.931712417] [minimal_publisher]: Publishing: "Hello World: 5"
[INFO] [1608563831.430346898] [minimal_publisher]: Publishing: "Hello World: 6"
Open a new terminal and run Subscriber.
$ . ./venv/bin/activate
LD_LIBRARY_PATH=....
(venv) $ python rclpy/topics/minimal_subscriber/examples_rclpy_minimal_subscriber/subscriber_member_function.py
[INFO] [1608563893.706370811] [minimal_subscriber]: I heard: "Hello World: 17"
[INFO] [1608563894.199072027] [minimal_subscriber]: I heard: "Hello World: 18"
[INFO] [1608563894.702395055] [minimal_subscriber]: I heard: "Hello World: 19"
[INFO] [1608563895.202343741] [minimal_subscriber]: I heard: "Hello World: 20"
[INFO] [1608563895.701980945] [minimal_subscriber]: I heard: "Hello World: 21"
[INFO] [1608563896.202263923] [minimal_subscriber]: I heard: "Hello World: 22"
[INFO] [1608563896.701910239] [minimal_subscriber]: I heard: "Hello World: 23"
You can create a ROS2 execution environment anywhere without root privileges, building from source, or using Docker.
The Python package can include files such as include
, lib
, and share
in the distribution package, starting from sys.prefix
. This time, I used this to put all the files included in each ROS2 package into a Python package. By doing this, sys.prefix
can be treated like/opt/ros/foxy
(AMENT_PREFIX_PATH), so you can build the ROS2 execution environment by passing through LD_LIBRARY_PATH
.
Since all include files, so files, ament_cmake
, etc. for C and C ++ are installed, it seems that you can build and execute the ROS2 package in this state as long as you write CMake for ROS2 (you can try it). I will introduce it again)
The script to create a Python package is available at https://github.com/otamachan/ros2py.git.
Actually, I wanted to bring it to the point where I could do pip install rviz2
, but I didn't have enough time. However, I am satisfied in the sense that ROS2 can be easily installed on both MacOS and Ubuntu for the time being.
The Python wheel can do almost something like apt in the sense that it distributes pre-built binaries, and the ROS2 build supports (complexes) the environment ament_cmake. It was this lesson that I was able to go around.
In the future, I would like to support the following TODO.
Happy Robot Programming!
Recommended Posts