PX4 SITL with ROS2 — RTPS BRIDGE
PX4 or Pixhawk is most used Open-Source UAV Flight Controller, and works with a lot of different Airframes. It provides a standard to deliver drone hardware support stack, allowing an ecosystem to build and maintain hardware and software in a scalable way. PX4 is used in a wide range of use-case from consumer drones to industrial applications. It is also the leading research platform for drones and has been successfully applied to other vehicles like submarines and boats.
This blog is about setting up PX4 SITL on UBUNTU, with ROS2 and over RTPS bridge.
The UAV is normally controlled and commanded by the Flight Controller, which is PX4 in this case, but if someone wants to run autonomous long distance mission with the drone, they would need a companion computer on-board to command the drone as a whole, at the same time logging data over the cloud. To do this we use the Companion Computer talking to the Flight Controller using the RTPS bridge and do the required.
The PX4 SITL is built with 3 major components — Pixhawk, Companion Computer & the RTPS Bridge connecting the two.
PX4 acts as the microRTPS Client.
OFFBOARD Computer acts as the microRTPS Agent.
The Agent commands the Client over the RTPS Bridge, either over a serial link (UART) or UDP Network (ETH). The uORB messages used by Pixhawk as a communication platform, are first CDR Serialized before being sent over the bridge and then deserialized at the receiving end.
Building the System
PX4
- Install PX4 Firmware
git clone https://github.com/PX4/PX4-Autopilot.git — recursive
- Install supporting PX4 Softwares like Gazebo and others
bash ./PX4-Autopilot/Tools/setup/ubuntu.sh
FastRTPS_DDS
Install Gradle
curl -s “https://get.sdkman.io" | bash
source “$HOME/.sdkman/bin/sdkman-init.sh”
sdk install gradle 6.3
Fast-RTPS Gen
git clone — recursive https://github.com/eProsima/Fast-DDS-Gen.git -b v1.0.4 ~/Fast-RTPS-Gen \
&& cd ~/Fast-RTPS-Gen \
&& gradle assemble \
&& sudo env “PATH=$PATH” gradle install
ROS 2
Install ROS2 via Debian Packages
locale # check for UTF-8
sudo apt update && sudo apt install locales
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8locale # verify settings
Setup Sources
sudo apt update && sudo apt install curl gnupg2 lsb-release
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpgecho “deb [arch=$(dpkg — print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(source /etc/os-release && echo $UBUNTU_CODENAME) main” | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
Install ROS2 Packages
sudo apt updatesudo apt install ros-foxy-desktop
Source Bash Profiles
echo “source /opt/ros/foxy/setup.bash” >> ~/.bashrc
source ~/.bashrc
Install all other requirements
sudo apt install python3-colcon-common-extensions
sudo apt install ros-foxy-eigen3-cmake-module
sudo pip3 install -U empy pyros-genmsg setuptools
sudo apt install ros-foxy-gazebo-ros-pkgs
Now that all the main requirements are installed, lets build the main ROS2 Workspaces and start the SITL
ROS2 WS
mkdir -p ~/px4_ros_com_ros2/src
git clone https://github.com/PX4/px4_ros_com.git ~/px4_ros_com_ros2/src/px4_ros_com
git clone https://github.com/PX4/px4_msgs.git ~/px4_ros_com_ros2/src/px4_msgs
Once all packages are installed, lets build the workspace by a custom build function
cd ~/px4_ros_com_ros2/src/px4_ros_com/scripts
source clean_all.bash
source build_ros2_workspace.bash
Now source the Workspace to the bashrc file
echo “source ~/px4_ros_com_ros2/install/setup.bash” >> ~/.bashrc
source ~/.bashrc
Once all this is built, lets start the simulation
cd PX4-Autopilot
make px4_sitl gazebo
This will launch a basic gazebo instance with an IRIS drone. You can command it to takeoff by typing commander takeoff in the terminal.
RTPS System
Now since our main goal is to control the drone from the off board system vis RTPS bridge lets initiate the RTPS bridge and control the drone from an offboard POV.
start PX4 RTPS by
make px4_sitl_rtps gazebo
now open a new termincal tab & initiate the connection over UDP (in SITL) by
micrortps_agent -t UDP
now to make the drone takeoff via an offboard command
ros2 run px4_ros_com offboard_control
This will arm the drone, make it take-off and go to point (3,3) in Local Frame and to a height of 3m.
This value can be changed by editing the offboard_control.cpp file under px4_ros_com package and multiple more functions can be added such as precision landing, deteact and avoid, mapping and much more.
references: