Building a ROS2 Node in Rust!
This post shows how to build a Robot Operating System 2 node using Rust, a systems programming language built for safety, security, and performance.
- Being a newer language means less community support and less components provided out of the box. For example, writing a desktop GUI in Rust is possible, but the libraries are still maturing.
- It's harder to learn than most languages. The stricter compiler means some normal programming patterns don't work, whcih means relearning some concepts and finding different ways to accomplish the same task.
- It's hard for a new language to gain traction! Rust has to prove it will stand the test of time.
1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
1
2
3
4
5
sudo apt install -y git libclang-dev python3-pip python3-vcstool # libclang-dev is required by bindgen
# Install these plugins for cargo and colcon:
cargo install --debug cargo-ament-build # --debug is faster to install
pip install git+https://github.com/colcon/colcon-cargo.git
pip install git+https://github.com/colcon/colcon-ros-cargo.git
~/ros2_ws
, the following commands can be used to check out the source code:1
2
3
4
cd ~/ros2_ws/src
git clone https://github.com/mikelikesrobots/aws-iot-node-rust.git
git clone https://github.com/ros2-rust/ros2_rust.git
git clone https://github.com/aws-samples/aws-iot-robot-connectivity-samples-ros2.git
vcs
to import the other repositories it needs:1
2
cd ~/ros2_ws
vcs import src < src/ros2_rust/ros2_rust_humble.repos
1
2
3
cd ~/ros2_ws
colcon build
source install/setup.bash
cargo
commands, such as:1
2
cargo build
cargo run --bin mock-telemetry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Create and build a workspace for ROS2 Rust
mkdir -p ~/ros2_rust_ws/src
cd ~/ros2_rust_ws/src
git clone https://github.com/ros2-rust/ros2_rust.git
cd ~/ros2_rust_ws
vcs import src < src/ros2_rust/ros2_rust_humble.repos
colcon build
source install/setup.bash
# Check out application code into main workspace
cd ~/ros2_ws/src
git clone https://github.com/mikelikesrobots/aws-iot-node-rust.git
git clone https://github.com/aws-samples/aws-iot-robot-connectivity-samples-ros2.git
cd ~/ros2_ws
colcon build
source install/local_setup.bash
~/.bashrc
:1
echo "source ~/ros2_rust_ws/install/setup.bash" >> ~/.bashrc
local_setup.bash
script, or it will overwrite the variables needed to access the ROS2 Rust libraries.IOT_CONFIG_FILE
variable set from the Python repository.1
2
3
4
source ~/ros2_ws/install/setup.bash # Both terminals
source ~/ros2_ws/install/local_setup.bash # If using the multi-workspace setup method
ros2 run aws_iot_node mqtt-telemetry --ros-args --param path_for_config:=$IOT_CONFIG_FILE # One terminal
ros2 run aws_iot_node mock-telemetry # Other terminal