Example of ROS 2 package written in Rust. This repository is a part of the tutorial published on by blog page.
Rust is not supported in ROS 2 by default. However, there are two solutions to provide such a support.
I have used the first one (ros2-rust) package. Mostly, because it's more actively developed and in my opinion has a better documentation. So, the presented example will be based on this approach.
I have used the following specification. However, newer versions should be also fine, especially Ubuntu 22.04 and ROS 2 Humble.
Used software:
- Ubuntu 20.04
- Python 3.9
- rustc 1.64.0 (Rust compiler)
- ROS 2 Galactic
- Install Python 3.8+
- Install ROS 2 Galactic (or Humble). Here is the instruction for Ubuntu.
- Install Rust (the script is intended for Linux).
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- If you have already installed the Rust, then just update it:
rustup update
- Install tools necessary to configure and build the
ros2-rust
ROS package
sudo apt install -y git libclang-dev python3-vcstool python3-pip
- Install packages necessary to use
Cargo
with ROS packaging systemcolcon
cargo install cargo-ament-build
pip install git+https://github.com/colcon/colcon-cargo.git
pip install git+https://github.com/colcon/colcon-ros-cargo.git
- Create a new workspace
mkdir -p ~/ros2_rust_ws/src
cd ~/ros2_rust_ws/src
- Clone the ros2-rust repository to the workspace
cd ~/ros2_rust_ws/src
git clone git@github.com:ros2-rust/ros2_rust.git
- Get all
ros2-rust
dependencies and source ROS configuration. Make sure that aROS_DISTRO
is set to the correct ROS version (currentlyros2_rust
supports foxy, galactic, humble and rolling)
cd ..
vcs import src < src/ros2_rust/ros2_rust_${ROS_DISTRO}.repos
. /opt/ros/${ROS_DISTRO}/setup.sh
- Build the workspace with
ros2-rust
colcon build --packages-up-to examples_rclrs_minimal_pub_sub
In case of building issues please check the original building instruction.
- Clone repository to workspace
cd ~/ros2_rust_ws/src
git clone git@github.com:mdrwiega/ros2_rust_example.git
- Build the package
colcon build --packages-select rust_example
- Run publisher and subscriber. Two terminal will be necessary. Run the publisher node in the first terminal
ros2 run rust_example publisher
- In the second terminal run the subscriber node
ros2 run rust_example subscriber
- Alternatively, run the nodes with created launch file
ros2 launch rust_example publisher_and_subscriber_launch.yaml